headroom/tests/test_provider_openclaw_wrap.py
chopratejas 265554d4ad fix(cli): proxy/perf/wrap UX cleanup + perf --hours correctness
Address user-reported UX gaps across the CLI surface:

- code-aware: add --code-aware/--no-code-aware (+ HEADROOM_CODE_AWARE_ENABLED env)
  to the Click CLI. PR #411 had added these only to the orphaned argparse main;
  the user-facing CLI couldn't reach the flag. Banner status text "remove
  --no-code-aware to enable" referenced a flag that didn't exist — fix to point
  at the actual flag/env. Surface code-aware in the click banner and add
  print_banner=False plumbing to run_server so the click path doesn't print
  two banners back-to-back.

- --mode: hide alias clutter via metavar=[token|cache] and rewrite help to
  lead with the two real modes. Legacy aliases (token_mode/token_savings/...)
  still validate.

- perf --hours: was documented but ignored. Records are now actually filtered,
  the report shows the actual time-range covered, and the count of records
  filtered out (so users can tell when raising --hours helps).

- perf TOIN: replace the hash-keyed pattern dump with a strategy-distribution
  view + recommendation-eligibility from the live store — actionable signal
  rather than opaque rows.

- code-graph: clarify in --help that it indexes cwd / project root.

- wrap: spell out supported tools, wrap-vs-proxy distinction, and that
  `headroom wrap opencode` isn't a thing (use `proxy` directly for opencode;
  openclaw is not opencode).

- mcp: note that mcp__headroom__headroom_retrieve is correct MCP namespacing,
  not a doubled-prefix bug. Renaming would break the proxy's tool injection.

- LLMLingua cleanup: remove [llmlingua] extra from pyproject (no live code
  uses it). Delete wiki/llmlingua.md and clean retired flag/class references
  in 6 other wiki pages. Point at [ml] (Kompress) where ML compression is
  documented.

- init -g openclaw: strip mcpServers from existing plugin entries before
  re-writing — newer openclaw schemas reject it, leaving stale entries from
  older installs unhealable. Pinned with regression test.

Tests: mock_run_server signatures in two existing tests accept **kwargs
(needed for the new print_banner plumbing). New test for the openclaw
mcpServers strip. Full suite: 4847 passed, 262 skipped, 0 failed.
2026-05-07 16:43:35 -07:00

150 lines
4.3 KiB
Python

from __future__ import annotations
from headroom.providers.openclaw.wrap import (
DEFAULT_GATEWAY_PROVIDER_IDS,
build_plugin_entry,
build_unwrap_entry,
decode_entry_json,
normalize_gateway_provider_ids,
)
def test_normalize_gateway_provider_ids_trims_deduplicates_and_defaults() -> None:
# Arrange / Act / Assert
assert normalize_gateway_provider_ids((" openai-codex ", "anthropic", "anthropic", "")) == [
"openai-codex",
"anthropic",
]
assert normalize_gateway_provider_ids(None) == DEFAULT_GATEWAY_PROVIDER_IDS
def test_decode_entry_json_handles_empty_valid_and_invalid_payloads() -> None:
# Arrange / Act / Assert
assert decode_entry_json(None) is None
assert decode_entry_json("") is None
assert decode_entry_json('{"enabled": true}') == {"enabled": True}
assert decode_entry_json("{not-json}") == "{not-json}"
def test_build_plugin_entry_preserves_unmanaged_values_and_removes_empty_python_path() -> None:
# Arrange
existing_entry = {
"enabled": False,
"name": "headroom",
"config": {
"keep": "value",
"proxyUrl": "https://user.example",
"pythonPath": "/old/python",
},
}
# Act
entry = build_plugin_entry(
existing_entry=existing_entry,
proxy_port=8787,
startup_timeout_ms=1500,
python_path=None,
no_auto_start=True,
gateway_provider_ids=(" openai-codex ", "anthropic", "anthropic"),
enabled=True,
)
# Assert
assert entry["enabled"] is True
assert entry["name"] == "headroom"
assert entry["config"] == {
"keep": "value",
"proxyUrl": "https://user.example",
"proxyPort": 8787,
"autoStart": False,
"startupTimeoutMs": 1500,
"gatewayProviderIds": ["openai-codex", "anthropic"],
}
def test_build_plugin_entry_creates_managed_defaults_for_non_mapping_input() -> None:
# Arrange / Act
entry = build_plugin_entry(
existing_entry="not-a-dict",
proxy_port=9000,
startup_timeout_ms=2500,
python_path="/usr/bin/python",
no_auto_start=False,
gateway_provider_ids=None,
enabled=False,
)
# Assert
assert entry == {
"enabled": False,
"config": {
"proxyPort": 9000,
"autoStart": True,
"startupTimeoutMs": 2500,
"gatewayProviderIds": ["openai-codex"],
"pythonPath": "/usr/bin/python",
},
}
def test_build_unwrap_entry_disables_plugin_and_removes_managed_keys_only() -> None:
# Arrange
existing_entry = {
"enabled": True,
"name": "headroom",
"config": {
"keep": "value",
"gatewayProviderIds": ["openai-codex"],
"proxyUrl": "https://managed.example",
"proxyPort": 8787,
"autoStart": True,
"startupTimeoutMs": 1000,
"pythonPath": "/usr/bin/python",
},
}
# Act
entry = build_unwrap_entry(existing_entry)
# Assert
assert entry == {
"enabled": False,
"name": "headroom",
"config": {"keep": "value"},
}
def test_build_unwrap_entry_handles_non_mapping_input() -> None:
# Arrange / Act / Assert
assert build_unwrap_entry("not-a-dict") == {"enabled": False, "config": {}}
def test_build_plugin_entry_strips_mcpServers_from_existing_entry() -> None:
"""Newer OpenClaw schemas reject `mcpServers` at the plugin-entry root.
`headroom init -g` was failing with `Config invalid: Unrecognized
key: "mcpServers"` because the prior plugin entry in the user's
config still had that legacy field, and we were spreading it back in
via `**existing_entry`. Pin the strip so we don't regress.
"""
existing_entry = {
"enabled": True,
"name": "headroom",
"mcpServers": {"some": "stale-block"}, # legacy, must be removed
"config": {"keep": "value"},
}
entry = build_plugin_entry(
existing_entry=existing_entry,
proxy_port=8787,
startup_timeout_ms=1500,
python_path=None,
no_auto_start=False,
gateway_provider_ids=None,
enabled=True,
)
assert "mcpServers" not in entry
assert entry["enabled"] is True
assert entry["name"] == "headroom"
assert entry["config"]["keep"] == "value"