Files
whisper_web/tests/fixtures/fake_sidecar.py
T

59 lines
2.1 KiB
Python

"""Protocol-faithful stand-in for server-py/sidecar.py (no faster-whisper).
Used by tests/transcribe.test.ts to exercise the real Bun-side supervisor
(createSidecar) against an actual child process. Behavior is keyed off the
requested model name:
"ok" two segments (the second reports the audio byte count,
language and task for pass-through assertions), then done
"boom" an error line
"die" exit(1) mid-request (crash simulation)
"wait-for-cancel" one segment, then block until the next stdin line
(the cancel), then done {cancelled: true}
"""
import json
import os
import sys
def emit(message):
sys.stdout.write(json.dumps(message) + "\n")
sys.stdout.flush()
emit({"type": "ready", "version": "fake"})
for line in sys.stdin:
line = line.strip()
if not line:
continue
message = json.loads(line)
kind = message.get("type")
if kind == "shutdown":
break
if kind != "transcribe":
continue
request_id = message.get("id")
model = message.get("model")
if model == "boom":
emit({"type": "error", "id": request_id, "message": "kaboom"})
elif model == "die":
sys.exit(1)
elif model == "wait-for-cancel":
emit({"type": "segment", "id": request_id, "start": 0.0, "end": 1.0,
"text": " before cancel"})
next_line = json.loads(sys.stdin.readline())
cancelled = (next_line.get("type") == "cancel"
and next_line.get("id") == request_id)
emit({"type": "done", "id": request_id, "cancelled": cancelled})
else:
size = os.path.getsize(message["audio_path"])
emit({"type": "segment", "id": request_id, "start": 0.0, "end": 1.5,
"text": " Hello"})
emit({"type": "segment", "id": request_id, "start": 1.5, "end": 3.0,
"text": " world. [model=%s bytes=%d lang=%s task=%s]"
% (model, size, message.get("language"), message.get("task"))})
emit({"type": "done", "id": request_id, "cancelled": False})