voice-dictation/tests/unit/test_gigaam_transcriber.py

107 lines
3.7 KiB
Python

"""Optional GigaAM runtime boundaries; no model downloads or torch imports."""
from __future__ import annotations
import hashlib
import json
import subprocess
import wave
from pathlib import Path
import pytest
from voice_dictation.core import gigaam_transcriber, model_manager
from voice_dictation.core.gigaam_transcriber import GigaAMTranscriber
from voice_dictation.core.model_manager import ModelError, ModelManager, ModelSpec
from voice_dictation.infrastructure.errors import TranscriptionError
@pytest.mark.parametrize(
"payload",
[
[],
{"text": "x", "duration_sec": True},
{"text": "x", "duration_sec": float("nan")},
{"text": "x", "duration_sec": -1},
{"text": None, "duration_sec": 1},
],
)
def test_invalid_worker_payload_rejected(tmp_path, monkeypatch, payload):
audio = tmp_path / "input.wav"
with wave.open(str(audio), "wb") as output:
output.setnchannels(1)
output.setsampwidth(2)
output.setframerate(16000)
output.writeframes(b"\x00\x00" * 16)
monkeypatch.setattr(gigaam_transcriber, "runtime_python", lambda: Path("/optional/python"))
monkeypatch.setattr(
gigaam_transcriber.subprocess,
"run",
lambda *a, **kw: subprocess.CompletedProcess(a, 0, json.dumps(payload), ""),
)
engine = GigaAMTranscriber(tmp_path / "weights.bin", "gigaam-v3-e2e-ctc")
with pytest.raises(TranscriptionError):
engine.transcribe(audio)
def test_missing_optional_runtime_does_not_install_or_use_app_python(tmp_path, monkeypatch):
monkeypatch.setenv("VOICE_GIGAAM_PYTHON", str(tmp_path / "absent"))
with pytest.raises(TranscriptionError, match="отдельный"):
gigaam_transcriber.runtime_python()
def test_tokenizer_is_required_and_leased_with_weights(tmp_path, monkeypatch):
weights, tokenizer = b"weights", b"tokenizer"
specs = (
ModelSpec(
"weights",
"Weights",
"https://example.invalid/weights",
len(weights),
hashlib.sha256(weights).hexdigest(),
dependencies=("tokenizer",),
),
ModelSpec(
"tokenizer",
"Tokenizer",
"https://example.invalid/tokenizer",
len(tokenizer),
hashlib.sha256(tokenizer).hexdigest(),
selectable=False,
),
)
monkeypatch.setattr(model_manager, "CATALOG", specs)
manager = ModelManager(tmp_path)
manager.path_for("weights").write_bytes(weights)
assert not manager.is_installed("weights")
manager.path_for("tokenizer").write_bytes(tokenizer)
assert manager.is_installed("weights")
with manager.use("weights"), pytest.raises(ModelError, match="используется"):
manager.delete("tokenizer")
def test_support_file_downloaded_before_main_model(tmp_path, monkeypatch):
payloads = {"weights": b"weights", "tokenizer": b"tokenizer"}
specs = tuple(
ModelSpec(
name,
name,
"https://example.invalid/" + name,
len(data),
hashlib.sha256(data).hexdigest(),
dependencies=("tokenizer",) if name == "weights" else (),
)
for name, data in payloads.items()
)
monkeypatch.setattr(model_manager, "CATALOG", specs)
visited = []
async def fake_download(spec, partial, progress, cancel, notify):
visited.append(spec.id)
partial.write_bytes(payloads[spec.id])
monkeypatch.setattr(ModelManager, "_download_async", staticmethod(fake_download))
manager = ModelManager(tmp_path)
manager.download("weights")
assert visited == ["tokenizer", "weights"]
assert manager.is_installed("weights")