forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_conversation.py
More file actions
108 lines (84 loc) · 4.3 KB
/
Copy pathtest_conversation.py
File metadata and controls
108 lines (84 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""Tests for ``omi conversation`` commands."""
from __future__ import annotations
import json
import pytest
from omi_cli.main import app
def test_conversation_list_renders(authed_profile, respx_mock, cli_runner) -> None:
respx_mock.get("/v1/dev/user/conversations").respond(
json=[
{
"id": "c1",
"structured": {"title": "hello", "category": "personal"},
"started_at": "2026-04-01T00:00:00Z",
"source": "phone",
}
]
)
result = cli_runner.invoke(app, ["--json", "conversation", "list"])
assert result.exit_code == 0
payload = json.loads(result.stdout)
assert payload[0]["id"] == "c1"
def test_conversation_create_posts_text(authed_profile, respx_mock, cli_runner) -> None:
route = respx_mock.post("/v1/dev/user/conversations").respond(
json={"id": "c1", "status": "completed", "discarded": False}
)
result = cli_runner.invoke(
app, ["--json", "conversation", "create", "--text", "the weather today is fine", "--language", "en"]
)
assert result.exit_code == 0
body = json.loads(route.calls.last.request.content)
assert body["text"].startswith("the weather")
assert body["language"] == "en"
def test_conversation_get_includes_transcript_param(authed_profile, respx_mock, cli_runner) -> None:
route = respx_mock.get("/v1/dev/user/conversations/c1").respond(
json={"id": "c1", "structured": {"title": "x"}, "transcript_segments": []}
)
result = cli_runner.invoke(app, ["--json", "conversation", "get", "c1", "--include-transcript"])
assert result.exit_code == 0
request = route.calls.last.request
assert request.url.params["include_transcript"] == "true"
def test_conversation_update_requires_field(authed_profile, cli_runner) -> None:
result = cli_runner.invoke(app, ["conversation", "update", "c1"])
assert result.exit_code == 1
assert "no fields to update" in result.stderr.lower()
def test_conversation_delete_with_yes(authed_profile, respx_mock, cli_runner) -> None:
respx_mock.delete("/v1/dev/user/conversations/c1").respond(json={"success": True})
result = cli_runner.invoke(app, ["conversation", "delete", "c1", "--yes"])
assert result.exit_code == 0
def test_conversation_from_segments_reads_file(authed_profile, respx_mock, cli_runner, tmp_path) -> None:
f = tmp_path / "segments.json"
segments = {
"transcript_segments": [
{"text": "hi", "start": 0.0, "end": 1.0, "speaker": "SPEAKER_00"},
{"text": "hello", "start": 1.5, "end": 2.5, "speaker": "SPEAKER_01"},
]
}
f.write_text(json.dumps(segments))
route = respx_mock.post("/v1/dev/user/conversations/from-segments").respond(
json={"id": "c1", "status": "completed", "discarded": False}
)
result = cli_runner.invoke(app, ["--json", "conversation", "from-segments", str(f), "--source", "phone"])
assert result.exit_code == 0
body = json.loads(route.calls.last.request.content)
assert len(body["transcript_segments"]) == 2
assert body["source"] == "phone"
@pytest.mark.parametrize("encoding", ["utf-8", "utf-8-sig", "utf-16", "utf-32"])
def test_conversation_from_segments_preserves_unicode(
authed_profile, respx_mock, cli_runner, tmp_path, encoding
) -> None:
segments = [{"text": "Caf\u00e9, \u65e5\u672c\u8a9e \U0001f642", "start": 0.0, "end": 1.0}]
source = tmp_path / "segments.json"
source.write_bytes(json.dumps(segments, ensure_ascii=False).encode(encoding))
route = respx_mock.post("/v1/dev/user/conversations/from-segments").respond(
json={"id": "c1", "status": "completed", "discarded": False}
)
result = cli_runner.invoke(app, ["--json", "conversation", "from-segments", str(source)])
assert result.exit_code == 0, result.output
assert json.loads(route.calls.last.request.content)["transcript_segments"] == segments
def test_conversation_from_segments_rejects_invalid_unicode(config_path, respx_mock, cli_runner, tmp_path) -> None:
source = tmp_path / "segments.json"
source.write_bytes(b'[{"text": "\xff", "start": 0, "end": 1}]')
result = cli_runner.invoke(app, ["--json", "conversation", "from-segments", str(source)])
assert result.exit_code == 1
assert "Invalid JSON" in result.stderr
assert not respx_mock.calls