forked from omnigent-ai/omnigent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_session_export.py
More file actions
225 lines (193 loc) · 6.97 KB
/
Copy pathtest_cli_session_export.py
File metadata and controls
225 lines (193 loc) · 6.97 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""Unit tests for ``omnigent session export``."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
from unittest.mock import patch
import httpx
import respx
from click.testing import CliRunner
from omnigent.cli import cli
_BASE = "http://localhost:6767"
_SESSION_META = {
"id": "conv_abc123",
"title": "test session",
"status": "idle",
"created_at": 1700000000,
"updated_at": 1700000001,
"agent_id": None,
"agent_name": None,
"items": [],
}
_ITEMS_PAGE = {
"data": [
{
"id": "msg_1",
"type": "message",
"status": "completed",
"response_id": "resp_1",
"role": "user",
"content": [{"type": "input_text", "text": "hello"}],
},
{
"id": "msg_2",
"type": "message",
"status": "completed",
"response_id": "resp_1",
"role": "assistant",
"content": [{"type": "output_text", "text": "hi there"}],
"model": "my-agent",
},
],
"first_id": "msg_1",
"last_id": "msg_2",
"has_more": False,
}
def _patch_server(base_url: str = _BASE) -> Any:
"""Patch the CLI so it uses *base_url* without spawning a real server."""
return patch("omnigent.cli._resolve_attach_server", return_value=base_url)
@respx.mock
def test_session_export_writes_jsonl(tmp_path: Path) -> None:
"""Export writes one session_meta line then one item line per item."""
respx.get(f"{_BASE}/v1/sessions/conv_abc123").mock(
return_value=httpx.Response(200, json=_SESSION_META)
)
respx.get(f"{_BASE}/v1/sessions/conv_abc123/items").mock(
return_value=httpx.Response(200, json=_ITEMS_PAGE)
)
out_file = tmp_path / "out.jsonl"
runner = CliRunner()
with _patch_server():
result = runner.invoke(
cli,
["session", "export", "--id", "conv_abc123", "--output", str(out_file)],
)
assert result.exit_code == 0, result.output
assert out_file.exists()
lines = [json.loads(line) for line in out_file.read_text().splitlines() if line]
assert len(lines) == 3 # 1 meta + 2 items
meta = lines[0]
assert meta["record_type"] == "session_meta"
assert meta["id"] == "conv_abc123"
assert meta["title"] == "test session"
item_lines = lines[1:]
assert all(r["record_type"] == "item" for r in item_lines)
assert [r["role"] for r in item_lines] == ["user", "assistant"]
assert item_lines[1]["content"] == [{"type": "output_text", "text": "hi there"}]
@respx.mock
def test_session_export_default_filename(tmp_path: Path) -> None:
"""Without --output, the file is named <session_id>.jsonl in cwd."""
respx.get(f"{_BASE}/v1/sessions/conv_abc123").mock(
return_value=httpx.Response(200, json=_SESSION_META)
)
respx.get(f"{_BASE}/v1/sessions/conv_abc123/items").mock(
return_value=httpx.Response(200, json={**_ITEMS_PAGE, "data": [], "has_more": False})
)
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=tmp_path), _patch_server():
result = runner.invoke(cli, ["session", "export", "--id", "conv_abc123"])
assert result.exit_code == 0, result.output
default_path = Path("conv_abc123.jsonl")
assert default_path.exists()
lines = [json.loads(line) for line in default_path.read_text().splitlines() if line]
assert len(lines) == 1
assert lines[0]["record_type"] == "session_meta"
assert lines[0]["id"] == "conv_abc123"
@respx.mock
def test_session_export_missing_session_errors(tmp_path: Path) -> None:
"""Export of an unknown session id exits non-zero with a clear message."""
respx.get(f"{_BASE}/v1/sessions/conv_doesnotexist").mock(
return_value=httpx.Response(404, json={"error": "not found"})
)
runner = CliRunner()
with _patch_server():
result = runner.invoke(
cli,
[
"session",
"export",
"--id",
"conv_doesnotexist",
"--output",
str(tmp_path / "out.jsonl"),
],
)
assert result.exit_code != 0
assert "conv_doesnotexist" in result.output
@respx.mock
def test_session_export_items_ordered_ascending(tmp_path: Path) -> None:
"""Items in the JSONL appear in ascending position order (user then assistant)."""
respx.get(f"{_BASE}/v1/sessions/conv_abc123").mock(
return_value=httpx.Response(200, json=_SESSION_META)
)
respx.get(f"{_BASE}/v1/sessions/conv_abc123/items").mock(
return_value=httpx.Response(200, json=_ITEMS_PAGE)
)
out_file = tmp_path / "ordered.jsonl"
runner = CliRunner()
with _patch_server():
result = runner.invoke(
cli,
["session", "export", "--id", "conv_abc123", "--output", str(out_file)],
)
assert result.exit_code == 0, result.output
records = [json.loads(line) for line in out_file.read_text().splitlines() if line]
item_records = [r for r in records if r["record_type"] == "item"]
assert len(item_records) == 2
assert item_records[0]["role"] == "user"
assert item_records[1]["role"] == "assistant"
@respx.mock
def test_session_export_pagination(tmp_path: Path) -> None:
"""Export follows has_more cursors to fetch all pages."""
page1 = {
"data": [
{
"id": "msg_1",
"type": "message",
"status": "completed",
"response_id": "r1",
"role": "user",
"content": [],
}
],
"first_id": "msg_1",
"last_id": "msg_1",
"has_more": True,
}
page2 = {
"data": [
{
"id": "msg_2",
"type": "message",
"status": "completed",
"response_id": "r1",
"role": "assistant",
"content": [],
"model": "ag",
}
],
"first_id": "msg_2",
"last_id": "msg_2",
"has_more": False,
}
respx.get(f"{_BASE}/v1/sessions/conv_abc123").mock(
return_value=httpx.Response(200, json=_SESSION_META)
)
# First call (no after param) → page1; second call (after=msg_1) → page2.
items_route = respx.get(f"{_BASE}/v1/sessions/conv_abc123/items")
items_route.side_effect = [
httpx.Response(200, json=page1),
httpx.Response(200, json=page2),
]
out_file = tmp_path / "paged.jsonl"
runner = CliRunner()
with _patch_server():
result = runner.invoke(
cli,
["session", "export", "--id", "conv_abc123", "--output", str(out_file)],
)
assert result.exit_code == 0, result.output
records = [json.loads(line) for line in out_file.read_text().splitlines() if line]
item_records = [r for r in records if r["record_type"] == "item"]
assert len(item_records) == 2
assert [r["id"] for r in item_records] == ["msg_1", "msg_2"]