view dictation/server_test.py @ 281:c57149ad216e default tip

Copilot-Session: f68442b1-fa8f-46a0-9689-81710613bbd4
author MrJuneJune <me@mrjunejune.com>
date Tue, 18 Aug 2026 22:18:15 -0700
parents 49e9e591c9bb
children
line wrap: on
line source

from pathlib import Path
import unittest

from fastapi.testclient import TestClient

from dictation.config import DictationConfig
from dictation.server import create_app
from dictation.transcriber import Transcript


class FakeTranscriber:
    async def warmup(self):
        return None

    async def transcribe(self, samples, *, final):
        return Transcript("test transcript", "en", 0.99)


def test_config() -> DictationConfig:
    return DictationConfig(
        host="127.0.0.1",
        port=8090,
        model_dir=Path("/tmp/model"),
        compute_type="int8_float16",
        max_sessions=1,
        partial_interval_ms=1200,
        silence_ms=700,
        max_utterance_seconds=30,
        speech_threshold=0.012,
    )


class ServerTest(unittest.TestCase):
    def test_health_and_assets(self):
        with TestClient(create_app(test_config(), FakeTranscriber())) as client:
            health = client.get("/health")
            self.assertEqual(health.status_code, 200)
            self.assertEqual(health.json()["status"], "ok")
            self.assertIn("Zenbu Dictation", client.get("/").text)
            javascript = client.get("/dictation.js").text
            self.assertIn("RTCPeerConnection", javascript)
            self.assertIn("zenbu.dictation.event", javascript)
            self.assertIn('publishDictation("partial"', javascript)
            self.assertIn('publishDictation("final"', javascript)
            self.assertIn('setStatus("Starting microphone..."', javascript)
            self.assertIn("ZenbuDictationToggle", javascript)
            self.assertIn("ZenbuDictationStart", javascript)
            self.assertIn("ZenbuDictationStop", javascript)
            self.assertIn("ZenbuDictationCommit", javascript)
            self.assertIn("zenbu.dictation.set-active", javascript)
            self.assertIn("zenbu.dictation.commit", javascript)
            self.assertIn("prepareCanvasMicrophone", javascript)
            self.assertIn("track.enabled = false", javascript)
            self.assertIn("if (!stream)", javascript)

    def test_rejects_non_offer_sdp(self):
        with TestClient(create_app(test_config(), FakeTranscriber())) as client:
            response = client.post(
                "/api/webrtc/offer",
                json={"sdp": "not-sdp", "type": "answer"},
            )
            self.assertEqual(response.status_code, 422)

    def test_close_is_idempotent(self):
        with TestClient(create_app(test_config(), FakeTranscriber())) as client:
            response = client.post(
                "/api/webrtc/session/00000000-0000-0000-0000-000000000000/close"
            )
            self.assertEqual(response.status_code, 200)
            self.assertEqual(response.json(), {"closed": True})


if __name__ == "__main__":
    unittest.main()