diff --git a/tests/conftest.py b/tests/conftest.py index 946ab206..546639bc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,9 @@ from __future__ import annotations import pytest +from strix.config import loader +from strix.interface import platform_identity + @pytest.fixture(autouse=True) def _isolate_mcp_config( @@ -26,6 +29,24 @@ def _isolate_mcp_config( monkeypatch.delenv("STRIX_MCP_EXCLUDE", raising=False) +@pytest.fixture(autouse=True) +def _isolate_user_state( + monkeypatch: pytest.MonkeyPatch, tmp_path_factory: pytest.TempPathFactory +) -> None: + """Keep the suite from writing the developer's real ``~/.strix`` files. + + Code the tests drive persists the CLI config and the device identity at paths + that are fixed when the modules are imported (``Path.home()`` at import time), + so setting ``HOME`` per test is too late for them. Without this, running the + suite replaces ``~/.strix/cli-config.json`` (the stored model, API key and API + base) and ``~/.strix/cli-identity.json`` with whatever the last test wrote. + Tests that exercise these paths set their own after this runs. + """ + state = tmp_path_factory.mktemp("strix-user-state") + monkeypatch.setattr(loader, "_DEFAULT_PATH", state / "cli-config.json") + monkeypatch.setattr(platform_identity, "IDENTITY_PATH", state / "cli-identity.json") + + @pytest.fixture(autouse=True) def _plain_terminal(monkeypatch: pytest.MonkeyPatch) -> None: """Make Rich output identical on every developer's machine. diff --git a/tests/test_cloud_cli.py b/tests/test_cloud_cli.py index 41962b9d..8626c939 100644 --- a/tests/test_cloud_cli.py +++ b/tests/test_cloud_cli.py @@ -17,10 +17,19 @@ import requests from rich.console import Console from strix.interface import cloud, platform_cli -from strix.interface.cloud import http, render, runner, workspaces +from strix.interface.cloud import billing, http, render, runner, workspaces from strix.interface.cloud.spec import GROUP_HELP, SPEC +@pytest.fixture(autouse=True) +def _wallet_cache_in_tmp( + monkeypatch: pytest.MonkeyPatch, tmp_path_factory: pytest.TempPathFactory +) -> None: + """Keep the top-up flow's npm cache (``~/.strix/wallet-npm-cache``) out of the real home.""" + cache = tmp_path_factory.mktemp("wallet-npm-cache") + monkeypatch.setattr(billing, "_wallet_npm_cache", lambda: cache) + + class FakeResponse: def __init__( self, diff --git a/tests/test_telemetry_resume.py b/tests/test_telemetry_resume.py index 9a0f92fd..6347996d 100644 --- a/tests/test_telemetry_resume.py +++ b/tests/test_telemetry_resume.py @@ -68,8 +68,10 @@ def test_scan_ended_reports_resumed_usage_delta( @pytest.mark.parametrize("telemetry", [posthog, scarf]) def test_scan_ended_reports_all_fresh_run_usage( telemetry: Any, + tmp_path: Any, monkeypatch: pytest.MonkeyPatch, ) -> None: + monkeypatch.chdir(tmp_path) # a run directory is created under ./strix_runs state = ReportState() state.record_sdk_usage( agent_id="agent",