"""One-off check of the documented speech endpoints with one sample file (W01.wav).
Writes the redacted responses to api_check/*.json so API.md can cite the real shape."""
import json, os, pathlib, httpx
HERE = pathlib.Path(__file__).resolve().parent
BASE = 'https://space.ai-builders.com/backend/v1'
wav = HERE.parent / 'fixture' / 'memos' / 'W01.wav'
H = {'Authorization': f"Bearer {os.environ['AI_BUILDER_TOKEN']}"}
out = {}
with httpx.Client(timeout=300) as h:
    r = h.post(f'{BASE}/audio/grok-transcription', headers=H, data={'language': 'zh'},
               files={'audio_file': (wav.name, wav.read_bytes(), 'audio/wav')})
    j = r.json()
    if isinstance(j, dict) and isinstance(j.get('words'), list):
        j['words'] = j['words'][:3] + [f'... ({len(j["words"])} items total)']
    out['grok_full'] = {'status': r.status_code, 'body': j}
    r = h.post(f'{BASE}/audio/grok-transcription', headers=H, data={'language': 'zh', 'simple': 'true'},
               files={'audio_file': (wav.name, wav.read_bytes(), 'audio/wav')})
    out['grok_simple'] = {'status': r.status_code, 'body': r.json()}
    r = h.post(f'{BASE}/audio/transcriptions', headers=H, data={'model': 'whisper-1'},
               files={'file': (wav.name, wav.read_bytes(), 'audio/wav')})
    out['compat'] = {'status': r.status_code, 'body': r.json()}
    r = h.post(f'{BASE}/audio/grok-transcription', headers={'Authorization': 'Bearer sk_invalid'},
               files={'audio_file': (wav.name, wav.read_bytes(), 'audio/wav')})
    out['grok_bad_key'] = {'status': r.status_code, 'body': r.text[:300]}
(HERE / 'api_check.json').write_text(json.dumps(out, ensure_ascii=False, indent=1), encoding='utf-8')
print(json.dumps(out, ensure_ascii=False, indent=1)[:4000])
