Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python
3.3 KB
Raw Blame
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
    )