## Summary A gateway, and the supervisor `raven web` leaves running beside it, held memory from startup on for things they never use or may never be asked to use. Two of them are no longer kept, and three are loaded on first use. On a key-less gateway with a warm home and no terminal, as `raven web` runs it, PSS 90 seconds after start goes from 257.2 MiB to 211.8 MiB, and the supervisor's from 77.3 MiB to 35.2 MiB. Nothing on the wire, on disk or in the page changes shape. - **The supervisor no longer imports the command table** (77.3 to 35.2 MiB). It only restarts its gateway, but it reached its loop through typer and the table. `raven/cli/entry.py` now hands the argv that `raven web` and `system.upgrade` start it with, `web --supervise --port N` with N in ASCII digits, to `serve_commands.supervise_from_argv`, which calls `_web` with the arguments the `web` command passes; a refusal ends the process with the code the command would have exited with. Any other argv is parsed by the table as before. Two process-wide settings stay ahead of the fast path in the entry point: the TLS switch, as before, and, moved there from the table's import, the switch of a Windows process's stdout and stderr to UTF-8 with `PYTHONIOENCODING` set for its children. The table's own setup, the saved language and the config migration notices, is skipped with it: the supervisor prints no translated text and never reads the config. - **Two tokenizer copies left by litellm's import are freed** (25.3 MiB). `litellm.utils` keeps the dict it parses from the bundled Anthropic tokenizer beside the string it re-serializes from it, and only the string is read: the Claude tokenizer is built from it. cl100k's registered encoding keeps a Python dict of its ranks beside the copy tiktoken's core encodes with; tiktoken reads the Python one only to pickle an encoding outside the registry, since a registered one pickles by name. `import_litellm` now drops the dict, only while the string is there, and the Python ranks. A failure is logged at debug level and leaves the import standing. - **A process whose terminal cannot be asked for its colours finds the console's host modules on its first console command** (6.2 MiB). Registering the console feature imported every CLI module to find the ones that host console commands. `cli_socket.register_cli` now also accepts a function, run once on the first `console_hosts()`. That covers a process with no terminal, as the gateway `raven web` detaches has none, and Windows, SSH, screen or tmux, and CI, which `_theme._can_probe` rules out. Where the terminal can be asked, as from `raven tui`, the host modules are still found at registration: `raven.cli._styles` asks the terminal for its colours as it is imported, and that question has to come before the Ink screen starts. - **The RPC models build each validator on first use** (6.0 MiB). `_Strict`, the base every model in `raven/rpc/models.py` derives from, sets `defer_build=True`. The reason is a comment rather than the docstring, because pydantic copies the docstring into the JSON schema. - **Loading the deck engine's plugin no longer loads python-pptx** (7.9 MiB). `plugins-dist/ppt-engine/raven_ppt/services/state.py` imported the template service, and with it python-pptx, so a gateway with the plugin installed held it even with the plugin's config slice off, which is the default. The service is now imported where a deck is read. With the slice enabled, python-pptx is still loaded by the check that decks can be made. The upgrade regression tests also use the native launcher filename (`raven.exe` on Windows, `raven` elsewhere), so the complete relaunch-argv and supervisor-handoff assertions run on both platforms. This follow-up changes test expectations only. ## Type - [ ] Fix - [ ] Feature - [ ] Docs - [ ] CI / tooling - [ ] Refactor - [x] Other ## Verification The original author's Linux performance and suite measurements below were recorded before the Windows test-fixture follow-up and rebase. The seven original patches remain equivalent after rebasing; the Windows measurements below are from the final tree. No new memory or live-provider measurements are claimed. - **Memory:** an isolated gateway with a key-less config and `memory.backend` null, started without a terminal on an exported copy of the base commit and of this head, each on its own home warmed by an earlier start, base and head runs alternating; PSS from `/proc/self/smaps_rollup` 90 seconds after start. The base read 257.2 MiB and this head 211.8 MiB, each within 0.1 MiB from run to run. The figure on each change above is what taking that change back out of this head, alone, puts back; the four add up to the difference. The supervisor's figures are its PSS under `raven web` started on each tree. - **Behaviour, base against this head on the same trees:** - Token ids and counts over the repo's docs, the `raven/agent` sources and the message catalogs, through raven's two estimators, cl100k directly and `litellm.token_counter` for Claude, GPT, GLM, DeepSeek and Gemini model names, with decoding, copying and pickling the encoding: identical. - The console: the same host modules, found at registration on the base and on the first lookup here. On a pseudo-terminal the colour query is written during registration on both. - Every model in `raven/rpc/models.py`: validation and serialization schemas, fields, and the outcome of validating the same probe payloads, identical. On the base all but eight were built at import, here none. Sixteen threads using every model for the first time at once: no error, one set of answers. - The deck engine's plugin imports without python-pptx here; on the base it loads it. - `raven web` end to end: it starts and serves the page; after a SIGKILL of the gateway the supervisor brings it back on the same port with `RAVEN_SERVE_PORT_STRICT=1`; `raven web --stop` leaves no process and no `web.json`; `web.log` carries the same lines, and the gateway's environment is the same key by key. - A streamed turn and a tool-calling turn on glm-5.3-flash through a live config's LiteLLMProvider: the same reply, the same tool call, the same prompt usage, and the same Raven estimate. - **New tests, against the base code:** of the 33 new or changed cases, 29 fail there. Four pass there by design: the token counts, a near-miss argv that goes to the table, a process whose terminal can be asked finding the host modules at registration, and the streams left alone off Windows. All 33 pass here. - **Mutation:** each construct was removed or bent one at a time, with the backup outside the tree, and each mutant failed the tests written for it: - the supervisor: no fast path, `isdigit` as the port check, extra arguments accepted, a refusal swallowed, every refusal ending with code 1, the port ignored; - the Windows streams: no switch, the switch after the fast path, no `PYTHONIOENCODING`, the switch off Windows too, strict errors, no guard around the switch; - the tokenizer copies: none freed, the dict dropped without its string, the string dropped too, the ranks kept (in this process and in a fresh one), no guard around the freeing; - the console: the finder run on every lookup, a reset keeping a finder that never ran, lazy everywhere, eager everywhere (in this process and in a fresh one), registration importing one of the finder's modules; - the validators built at import; the template service imported with the state module. - **Directly affected and neighbouring files:** `uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider --idle-ceiling-strict` over the seven changed test files and `tests/test_ppt_engine_state.py`, `tests/test_ppt_engine_prepare.py`, `tests/test_rpc_console.py`, `tests/test_cli_tui_commands.py`, `tests/test_cli_gateway_commands.py`, `tests/test_rpc_contract_shapes.py`, `tests/test_cli_smoke.py`, `tests/test_updates_upgrade.py` and `tests/test_security_tls.py`. Result at this head: 1522 passed. - **Full suite at this head:** `uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider` (repo addopts: `-n 4`, integration and e2e deselected; proxy variables unset). Result: 3 failed, 31663 passed, 164 skipped, 2 xfailed. The three are this box's environment and fail the same way on an exported copy of the base commit: `tests/test_install_script.py::test_resolve_node_dir_answers_each_case_it_exists_for`, `tests/test_knowledge_parser_excel.py::test_a_legacy_workbook_is_converted_and_read` (no LibreOffice) and `tests/test_subagent_node_runtime.py::test_what_cannot_be_read_names_nothing` (run as root). - **Exported trees:** the same suite on exported copies of the base commit and of this head, compared case by case: every case they share has the same outcome, apart from two timing-sensitive cases that failed in the base run and pass in this head's run and on repeated runs of both trees: `tests/test_subagent_acp.py::test_an_interrupted_first_turn_still_binds_its_session` and `tests/test_subagent_cli_remote.py::test_a_cancel_ends_the_agent_s_whole_process_tree_on_the_machine`. - **Diff coverage:** `uv run --frozen --python 3.12 python scripts/coverage_gate.py diff --base-ref origin/main --threshold 90` over the neighbouring run's coverage. Result: 100.00%, 50 of 50 executable changed lines. The gate measures `raven/`; `tests/test_ppt_engine_*.py` executes both imports moved in the deck engine. - **Lint and gates:** `make lint-python lint-imports lint-deps lint-types check-large-files check-source-language`, and `commitlint --from origin/main --to HEAD --config commitlint.config.cjs` plus `scripts/check_commit_messages.py origin/main..HEAD`. Result: all exit 0; import contracts 10 kept, 0 broken. **Native Windows follow-up (2026-10-15):** Windows 10.0.26300, Python 3.13.12, `PYTHONUTF8=1`, isolated Raven/pytest homes and a dedicated worktree. Setup: `uv sync --frozen --python 3.13 --all-packages --extra dev`. Imports were verified against the tested worktree. The same frozen sync was repeated after the rebase picked up the memory-plugin dependency update. - Before the fix, `uv run --frozen --no-sync pytest tests/test_rpc_system.py -n 0 --tb=short` returned **3 failed, 40 passed**. The gateway-hosted handoff, standalone handoff and dev-flag regression each expected `raven` where native Windows correctly selected `raven.exe`. - After the fix, `uv run --frozen --no-sync pytest tests/test_rpc_system.py tests/test_cli_serve_commands.py::TestTheSupervisorStartsWithoutTheCommandTable tests/test_cli_system_ca.py -n 0 --idle-ceiling-strict --tb=short` returned **66 passed, 4 skipped**. - On the final rebased tree, the following command returned **631 passed, 4 skipped**, exit 0 with the idle ceiling enforced. The four skips require the Linux certificate store; none are counted as Windows passes. JUnit output paths are omitted from these commands. ```powershell uv run --frozen --no-sync pytest tests/test_rpc_system.py tests/test_cli_serve_commands.py::TestTheSupervisorStartsWithoutTheCommandTable tests/test_cli_system_ca.py tests/test_litellm_setup.py tests/test_ppt_engine_plugin.py::test_loading_the_plugin_leaves_python_pptx_for_the_first_deck tests/test_ppt_engine_state.py tests/test_rpc_cli_dispatch.py tests/test_rpc_schema_match.py -n 0 --idle-ceiling-strict --tb=short ``` - `uv run --frozen --no-sync ruff check raven evolver agents plugins-dist tests scripts docs-site` and the matching `ruff format --check`: passed (2,337 files formatted). - `uv run --frozen --no-sync lint-imports`: 10 contracts kept. `uv run --frozen --no-sync ty check raven/cli/_console_feature.py raven/cli/commands.py raven/cli/entry.py raven/cli/serve_commands.py raven/providers/litellm_setup.py raven/rpc/cli_socket.py raven/rpc/models.py plugins-dist/ppt-engine/raven_ppt/services/state.py`: passed. - `uv run --frozen --no-sync pre-commit run --from-ref origin/main --to-ref HEAD`, `node node_modules/@commitlint/cli/cli.js --from origin/main --to HEAD --config commitlint.config.cjs`, `uv run --frozen --no-sync python scripts/check_commit_messages.py origin/main..HEAD`, `uv run --frozen --no-sync python scripts/check_large_files.py origin/main...HEAD`, `uv run --frozen --no-sync python scripts/check_source_language.py origin/main...HEAD`, and `git diff --check origin/main...HEAD`: passed. The test correction retains the complete executable path, argv, credentials, delayed shutdown and supervisor-handoff assertions, without adding a skip. Upgrade spawning and the supervisor loop are substituted in these tests. No real upgrade or resident restart, live provider call, Windows memory measurement, full Windows suite or new Linux/macOS run was performed by this follow-up. - [x] Relevant tests pass locally - [x] Relevant lint / type checks pass locally - [ ] User-facing docs or screenshots are updated when needed No user-facing docs change, because no command, flag, setting or file format does. ## Risk User-visible behaviour changes: - **The first console command in a process whose terminal cannot be asked** imports the finder's modules then, once, on the event loop and under the console's dispatch lock, instead of at startup: about 0.1 to 0.2 seconds on this box. A CLI module that fails to import then fails each console command, where before it failed the process's startup. - **Each RPC model builds its validator the first time it is used**, so a method's first call pays for its own models, and a model whose fields pydantic cannot build a validator for fails there rather than when `raven.rpc.models` is imported. - **An unhandled exception in the supervisor** reaches `web.log` as Python's plain traceback rather than typer's formatted one. - **The deck engine's template service is imported at the first deck read** instead of when the plugin loads. A python-pptx that cannot be imported therefore no longer stops the plugin from loading: with its config slice enabled, the check that decks can be made withholds the deck tools, and the turn hook's note on deck state, which logs an unreadable state at debug level and goes on, meets the import error there. - **Code in the same process that reads the private `_mergeable_ranks` off the registered cl100k encoding**, as the extension example in tiktoken's README does, gets an `AttributeError` once raven has imported litellm. In raven, its plugins and the locked dependencies, only tiktoken's educational module does. Left as today: - **Any other supervisor argv**, for example one typed by hand with `--port=N` or the flags reordered, goes through the command table and keeps today's memory. - **Freeing the tokenizer copies relies on litellm's and tiktoken's internals.** The tests read each copy where it lives, check in a fresh interpreter that litellm's own import registers the encoding whose ranks are freed, and pin the token counts, so a release that moves a copy, starts reading one or stops registering the encoding fails them. - [x] Security impact considered - [x] Backward compatibility considered - [x] Rollback path is clear for risky changes Rollback is a revert. Nothing written to disk or sent over the wire changes. ## Related Issues N/A
832 lines
34 KiB
Python
832 lines
34 KiB
Python
"""Tests for rpc cli.dispatch handler + Console injection + ANSI filter.
|
||
|
||
Acceptance is gated by:
|
||
- `test_happy_path_fake_echo` — end-to-end dispatch + Console injection + width sync
|
||
- `test_standalone_mode_false_catches_usage_error` — no process death on bad arg
|
||
- `test_width_sync_propagates_to_console` — TUI-supplied width flows to Rich
|
||
|
||
Implementation contract: monkey-patch the EC CLI modules' module-level
|
||
``console``.
|
||
|
||
Error mapping:
|
||
- -32013 cli_command_failed → NOT raised; signaled via exit_code != 0 in result
|
||
- -32014 cli_command_timeout → raised so the dispatcher emits a JSON-RPC error frame
|
||
- -32015 not_dispatch_compatible → raised so the dispatcher emits a JSON-RPC error frame
|
||
|
||
Why: the CliResult shape embeds error_code in the result, but timeout / not-compat
|
||
are *errors*. We resolve in favor of treating them as errors: the handler raises
|
||
CliCommandTimeoutError / NotDispatchCompatibleError; the dispatcher converts them
|
||
to JSON-RPC error frames. Only exit_code != 0 (-32013) stays in-band.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
from io import StringIO
|
||
|
||
import click
|
||
import pytest
|
||
import typer
|
||
|
||
from raven.rpc._ansi_filter import filter_ansi
|
||
from raven.rpc._console_injection import inject_consoles
|
||
from raven.rpc.dispatcher import Dispatcher
|
||
from raven.rpc.errors import (
|
||
CliCommandTimeoutError,
|
||
ConfigValidationError,
|
||
NotDispatchCompatibleError,
|
||
)
|
||
from raven.rpc.methods.cli_dispatch import (
|
||
_is_dispatch_compatible,
|
||
cli_dispatch,
|
||
register_cli_methods,
|
||
)
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test fixtures — build a fake Typer app and patch ec_cli.app for isolation.
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
def _make_fake_app() -> typer.Typer:
|
||
"""Build a small Typer app with deterministic commands for testing.
|
||
|
||
Commands:
|
||
echo <text> — print <text> to stdout via module-level console
|
||
boom — raise click.UsageError("bad arg")
|
||
slow — time.sleep(10) (used by the timeout test)
|
||
"""
|
||
fake = typer.Typer(no_args_is_help=False)
|
||
|
||
@fake.command()
|
||
def echo(text: str) -> None:
|
||
# Use the patched module-level console so injection is testable.
|
||
import raven.cli.commands as ec_commands
|
||
|
||
ec_commands.console.print(text)
|
||
|
||
@fake.command()
|
||
def boom() -> None:
|
||
raise click.UsageError("bad arg")
|
||
|
||
@fake.command()
|
||
def typer_exit_one() -> None:
|
||
"""B1 regression test: typer.Exit(1) must propagate to exit_code=1."""
|
||
raise typer.Exit(1)
|
||
|
||
@fake.command()
|
||
def typer_exit_three() -> None:
|
||
"""B1 regression test: typer.Exit(3) must propagate to exit_code=3."""
|
||
raise typer.Exit(3)
|
||
|
||
@fake.command()
|
||
def slow() -> None:
|
||
import time
|
||
|
||
# asyncio.to_thread cannot be cancelled: when the dispatch times out this
|
||
# thread runs on, and the loop's shutdown joins it -- so this sleep is
|
||
# paid in full by the teardown, not bounded by the timeout under test.
|
||
time.sleep(1.0)
|
||
|
||
@fake.command()
|
||
def aborts() -> None:
|
||
"""C1 test: raise click.Abort (the EOF-stdin confirm failure mode)."""
|
||
raise click.exceptions.Abort()
|
||
|
||
@fake.command()
|
||
def render_table(rows: int = 3) -> None:
|
||
"""Render a Rich Table; used by the width-sync test."""
|
||
from rich.table import Table
|
||
|
||
import raven.cli.commands as ec_commands
|
||
|
||
table = Table(title="Sample")
|
||
table.add_column("col1")
|
||
table.add_column("col2")
|
||
for i in range(rows):
|
||
table.add_row(f"r{i}-a-very-long-cell", f"r{i}-b-also-long-cell")
|
||
ec_commands.console.print(table)
|
||
|
||
return fake
|
||
|
||
|
||
@pytest.fixture
|
||
def fake_app_patch(monkeypatch):
|
||
"""Patch `raven.cli.commands.app` to a fake Typer; whitelist its commands.
|
||
|
||
The cli_dispatch handler calls `ec_cli.app(argv, standalone_mode=False)` where
|
||
`ec_cli` is `raven.cli.commands`. We swap `app` for our fake and extend the
|
||
whitelist for the duration of the test.
|
||
"""
|
||
import raven.cli.commands as ec_commands
|
||
|
||
fake = _make_fake_app()
|
||
monkeypatch.setattr(ec_commands, "app", fake)
|
||
|
||
# Reflection picks up the fake Typer commands directly off
|
||
# ``ec_commands.app`` — no whitelist injection needed.
|
||
yield fake
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 1 — happy path end-to-end (fake echo via patched app)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
async def test_happy_path_fake_echo(fake_app_patch):
|
||
"""Acceptance proxy: argv → stdout → exit_code=0."""
|
||
result = await cli_dispatch({"argv": ["echo", "hello-tui"], "width": 80})
|
||
assert result["exit_code"] == 0
|
||
assert "hello-tui" in result["stdout"]
|
||
assert "error_code" not in result or result.get("error_code") is None
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 2 — Console injection swaps all CLI modules
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
def test_console_injection_patches_all_modules():
|
||
"""inject_consoles must replace `mod.console` on all hosts and restore on exit.
|
||
|
||
The patch list was extended 4 → 12 after a merge introduced 8 new CLI modules.
|
||
"""
|
||
# The host set is every raven.cli module that defines a module-level rich
|
||
# console, read off the source so a discovery that silently skipped one
|
||
# would show up here.
|
||
import ast
|
||
from pathlib import Path
|
||
|
||
cli_dir = Path(__file__).resolve().parents[1] / "raven" / "cli"
|
||
assigned = set()
|
||
for path in cli_dir.glob("*.py"):
|
||
tree = ast.parse(path.read_text(errors="replace"))
|
||
for node in tree.body:
|
||
if isinstance(node, ast.Assign) and any(
|
||
isinstance(x, ast.Name) and x.id == "console" for x in node.targets
|
||
):
|
||
assigned.add(f"raven.cli.{path.stem}")
|
||
from raven.rpc import cli_socket
|
||
|
||
found = {m.__name__ for m in cli_socket.console_hosts()}
|
||
# Every module that assigns a console is a host; modules that bind one by
|
||
# import (the onboarding screens share _onboard_shared's) are hosts too, so
|
||
# the discovered set may be larger, never smaller.
|
||
assert assigned <= found, f"console hosts missing modules that assign a console: {sorted(assigned - found)}"
|
||
assert len(found) >= 20
|
||
from raven.rpc import cli_socket
|
||
|
||
hosts = cli_socket.console_hosts()
|
||
originals = {mod: mod.console for mod in hosts}
|
||
|
||
from rich.console import Console as RichConsole
|
||
|
||
sentinel = RichConsole(file=StringIO(), force_terminal=True, color_system="truecolor", width=80)
|
||
with inject_consoles(sentinel):
|
||
for mod in hosts:
|
||
assert mod.console is sentinel, f"{mod.__name__}.console was not patched"
|
||
|
||
# After exit: all restored.
|
||
for mod, orig in originals.items():
|
||
assert mod.console is orig, f"{mod.__name__}.console not restored"
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 3 — whitelist (_is_dispatch_compatible)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
def test_dispatch_compatible_whitelist():
|
||
# Acceptance proxy: channels status MUST be allowed (v0.0.1 demo)
|
||
assert _is_dispatch_compatible(["channels", "status"]) is True
|
||
assert _is_dispatch_compatible(["status"]) is True
|
||
assert _is_dispatch_compatible(["skill", "list"]) is True
|
||
# Out-of-scope
|
||
assert _is_dispatch_compatible(["run"]) is False
|
||
assert _is_dispatch_compatible(["nonexistent"]) is False
|
||
# Empty argv → reject (nothing to dispatch)
|
||
assert _is_dispatch_compatible([]) is False
|
||
|
||
|
||
def test_dispatch_compatible_b2_whitelist_extension():
|
||
"""B2 surface: P1 commands added via Task A coverage report (post-refactor CLI).
|
||
|
||
NOTE 2026-05-22 post merge from refactor/Raven: trunk removed 6 skill
|
||
subcommands (refresh / stats / rebuild-index / import-files / cases /
|
||
rollback) — only ``skill list`` + ``skill get`` remain on trunk. The
|
||
reflection-based dispatch model handles this gracefully (the dropped
|
||
commands no longer exist in ``ec.cli.commands.app`` → reflection returns
|
||
False, indistinguishable from a typo). Dropping ``skill stats`` from this
|
||
assertion accordingly.
|
||
"""
|
||
assert _is_dispatch_compatible(["cron", "list"]) is True
|
||
assert _is_dispatch_compatible(["cron", "get"]) is True
|
||
assert _is_dispatch_compatible(["cron", "get", "42"]) is True # positional arg prefix
|
||
assert _is_dispatch_compatible(["sentinel", "status"]) is True
|
||
assert _is_dispatch_compatible(["sentinel", "routines"]) is True
|
||
assert _is_dispatch_compatible(["sentinel", "nudges"]) is True
|
||
assert _is_dispatch_compatible(["sentinel", "decisions"]) is True
|
||
assert _is_dispatch_compatible(["sandbox", "list"]) is True
|
||
|
||
|
||
def test_dispatch_blacklist_p3():
|
||
"""Interactive/long-running blacklist commands are hard-rejected."""
|
||
assert _is_dispatch_compatible(["gateway"]) is False # long-running daemon
|
||
assert _is_dispatch_compatible(["gateway", "start"]) is False # prefix match
|
||
assert _is_dispatch_compatible(["provider", "login"]) is False # OAuth flow
|
||
# channels login is gated per-channel now (weixin/whatsapp only).
|
||
assert _is_dispatch_compatible(["channels", "login", "weixin"]) is False # QR long-poll
|
||
assert _is_dispatch_compatible(["channels", "login", "whatsapp"]) is False # npm subprocess
|
||
assert _is_dispatch_compatible(["sandbox", "shell"]) is False # stdin hijack
|
||
|
||
|
||
def test_channels_login_normal_channels_dispatch():
|
||
"""Normal (no-op) channel logins are dispatch-compatible; only
|
||
weixin/whatsapp stay blacklisted.
|
||
|
||
The other ~10 channels inherit BaseChannel.login (a no-op returning True),
|
||
so `channels login <normal>` performs no terminal I/O and can run in-process.
|
||
A bare `channels login` (no channel name) also dispatches now — it resolves
|
||
to a valid group+subcommand and surfaces Typer's missing-argument error
|
||
rather than the blanket terminal-only toast.
|
||
"""
|
||
assert _is_dispatch_compatible(["channels", "login", "telegram"]) is True
|
||
assert _is_dispatch_compatible(["channels", "login", "slack"]) is True
|
||
assert _is_dispatch_compatible(["channels", "login"]) is True # group + valid subcommand
|
||
# weixin/whatsapp remain gated (real interactive login flows).
|
||
assert _is_dispatch_compatible(["channels", "login", "weixin"]) is False
|
||
assert _is_dispatch_compatible(["channels", "login", "whatsapp"]) is False
|
||
|
||
|
||
def test_blacklist_full_coverage():
|
||
"""9-entry blacklist (5 original + tui + onboard + upgrade + tracing compact).
|
||
|
||
`tui` blocks recursive Ink+Node spawn; `onboard` blocks prompt_toolkit
|
||
wizard stdin hijack; `upgrade` blocks replacing the active Raven process;
|
||
`tracing compact` blocks a full-store hash walk that can outrun the RPC
|
||
timeout with no cancellation of the backing thread. All are necessary
|
||
because reflection will otherwise let them through.
|
||
"""
|
||
from raven.rpc.methods.cli_dispatch import _DISPATCH_BLACKLIST
|
||
|
||
expected_entries = {
|
||
("gateway",),
|
||
("provider", "login"),
|
||
("channels", "login", "weixin"),
|
||
("channels", "login", "whatsapp"),
|
||
("sandbox", "shell"),
|
||
("tui",),
|
||
("onboard",),
|
||
("upgrade",),
|
||
("tracing", "compact"),
|
||
}
|
||
assert _DISPATCH_BLACKLIST == expected_entries, (
|
||
f"blacklist drift; expected 9 prefix tuples (+ agent REPL special-case), got {_DISPATCH_BLACKLIST}"
|
||
)
|
||
# Hard-reject probe (prefix match)
|
||
assert _is_dispatch_compatible(["tui"]) is False
|
||
assert _is_dispatch_compatible(["tui", "--help"]) is False # prefix
|
||
assert _is_dispatch_compatible(["onboard"]) is False
|
||
assert _is_dispatch_compatible(["onboard", "--reset"]) is False # prefix
|
||
assert _is_dispatch_compatible(["upgrade"]) is False
|
||
assert _is_dispatch_compatible(["upgrade", "--check"]) is False # prefix
|
||
assert _is_dispatch_compatible(["tracing", "compact"]) is False
|
||
assert _is_dispatch_compatible(["tracing", "compact", "--dry-run"]) is False # prefix
|
||
# Bare `tracing` and its other action stay dispatchable -- only the
|
||
# specific `compact` action is terminal-only.
|
||
assert _is_dispatch_compatible(["tracing"]) is True
|
||
assert _is_dispatch_compatible(["tracing", "stop"]) is True
|
||
|
||
|
||
def test_tracing_compact_is_rejected_whatever_the_option_order():
|
||
"""`compact` is positional, so Click takes it after any option.
|
||
|
||
The blacklist compares fixed argv positions, so the prefix tuple alone
|
||
only catches the action at argv[1]: `tracing --port 1 compact` would
|
||
otherwise reach dispatch and run a real, writing compaction detached
|
||
past the timeout. `_is_tracing_compact` closes every order.
|
||
"""
|
||
for argv in (
|
||
["tracing", "--dry-run", "compact"],
|
||
["tracing", "compact", "--dry-run"],
|
||
["tracing", "--port", "1", "compact"],
|
||
["tracing", "-p", "1", "compact"],
|
||
["tracing", "--foreground", "compact", "--dry-run"],
|
||
):
|
||
assert _is_dispatch_compatible(argv) is False, argv
|
||
|
||
# Not over-tight: every tracing invocation without the compact action
|
||
# stays dispatchable.
|
||
for argv in (
|
||
["tracing"],
|
||
["tracing", "stop"],
|
||
["tracing", "--port", "4318"],
|
||
["tracing", "--foreground"],
|
||
):
|
||
assert _is_dispatch_compatible(argv) is True, argv
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Reflection-based dispatch compat (_DISPATCH_WHITELIST deleted)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
def test_dispatch_compatible_via_reflection_newly_active():
|
||
"""Commands previously absent from _DISPATCH_WHITELIST now dispatch-OK
|
||
purely because reflection sees them registered on ec.cli.commands.app.
|
||
|
||
Covers 1 representative entry per CLI group that was previously unreachable.
|
||
"""
|
||
assert _is_dispatch_compatible(["channels", "enable"]) is True
|
||
assert _is_dispatch_compatible(["channels", "enable", "telegram"]) is True # prefix
|
||
assert _is_dispatch_compatible(["provider", "list"]) is True
|
||
assert _is_dispatch_compatible(["provider", "test"]) is True
|
||
assert _is_dispatch_compatible(["cron", "delete"]) is True
|
||
assert _is_dispatch_compatible(["cron", "add"]) is True
|
||
assert _is_dispatch_compatible(["sentinel", "discover-now"]) is True # hyphen subcmd
|
||
assert _is_dispatch_compatible(["sandbox", "exec"]) is True
|
||
assert _is_dispatch_compatible(["sandbox", "ls"]) is True # alias of list
|
||
# NOTE: trunk dropped 6 skill subcommands
|
||
# (refresh / stats / rebuild-index / import-files / cases / rollback);
|
||
# only ``skill list`` + ``skill get`` remain. The reflection model
|
||
# auto-tracks this — no code change needed, just the surface shifts.
|
||
# New on trunk: ``doctor`` top-level command — reflection picks it up
|
||
# for free, demonstrating the architectural payoff of the dynamic catalog.
|
||
assert _is_dispatch_compatible(["doctor"]) is True
|
||
|
||
|
||
def test_dispatch_rejects_unknown_commands():
|
||
"""The 5 'broken whitelist' entries (mcp/model/config groups
|
||
that the real CLI doesn't ship) now auto-clean.
|
||
|
||
Reflection does not find these groups on ec.cli.commands.app, so dispatch
|
||
returns False up front — TUI surfaces -32015 not_dispatch_compatible
|
||
(clean error toast) instead of the 'exit_code != 0 / no such command'
|
||
dirty path.
|
||
"""
|
||
# Unknown top-level group → False
|
||
assert _is_dispatch_compatible(["mcp", "list"]) is False
|
||
assert _is_dispatch_compatible(["mcp", "show"]) is False
|
||
assert _is_dispatch_compatible(["model", "list"]) is False
|
||
assert _is_dispatch_compatible(["model", "show"]) is False
|
||
assert _is_dispatch_compatible(["config", "show"]) is False
|
||
# Unknown subcommand under a real group
|
||
assert _is_dispatch_compatible(["channels", "nonexistent-sub"]) is False
|
||
assert _is_dispatch_compatible(["skill", "fly-to-the-moon"]) is False
|
||
# Incomplete (group head only, when group has subcommands) → False
|
||
assert _is_dispatch_compatible(["channels"]) is False
|
||
assert _is_dispatch_compatible(["skill"]) is False
|
||
|
||
|
||
# REQ-3 regression guard — table-driven; any single False here is a release block.
|
||
#
|
||
# History:
|
||
# - v0.0.2 had 14 hardcoded whitelist entries (status / channels status+list /
|
||
# skill list+get+refresh+stats / cron list+show / sentinel × 4 / sandbox list).
|
||
# - 2026-05-22 post merge from refactor/Raven: trunk dropped 6 ``skill``
|
||
# subcommands (refresh + stats + 4 others), leaving only ``skill list`` and
|
||
# ``skill get``. The guard list shrinks from 14 → 12 accordingly — those
|
||
# commands no longer exist in the CLI surface; reflection cannot find them
|
||
# and must not be expected to.
|
||
_WORKING_ENTRIES = [
|
||
("status",),
|
||
("channels", "status"),
|
||
("channels", "list"),
|
||
("skill", "list"),
|
||
("skill", "get"),
|
||
("cron", "list"),
|
||
("cron", "get"),
|
||
("sentinel", "status"),
|
||
("sentinel", "routines"),
|
||
("sentinel", "nudges"),
|
||
("sentinel", "decisions"),
|
||
("sandbox", "list"),
|
||
]
|
||
# Sentinel — protects the "currently 12" claim. If someone removes an entry
|
||
# while touching this list, the count test fails before the parametrised cases
|
||
# silently weaken the regression guard. The number itself is *not* a forever
|
||
# invariant — it's a snapshot. Update intentionally when trunk-side CLI
|
||
# surface evolves; never silently shrink.
|
||
_EXPECTED_WORKING_COUNT = 12
|
||
assert len(_WORKING_ENTRIES) == _EXPECTED_WORKING_COUNT, (
|
||
f"working entries snapshot drifted from {_EXPECTED_WORKING_COUNT} to "
|
||
f"{len(_WORKING_ENTRIES)}; bump _EXPECTED_WORKING_COUNT only when CLI "
|
||
f"surface intentionally changes (record reason in commit message)"
|
||
)
|
||
|
||
|
||
@pytest.mark.parametrize("argv", _WORKING_ENTRIES)
|
||
def test_working_entries_remain_compatible(argv):
|
||
"""Every still-present working entry must dispatch-OK after
|
||
_DISPATCH_WHITELIST removal. Currently 12 entries (trunk dropped 6 skill
|
||
subs; we keep the surviving 12 in scope). Any failure here is a hard
|
||
regression of a working slash that the user could previously type.
|
||
"""
|
||
assert _is_dispatch_compatible(list(argv)) is True, (
|
||
f"working entry {argv!r} regressed — reflection or filter now rejects what was previously dispatch-compatible"
|
||
)
|
||
|
||
|
||
def test_dispatch_agent_repl_blacklist_with_m_exception():
|
||
"""Task B: `agent` (no -m) is REPL → blacklist; `agent -m` is one-shot.
|
||
|
||
Note: `agent -m "msg"` isn't in whitelist either (one-shot agent is v0.0.3+
|
||
scope), so it's still rejected by whitelist — but NOT by the agent-REPL
|
||
blacklist check (which is the salient distinction for the test).
|
||
"""
|
||
from raven.rpc.methods.cli_dispatch import _is_agent_repl
|
||
|
||
assert _is_agent_repl(["agent"]) is True # REPL — blocked
|
||
assert _is_agent_repl(["agent", "-m", "hi"]) is False # one-shot — agent-REPL doesn't fire
|
||
assert _is_agent_repl(["agent", "--message", "hi"]) is False # long form
|
||
assert _is_agent_repl(["agent", "--help"]) is True # no -m → still REPL
|
||
assert _is_agent_repl(["status"]) is False # not agent at all
|
||
assert _is_agent_repl([]) is False # empty
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 4 — non-compatible argv raises NotDispatchCompatibleError (-32015)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
async def test_blacklist_raises_32015():
|
||
"""-32015 is an error frame → handler raises."""
|
||
with pytest.raises(NotDispatchCompatibleError) as exc_info:
|
||
await cli_dispatch({"argv": ["run"], "width": 80})
|
||
assert exc_info.value.code == -32015
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 5 — width validation (Pydantic Field constraints)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
async def test_width_too_small_rejected():
|
||
with pytest.raises(ConfigValidationError):
|
||
await cli_dispatch({"argv": ["status"], "width": 10})
|
||
|
||
|
||
async def test_width_too_large_rejected():
|
||
with pytest.raises(ConfigValidationError):
|
||
await cli_dispatch({"argv": ["status"], "width": 600})
|
||
|
||
|
||
async def test_width_in_range_passes_validation(fake_app_patch):
|
||
# width=80 with a known-compatible fake command → no validation error
|
||
result = await cli_dispatch({"argv": ["echo", "ok"], "width": 80})
|
||
assert result["exit_code"] == 0
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 6 — standalone_mode=False catches UsageError, process survives
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
async def test_standalone_mode_false_catches_usage_error(fake_app_patch):
|
||
"""Acceptance proxy: bad arg → exit_code != 0, process alive."""
|
||
result = await cli_dispatch({"argv": ["boom"], "width": 80})
|
||
assert result["exit_code"] != 0, "UsageError should produce non-zero exit_code"
|
||
# The process did NOT exit; we can call again.
|
||
result2 = await cli_dispatch({"argv": ["echo", "still-alive"], "width": 80})
|
||
assert result2["exit_code"] == 0
|
||
assert "still-alive" in result2["stdout"]
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 6.5 — B1 regression: typer.Exit(N) must propagate to exit_code=N
|
||
# ---------------------------------------------------------------------------
|
||
#
|
||
# Root cause:
|
||
# Click's ``standalone_mode=False`` catches ``typer.Exit`` (a ``RuntimeError``
|
||
# subclass that does NOT inherit ``SystemExit``) and **returns** the exit_code
|
||
# from ``app()`` instead of raising. ``_invoke_ec_cli`` previously discarded
|
||
# that return value, so all ``typer.Exit(N)`` paths silently reported
|
||
# ``exit_code=0`` to the TUI. Fix: capture the return value of
|
||
# ``ec_cli.app(argv, standalone_mode=False)`` and pass it back as exit_code.
|
||
|
||
|
||
async def test_b1_typer_exit_one_propagates(fake_app_patch):
|
||
"""typer.Exit(1) (raised by command body, caught by click+standalone=False) → exit_code=1."""
|
||
result = await cli_dispatch({"argv": ["typer-exit-one"], "width": 80})
|
||
assert result["exit_code"] == 1, "typer.Exit(1) must propagate as exit_code=1"
|
||
|
||
|
||
async def test_b1_typer_exit_three_propagates(fake_app_patch):
|
||
"""typer.Exit(3) must propagate as exit_code=3 (any non-zero code path)."""
|
||
result = await cli_dispatch({"argv": ["typer-exit-three"], "width": 80})
|
||
assert result["exit_code"] == 3, "typer.Exit(3) must propagate as exit_code=3"
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 7 — timeout raises CliCommandTimeoutError (-32014)
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
async def test_timeout_raises_32014(fake_app_patch):
|
||
with pytest.raises(CliCommandTimeoutError) as exc_info:
|
||
await cli_dispatch({"argv": ["slow"], "width": 80, "timeout_s": 0.05})
|
||
assert exc_info.value.code == -32014
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 8 / 9 — ANSI filter
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
def test_ansi_filter_strips_cursor_movement():
|
||
raw = "\x1b[31mred\x1b[2A\x1b[H\x1b[2J\x1b[0m"
|
||
out = filter_ansi(raw)
|
||
assert "\x1b[31m" in out
|
||
assert "\x1b[0m" in out
|
||
assert "red" in out
|
||
# Cursor + clear-screen sequences gone
|
||
assert "\x1b[2A" not in out
|
||
assert "\x1b[H" not in out
|
||
assert "\x1b[2J" not in out
|
||
|
||
|
||
def test_ansi_filter_preserves_truecolor():
|
||
raw = "\x1b[38;2;255;128;0morange\x1b[0m"
|
||
assert filter_ansi(raw) == raw
|
||
|
||
|
||
def test_ansi_filter_strips_osc8_hyperlink():
|
||
"""OSC 8 hyperlink: \\x1b]8;;url\\x07TEXT\\x1b]8;;\\x07 — strip the OSC envelopes."""
|
||
raw = "\x1b]8;;https://example.com\x07click\x1b]8;;\x07"
|
||
out = filter_ansi(raw)
|
||
# The visible text survives; the OSC 8 envelopes are stripped.
|
||
assert "click" in out
|
||
assert "\x1b]8" not in out
|
||
|
||
|
||
def test_ansi_filter_strips_decset():
|
||
raw = "\x1b[?1049hbody\x1b[?1049l"
|
||
out = filter_ansi(raw)
|
||
assert "body" in out
|
||
assert "?1049" not in out
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Test 10 — width sync propagates to Console
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
async def test_width_sync_propagates_to_console(fake_app_patch):
|
||
"""Acceptance proxy: width=60 → output lines ≤ 60 cells."""
|
||
result = await cli_dispatch({"argv": ["render-table", "--rows", "2"], "width": 60})
|
||
assert result["exit_code"] == 0
|
||
# ANSI-strip + check line length budget. Rich wraps tables to console width.
|
||
import re
|
||
|
||
plain = re.sub(r"\x1b\[[\d;]*m", "", result["stdout"])
|
||
for line in plain.splitlines():
|
||
# Allow trailing whitespace; the table border should be ≤ width.
|
||
assert len(line.rstrip()) <= 60, f"line exceeds width=60: {line!r}"
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Bonus — register_cli_methods wires up dispatcher
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
async def test_register_cli_methods_registers_dispatch():
|
||
d = Dispatcher()
|
||
register_cli_methods(d)
|
||
assert "cli.dispatch" in d.methods()
|
||
|
||
|
||
async def test_dispatcher_maps_not_compat_to_error_frame(fake_app_patch):
|
||
"""End-to-end: dispatcher receives non-whitelisted argv → -32015 error frame."""
|
||
d = Dispatcher()
|
||
register_cli_methods(d)
|
||
frame = {
|
||
"jsonrpc": "2.0",
|
||
"id": 1,
|
||
"method": "cli.dispatch",
|
||
"params": {"argv": ["run"], "width": 80},
|
||
}
|
||
response = await d.dispatch(frame)
|
||
assert "error" in response
|
||
assert response["error"]["code"] == -32015
|
||
|
||
|
||
async def test_dispatcher_happy_path_returns_result_frame(fake_app_patch):
|
||
d = Dispatcher()
|
||
register_cli_methods(d)
|
||
frame = {
|
||
"jsonrpc": "2.0",
|
||
"id": 2,
|
||
"method": "cli.dispatch",
|
||
"params": {"argv": ["echo", "via-dispatcher"], "width": 80},
|
||
}
|
||
response = await d.dispatch(frame)
|
||
assert "result" in response, f"expected result, got {response}"
|
||
assert response["result"]["exit_code"] == 0
|
||
assert "via-dispatcher" in response["result"]["stdout"]
|
||
|
||
|
||
async def test_concurrent_dispatches_serialized(fake_app_patch):
|
||
"""Q7 risk 4 — `asyncio.Lock` must serialize so console patches don't race."""
|
||
results = await asyncio.gather(
|
||
cli_dispatch({"argv": ["echo", "a"], "width": 80}),
|
||
cli_dispatch({"argv": ["echo", "b"], "width": 80}),
|
||
cli_dispatch({"argv": ["echo", "c"], "width": 80}),
|
||
)
|
||
for r in results:
|
||
assert r["exit_code"] == 0
|
||
outputs = {r["stdout"].strip() for r in results}
|
||
assert outputs == {"a", "b", "c"}
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Confirm round-trip timeout grace
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
async def test_dispatch_timeout_includes_confirm_grace(fake_app_patch, monkeypatch):
|
||
"""With a ConfirmBroker present, the dispatch wait_for budget = timeout_s +
|
||
_CONFIRM_HARD_LIMIT_S (fixed enlargement)."""
|
||
from raven.rpc import confirm_broker as cb
|
||
from raven.rpc.confirm_broker import ConfirmBroker
|
||
|
||
seen: list[float] = []
|
||
real_wait_for = asyncio.wait_for
|
||
|
||
async def spy(aw, timeout):
|
||
seen.append(timeout)
|
||
return await real_wait_for(aw, timeout)
|
||
|
||
monkeypatch.setattr(asyncio, "wait_for", spy)
|
||
|
||
async def _send(_frame):
|
||
return None
|
||
|
||
broker = ConfirmBroker(_send)
|
||
result = await cli_dispatch(
|
||
{"argv": ["echo", "hi"], "width": 80, "timeout_s": 20.0},
|
||
confirm_broker=broker,
|
||
)
|
||
assert result["exit_code"] == 0
|
||
assert (20.0 + cb._CONFIRM_HARD_LIMIT_S) in seen
|
||
|
||
|
||
async def test_dispatch_timeout_unchanged_without_broker(fake_app_patch, monkeypatch):
|
||
"""No broker → no grace; wait_for budget stays the plain timeout_s."""
|
||
seen: list[float] = []
|
||
real_wait_for = asyncio.wait_for
|
||
|
||
async def spy(aw, timeout):
|
||
seen.append(timeout)
|
||
return await real_wait_for(aw, timeout)
|
||
|
||
monkeypatch.setattr(asyncio, "wait_for", spy)
|
||
|
||
await cli_dispatch({"argv": ["echo", "hi"], "width": 80, "timeout_s": 20.0})
|
||
assert 20.0 in seen
|
||
|
||
|
||
async def test_abort_returns_confirmation_hint(fake_app_patch):
|
||
"""C1: click.Abort downgrades to a --yes hint, not `Internal error: Abort`."""
|
||
result = await cli_dispatch({"argv": ["aborts"], "width": 80})
|
||
assert result["exit_code"] != 0
|
||
assert "--yes" in result["stderr"]
|
||
assert "Internal error" not in result["stderr"]
|
||
assert "Abort" not in result["stderr"]
|
||
|
||
|
||
async def test_an_unregistered_host_is_not_dispatch_compatible():
|
||
"""The socket is the feature's only door: a host that never registered a
|
||
console (an acp server, a bare test stack) must see every argv as
|
||
not-dispatch-compatible rather than import its way to the cli."""
|
||
from raven.rpc import cli_socket
|
||
from raven.rpc.methods.cli_dispatch import _is_dispatch_compatible as is_dispatch_compatible
|
||
|
||
saved = (cli_socket.cli_commands(), cli_socket.console_hosts())
|
||
cli_socket.reset()
|
||
try:
|
||
assert is_dispatch_compatible(["version"]) is False
|
||
finally:
|
||
if saved[0] is not None:
|
||
cli_socket.register_cli(saved[0], saved[1])
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Finding the console hosts
|
||
# ---------------------------------------------------------------------------
|
||
|
||
|
||
@pytest.fixture
|
||
def unregistered_socket():
|
||
"""The socket with nothing registered, then what the suite registered put back."""
|
||
from raven.rpc import cli_socket
|
||
|
||
saved = (cli_socket.cli_commands(), cli_socket.console_hosts())
|
||
cli_socket.reset()
|
||
yield cli_socket
|
||
cli_socket.reset()
|
||
if saved[0] is not None:
|
||
cli_socket.register_cli(saved[0], saved[1])
|
||
|
||
|
||
def test_a_finder_handed_to_the_socket_runs_once_on_the_first_lookup(unregistered_socket) -> None:
|
||
import types
|
||
|
||
host = types.ModuleType("host")
|
||
finds: list[str] = []
|
||
|
||
def find() -> tuple[types.ModuleType, ...]:
|
||
finds.append("find")
|
||
return (host,)
|
||
|
||
unregistered_socket.register_cli(types.ModuleType("commands"), find)
|
||
assert finds == []
|
||
|
||
assert unregistered_socket.console_hosts() == (host,)
|
||
assert unregistered_socket.console_hosts() == (host,)
|
||
assert finds == ["find"]
|
||
|
||
|
||
def test_a_reset_drops_a_finder_that_never_ran(unregistered_socket) -> None:
|
||
import types
|
||
|
||
finds: list[str] = []
|
||
|
||
def find() -> tuple[types.ModuleType, ...]:
|
||
finds.append("find")
|
||
return ()
|
||
|
||
unregistered_socket.register_cli(types.ModuleType("commands"), find)
|
||
unregistered_socket.reset()
|
||
|
||
assert unregistered_socket.console_hosts() == ()
|
||
assert finds == []
|
||
|
||
|
||
@pytest.mark.parametrize(
|
||
("askable", "at_registration"),
|
||
[(False, []), (True, ["find"])],
|
||
ids=["the terminal cannot be asked", "the terminal can be asked"],
|
||
)
|
||
def test_the_hosts_are_found_at_registration_only_where_the_terminal_can_be_asked(
|
||
unregistered_socket, monkeypatch, askable: bool, at_registration: list[str]
|
||
) -> None:
|
||
"""Finding the hosts imports every CLI module, and one of them asks the terminal
|
||
for its colours as it is imported. Where the terminal cannot be asked -- none, as
|
||
for the gateway `raven web` detaches, or Windows, SSH, screen or tmux, CI -- they
|
||
are found on the first console command; where it can, as from the TUI's host
|
||
before the Ink screen starts, at registration. The query itself is stubbed: a
|
||
case may be the first to import the module that makes it."""
|
||
from raven.cli import _console_feature, _theme
|
||
from raven.cli import commands as commands_module
|
||
|
||
find = _console_feature._console_host_modules
|
||
finds: list[str] = []
|
||
|
||
def counted():
|
||
finds.append("find")
|
||
return find()
|
||
|
||
monkeypatch.setattr(_console_feature, "_console_host_modules", counted)
|
||
monkeypatch.setattr(_theme, "_can_probe", lambda: askable)
|
||
monkeypatch.setattr(_theme, "_probe_osc11", lambda: None)
|
||
|
||
_console_feature.register_console_feature()
|
||
assert finds == at_registration
|
||
|
||
assert commands_module in unregistered_socket.console_hosts()
|
||
assert finds == ["find"]
|
||
|
||
|
||
@pytest.mark.slow
|
||
def test_where_the_terminal_cannot_be_asked_the_finder_imports_on_the_first_lookup(tmp_path) -> None:
|
||
"""Checked in a fresh interpreter whose stdin and stdout are not a terminal. The
|
||
modules counted are the ones only the finder imports: the rest of the CLI came in
|
||
with the command table, which every host has imported by then."""
|
||
import subprocess
|
||
import sys
|
||
|
||
probe = (
|
||
"import pkgutil, sys\n"
|
||
"import raven.cli, raven.cli.commands\n"
|
||
"from raven.cli import _console_feature, _theme\n"
|
||
"from raven.rpc import cli_socket\n"
|
||
"every = {f'raven.cli.{m.name}' for m in pkgutil.iter_modules(raven.cli.__path__) if not m.name.startswith('__')}\n"
|
||
"finders = every - set(sys.modules)\n"
|
||
"print('only the finder imports the colour query:', 'raven.cli._styles' in finders)\n"
|
||
"_console_feature.register_console_feature()\n"
|
||
"print('imported at registration:', sorted(finders & set(sys.modules)))\n"
|
||
"hosts = cli_socket.console_hosts()\n"
|
||
"print('left after the first lookup:', sorted(finders - set(sys.modules)), len(hosts) > 0)\n"
|
||
)
|
||
done = subprocess.run(
|
||
[sys.executable, "-P", "-c", probe],
|
||
stdin=subprocess.DEVNULL,
|
||
cwd=tmp_path,
|
||
capture_output=True,
|
||
text=True,
|
||
timeout=120,
|
||
)
|
||
|
||
assert "only the finder imports the colour query: True" in done.stdout, done.stdout + done.stderr
|
||
assert "imported at registration: []" in done.stdout, done.stdout + done.stderr
|
||
assert "left after the first lookup: [] True" in done.stdout, done.stdout + done.stderr
|