File size: 3,421 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
import json
from unittest.mock import patch

import pytest

from free_claude_code.cli.launchers.catalog_http import fetch_proxy_model_catalog
from tests.cli.test_launcher_workflow import launch


class _ModelsResponse:
    def __init__(self, body: bytes) -> None:
        self._body = body

    def __enter__(self) -> _ModelsResponse:
        return self

    def __exit__(self, *exc_info: object) -> None:
        del exc_info

    def read(self) -> bytes:
        return self._body


def test_fetch_proxy_models_uses_canonical_bearer_request() -> None:
    body = json.dumps(
        {
            "default_model_id": "provider/model",
            "data": [{"id": "provider/model", "provider_model_ref": "provider/model"}],
        }
    ).encode()
    with patch(
        "free_claude_code.cli.launchers.catalog_http.open_local_request",
        return_value=_ModelsResponse(body),
    ) as open_local_request:
        response = fetch_proxy_model_catalog("http://127.0.0.1:9191/", "proxy-token")

    assert response.default_model_id == "provider/model"
    assert [model.wire_slug for model in response.models] == ["provider/model"]
    request = open_local_request.call_args.args[0]
    assert request.full_url == "http://127.0.0.1:9191/v1/models?view=responses"
    assert request.get_method() == "GET"
    assert request.get_header("Authorization") == "Bearer proxy-token"


def test_fetch_proxy_models_can_request_messages_view() -> None:
    body = json.dumps(
        {
            "default_model_id": "provider/model",
            "data": [{"id": "provider/model", "provider_model_ref": "provider/model"}],
        }
    ).encode()
    with patch(
        "free_claude_code.cli.launchers.catalog_http.open_local_request",
        return_value=_ModelsResponse(body),
    ) as open_local_request:
        response = fetch_proxy_model_catalog(
            "http://127.0.0.1:9191/",
            "proxy-token",
            view="messages",
        )

    assert response.default_model_id == "provider/model"
    assert [model.wire_slug for model in response.models] == ["provider/model"]
    request = open_local_request.call_args.args[0]
    assert request.full_url == "http://127.0.0.1:9191/v1/models?view=messages"
    assert request.get_method() == "GET"
    assert request.get_header("Authorization") == "Bearer proxy-token"


def test_fetch_proxy_models_rejects_non_object_json() -> None:
    with (
        patch(
            "free_claude_code.cli.launchers.catalog_http.open_local_request",
            return_value=_ModelsResponse(b"[]"),
        ),
        pytest.raises(ValueError, match="JSON object"),
    ):
        fetch_proxy_model_catalog("http://127.0.0.1:9191", "proxy-token")


@pytest.mark.parametrize("harness", ["codex", "opencode", "aider"])
def test_native_launch_uses_current_catalog_without_waiting_for_more_models(
    launch_capture, harness
):
    launch_capture.catalog = {
        "default_model_id": "nvidia_nim/configured",
        "data": [
            {
                "id": "nvidia_nim/configured",
                "provider_model_ref": "nvidia_nim/configured",
            }
        ],
    }
    launch(harness, [])
    assert len(launch_capture.commands) == 1
    assert (
        len(
            [
                request
                for request in launch_capture.requests
                if "/v1/models" in request.full_url
            ]
        )
        == 1
    )