forked from omnigent-ai/omnigent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_opencode_native_provider.py
More file actions
140 lines (109 loc) · 5.37 KB
/
Copy pathtest_opencode_native_provider.py
File metadata and controls
140 lines (109 loc) · 5.37 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
"""Unit tests for opencode-native provider-config synthesis."""
from __future__ import annotations
import json
import stat
import sys
import types
from pathlib import Path
import pytest
from omnigent.opencode_native_provider import (
DEFAULT_DATABRICKS_GATEWAY_MODEL,
OpenCodeGatewayResolution,
_gateway_endpoint_for_model,
build_opencode_model_default_config,
build_opencode_provider_config,
resolve_databricks_gateway,
write_opencode_provider_config,
)
def test_build_model_default_config_pins_model_without_provider_block() -> None:
cfg = build_opencode_model_default_config("anthropic/claude-sonnet-4-5")
assert cfg == {
"$schema": "https://opencode.ai/config.json",
"model": "anthropic/claude-sonnet-4-5",
}
# No provider block: opencode resolves the provider from the model prefix.
assert "provider" not in cfg
def test_model_default_config_round_trips_through_writer(tmp_path: Path) -> None:
path = write_opencode_provider_config(
tmp_path, build_opencode_model_default_config("openai/gpt-5.5")
)
written = json.loads(path.read_text(encoding="utf-8"))
assert written["model"] == "openai/gpt-5.5"
def test_qualified_model_joins_provider_and_endpoint() -> None:
res = OpenCodeGatewayResolution(
base_url="https://ws/serving-endpoints",
api_key="tok",
model_id="databricks-claude-sonnet-4-6",
provider_id="databricks-gateway",
)
assert res.qualified_model == "databricks-gateway/databricks-claude-sonnet-4-6"
def test_build_provider_config_shape() -> None:
res = OpenCodeGatewayResolution(
base_url="https://ws/serving-endpoints",
api_key="sekret",
model_id="databricks-claude-sonnet-4-6",
)
cfg = build_opencode_provider_config(res)
block = cfg["provider"]["databricks-gateway"]
assert block["npm"] == "@ai-sdk/openai-compatible"
assert block["options"] == {"baseURL": "https://ws/serving-endpoints", "apiKey": "sekret"}
assert "databricks-claude-sonnet-4-6" in block["models"]
assert cfg["$schema"].endswith("config.json")
def test_write_provider_config_is_0600_and_valid_json(tmp_path: Path) -> None:
res = OpenCodeGatewayResolution(
base_url="https://ws/serving-endpoints", api_key="tok", model_id="databricks-x"
)
path = write_opencode_provider_config(tmp_path, build_opencode_provider_config(res))
assert path == tmp_path / "opencode" / "opencode.json"
# Token-bearing config must not be world/group readable.
assert stat.S_IMODE(path.stat().st_mode) == 0o600
parsed = json.loads(path.read_text())
assert parsed["provider"]["databricks-gateway"]["options"]["apiKey"] == "tok"
@pytest.mark.parametrize(
"model_id,expected",
[
("databricks-claude-sonnet-4-6", "databricks-claude-sonnet-4-6"),
("databricks/databricks-gpt-5-5", "databricks-gpt-5-5"),
("claude-opus-4", None), # not a gateway endpoint name
("anthropic/claude-opus-4", None),
(None, None),
],
)
def test_gateway_endpoint_normalization(model_id: str | None, expected: str | None) -> None:
assert _gateway_endpoint_for_model(model_id) == expected
def test_resolve_gateway_none_without_profile() -> None:
assert resolve_databricks_gateway(None) is None
assert resolve_databricks_gateway("") is None
def test_resolve_gateway_none_when_sdk_absent(monkeypatch: pytest.MonkeyPatch) -> None:
# Simulate databricks-sdk not installed: the import inside the function raises.
monkeypatch.setitem(sys.modules, "databricks.sdk.core", None)
assert resolve_databricks_gateway("oss") is None
def _install_fake_sdk(monkeypatch: pytest.MonkeyPatch, *, host: str, token: str | None) -> None:
fake = types.ModuleType("databricks.sdk.core")
class _Config:
def __init__(self, *, profile: str) -> None:
self.profile = profile
self.host = host
def authenticate(self) -> dict[str, str]:
return {"Authorization": f"Bearer {token}"} if token else {}
fake.Config = _Config # type: ignore[attr-defined]
# Ensure parent packages resolve for the dotted import.
monkeypatch.setitem(sys.modules, "databricks", types.ModuleType("databricks"))
monkeypatch.setitem(sys.modules, "databricks.sdk", types.ModuleType("databricks.sdk"))
monkeypatch.setitem(sys.modules, "databricks.sdk.core", fake)
def test_resolve_gateway_success(monkeypatch: pytest.MonkeyPatch) -> None:
_install_fake_sdk(monkeypatch, host="https://ws.cloud.databricks.com/", token="abc123")
res = resolve_databricks_gateway("oss", model_id="databricks-gpt-5-5")
assert res is not None
assert res.base_url == "https://ws.cloud.databricks.com/serving-endpoints"
assert res.api_key == "abc123"
assert res.model_id == "databricks-gpt-5-5"
assert res.qualified_model == "databricks-gateway/databricks-gpt-5-5"
def test_resolve_gateway_defaults_non_gateway_model(monkeypatch: pytest.MonkeyPatch) -> None:
_install_fake_sdk(monkeypatch, host="https://ws.databricks.com", token="t")
res = resolve_databricks_gateway("oss", model_id="claude-opus-4")
assert res is not None
assert res.model_id == DEFAULT_DATABRICKS_GATEWAY_MODEL
def test_resolve_gateway_none_when_no_token(monkeypatch: pytest.MonkeyPatch) -> None:
_install_fake_sdk(monkeypatch, host="https://ws.databricks.com", token=None)
assert resolve_databricks_gateway("oss") is None