Spaces:
Running
Running
File size: 7,991 Bytes
a9e27c0 | 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 | import asyncio
import json
import threading
from unittest.mock import AsyncMock, MagicMock
import pytest
import pytest_asyncio
from free_claude_code.application.model_metadata import ProviderModelInfo
from free_claude_code.config.loader import ManagedConfigStore
from free_claude_code.config.settings import Settings
from free_claude_code.harnesses import vscode_chat_integration as vscode
from free_claude_code.providers.base import BaseProvider
from free_claude_code.providers.runtime.runtime import ProviderRuntime
from free_claude_code.runtime.application import ApplicationRuntime
from free_claude_code.runtime.configuration import ConfigurationService
from free_claude_code.runtime.provider_manager import ProviderRuntimeManager
from tests.runtime.catalog_support import (
finish_discovery,
finish_publication,
pause_next_write,
)
@pytest_asyncio.fixture
async def runtime(request):
settings = Settings(
model="nvidia_nim/one",
nvidia_nim_api_keys="test",
proxy_auth_token="managed",
messaging_platform="none",
)
provider = MagicMock(spec=BaseProvider)
provider.list_model_infos = AsyncMock(
return_value=frozenset({ProviderModelInfo("one")})
)
async def construct(provider_id, *_):
if provider_id in {"lmstudio", "llamacpp", "ollama"}:
raise ConnectionError()
return provider
manager = ProviderRuntimeManager(
settings,
runtime_factory=lambda s, admission_registry, history_cache=None: (
ProviderRuntime(
s,
admission_registry,
provider_constructor=construct,
)
),
)
app = ApplicationRuntime(
manager,
configuration=ConfigurationService(ManagedConfigStore()),
transcriber=None,
)
if getattr(request, "param", False):
vscode.configure(vscode.config_path(), "http://127.0.0.1:1", "old", ())
await app.start()
try:
yield app
finally:
await app.close()
async def settle(runtime):
await finish_discovery(runtime.provider_manager)
task = runtime._integrations._vscode_update.task
if task:
await asyncio.wait_for(asyncio.shield(task), 5)
@pytest.mark.asyncio
async def test_catalog_updates_connected_file_and_disconnect_stays_removed(runtime):
await runtime.connect_vscode_chat()
await settle(runtime)
path = vscode.config_path()
runtime.provider_manager.cache_model_infos(
"nvidia_nim", (ProviderModelInfo("two"),)
)
await finish_publication(runtime.provider_manager)
await settle(runtime)
models = json.loads(path.read_text())[0]["models"]
assert {m["id"] for m in models} == {"nvidia_nim/one", "nvidia_nim/two"}
assert (await runtime.vscode_chat_status())["connected"]
await runtime.disconnect_vscode_chat()
runtime._integrations.catalog_changed()
await settle(runtime)
assert json.loads(path.read_text()) == []
assert not (await runtime.vscode_chat_status())["connected"]
@pytest.mark.asyncio
async def test_update_arriving_during_write_is_not_lost(runtime, monkeypatch):
await runtime.connect_vscode_chat()
await settle(runtime)
entered, release = asyncio.Event(), threading.Event()
loop = asyncio.get_running_loop()
original = vscode.configure
calls = 0
def held(*args, **kwargs):
nonlocal calls
calls += 1
if calls == 1:
loop.call_soon_threadsafe(entered.set)
assert release.wait(5)
return original(*args, **kwargs)
monkeypatch.setattr(vscode, "configure", held)
try:
runtime._integrations.catalog_changed()
await asyncio.wait_for(entered.wait(), 5)
runtime.provider_manager.cache_model_infos(
"nvidia_nim", (ProviderModelInfo("late"),)
)
await finish_publication(runtime.provider_manager)
finally:
release.set()
await settle(runtime)
assert calls >= 2
assert "nvidia_nim/late" in [
m["id"] for m in json.loads(vscode.config_path().read_text())[0]["models"]
]
@pytest.mark.asyncio
async def test_bad_file_does_not_fail_catalog_and_disconnect_needs_no_catalog(
runtime, monkeypatch
):
await settle(runtime)
path = vscode.config_path()
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("{broken")
await runtime.refresh_vscode_chat()
await settle(runtime)
assert runtime._integrations._vscode_update.state == "failed"
assert runtime.provider_manager.catalog_status()["catalog_file"] == "ready"
path.unlink()
await runtime.connect_vscode_chat()
monkeypatch.setattr(
runtime.provider_manager,
"catalog_snapshot",
AsyncMock(side_effect=AssertionError("Must not wait")),
)
assert (await runtime.disconnect_vscode_chat())["connected"] is False
@pytest.mark.asyncio
async def test_disconnected_startup_does_not_create_file(runtime):
await settle(runtime)
assert not vscode.config_path().exists()
@pytest.mark.asyncio
@pytest.mark.parametrize("runtime", [True], indirect=True)
async def test_startup_refreshes_owned_group_and_only_writes_once(runtime):
await settle(runtime)
path = vscode.config_path()
assert (
json.loads(path.read_text())[0]["models"][0]["requestHeaders"]["x-api-key"]
== "managed"
)
assert json.loads(path.read_text())[0]["models"]
before = path.stat().st_mtime_ns
await runtime.refresh_vscode_chat()
await settle(runtime)
assert path.stat().st_mtime_ns == before
assert not runtime._integrations._vscode_update.changed
@pytest.mark.asyncio
async def test_disconnect_while_refresh_waits_for_ownership_never_reconnects(
runtime, monkeypatch
):
await runtime.connect_vscode_chat()
await settle(runtime)
entered, release = pause_next_write(
monkeypatch, runtime._integrations._vscode_update.access
)
try:
await runtime.refresh_vscode_chat()
await asyncio.wait_for(entered.wait(), 5)
await asyncio.wait_for(runtime.disconnect_vscode_chat(), 5)
finally:
release.set()
await settle(runtime)
assert json.loads(vscode.config_path().read_text()) == []
@pytest.mark.asyncio
async def test_replaced_generation_cannot_publish_old_url_or_token(
runtime, monkeypatch
):
await runtime.connect_vscode_chat()
await settle(runtime)
entered, release = pause_next_write(
monkeypatch, runtime._integrations._vscode_update.access
)
try:
await runtime.refresh_vscode_chat()
await asyncio.wait_for(entered.wait(), 5)
await runtime.provider_manager.replace(
runtime.settings.model_copy(
update={"port": 12345, "proxy_auth_token": "rotated"}
),
commit=AsyncMock(),
reason="test",
)
finally:
release.set()
await settle(runtime)
group = json.loads(vscode.config_path().read_text())[0]
assert all(m["requestHeaders"] == {"x-api-key": "rotated"} for m in group["models"])
assert all(
m["url"] == "http://127.0.0.1:12345/v1/messages" for m in group["models"]
)
@pytest.mark.asyncio
async def test_codex_publication_failure_does_not_block_vscode(runtime):
await runtime.connect_vscode_chat()
await settle(runtime)
publisher = MagicMock()
publisher.publish.side_effect = PermissionError("denied")
runtime.provider_manager._model_catalog_publisher = publisher
runtime.provider_manager.cache_model_infos(
"nvidia_nim", (ProviderModelInfo("new"),)
)
await finish_publication(runtime.provider_manager)
await settle(runtime)
assert runtime.provider_manager.catalog_status()["catalog_file"] == "failed"
assert runtime._integrations._vscode_update.state == "ready"
assert "nvidia_nim/new" in [
m["id"] for m in json.loads(vscode.config_path().read_text())[0]["models"]
]
|