"""Горячий путь claude_bridge: эфемерный ~/.claude по профилю + --model из профиля. _ephemeral_claude_home(settings_path, exclude_creds): внешний провайдер (exclude_creds=True) не тащит OAuth-креды подписки. stream_claude берёт модель из профиля провайдера юзера (provider_chain[0].model), а НЕ из глобальной CLAUDE_MODEL. """ import asyncio import importlib import os import shutil import sys import pytest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if ROOT not in sys.path: sys.path.insert(0, ROOT) def _src_home(tmp_path): src = tmp_path / "claude_src" src.mkdir() (src / ".credentials.json").write_text("oauth-secret") (src / "CLAUDE.md").write_text("instructions") return src def test_ephemeral_excludes_creds_when_flag(monkeypatch, tmp_path): import claude_bridge monkeypatch.setattr(claude_bridge, "CLAUDE_CONFIG_DIR", str(_src_home(tmp_path))) monkeypatch.setenv("HOME", str(tmp_path)) # ~/.claude.json отсутствует → "{}" settings = tmp_path / "s.json" settings.write_text("{}") cdir, _cjson, tmp = claude_bridge._ephemeral_claude_home(str(settings), exclude_creds=True) try: # mutation: убрать `if exclude_creds` → креды скопируются и этот assert упадёт assert not os.path.exists(os.path.join(cdir, ".credentials.json")) assert os.path.exists(os.path.join(cdir, "CLAUDE.md")) finally: shutil.rmtree(tmp, ignore_errors=True) def test_ephemeral_keeps_creds_without_flag(monkeypatch, tmp_path): import claude_bridge monkeypatch.setattr(claude_bridge, "CLAUDE_CONFIG_DIR", str(_src_home(tmp_path))) monkeypatch.setenv("HOME", str(tmp_path)) settings = tmp_path / "s.json" settings.write_text("{}") cdir, _cjson, tmp = claude_bridge._ephemeral_claude_home(str(settings), exclude_creds=False) try: assert os.path.exists(os.path.join(cdir, ".credentials.json")) finally: shutil.rmtree(tmp, ignore_errors=True) class _FakeStream: async def readline(self): return b"" # EOF сразу — стрим завершается, docker не запускается class _FakeProc: def __init__(self): self.stdout = _FakeStream() self.stderr = _FakeStream() self.returncode = 0 # не None → finally не зовёт terminate async def wait(self): return 0 def terminate(self): pass def test_stream_claude_uses_profile_model(monkeypatch, tmp_path): monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db")) monkeypatch.setenv("DATA_DIR", str(tmp_path / "data")) monkeypatch.setenv("HOME", str(tmp_path)) monkeypatch.chdir(ROOT) import db importlib.reload(db) db.init_db() import claude_bridge importlib.reload(claude_bridge) import agent_provider monkeypatch.setattr(claude_bridge, "CLAUDE_CONFIG_DIR", str(_src_home(tmp_path))) settings = tmp_path / "s.json" settings.write_text("{}") fixed = agent_provider.ProviderProfile( name="deepseek", settings_path=str(settings), model="deepseek-v4-flash", uses_api_token=True, ) monkeypatch.setattr(agent_provider, "provider_chain", lambda _u: [fixed]) captured = {} async def fake_exec(*cmd, **kwargs): captured["cmd"] = list(cmd) return _FakeProc() monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_exec) async def run(): # message без смета+единица-триггеров → не тянем corpus-инъекцию return [c async for c in claude_bridge.stream_claude("привет", "u@x", "tab")] asyncio.run(run()) cmd = captured["cmd"] i = cmd.index("--model") # mutation: вернуть CLAUDE_MODEL вместо prof.model → тут будет "sonnet", assert упадёт assert cmd[i + 1] == "deepseek-v4-flash"