forked from omnigent-ai/omnigent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_conversation_browser.py
More file actions
292 lines (228 loc) · 9.2 KB
/
Copy pathtest_conversation_browser.py
File metadata and controls
292 lines (228 loc) · 9.2 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""Tests for browser-opening helpers used by CLI frontends."""
from __future__ import annotations
import subprocess
import pytest
import omnigent.conversation_browser as browser
@pytest.mark.parametrize(
"url,expected",
[
# Databricks workspace API mount → the recognizable /omnigent SPA URL.
(
"https://e2-dogfood.staging.cloud.databricks.com/api/2.0/omnigent",
"https://e2-dogfood.staging.cloud.databricks.com/omnigent",
),
# A trailing ``?o=<org>`` selector on the API base is dropped.
(
"https://ws.databricks.com/api/2.0/omnigent?o=123",
"https://ws.databricks.com/omnigent",
),
# Trailing slash on the API mount still maps cleanly.
(
"https://ws.databricks.com/api/2.0/omnigent/",
"https://ws.databricks.com/omnigent",
),
# Non-Databricks URLs pass through unchanged (sans trailing slash).
("http://127.0.0.1:6767", "http://127.0.0.1:6767"),
("https://omnigent-02m5.onrender.com/", "https://omnigent-02m5.onrender.com"),
],
)
def test_display_server_url_maps_databricks_api_mount(url: str, expected: str) -> None:
"""
``display_server_url`` rewrites the Databricks API mount to the SPA URL.
What this proves: the startup banner shows the workspace ``/omnigent``
URL a user recognizes instead of the internal ``/api/2.0/omnigent``
proxy path, while every other target is shown verbatim. A regression
that stopped mapping would leak the API path back into the banner.
:returns: None.
"""
assert browser.display_server_url(url) == expected
@pytest.mark.parametrize(
"url,expected",
[
("https://ws.databricks.com/api/2.0/omnigent", True),
("https://ws.databricks.com/api/2.0/omnigent/", True),
("https://ws.databricks.com/omnigent", False), # the SPA URL, not the API mount
("http://127.0.0.1:6767", False),
("https://omnigent-02m5.onrender.com", False),
],
)
def test_is_workspace_hosted_url(url: str, expected: bool) -> None:
"""
``is_workspace_hosted_url`` is true only for the workspace API mount.
What this proves: the predicate the banner uses to suppress the
server-version row fires for ``/api/2.0/omnigent`` and nothing else, so
non-Databricks targets keep showing their version.
:returns: None.
"""
assert browser.is_workspace_hosted_url(url) is expected
def test_conversation_url_quotes_session_id() -> None:
"""
Conversation URLs percent-encode ids before appending them to the base URL.
:returns: None.
"""
url = browser.conversation_url(
"https://example.com/app/",
"conv with/slash?query",
)
assert url == "https://example.com/app/c/conv%20with%2Fslash%3Fquery"
def test_open_conversation_url_uses_macos_open(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""
macOS launches use the platform ``open`` command with the URL as one argv.
:param monkeypatch: Pytest monkeypatch fixture.
:returns: None.
"""
calls: list[tuple[list[str], object, object, bool]] = []
def fake_run(
args: list[str],
*,
stdout: object,
stderr: object,
check: bool,
) -> subprocess.CompletedProcess[bytes]:
"""
Capture the subprocess launch request.
:param args: Command argv, e.g. ``["open", "http://..."]``.
:param stdout: Captured stdout target.
:param stderr: Captured stderr target.
:param check: Whether non-zero exits should raise.
:returns: Completed process object for the fake ``open`` command.
"""
calls.append((args, stdout, stderr, check))
return subprocess.CompletedProcess(args=args, returncode=0)
monkeypatch.setattr(browser.sys, "platform", "darwin")
monkeypatch.setattr(browser.subprocess, "run", fake_run)
opened = browser.open_conversation_url("http://127.0.0.1:8000/c/conv_abc")
assert opened is True
assert calls == [
(
["open", "http://127.0.0.1:8000/c/conv_abc"],
browser.subprocess.DEVNULL,
browser.subprocess.DEVNULL,
False,
)
]
def test_open_conversation_link_skips_when_disabled(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""
Disabled automatic browser opens return before building or opening a URL.
:param monkeypatch: Pytest monkeypatch fixture.
:returns: None.
"""
warnings: list[str] = []
def fail_open(url: str) -> bool:
"""
Fail if the disabled guard calls the opener.
:param url: Browser URL, e.g. ``"http://localhost/c/conv_abc"``.
:returns: Never returns; this stub always fails the test.
:raises AssertionError: Always raised when called.
"""
raise AssertionError(f"disabled guard should not open {url}")
monkeypatch.setattr(browser, "open_conversation_url", fail_open)
browser.open_conversation_link_if_enabled(
base_url="http://127.0.0.1:8000/",
conversation_id="conv abc",
enabled=False,
warn=warnings.append,
)
assert warnings == []
def test_open_conversation_link_warns_when_opener_declines(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""
Failed opener attempts surface a warning with the conversation URL.
:param monkeypatch: Pytest monkeypatch fixture.
:returns: None.
"""
warnings: list[str] = []
def fake_open(url: str) -> bool:
"""
Simulate a platform opener that declines the URL.
:param url: Browser URL, e.g. ``"http://localhost/c/conv_abc"``.
:returns: ``False`` to signal that no opener accepted the URL.
"""
return False
monkeypatch.setattr(browser, "open_conversation_url", fake_open)
browser.open_conversation_link_if_enabled(
base_url="http://127.0.0.1:8000/",
conversation_id="conv abc",
enabled=True,
warn=warnings.append,
)
assert warnings == [
"Warning: no browser opener accepted conversation URL http://127.0.0.1:8000/c/conv%20abc"
]
def test_open_conversation_link_warns_when_opener_raises_oserror(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""
OSError from the platform opener is surfaced through the warning callback.
:param monkeypatch: Pytest monkeypatch fixture.
:returns: None.
"""
warnings: list[str] = []
def fail_open(url: str) -> bool:
"""
Simulate an opener executable that cannot be started.
:param url: Browser URL, e.g. ``"http://localhost/c/conv_abc"``.
:returns: Never returns because the opener raises.
:raises OSError: Always raised to simulate a missing opener.
"""
raise OSError("missing opener")
monkeypatch.setattr(browser, "open_conversation_url", fail_open)
browser.open_conversation_link_if_enabled(
base_url="http://127.0.0.1:8000/",
conversation_id="conv abc",
enabled=True,
warn=warnings.append,
)
assert warnings == [
"Warning: failed to open conversation URL "
"http://127.0.0.1:8000/c/conv%20abc: missing opener"
]
def test_conversation_url_maps_workspace_hosted_server_to_ui_mount(tmp_path, monkeypatch) -> None:
"""Workspace-hosted servers link to the SPA mount with the org selector.
The server base is the API proxy (``/api/2.0/omnigent``) — linking
there returns JSON, not the web UI. The browser URL must land on
``/omnigent`` and carry ``?o=<org>`` recorded by ``omnigent
login`` so multi-org workspaces open in the right one.
"""
from omnigent.cli_auth import store_databricks_auth
from omnigent.conversation_browser import conversation_url
monkeypatch.setattr(
"omnigent.cli_auth._token_file_path",
lambda: tmp_path / "auth_tokens.json",
)
server = "https://example.databricks.com/api/2.0/omnigent"
store_databricks_auth(
server,
"https://example.databricks.com",
org_id="2850744067564480",
)
url = conversation_url(server, "conv_abc123")
assert url == ("https://example.databricks.com/omnigent/c/conv_abc123?o=2850744067564480")
def test_conversation_url_workspace_hosted_without_org_record(tmp_path, monkeypatch) -> None:
"""No recorded org id → SPA mount link without the ?o selector.
Single-org workspaces resolve fine without it; inventing an org id
would be worse than omitting it.
"""
from omnigent.conversation_browser import conversation_url
monkeypatch.setattr(
"omnigent.cli_auth._token_file_path",
lambda: tmp_path / "auth_tokens.json",
)
url = conversation_url("https://example.databricks.com/api/2.0/omnigent", "conv_abc123")
assert url == "https://example.databricks.com/omnigent/c/conv_abc123"
def test_conversation_url_plain_server_unchanged(tmp_path, monkeypatch) -> None:
"""Non-workspace servers keep the plain /c/<id> link shape."""
from omnigent.conversation_browser import conversation_url
monkeypatch.setattr(
"omnigent.cli_auth._token_file_path",
lambda: tmp_path / "auth_tokens.json",
)
assert (
conversation_url("http://127.0.0.1:6767", "conv_abc123")
== "http://127.0.0.1:6767/c/conv_abc123"
)