"""A recording stand-in for the CLI, and two image fixtures, for the MCP tests. The MCP server calls ``hhstream.cli``'s handlers in this process, so what sits between a tool and the broker is **the argv list** -- and that list is the only thing this layer decides. :class:`FakeCli` is therefore an entry point that records the argv it was given, prints whatever the test told it to, and returns the exit code the test chose: everything the real entry point is, minus the broker. That is also why there is no live socket anywhere in this suite. The broker's behaviour is `tests/sessions`' subject and the VM checks'; what is left here is the shim. Two tests in ``test_mcp_invoke.py`` do drive the **real** entry point against a socket path that does not exist, which is the honest way to prove the dispatch, the capture and the exit-code mapping without standing a daemon up. """ from __future__ import annotations import json import pathlib import struct import sys import zlib from typing import Any class FakeCli: """Where :func:`hhstream_mcp.invoke.dispatch` goes in a test. Prints through ``sys.stdout``/``sys.stderr`` rather than returning text, so the capture under test is exercised rather than bypassed. """ def __init__( self, *, stdout: str = "", stderr: str = "", exit_code: int = 0, write_png: bytes | None = None, raises: BaseException | None = None, ) -> None: self.stdout = stdout self.stderr = stderr self.exit_code = exit_code self.write_png = write_png self.raises = raises #: Every argv this was called with, in order. self.calls: list[list[str]] = [] def __call__(self, argv: list[str]) -> int: self.calls.append(list(argv)) if self.write_png is not None: destination = _value_of("--out", argv) if destination: pathlib.Path(destination).write_bytes(self.write_png) if self.stdout: sys.stdout.write(self.stdout) if self.stderr: sys.stderr.write(self.stderr) if self.raises is not None: raise self.raises return self.exit_code @property def argv(self) -> list[str]: """The single call's argv. Fails loudly if there was not exactly one.""" assert len(self.calls) == 1, f"expected one CLI call, got {self.calls}" return self.calls[0] def _value_of(name: str, argv: list[str]) -> str | None: prefix = f"{name}=" for argument in argv: if argument.startswith(prefix): return argument[len(prefix) :] return None def reply(**fields: Any) -> str: """A protocol object the way the CLI prints one: ``json.dumps(indent=2)``.""" return json.dumps(fields, indent=2, sort_keys=True) + "\n" def png(width: int = 8, height: int = 4) -> bytes: """A minimal valid truecolour PNG of a known size. Built here rather than shared with the suite's other helper because what is under test is "the server reports the geometry the file actually has", so the size has to be the test's to choose. """ def chunk(kind: bytes, data: bytes) -> bytes: return ( struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF) ) raster = (b"\x00" + b"\x10\x20\x30" * width) * height return ( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(raster, 9)) + chunk(b"IEND", b"") ) def jpeg(width: int = 6, height: int = 3) -> bytes: """Enough of a JPEG to carry a SOF0: magic, APP0, SOF0, EOI. Not a decodable image, deliberately: what is under test is the header scan that has to skip a segment it does not care about and find the one it does. """ app0 = ( b"\xff\xe0" + struct.pack(">H", 16) + b"JFIF\x00" + b"\x01\x01\x00" + b"\x00\x01" * 2 + b"\x00\x00" ) sof0 = ( b"\xff\xc0" + struct.pack(">H", 11) + b"\x08" + struct.pack(">HH", height, width) + b"\x01\x01\x11\x00" ) return b"\xff\xd8" + app0 + sof0 + b"\xff\xd9" def isolate(monkeypatch, tmp_path: pathlib.Path) -> None: """Point every path the CLI reads from the environment at ``tmp_path``. A developer's shell has `$HHSTREAM_SOCKET` set, and the real entry point reads it -- so without this a test could reach a live broker on the machine it is running on. The lease directory is redirected for the same reason: `pointer acquire` writes a cache there, and a test must not touch the one a real session is using. """ monkeypatch.setenv("HHSTREAM_SOCKET", str(tmp_path / "control.sock")) monkeypatch.setenv("HHSTREAM_FRAME_SOCKET", str(tmp_path / "stream.sock")) monkeypatch.setenv("HHSTREAM_LEASE_DIR", str(tmp_path / "leases")) # `$HHSTREAM_LEASE` outranks the cache in `hhstream_session.lease.recall`, # so an inherited one would hand a gesture a lease it never acquired. monkeypatch.delenv("HHSTREAM_LEASE", raising=False)