373 unit tests, up from 266. Three files, split the way the modules are: `test_mcp_authorizations.py` is standard library and touches no SDK, `test_mcp_http.py` drives a real ASGI stack, and the additions to `test_mcp_server.py` are about the command line now that it has subcommands. Four of these exist for properties that a refactor breaks silently rather than loudly, which is the only reason they are worth their length: * **the file never holds a token.** A store that started writing the secret instead of its digest would pass every functional test here, so one test reads the bytes on disk and looks for the token and for a usable prefix of it. * **a concurrent mint is not lost.** Thirty threads mint into one table and all thirty tokens must still verify. The atomic replace stops a torn read; it does nothing about two writers loading the same table and the second replacing the first's addition. Without the flock this fails. * **`--toolsets` before the subcommand survives it.** argparse copies a subparser's namespace over the top-level one, so an ordinary default on the subcommand's copy would overwrite the value with `None` and `hhstream-mcp --toolsets=frames serve` would quietly serve all twenty-one tools. `SUPPRESS` prevents it and this test notices if it goes. * **a token verb never claims the transport.** `mint` prints a secret on stdout, which on stdio *is* the transport, so `claim_transport` is monkeypatched to fail the test if it is ever reached. The refusal tests assert the absence of information as much as the status code: an empty body, no echo of the token in body or headers, and unknown/revoked/expired arriving as one answer byte for byte -- telling them apart would tell someone who guessed a real id that they had. An unknown path is a 401 before a 404, so an unauthenticated caller does not learn what is mounted; the token is checked before the Host, so it does not learn the allowlist either. `test_resources_are_not_narrowed_by_a_token` asserts a *limit*, on purpose: a token scoped to `frames` can still read `hhstream://sessions`, and that is written down rather than left for someone to discover. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KchcvnbfhhZhUHy83YL3Ya
129 lines
5.2 KiB
Nix
129 lines
5.2 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.
|
|
# `starlette` and `httpx` beside `mcp`, and for the same reason the comment
|
|
# above gives: `tests/unit/test_mcp_http.py` drives the HTTP transport
|
|
# through `starlette.testclient`, which needs `httpx`. Both arrive through
|
|
# the SDK today -- measured -- and relying on that is the trap. An SDK
|
|
# release that moved its HTTP transport behind an optional extra would make
|
|
# 43 tests vanish from this gate, and a gate that loses tests silently is
|
|
# worse than one that fails.
|
|
pytestEnv = python3.withPackages (ps: [
|
|
ps.pytest
|
|
ps.pywayland
|
|
ps.xkbcommon
|
|
ps.mcp
|
|
ps.starlette
|
|
ps.httpx
|
|
]);
|
|
|
|
# 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")'
|
|
# The HTTP transport's stack, proved present here rather than discovered as a
|
|
# skip in the report: `test_mcp_http.py` imports `starlette.testclient` at
|
|
# module scope and is not allowed to skip.
|
|
python3 -c 'import starlette.testclient, hhstream_mcp.http; print("http transport imports")'
|
|
|
|
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
|
|
''
|