from pathlib import Path


VOICE_CLIENT_HTML = (
    Path(__file__).resolve().parents[2]
    / "src"
    / "app"
    / "ws_gateway"
    / "voice_client.html"
)


def test_voice_client_exposes_required_protocolo_for_oferta() -> None:
    html = VOICE_CLIENT_HTML.read_text(encoding="utf-8")

    assert "oferta: [" in html
    assert 'key: "protocolo"' in html
    assert 'inputId: "agentFieldProtocolo"' in html
    assert 'label: "PROTOCOLO"' in html
    assert 'required: true' in html
    assert 'target: "data"' in html


def test_voice_client_keeps_id_fatura_inside_agent_data() -> None:
    html = VOICE_CLIENT_HTML.read_text(encoding="utf-8")

    assert "const agentData = {};" in html
    assert 'key: "idFatura"' in html
    assert 'data.agentData = agentData;' in html


def test_voice_client_exposes_required_protocol_id_for_conta() -> None:
    html = VOICE_CLIENT_HTML.read_text(encoding="utf-8")

    assert "conta: [" in html
    assert 'key: "protocol_id"' in html
    assert 'inputId: "agentFieldProtocolId"' in html
    assert 'label: "PROTOCOL_ID"' in html
    assert 'target: "data"' in html


def test_voice_client_sends_session_id_in_start_data() -> None:
    html = VOICE_CLIENT_HTML.read_text(encoding="utf-8")

    assert 'id="sessionId"' in html
    assert "function defaultSessionId()" in html
    assert "session_id: sessionId" in html


def test_voice_client_guards_stale_microphone_streams() -> None:
    html = VOICE_CLIENT_HTML.read_text(encoding="utf-8")

    assert "function stopMicStreaming()" in html
    assert "const wsAtStart = state.ws;" in html
    assert "state.ws !== wsAtStart" in html
    assert "mediaStream.getTracks().forEach((track) => track.stop());" in html
