Files
wehub-resource-sync b4fbd6fe9f
Deploy Site / deploy-vercel (push) Has been skipped
Deploy Site / deploy-docs (push) Has been skipped
Build Skills Index / build-index (push) Has been skipped
Build Skills Index / trigger-deploy (push) Waiting to run
CI / Deny unrelated histories (push) Has been skipped
CI / CI timing report (push) Blocked by required conditions
CI / Detect affected areas (push) Successful in 27m35s
CI / OSV scan (push) Failing after 4s
CI / Build&Test Docker image (push) Successful in 9s
CI / Supply-chain scan (push) Has been skipped
CI / Lint Docker scripts (push) Failing after 5m13s
CI / Check contributors (push) Failing after 12m8s
CI / Docs Site (push) Failing after 12m8s
CI / TypeScript (push) Failing after 12m8s
CI / Python lints (push) Failing after 12m9s
CI / Python tests (push) Failing after 12m9s
CI / Check uv.lock (push) Failing after 23m22s
CI / All required checks pass (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 11:56:03 +08:00

106 lines
3.1 KiB
Python

"""Tests for hermes_cli.context_switch_guard."""
from __future__ import annotations
from types import SimpleNamespace
from hermes_cli.context_switch_guard import merge_preflight_compression_warning
from hermes_cli.model_switch import ModelSwitchResult
def _result(*, model: str = "small-model") -> ModelSwitchResult:
return ModelSwitchResult(
success=True,
new_model=model,
target_provider="openrouter",
provider_changed=False,
api_key="k",
base_url="https://example.com/v1",
api_mode="chat_completions",
provider_label="openrouter",
model_info={"context_length": 32_000},
)
def _compressor(monkeypatch, *, context_length: int = 200_000):
from agent.context_compressor import ContextCompressor
monkeypatch.setattr(
"agent.context_compressor.get_model_context_length",
lambda *a, **k: context_length,
)
return ContextCompressor(
model="big-model",
threshold_percent=0.5,
protect_first_n=3,
protect_last_n=20,
quiet_mode=True,
config_context_length=context_length,
)
def test_no_warning_when_below_new_threshold(monkeypatch):
monkeypatch.setattr(
"hermes_cli.context_switch_guard.resolve_display_context_length",
lambda *a, **k: 32_000,
)
cc = _compressor(monkeypatch)
cc.last_prompt_tokens = 10_000
agent = SimpleNamespace(
context_compressor=cc,
compression_enabled=True,
conversation_history=[],
base_url="",
api_key="",
)
result = _result()
merge_preflight_compression_warning(result, agent=agent)
assert not result.warning_message
def test_warns_when_estimate_exceeds_new_threshold(monkeypatch):
monkeypatch.setattr(
"hermes_cli.context_switch_guard.resolve_display_context_length",
lambda *a, **k: 32_000,
)
monkeypatch.setattr(
"hermes_cli.context_switch_guard._estimate_tokens",
lambda *a, **k: 90_000,
)
cc = _compressor(monkeypatch)
agent = SimpleNamespace(
context_compressor=cc,
compression_enabled=True,
conversation_history=[],
base_url="",
api_key="",
)
result = _result()
merge_preflight_compression_warning(result, agent=agent)
assert result.warning_message
assert "preflight compression" in result.warning_message
assert "shrinks" in result.warning_message
def test_merge_appends_to_existing_warning(monkeypatch):
monkeypatch.setattr(
"hermes_cli.context_switch_guard._estimate_tokens",
lambda *a, **k: 90_000,
)
monkeypatch.setattr(
"hermes_cli.context_switch_guard.resolve_display_context_length",
lambda *a, **k: 32_000,
)
cc = _compressor(monkeypatch)
agent = SimpleNamespace(
context_compressor=cc,
compression_enabled=True,
base_url="",
api_key="",
)
result = _result()
result.warning_message = "expensive"
merge_preflight_compression_warning(result, agent=agent)
assert "expensive" in result.warning_message
assert "preflight compression" in result.warning_message