-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtest_e2e.py
More file actions
1132 lines (933 loc) · 48.8 KB
/
Copy pathtest_e2e.py
File metadata and controls
1132 lines (933 loc) · 48.8 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""End-to-end integration tests that require a live Databricks workspace.
Run with:
UCODE_TEST_WORKSPACE=https://your-workspace.databricks.com uv run pytest tests/test_e2e.py -v
All tests in this file are skipped automatically when the env var is not set.
The agent-launch tests are also skipped per-agent/model when the binary is not
installed or no models are available.
"""
from __future__ import annotations
import json
import os
import shutil
import subprocess
from urllib import error as urllib_error
from urllib import request as urllib_request
import pytest
from ucode.databricks import (
build_shared_base_urls,
build_tool_base_url,
discover_sql_warehouse_http_path,
ensure_ai_gateway_v2,
fetch_ai_gateway_claude_models,
fetch_codex_models,
fetch_gemini_models,
has_valid_databricks_auth,
is_model_provider_feature_unavailable,
list_model_provider_services,
list_tool_provider_services,
service_usable_for_tool,
workspace_hostname,
)
from ucode.ui import normalize_workspace_url
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _ws() -> str:
raw = os.environ.get("UCODE_TEST_WORKSPACE", "").strip().rstrip("/")
return normalize_workspace_url(raw) if raw else ""
def _skip_if_no_workspace():
if not _ws():
pytest.skip("Set UCODE_TEST_WORKSPACE=https://... to run E2E tests")
def _run_agent(
cmd: list[str], env: dict | None = None, timeout: int = 60
) -> subprocess.CompletedProcess:
return subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=timeout,
env=env,
stdin=subprocess.DEVNULL,
)
def _run_gemini_gateway_smoke(workspace: str, model: str, token: str) -> str:
"""Call the Gemini gateway directly with a text-only prompt.
This keeps auth recovery coverage focused on the recovered Databricks token
instead of Gemini CLI's separate tool-calling request shape.
"""
url = f"{build_tool_base_url('gemini', workspace)}/v1beta/models/{model}:generateContent"
payload = {
"contents": [
{"role": "user", "parts": [{"text": "say hi in 5 words or less"}]},
],
}
req = urllib_request.Request(
url,
data=json.dumps(payload).encode("utf-8"),
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"Accept": "application/json",
},
method="POST",
)
try:
with urllib_request.urlopen(req, timeout=30) as response:
body = response.read().decode("utf-8")
except urllib_error.HTTPError as exc:
body = exc.read().decode("utf-8", errors="replace") if exc.fp else ""
raise AssertionError(f"Gemini gateway smoke failed: HTTP {exc.code}: {body[:500]}") from exc
data = json.loads(body)
return data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "")
def _launchable_model_items(models: dict) -> list[tuple[str, str]]:
return [(family, model_id) for family, model_id in models.items() if model_id]
# ---------------------------------------------------------------------------
# Databricks auth / token
# ---------------------------------------------------------------------------
class TestDatabricksAuth:
def test_has_valid_auth(self, e2e_workspace):
assert has_valid_databricks_auth(e2e_workspace), (
"No valid Databricks auth found. Run `databricks auth login` first."
)
def test_get_token_returns_non_empty_string(self, e2e_token):
assert isinstance(e2e_token, str) and len(e2e_token) > 10
# ---------------------------------------------------------------------------
# AI Gateway v2 probe
# ---------------------------------------------------------------------------
class TestAiGatewayV2:
def test_ensure_ai_gateway_v2_does_not_raise(self, e2e_workspace, e2e_token):
ensure_ai_gateway_v2(e2e_workspace, e2e_token)
def test_workspace_hostname_resolves(self, e2e_workspace):
hostname = workspace_hostname(e2e_workspace)
assert "." in hostname
# ---------------------------------------------------------------------------
# Model discovery
# ---------------------------------------------------------------------------
class TestModelDiscovery:
def test_fetch_claude_models_returns_dict(self, e2e_workspace, e2e_token):
models = fetch_ai_gateway_claude_models(e2e_workspace, e2e_token)
assert isinstance(models, dict)
assert models, "No Claude models found — is the Anthropic route enabled on this workspace?"
def test_fetch_gemini_models_returns_list(self, e2e_workspace, e2e_token):
models = fetch_gemini_models(e2e_workspace, e2e_token)
assert isinstance(models, list)
def test_fetch_codex_models_returns_list(self, e2e_workspace, e2e_token):
models = fetch_codex_models(e2e_workspace, e2e_token)
assert isinstance(models, list)
# ---------------------------------------------------------------------------
# Model Provider Services discovery
# ---------------------------------------------------------------------------
class TestModelProviderServicesDiscovery:
def test_list_returns_services_or_skips_when_feature_off(self, e2e_workspace, e2e_token):
services, reason = list_model_provider_services(e2e_workspace, e2e_token)
if is_model_provider_feature_unavailable(reason):
pytest.skip("Model Provider Service feature not enabled on this workspace")
assert reason is None, f"listing failed: {reason}"
assert isinstance(services, list)
for svc in services:
assert set(svc) >= {"name", "provider_type", "targets", "allow_all_targets"}
# Names are stripped of the `model-provider-services/` API prefix.
assert svc["name"] and "/" not in svc["name"]
def test_tool_filter_matches_provider_type(self, e2e_workspace, e2e_token):
services, reason = list_model_provider_services(e2e_workspace, e2e_token)
if is_model_provider_feature_unavailable(reason):
pytest.skip("Model Provider Service feature not enabled on this workspace")
assert reason is None
claude_names, _ = list_tool_provider_services("claude", e2e_workspace, e2e_token)
codex_names, _ = list_tool_provider_services("codex", e2e_workspace, e2e_token)
# claude routes through Anthropic and Bedrock services (Bedrock only when
# it exposes Claude models); codex through OpenAI.
assert set(claude_names) == {
s["name"] for s in services if service_usable_for_tool("claude", s)
}
assert set(codex_names) == {
s["name"] for s in services if service_usable_for_tool("codex", s)
}
# ---------------------------------------------------------------------------
# URL builders
# ---------------------------------------------------------------------------
class TestUrlBuilders:
def test_codex_url_contains_workspace(self, e2e_workspace):
assert e2e_workspace in build_tool_base_url("codex", e2e_workspace)
def test_claude_url_contains_workspace(self, e2e_workspace):
assert e2e_workspace in build_tool_base_url("claude", e2e_workspace)
def test_shared_base_urls_all_tools(self, e2e_workspace):
urls = build_shared_base_urls(e2e_workspace)
for tool in ("codex", "claude", "gemini", "opencode", "copilot", "pi"):
assert tool in urls
# ---------------------------------------------------------------------------
# State round-trip
# ---------------------------------------------------------------------------
class TestStateRoundTrip:
def test_configure_shared_state_and_reload(
self, tmp_path, monkeypatch, e2e_state, e2e_workspace
):
import ucode.config_io as config_io_mod
import ucode.state as state_mod
from ucode.state import load_state, save_state
monkeypatch.setattr(state_mod, "STATE_PATH", tmp_path / "state.json")
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
save_state(e2e_state)
loaded = load_state()
assert loaded["workspace"] == e2e_workspace
assert loaded["claude_models"] == e2e_state["claude_models"]
assert loaded["base_urls"]["codex"] == f"{e2e_workspace}/ai-gateway/codex/v1"
# ---------------------------------------------------------------------------
# SQL warehouse discovery
# ---------------------------------------------------------------------------
class TestSqlWarehouseDiscovery:
def test_discovers_http_path(self, e2e_workspace, e2e_token):
try:
http_path = discover_sql_warehouse_http_path(e2e_workspace, e2e_token, quiet=True)
except RuntimeError as exc:
pytest.skip(f"No SQL warehouse available: {exc}")
assert http_path.startswith("/sql/1.0/warehouses/")
# ---------------------------------------------------------------------------
# Configure flow with user-selected subset
# ---------------------------------------------------------------------------
#
# Verifies that when the user picks a subset in the multi-select prompt,
# only those tools get configured and previously-configured tools are
# preserved in state["available_tools"].
# ---------------------------------------------------------------------------
class TestConfigureSubset:
def _redirect_config_paths(self, monkeypatch, tmp_path):
"""Redirect every agent's config path into tmp_path so the test
doesn't touch the developer's real ~/.codex, ~/.claude, etc."""
import ucode.config_io as config_io_mod
from ucode.agents import claude, codex, copilot, gemini, opencode, pi
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
codex_dir = tmp_path / "codex_home" / ".codex"
codex_dir.mkdir(parents=True, exist_ok=True)
monkeypatch.setattr(codex, "CODEX_CONFIG_PATH", codex_dir / "ucode.config.toml")
monkeypatch.setattr(codex, "CODEX_BACKUP_PATH", tmp_path / "codex.backup.toml")
monkeypatch.setattr(claude, "CLAUDE_SETTINGS_PATH", tmp_path / "claude-settings.json")
monkeypatch.setattr(claude, "CLAUDE_BACKUP_PATH", tmp_path / "claude.backup.json")
monkeypatch.setattr(gemini, "GEMINI_ENV_PATH", tmp_path / "gemini-ucode.env")
monkeypatch.setattr(gemini, "GEMINI_BACKUP_PATH", tmp_path / "gemini.backup")
monkeypatch.setattr(opencode, "OPENCODE_CONFIG_PATH", tmp_path / "opencode.json")
monkeypatch.setattr(opencode, "OPENCODE_BACKUP_PATH", tmp_path / "opencode.backup.json")
monkeypatch.setattr(copilot, "COPILOT_ENV_PATH", tmp_path / ".copilot-env")
monkeypatch.setattr(copilot, "COPILOT_BACKUP_PATH", tmp_path / "copilot.backup")
monkeypatch.setattr(pi, "PI_CONFIG_PATH", tmp_path / "pi-models.json")
monkeypatch.setattr(pi, "PI_BACKUP_PATH", tmp_path / "pi-models.backup.json")
return codex_dir / "ucode.config.toml"
def test_only_picks_codex_writes_only_codex_config(self, tmp_path, monkeypatch, e2e_workspace):
"""User selects only codex → only codex's config file is written and
state['available_tools'] contains exactly ['codex']."""
import ucode.cli as cli_mod
import ucode.state as state_mod
from ucode.state import load_state
codex_path = self._redirect_config_paths(monkeypatch, tmp_path)
monkeypatch.setattr(state_mod, "STATE_PATH", tmp_path / "state.json")
# Don't actually run `databricks auth login`; the developer running
# this suite is already authenticated.
monkeypatch.setattr("ucode.cli.run_databricks_login", lambda ws, profile=None: None)
# Skip the workspace prompt and the multi-select picker.
monkeypatch.setattr(
cli_mod, "_prompt_for_configuration", lambda tool=None: (e2e_workspace, None)
)
monkeypatch.setattr(cli_mod, "prompt_for_tools", lambda available: ["codex"])
# Skip binary install + post-config validation; we're testing the
# selection plumbing, not the agent binaries themselves.
monkeypatch.setattr(cli_mod, "install_tool_binary", lambda tool, **kwargs: True)
monkeypatch.setattr(cli_mod, "validate_all_tools", lambda state: None)
# Answer the interactive prompts (provider picker + AI Tools opt-in) so no
# prompt reads stdin under capture; "databricks" keeps the Databricks path.
monkeypatch.setattr(cli_mod, "prompt_for_selection", lambda prompt, options: "databricks")
monkeypatch.setattr(cli_mod, "prompt_yes_no_default", lambda prompt, *, default: default)
rc = cli_mod.configure_workspace_command()
assert rc == 0
assert codex_path.exists(), "codex config should have been written"
assert not (tmp_path / "claude-settings.json").exists(), "claude config should NOT exist"
assert not (tmp_path / "gemini-ucode.env").exists(), "gemini env should NOT exist"
assert not (tmp_path / "opencode.json").exists(), "opencode config should NOT exist"
assert not (tmp_path / ".copilot-env").exists(), "copilot env should NOT exist"
assert not (tmp_path / "pi-models.json").exists(), "pi config should NOT exist"
state = load_state()
assert state["available_tools"] == ["codex"]
def test_rerun_with_different_pick_preserves_previous(
self, tmp_path, monkeypatch, e2e_workspace
):
"""First run picks codex; second run picks claude. State should end
up with both tools in available_tools (the un-picked codex is not
dropped on the second run)."""
import ucode.cli as cli_mod
import ucode.state as state_mod
from ucode.state import load_state
self._redirect_config_paths(monkeypatch, tmp_path)
monkeypatch.setattr(state_mod, "STATE_PATH", tmp_path / "state.json")
monkeypatch.setattr("ucode.cli.run_databricks_login", lambda ws, profile=None: None)
monkeypatch.setattr(
cli_mod, "_prompt_for_configuration", lambda tool=None: (e2e_workspace, None)
)
monkeypatch.setattr(cli_mod, "install_tool_binary", lambda tool, **kwargs: True)
monkeypatch.setattr(cli_mod, "validate_all_tools", lambda state: None)
# Answer the interactive prompts (provider picker + AI Tools opt-in) so no
# prompt reads stdin under capture; "databricks" keeps the Databricks path.
monkeypatch.setattr(cli_mod, "prompt_for_selection", lambda prompt, options: "databricks")
monkeypatch.setattr(cli_mod, "prompt_yes_no_default", lambda prompt, *, default: default)
# First run: pick codex.
monkeypatch.setattr(cli_mod, "prompt_for_tools", lambda available: ["codex"])
assert cli_mod.configure_workspace_command() == 0
assert load_state()["available_tools"] == ["codex"]
# Claude needs to be available on this workspace for the second run
# to be a meaningful test.
from ucode.databricks import fetch_ai_gateway_claude_models, get_databricks_token
token = get_databricks_token(e2e_workspace)
if not fetch_ai_gateway_claude_models(e2e_workspace, token):
pytest.skip("No Claude models on this workspace; can't test multi-tool merge.")
# Second run: pick claude only. Codex should remain in available_tools.
monkeypatch.setattr(cli_mod, "prompt_for_tools", lambda available: ["claude"])
assert cli_mod.configure_workspace_command() == 0
assert set(load_state()["available_tools"]) == {"codex", "claude"}
def test_empty_pick_returns_zero_and_writes_nothing(self, tmp_path, monkeypatch, e2e_workspace):
"""User unchecks everything in the picker → no config files are
written and the command exits 0."""
import ucode.cli as cli_mod
import ucode.state as state_mod
codex_path = self._redirect_config_paths(monkeypatch, tmp_path)
monkeypatch.setattr(state_mod, "STATE_PATH", tmp_path / "state.json")
monkeypatch.setattr("ucode.cli.run_databricks_login", lambda ws, profile=None: None)
monkeypatch.setattr(
cli_mod, "_prompt_for_configuration", lambda tool=None: (e2e_workspace, None)
)
monkeypatch.setattr(cli_mod, "prompt_for_tools", lambda available: [])
install_calls: list[str] = []
monkeypatch.setattr(
cli_mod,
"install_tool_binary",
lambda tool, **kwargs: install_calls.append(tool) or True,
)
monkeypatch.setattr(cli_mod, "validate_all_tools", lambda state: None)
rc = cli_mod.configure_workspace_command()
assert rc == 0
assert not codex_path.exists()
assert install_calls == [], "no tool binaries should be installed when nothing is picked"
# ---------------------------------------------------------------------------
# Agent launch tests — one test per (agent, model)
# ---------------------------------------------------------------------------
#
# Each test:
# 1. Writes the agent config for the specific model
# 2. Runs the binary with the validate_cmd prompt
# 3. Asserts exit code 0 and non-empty stdout
#
# Tests are skipped when the binary is not installed or no models are available.
# ---------------------------------------------------------------------------
def _require_binary(binary: str):
if not shutil.which(binary):
pytest.skip(f"`{binary}` is not installed")
class TestCodexLaunch:
"""Run codex against every available codex model."""
# Substrings of model IDs that are known-incompatible with the codex CLI on
# Databricks today. Each entry should have a comment explaining why.
CODEX_INCOMPATIBLE_MODEL_FRAGMENTS = (
# nano endpoint is unreliably slow and times out past the 60s budget.
"gpt-5-4-nano",
# These endpoints are discoverable as responses-capable, but the
# backing OpenAI endpoint returns ENDPOINT_NOT_FOUND in the CI region.
"gpt-5-6-luna",
"gpt-5-6-sol",
"gpt-5-6-terra",
)
def _codex_models(self, e2e_state: dict) -> list[str]:
models = [
model
for model in (e2e_state.get("codex_models") or [])
if not any(frag in model for frag in self.CODEX_INCOMPATIBLE_MODEL_FRAGMENTS)
]
if not models:
pytest.skip("No Codex models available on this workspace")
return models
def test_launch_codex_per_model(self, tmp_path, monkeypatch, e2e_state, e2e_workspace):
"""Parametrized inline — iterates over all codex models and asserts each works."""
import ucode.config_io as config_io_mod
from ucode.agents import codex
_require_binary("codex")
models = self._codex_models(e2e_state)
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
config_dir = tmp_path / "codex_home" / ".codex"
config_dir.mkdir(parents=True)
config_path = config_dir / "ucode.config.toml"
backup_path = tmp_path / "codex-config.backup.toml"
monkeypatch.setattr(codex, "CODEX_CONFIG_PATH", config_path)
monkeypatch.setattr(codex, "CODEX_BACKUP_PATH", backup_path)
failures = []
timeout_seconds = int(os.environ.get("UCODE_E2E_AGENT_TIMEOUT", "60"))
for model in models:
state = {**e2e_state, "workspace": e2e_workspace}
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
codex.write_tool_config(state, model)
cmd = codex.validate_cmd("codex")
try:
result = _run_agent(
cmd,
env={**os.environ, "CODEX_HOME": str(config_dir)},
timeout=timeout_seconds,
)
except subprocess.TimeoutExpired:
failures.append(f"model={model} timed out after {timeout_seconds}s")
continue
if result.returncode != 0 or not (result.stdout or result.stderr).strip():
failures.append(
f"model={model} rc={result.returncode} "
f"stdout={result.stdout[:200]!r} stderr={result.stderr[:200]!r}"
)
assert not failures, "Codex launch failures:\n" + "\n".join(failures)
class TestClaudeLaunch:
"""Run claude against every available claude model (sonnet, opus, haiku)."""
def test_launch_claude_per_model(
self, tmp_path, monkeypatch, e2e_state, e2e_workspace, e2e_token
):
import ucode.config_io as config_io_mod
from ucode.agents import claude
_require_binary("claude")
claude_models: dict = e2e_state.get("claude_models") or {}
if not claude_models:
pytest.skip("No Claude models available on this workspace")
launchable_models = _launchable_model_items(claude_models)
if not launchable_models:
pytest.skip("No launchable Claude models available on this workspace")
# Use an isolated config dir so the claude subprocess never reads or
# writes ~/.claude/settings.json during this test.
config_dir = tmp_path / "claude_config"
config_dir.mkdir()
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
monkeypatch.setattr(claude, "CLAUDE_SETTINGS_PATH", config_dir / "settings.json")
monkeypatch.setattr(claude, "CLAUDE_BACKUP_PATH", tmp_path / "claude-settings.backup.json")
base_url = build_tool_base_url("claude", e2e_workspace)
failures = []
for family, model_id in launchable_models:
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
claude.write_tool_config({**e2e_state, "workspace": e2e_workspace}, model_id)
env = {
**os.environ,
"CLAUDE_CONFIG_DIR": str(config_dir),
"ANTHROPIC_MODEL": model_id,
"ANTHROPIC_BASE_URL": base_url,
"ANTHROPIC_API_KEY": e2e_token,
}
cmd = claude.validate_cmd("claude")
result = _run_agent(cmd, env=env, timeout=90)
combined = (result.stdout + result.stderr).strip()
if result.returncode != 0 or not combined:
failures.append(
f"family={family} model={model_id} rc={result.returncode} "
f"stdout={result.stdout[:300]!r} stderr={result.stderr[:300]!r}"
)
assert not failures, "Claude launch failures:\n" + "\n".join(failures)
class TestModelProviderLaunch:
"""Launch claude/codex routed through a real Model Provider Service.
Picks the first matching service on the workspace, writes a provider config
(no Databricks model pinned), and runs the agent so a real request flows
through the MPS gateway. Skips when the feature is off, no service exists, or
the caller lacks permission on the backing connection.
"""
@staticmethod
def _first_service(tool: str, workspace: str, token: str) -> str:
names, reason = list_tool_provider_services(tool, workspace, token)
if is_model_provider_feature_unavailable(reason):
pytest.skip("Model Provider Service feature not enabled on this workspace")
if reason is not None:
pytest.skip(f"could not list provider services: {reason}")
if not names:
pytest.skip(f"no {tool} model provider services available on this workspace")
return names[0]
@staticmethod
def _skip_if_no_permission(combined: str, provider: str) -> None:
if "USE CONNECTION" in combined or "EXECUTE" in combined:
pytest.skip(f"no permission on provider {provider}: {combined[:200]}")
def test_launch_claude_through_provider(
self, tmp_path, monkeypatch, e2e_state, e2e_workspace, e2e_token
):
import ucode.config_io as config_io_mod
from ucode.agents import claude
_require_binary("claude")
provider = self._first_service("claude", e2e_workspace, e2e_token)
config_dir = tmp_path / "claude_config"
config_dir.mkdir()
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
monkeypatch.setattr(claude, "CLAUDE_SETTINGS_PATH", config_dir / "settings.json")
monkeypatch.setattr(claude, "CLAUDE_BACKUP_PATH", tmp_path / "claude-settings.backup.json")
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
# No model pinned — the provider header (written into the settings
# env block) routes the agent's own canonical model name.
claude.write_tool_config(
{**e2e_state, "workspace": e2e_workspace}, None, provider=provider
)
env = {
**os.environ,
"CLAUDE_CONFIG_DIR": str(config_dir),
"ANTHROPIC_BASE_URL": build_tool_base_url("claude", e2e_workspace),
"ANTHROPIC_API_KEY": e2e_token,
}
result = _run_agent(claude.validate_cmd("claude"), env=env, timeout=90)
combined = (result.stdout + result.stderr).strip()
self._skip_if_no_permission(combined, provider)
assert result.returncode == 0 and combined, (
f"provider={provider} rc={result.returncode} "
f"stdout={result.stdout[:300]!r} stderr={result.stderr[:300]!r}"
)
def test_launch_codex_through_provider(
self, tmp_path, monkeypatch, e2e_state, e2e_workspace, e2e_token
):
import ucode.config_io as config_io_mod
from ucode.agents import codex
_require_binary("codex")
provider = self._first_service("codex", e2e_workspace, e2e_token)
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
config_dir = tmp_path / "codex_home" / ".codex"
config_dir.mkdir(parents=True)
monkeypatch.setattr(codex, "CODEX_CONFIG_PATH", config_dir / "ucode.config.toml")
monkeypatch.setattr(codex, "CODEX_BACKUP_PATH", tmp_path / "codex-config.backup.toml")
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
codex.write_tool_config(
{**e2e_state, "workspace": e2e_workspace}, None, provider=provider
)
timeout_seconds = int(os.environ.get("UCODE_E2E_AGENT_TIMEOUT", "60"))
try:
result = _run_agent(
codex.validate_cmd("codex"),
env={**os.environ, "CODEX_HOME": str(config_dir)},
timeout=timeout_seconds,
)
except subprocess.TimeoutExpired:
pytest.fail(f"provider={provider} timed out after {timeout_seconds}s")
combined = (result.stdout + result.stderr).strip()
self._skip_if_no_permission(combined, provider)
assert result.returncode == 0 and combined, (
f"provider={provider} rc={result.returncode} "
f"stdout={result.stdout[:300]!r} stderr={result.stderr[:300]!r}"
)
class TestGeminiLaunch:
"""Run gemini against every available gemini model."""
def test_launch_gemini_per_model(
self, tmp_path, monkeypatch, e2e_state, e2e_workspace, e2e_token
):
import ucode.config_io as config_io_mod
from ucode.agents import gemini, validate_tool
_require_binary("gemini")
# Gemini CLI >= 0.45 rewrites forced flash model ids (e.g.
# 'databricks-gemini-3-5-flash') to 'gemini-3.5-flash', which Unity
# Catalog rejects. ucode caps the supported version below 0.45 and
# offers a downgrade; skip here if the runner still has a too-new build
# rather than asserting against a version we deliberately don't support.
too_new = gemini.too_new_version()
if too_new is not None:
pytest.skip(
f"Installed Gemini CLI {too_new} is past the supported ceiling "
f"({gemini.MAX_GEMINI_VERSION_TEXT}); run `ucode gemini` to downgrade."
)
gemini_models: list = e2e_state.get("gemini_models") or []
if not gemini_models:
pytest.skip("No Gemini models available on this workspace")
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
monkeypatch.setattr(gemini, "GEMINI_ENV_PATH", tmp_path / "ucode.env")
monkeypatch.setattr(gemini, "GEMINI_BACKUP_PATH", tmp_path / "gemini-ucode-env.backup")
monkeypatch.setattr(gemini, "GEMINI_HOME_DIR", tmp_path / ".gemini-home")
monkeypatch.setattr(
gemini, "GEMINI_SETTINGS_PATH", tmp_path / ".gemini-home" / ".gemini" / "settings.json"
)
# Run from tmp_path so Gemini sees an untrusted folder — that mirrors
# what users hit on a fresh checkout and exercises the trust + .env
# discovery code paths that previously broke validation.
monkeypatch.chdir(tmp_path)
failures = []
for model in gemini_models:
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
mp.setattr(
"ucode.agents.gemini.get_databricks_token",
lambda ws, profile=None, **kwargs: e2e_token,
)
state = {**e2e_state, "workspace": e2e_workspace}
gemini.write_tool_config(state, model, token=e2e_token)
# Exercise the real production validate flow — same code path
# that `ucode configure` invokes after writing the config.
captured_state = state
mp.setattr("ucode.agents.load_state", lambda s=captured_state: s)
ok, err = validate_tool("gemini")
if not ok:
failures.append(f"model={model} err={err}")
assert not failures, "Gemini launch failures:\n" + "\n".join(failures)
class TestGeminiFreshInstall:
"""Verify Gemini works from ucode env without writing user settings.json."""
def test_does_not_write_settings_json_for_auth(
self, tmp_path, monkeypatch, e2e_state, e2e_workspace, e2e_token
):
import ucode.config_io as config_io_mod
from ucode.agents import gemini
settings_path = tmp_path / "settings.json"
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
monkeypatch.setattr(gemini, "GEMINI_ENV_PATH", tmp_path / "ucode.env")
monkeypatch.setattr(gemini, "GEMINI_BACKUP_PATH", tmp_path / "gemini-ucode-env.backup")
gemini_models: list = e2e_state.get("gemini_models") or []
model = gemini_models[0] if gemini_models else "some-model"
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
gemini.write_tool_config(
{**e2e_state, "workspace": e2e_workspace}, model, token=e2e_token
)
assert not settings_path.exists(), "settings.json should not be created"
assert (tmp_path / "ucode.env").exists(), "ucode Gemini env file was not created"
class TestOpencodeLaunch:
"""Run opencode against every available opencode model (anthropic + gemini)."""
# Models that hang opencode well past 180s on the staging gateway with
# no stderr beyond the initial `> build · <model>` line, while every
# other configured model returns in ~3s. Backend-side latency we can't
# influence from this repo; skip rather than block CI.
SKIP_MODELS: frozenset[str] = frozenset(
{"databricks-gemini-3-1-flash-lite", "databricks-gemini-3-1-flash-lite-image"}
)
def _all_models(self, e2e_state: dict) -> list[tuple[str, str]]:
"""Return [(provider, model_id), ...] for all opencode models."""
opencode_models: dict = e2e_state.get("opencode_models") or {}
out: list[tuple[str, str]] = []
for provider, models in opencode_models.items():
for model in models or []:
if model in self.SKIP_MODELS:
continue
out.append((provider, model))
return out
def test_launch_opencode_per_model(
self, tmp_path, monkeypatch, e2e_state, e2e_workspace, e2e_token
):
import ucode.config_io as config_io_mod
from ucode.agents import opencode
_require_binary("opencode")
models = self._all_models(e2e_state)
if not models:
pytest.skip("No OpenCode models available on this workspace")
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
xdg = tmp_path / "opencode-xdg"
config_path = xdg / "opencode" / "opencode.json"
backup_path = tmp_path / "opencode-config.backup.json"
monkeypatch.setattr(opencode, "OPENCODE_XDG_CONFIG_HOME", xdg)
monkeypatch.setattr(opencode, "OPENCODE_CONFIG_PATH", config_path)
monkeypatch.setattr(opencode, "OPENCODE_BACKUP_PATH", backup_path)
import sys
import time
print(f"\n[opencode-per-model] {len(models)} models to test", flush=True)
failures = []
for provider, model in models:
# Reset config file before each model so configs don't bleed together
if config_path.exists():
config_path.unlink()
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
mp.setattr(
"ucode.agents.opencode.get_databricks_token",
lambda ws, profile=None, **kwargs: e2e_token,
)
opencode.write_tool_config(
{**e2e_state, "workspace": e2e_workspace},
model,
token=e2e_token,
)
cmd = opencode.validate_cmd("opencode")
print(f"[opencode-per-model] -> {provider}/{model}", flush=True)
t0 = time.monotonic()
try:
result = _run_agent(cmd, env=opencode.build_runtime_env(e2e_token), timeout=180)
except subprocess.TimeoutExpired as exc:
elapsed = time.monotonic() - t0
partial_stdout = (exc.stdout or b"").decode("utf-8", errors="replace")
partial_stderr = (exc.stderr or b"").decode("utf-8", errors="replace")
print(
f"[opencode-per-model] {provider}/{model} TIMEOUT after {elapsed:.1f}s\n"
f" partial stdout: {partial_stdout[:500]!r}\n"
f" partial stderr: {partial_stderr[:500]!r}",
flush=True,
file=sys.stderr,
)
failures.append(
f"provider={provider} model={model} TIMEOUT after {elapsed:.1f}s "
f"stderr={partial_stderr[:300]!r}"
)
continue
elapsed = time.monotonic() - t0
combined = (result.stdout + result.stderr).strip()
status = "OK" if result.returncode == 0 and combined else f"FAIL rc={result.returncode}"
print(f"[opencode-per-model] {provider}/{model} {status} ({elapsed:.1f}s)", flush=True)
if result.returncode != 0 or not combined:
failures.append(
f"provider={provider} model={model} rc={result.returncode} "
f"elapsed={elapsed:.1f}s "
f"stdout={result.stdout[:300]!r} stderr={result.stderr[:300]!r}"
)
assert not failures, "OpenCode launch failures:\n" + "\n".join(failures)
class TestCopilotLaunch:
"""Run copilot against every Claude/codex model via the MLflow chat-completions gateway.
Gemini is excluded by design — Databricks' Gemini translator rejects the
`stream_options` field Copilot CLI sends. Some codex variants are also
incompatible upstream and are listed in COPILOT_INCOMPATIBLE_MODEL_FRAGMENTS.
"""
# Substrings of model IDs that are known-incompatible with Copilot CLI on
# Databricks today. Each entry should have a comment explaining why.
COPILOT_INCOMPATIBLE_MODEL_FRAGMENTS = (
# Codex-tuned endpoints expose only openai/v1/responses and
# cursor/v1/chat/completions, not mlflow/v1/chat/completions.
"-codex",
# gpt-5.5 rejects function tools + reasoning_effort on /chat/completions
# ("Please use /v1/responses instead").
"gpt-5-5",
# gpt-5.6 models similarly reject /chat/completions with 404.
"gpt-5-6",
)
def _all_models(self, e2e_state: dict) -> list[tuple[str, str]]:
"""Return [(family, model_id), ...] for every model copilot can talk to."""
out: list[tuple[str, str]] = []
claude_models: dict = e2e_state.get("claude_models") or {}
for family, model_id in _launchable_model_items(claude_models):
out.append((f"claude-{family}", model_id))
for model in e2e_state.get("codex_models") or []:
if any(frag in model for frag in self.COPILOT_INCOMPATIBLE_MODEL_FRAGMENTS):
continue
out.append(("codex", model))
return out
def test_launch_copilot_per_model(
self, tmp_path, monkeypatch, e2e_state, e2e_workspace, e2e_token
):
import ucode.config_io as config_io_mod
from ucode.agents import copilot
_require_binary("copilot")
models = self._all_models(e2e_state)
if not models:
pytest.skip("No Copilot-compatible models available on this workspace")
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
env_path = tmp_path / ".copilot-env"
backup_path = tmp_path / "copilot-env.backup"
monkeypatch.setattr(copilot, "COPILOT_ENV_PATH", env_path)
monkeypatch.setattr(copilot, "COPILOT_BACKUP_PATH", backup_path)
failures = []
for family, model in models:
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
mp.setattr(
"ucode.agents.copilot.get_databricks_token",
lambda ws, profile=None, **kwargs: e2e_token,
)
copilot.write_tool_config(
{**e2e_state, "workspace": e2e_workspace}, model, token=e2e_token
)
env = copilot.build_runtime_env(e2e_workspace, model, e2e_token)
cmd = copilot.validate_cmd("copilot")
result = _run_agent(cmd, env=env, timeout=120)
combined = (result.stdout + result.stderr).strip()
if result.returncode != 0 or not combined:
failures.append(
f"family={family} model={model} rc={result.returncode} "
f"stdout={result.stdout[:300]!r} stderr={result.stderr[:300]!r}"
)
assert not failures, "Copilot launch failures:\n" + "\n".join(failures)
class TestPiLaunch:
"""Run pi against every available model across all four providers.
Pi has dedicated providers per family (claude, codex, gemini, oss); this
test exercises each one end-to-end through the validation path.
"""
def _all_models(self, e2e_state: dict) -> list[tuple[str, str]]:
out: list[tuple[str, str]] = []
claude_models: dict = e2e_state.get("claude_models") or {}
for family, model_id in _launchable_model_items(claude_models):
out.append((f"claude-{family}", model_id))
for model in e2e_state.get("codex_models") or []:
out.append(("codex", model))
for model in e2e_state.get("gemini_models") or []:
out.append(("gemini", model))
return out
def test_launch_pi_per_model(self, tmp_path, monkeypatch, e2e_state, e2e_workspace, e2e_token):
import ucode.config_io as config_io_mod
from ucode.agents import pi
_require_binary("pi")
models = self._all_models(e2e_state)
if not models:
pytest.skip("No Pi-compatible models available on this workspace")
monkeypatch.setattr(config_io_mod, "APP_DIR", tmp_path)
# Point PI_CODING_AGENT_DIR and ucode's config writer at the same
# isolated directory without changing the process HOME.
pi_home = tmp_path / "pi-home"
pi_dir = pi_home / ".pi" / "agent"
config_path = pi_dir / "models.json"
backup_path = tmp_path / "pi-models.backup.json"
monkeypatch.setattr(pi, "PI_UCODE_HOME", pi_home)
monkeypatch.setattr(pi, "PI_CONFIG_DIR", pi_dir)
monkeypatch.setattr(pi, "PI_CONFIG_PATH", config_path)
monkeypatch.setattr(pi, "PI_SETTINGS_PATH", pi_dir / "settings.json")
monkeypatch.setattr(pi, "PI_SETTINGS_BACKUP_PATH", tmp_path / "pi-settings.backup.json")
monkeypatch.setattr(pi, "PI_BACKUP_PATH", backup_path)
failures = []
for family, model in models:
if config_path.exists():
config_path.unlink()
with pytest.MonkeyPatch().context() as mp:
mp.setattr("ucode.state.save_state", lambda s: None)
mp.setattr(
"ucode.agents.pi.get_databricks_token",
lambda ws, profile=None, **kwargs: e2e_token,
)
pi.write_tool_config(
{**e2e_state, "workspace": e2e_workspace},
model,
token=e2e_token,
)
env = pi.build_runtime_env(e2e_token)
cmd = pi.validate_cmd("pi")
result = _run_agent(cmd, env=env, timeout=120)
combined = (result.stdout + result.stderr).strip()
if result.returncode != 0 or not combined:
failures.append(
f"family={family} model={model} rc={result.returncode} "
f"stdout={result.stdout[:300]!r} stderr={result.stderr[:300]!r}"
)
assert not failures, "Pi launch failures:\n" + "\n".join(failures)
# ---------------------------------------------------------------------------
# Web search MCP — Databricks-backed Responses API
# ---------------------------------------------------------------------------
#
# Verifies the web_search MCP path against a real workspace:
# 1. The Responses API call with tools=[{type: web_search}] returns text.
# 2. The MCP server subprocess answers initialize/tools/list and tools/call
# correctly when DATABRICKS_HOST and UCODE_WEB_SEARCH_MODEL are set.
#
# Skipped when the workspace has no Responses-API endpoint (codex_models
# empty), since web_search is unavailable in that case by design.
# ---------------------------------------------------------------------------
def _first_codex_model(e2e_state: dict) -> str:
models = e2e_state.get("codex_models") or []
if not models:
pytest.skip("No Responses-API (codex) models available on this workspace")
return models[0]
class TestWebSearchResponsesApi:
"""Hit the real Databricks Codex (Responses API) endpoint with native
web_search and assert the model returns non-empty text."""
def test_call_responses_api_returns_text(self, monkeypatch, e2e_state, e2e_workspace):
from ucode import mcp_web_search
model = _first_codex_model(e2e_state)
monkeypatch.setenv("DATABRICKS_HOST", e2e_workspace)
monkeypatch.setenv("UCODE_WEB_SEARCH_MODEL", model)
payload = mcp_web_search._call_responses_api(
"What is today's date? Use web search to confirm."
)
assert isinstance(payload, dict)
text = mcp_web_search._extract_response_text(payload)
assert text, (
f"Responses API returned no text output. Full payload (truncated): {str(payload)[:500]}"
)
class TestWebSearchMcpSubprocess:
"""Drive the `ucode mcp web-search` subprocess over stdio and assert the