Files
hyprland-headless-stream-pl…/checks/unit.nix
T
HermenixandClaude Opus 5 fea55eecde test(mcp): the argv each tool builds, the mapping, and the server in a guest
101 unit tests under `tests/unit/` and one new VM check.

The unit suite's subject is the only thing this layer decides: the argv list.
`mcp_fake.FakeCli` is a recording stand-in put where the CLI goes, so every
test can assert on the exact argv a tool handed over -- the `=` form of each
flag, the `--` before each positional, the omission of a flag the caller did
not give. Three tests drive the **real** entry point against a control socket
that does not exist, which proves the dispatch, the capture and the exit-code
mapping against the CLI's actual parser with no daemon standing up: argparse's
exit 2 for a bad `--button`, exit 5 for the unreachable broker, exit 9 for a
gesture with no lease.

`test_mcp_server.py` imports the MCP SDK unconditionally and runs the server
in process over the SDK's memory transport. It is not allowed to skip: the tool
table is stdlib and tested without the SDK, but whether every schema validates
and every name is one the specification allows is a question only the SDK can
answer, and a file that skipped when it was absent would be green in exactly
the build where that mattered. `checks/unit.nix` therefore puts `mcp` in the
test environment and `hhstream-mcp` on PYTHONPATH beside `hhstream`.

`checks/vm-mcp.nix` boots the session machine with `mcp.enable = true` and
drives the real binary over a real pipe with `checks/lib/mcp_client.py` -- raw
JSON-RPC, deliberately not the SDK's client, for the reason
`tests/assert_stream.py` is a second implementation of the frame contract. It
asserts the handshake, the 26-tool listing with its annotations and its two
rules, a call that reaches the live broker, `shot` as image content, the lease
granted/used/returned against the real driver, and exits 9 and 3 arriving as
tool errors.

The geometry assertion in that check is one it got wrong first time and now
records: the published frame is 2560x720 on a guest configured for 1280x720,
because the publisher's default capture takes the whole compositor layout and a
session's container is a second output beside the main one. `shot` with the
container's own `output` is the one that is exactly 1280x720.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011ZeH65zd6pFTgBSu52W9PE
2026-10-11 06:32:55 +00:00

116 lines
4.4 KiB
Nix

# checks.<system>.unit -- the Python unit suite from tests/, run against the
# package built by nix/package.nix, inside the build sandbox.
#
# The whole tree, not one directory: it used to take tests/unit alone, which
# meant the broker's and the pointer driver's suites (and the sessions one) were
# never collected by any check -- a gate that could not see 432 of 551 tests.
#
# pywayland is in the test environment because the pointer driver imports it;
# without it those tests fail as an import error rather than as the thing they
# test.
#
# nix/package.nix sets doCheck = false deliberately, so this is the only place
# the suite runs in a build.
{
lib,
runCommand,
python3,
hhstream,
hhstream-mcp,
}:
let
# `xkbcommon` beside `pywayland`, and it is not optional: `tests/pointer/
# test_keymap.py` compiles a real `us` keymap and a real `de` one, because
# only xkbcommon can say that `é` is unmappable on `us` and that `@` on `de`
# needs a level-three mask rather than Shift. A suite that could skip those
# would let the unmappable-character path rot, which is the path that decides
# whether a bad string types a prefix.
#
# `mcp` beside them, for `tests/unit/test_mcp_server.py`. That file imports
# the SDK on purpose and is not allowed to skip: the tool table itself is
# stdlib-only and tested without it, but the step that turns the table into
# `mcp.types.Tool` objects is where a malformed schema or a name the spec
# refuses would show up, and a file that skipped when the SDK was absent
# would be green in exactly the build where that mattered.
pytestEnv = python3.withPackages (ps: [
ps.pytest
ps.pywayland
ps.xkbcommon
ps.mcp
]);
# Only the suite itself, so an editor's droppings or a local .pytest_cache
# cannot change the derivation's hash.
testSrc = lib.cleanSourceWith {
name = "hhstream-tests";
src = ../tests;
filter =
path: type:
let
base = baseNameOf (toString path);
in
!(type == "directory" && (base == "__pycache__" || base == ".pytest_cache"))
&& !lib.hasSuffix ".pyc" base;
};
in
runCommand "hhstream-unit"
{
nativeBuildInputs = [ pytestEnv ];
passthru.testSrc = testSrc;
meta.description = "hhstream unit tests (pytest)";
} ''
cp -r ${testSrc} suite
chmod -R u+w suite
# `tests/sessions/integration/test_lifecycle.py` locates the repository by
# looking upward for a `pyproject.toml` (it needs the root for `src/` on
# PYTHONPATH and as a cwd for the subprocesses it spawns). In a build there is
# no repository above the copied suite, so it raises at import time and takes
# the whole run down as a collection error. Giving it the marker it documents
# is the fix; the test itself is hermetic -- no compositor, no network.
cp ${../pyproject.toml} suite/pyproject.toml
# The package is an application, so its library is not on the default path:
# point PYTHONPATH at what nix/package.nix installed. Importing it here rather
# than letting pytest do it makes a packaging problem distinguishable from a
# test failure.
export PYTHONPATH=${hhstream}/${python3.sitePackages}:${hhstream-mcp}/${python3.sitePackages}
export PYTHONDONTWRITEBYTECODE=1
export HOME=$PWD
python3 -c 'import hhstream; print("testing hhstream", hhstream.__version__, "from", hhstream.__file__)'
python3 -c 'import hhstreamd.ipc, hhstream_pointer; print("broker ipc client + pointer driver import")'
# The second distribution, on the path beside the first. `tests/unit`'s
# conftest adds `../src` to sys.path, which does not exist in this sandbox --
# so here, as for `hhstream`, the import comes from what Nix installed, and a
# packaging mistake is distinguishable from a test failure.
python3 -c 'import hhstream_mcp.tools as t; print("mcp tool table:", len(t.TOOLS), "tools")'
set +e
pytest suite \
--color=no -rf -q \
--junit-xml=report.xml \
-p no:cacheprovider \
--strict-markers \
--strict-config
pytest_rc=$?
set -e
echo
set +e
python3 ${./lib/check_report.py} report.xml
report_rc=$?
set -e
if [ "$report_rc" -eq 1 ]; then
echo "unit check: the suite is empty or its report is unusable -- failing." >&2
exit 1
fi
if [ "$report_rc" -ne 0 ] || [ "$pytest_rc" -ne 0 ]; then
echo "unit check: the suite ran but did not pass (pytest exit $pytest_rc)." >&2
exit 1
fi
mkdir -p $out
cp report.xml $out/junit.xml
''