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
This commit is contained in:
1 parent
549c73e8ae
commit
fea55eecde
8 files changed
+1984
-3
No files matched your search
+8
-1
@@ -1,6 +1,7 @@
|
||||
# The flake's `checks` output. One file per check; this is only the wiring.
|
||||
#
|
||||
# unit -- pytest over tests/, against the nix/package.nix build
|
||||
# vm-mcp -- the MCP server over stdio, against a live broker
|
||||
# vm-basic -- nixosTest with a fake capture backend: module + wire contract
|
||||
# vm-hyprland -- nixosTest with the real compositor and grim
|
||||
# vm-sessions -- nixosTest with the real compositor and the session broker
|
||||
@@ -32,6 +33,7 @@
|
||||
}:
|
||||
let
|
||||
hhstream = self.packages.${system}.hhstream;
|
||||
hhstream-mcp = self.packages.${system}.hhstream-mcp;
|
||||
hhsModule = self.nixosModules.default;
|
||||
testTools = pkgs.callPackage ./lib/test-tools.nix { };
|
||||
# The session checks take the same arguments; naming them once keeps a new
|
||||
@@ -42,7 +44,7 @@ let
|
||||
};
|
||||
in
|
||||
{
|
||||
unit = pkgs.callPackage ./unit.nix { inherit hhstream; };
|
||||
unit = pkgs.callPackage ./unit.nix { inherit hhstream hhstream-mcp; };
|
||||
|
||||
vm-basic = pkgs.callPackage ./vm-basic.nix {
|
||||
inherit hhstream hhsModule testTools;
|
||||
@@ -73,4 +75,9 @@ in
|
||||
vm-stream-firewall = pkgs.callPackage ./vm-stream-firewall.nix {
|
||||
inherit hhstream hhsModule testTools;
|
||||
};
|
||||
|
||||
# The session machine again, plus `mcp.enable`, driven over stdio by a
|
||||
# JSON-RPC client the check writes itself. `sessionArgs` plus the one package
|
||||
# the others have no reason to carry.
|
||||
vm-mcp = pkgs.callPackage ./vm-mcp.nix (sessionArgs // { inherit hhstream-mcp; });
|
||||
}
|
||||
Executable
+175
@@ -0,0 +1,175 @@
|
||||
#!/usr/bin/env python3
|
||||
"""``hhs-mcp-call`` -- drive an MCP server over stdio from a shell.
|
||||
|
||||
What `checks/vm-mcp.nix` uses to talk to `hhstream-mcp` in the guest, and
|
||||
deliberately **not** the MCP SDK's own client: the SDK on both ends would agree
|
||||
with itself about a framing neither side had to get right. This is raw
|
||||
JSON-RPC, newline-delimited, written against the specification -- the same
|
||||
choice, and for the same reason, as `tests/assert_stream.py` being a second
|
||||
implementation of the frame contract.
|
||||
|
||||
One server process per invocation, which means every call re-does the
|
||||
`initialize` handshake. That is a feature here: the handshake is one of the
|
||||
things under test, and a client that did it once would prove it once.
|
||||
|
||||
hhs-mcp-call --initialize the handshake's own result
|
||||
hhs-mcp-call --list every tool, as JSON
|
||||
hhs-mcp-call session_list one call, no arguments
|
||||
hhs-mcp-call session_touch '{"id": "s-1"}' one call, arguments as JSON
|
||||
|
||||
Always exits 0 when the server answered, whatever the answer was: a refusal is
|
||||
a result the test asserts on, not this program's failure. Exit 2 is "the
|
||||
server did not answer" -- it died, it timed out, or it sent something that is
|
||||
not JSON-RPC -- which is the one outcome no assertion should be able to mistake
|
||||
for a reply.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Any
|
||||
|
||||
#: The protocol version this client speaks. Named rather than negotiated: a
|
||||
#: server that answers a different one is a thing the test should see.
|
||||
PROTOCOL_VERSION = "2025-06-18"
|
||||
|
||||
|
||||
def fail(message: str) -> None:
|
||||
print(f"hhs-mcp-call: {message}", file=sys.stderr)
|
||||
raise SystemExit(2)
|
||||
|
||||
|
||||
class Server:
|
||||
"""One ``hhstream-mcp`` process, spoken to over its stdin and stdout."""
|
||||
|
||||
def __init__(self, command: list[str], timeout: int) -> None:
|
||||
self.timeout = timeout
|
||||
# stderr is left alone on purpose: the server logs there, and in a
|
||||
# nixosTest that ends up in the build log beside the assertion that
|
||||
# failed, which is where it is wanted.
|
||||
self.process = subprocess.Popen(
|
||||
command,
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
)
|
||||
self.next_id = 0
|
||||
|
||||
def send(self, message: dict[str, Any]) -> None:
|
||||
assert self.process.stdin is not None
|
||||
self.process.stdin.write(json.dumps(message) + "\n")
|
||||
self.process.stdin.flush()
|
||||
|
||||
def read(self) -> dict[str, Any]:
|
||||
assert self.process.stdout is not None
|
||||
# An alarm rather than a poll loop: a server that never answers must
|
||||
# fail the check, not hang the test driver until its global timeout.
|
||||
signal.alarm(self.timeout)
|
||||
try:
|
||||
line = self.process.stdout.readline()
|
||||
except KeyboardInterrupt: # pragma: no cover -- the alarm handler
|
||||
fail(f"no answer within {self.timeout}s")
|
||||
raise
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
if not line:
|
||||
fail(f"the server closed its stream (exit {self.process.poll()})")
|
||||
try:
|
||||
return json.loads(line)
|
||||
except json.JSONDecodeError:
|
||||
# The failure a stray `print` on stdout produces, named as such:
|
||||
# the tools run the CLI in process, and this is what it looks like
|
||||
# when something it printed reached the transport.
|
||||
fail(f"not JSON-RPC on the server's stdout: {line!r}")
|
||||
raise AssertionError("unreachable")
|
||||
|
||||
def request(self, method: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
self.next_id += 1
|
||||
wanted = self.next_id
|
||||
self.send(
|
||||
{"jsonrpc": "2.0", "id": wanted, "method": method, "params": params or {}}
|
||||
)
|
||||
answer = self.read()
|
||||
if answer.get("id") != wanted:
|
||||
fail(f"answer for id {answer.get('id')!r}, expected {wanted}")
|
||||
if "error" in answer:
|
||||
fail(f"{method} was refused at the protocol level: {answer['error']}")
|
||||
return answer["result"]
|
||||
|
||||
def initialize(self) -> dict[str, Any]:
|
||||
result = self.request(
|
||||
"initialize",
|
||||
{
|
||||
"protocolVersion": PROTOCOL_VERSION,
|
||||
"capabilities": {},
|
||||
"clientInfo": {"name": "hhs-mcp-call", "version": "0"},
|
||||
},
|
||||
)
|
||||
self.send({"jsonrpc": "2.0", "method": "notifications/initialized"})
|
||||
return result
|
||||
|
||||
def close(self) -> int:
|
||||
assert self.process.stdin is not None
|
||||
self.process.stdin.close()
|
||||
try:
|
||||
return self.process.wait(timeout=self.timeout)
|
||||
except subprocess.TimeoutExpired: # pragma: no cover
|
||||
self.process.kill()
|
||||
return -1
|
||||
|
||||
|
||||
def _alarm(signum, frame): # pragma: no cover -- raises into read()
|
||||
raise KeyboardInterrupt
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="hhs-mcp-call", description=__doc__)
|
||||
parser.add_argument(
|
||||
"--server",
|
||||
default="hhstream-mcp",
|
||||
help="the MCP server to spawn (default: %(default)s, from PATH)",
|
||||
)
|
||||
parser.add_argument("--timeout", type=int, default=120, metavar="SECONDS")
|
||||
parser.add_argument("--initialize", action="store_true", help="print the handshake result")
|
||||
parser.add_argument("--list", action="store_true", help="print the tool listing")
|
||||
parser.add_argument("tool", nargs="?", help="the tool to call")
|
||||
parser.add_argument("arguments", nargs="?", default="{}", help="its arguments, as JSON")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
signal.signal(signal.SIGALRM, _alarm)
|
||||
|
||||
try:
|
||||
arguments = json.loads(args.arguments)
|
||||
except json.JSONDecodeError as exc:
|
||||
fail(f"arguments are not JSON: {exc}")
|
||||
return 2
|
||||
if not isinstance(arguments, dict):
|
||||
fail("arguments must be a JSON object")
|
||||
|
||||
server = Server([args.server], args.timeout)
|
||||
try:
|
||||
handshake = server.initialize()
|
||||
if args.initialize:
|
||||
print(json.dumps(handshake, indent=2, sort_keys=True))
|
||||
return 0
|
||||
if args.list:
|
||||
print(json.dumps(server.request("tools/list"), indent=2, sort_keys=True))
|
||||
return 0
|
||||
if not args.tool:
|
||||
fail("name a tool, or pass --list or --initialize")
|
||||
result = server.request(
|
||||
"tools/call", {"name": args.tool, "arguments": arguments}
|
||||
)
|
||||
print(json.dumps(result, indent=2, sort_keys=True))
|
||||
return 0
|
||||
finally:
|
||||
server.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
+20
-2
@@ -16,6 +16,7 @@
|
||||
runCommand,
|
||||
python3,
|
||||
hhstream,
|
||||
hhstream-mcp,
|
||||
}:
|
||||
let
|
||||
# `xkbcommon` beside `pywayland`, and it is not optional: `tests/pointer/
|
||||
@@ -24,7 +25,19 @@ let
|
||||
# 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.
|
||||
pytestEnv = python3.withPackages (ps: [ ps.pytest ps.pywayland ps.xkbcommon ]);
|
||||
#
|
||||
# `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.
|
||||
@@ -61,11 +74,16 @@ runCommand "hhstream-unit"
|
||||
# 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}
|
||||
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 \
|
||||
|
||||
@@ -0,0 +1,375 @@
|
||||
# checks.<system>.vm-mcp -- the MCP server over stdio, in a guest, against a
|
||||
# live broker and a real compositor.
|
||||
#
|
||||
# The same machine the other session checks use (`lib/session-machine.nix`),
|
||||
# plus `services.hyprland-headless-stream.mcp.enable = true` -- which is also
|
||||
# the first thing asserted, because "installing it" is the whole of that
|
||||
# option. Then `hhs-mcp-call` (`lib/mcp_client.py`) spawns `hhstream-mcp` as the
|
||||
# operator account and talks raw JSON-RPC to it: initialize, tools/list,
|
||||
# tools/call.
|
||||
#
|
||||
# WHAT THIS PROVES, AND WHAT IT DOES NOT.
|
||||
#
|
||||
# It proves the *surface*: the handshake completes over a real pipe, all
|
||||
# twenty-six tools are listed, a tool call reaches the broker and comes back
|
||||
# with the broker's own protocol object, an image arrives as image content with
|
||||
# the geometry the module configured, and a refusal arrives as a tool error
|
||||
# carrying the frozen exit code rather than as a success.
|
||||
#
|
||||
# It does not prove the compositor behaviour the other checks own, and it must
|
||||
# not be read as doing so. `vm-pointer` is where a click is shown to land in the
|
||||
# window it was aimed at; `vm-keyboard` is where a typed symbol is shown to be
|
||||
# decoded by a client from the keymap the driver uploaded. Here the victim is
|
||||
# `foot`, which ignores `$HHSTREAM_APP_ID` and so matches no container rule
|
||||
# (README.dev.md, known gap 4) -- so what the pointer and keyboard subtests
|
||||
# below assert is that the lease was granted and the driver accepted the
|
||||
# injection, which is the part the MCP layer is responsible for.
|
||||
#
|
||||
# The client is not the MCP SDK's, deliberately: the SDK on both ends would
|
||||
# agree with itself about a framing neither side had to get right. Same choice
|
||||
# as `tests/assert_stream.py` being a second implementation of the frame
|
||||
# contract, and for the same reason.
|
||||
#
|
||||
# Habits copied from the other session checks: every wait is bounded, each
|
||||
# subtest asserts once, and a failure dumps the compositor's diagnostics and
|
||||
# the broker's journal before it propagates.
|
||||
{
|
||||
testers,
|
||||
hhstream,
|
||||
hhstream-mcp,
|
||||
hhsModule,
|
||||
testTools,
|
||||
hyprland,
|
||||
foot,
|
||||
jq,
|
||||
grim,
|
||||
procps,
|
||||
coreutils,
|
||||
dejavu_fonts,
|
||||
width ? 1280,
|
||||
height ? 720,
|
||||
sessionUser ? "hermes",
|
||||
}:
|
||||
testers.runNixOSTest (
|
||||
{ pkgs, ... }:
|
||||
let
|
||||
tools = pkgs.callPackage ./lib/session-tools.nix { inherit hhstream; };
|
||||
guest = import ./lib/session-machine.nix {
|
||||
inherit
|
||||
hhsModule
|
||||
hhstream
|
||||
hyprland
|
||||
testTools
|
||||
foot
|
||||
jq
|
||||
procps
|
||||
grim
|
||||
coreutils
|
||||
dejavu_fonts
|
||||
;
|
||||
sessionTools = tools;
|
||||
};
|
||||
prelude = import ./lib/session-prelude.nix {
|
||||
inherit hhstream coreutils sessionUser;
|
||||
};
|
||||
|
||||
# The client, with a plain python3: it speaks JSON-RPC over a pipe and
|
||||
# imports nothing but the standard library, which is what makes it a second
|
||||
# implementation rather than the SDK talking to itself.
|
||||
client = pkgs.runCommandLocal "hhs-mcp-call" { } ''
|
||||
install -Dm755 ${./lib/mcp_client.py} $out/bin/hhs-mcp-call
|
||||
sed -i '1s|.*|#!${pkgs.python3}/bin/python3|' $out/bin/hhs-mcp-call
|
||||
$out/bin/hhs-mcp-call --help > /dev/null
|
||||
# It must refuse to invent a request rather than printing nothing: a
|
||||
# client that exits 0 having done nothing would make every subtest below
|
||||
# vacuous.
|
||||
if $out/bin/hhs-mcp-call --server ${coreutils}/bin/true; then
|
||||
echo "hhs-mcp-call succeeded against a server that says nothing" >&2
|
||||
exit 1
|
||||
fi
|
||||
'';
|
||||
in
|
||||
{
|
||||
name = "hhstream-vm-mcp";
|
||||
|
||||
globalTimeout = 1800;
|
||||
|
||||
nodes.machine = {
|
||||
imports = [
|
||||
(guest {
|
||||
inherit width height sessionUser;
|
||||
pointer = true;
|
||||
apps.foot = {
|
||||
exec = "${foot}/bin/foot";
|
||||
# The broker refuses unlisted arguments and this check passes none:
|
||||
# `foot` with no argv opens a window, which is all the victim has
|
||||
# to do.
|
||||
argsAllow = [ ];
|
||||
};
|
||||
})
|
||||
];
|
||||
|
||||
# The option under test. Installing the package IS the option, so the
|
||||
# first subtest asserts the binary is on PATH and nothing else had to
|
||||
# happen.
|
||||
services.hyprland-headless-stream.mcp = {
|
||||
enable = true;
|
||||
package = hhstream-mcp;
|
||||
};
|
||||
|
||||
environment.systemPackages = [ client ];
|
||||
};
|
||||
|
||||
testScript =
|
||||
prelude
|
||||
+ ''
|
||||
import base64
|
||||
import shlex
|
||||
|
||||
MCP = "${client}/bin/hhs-mcp-call"
|
||||
WIDTH = ${toString width}
|
||||
HEIGHT = ${toString height}
|
||||
|
||||
|
||||
def mcp(argv, timeout=180):
|
||||
"""`hhs-mcp-call …` as USER, parsed. Fails loudly if it printed no JSON."""
|
||||
|
||||
out = machine.succeed(as_user(f"{MCP} {argv}"), timeout=timeout)
|
||||
try:
|
||||
return json.loads(out)
|
||||
except json.JSONDecodeError:
|
||||
raise AssertionError(f"not JSON from `hhs-mcp-call {argv}`: {out!r}")
|
||||
|
||||
|
||||
def call(tool, arguments=None, timeout=180):
|
||||
"""One tools/call. Returns the CallToolResult as a dict."""
|
||||
|
||||
payload = json.dumps(arguments or {})
|
||||
return mcp(f"{tool} {shlex.quote(payload)}", timeout=timeout)
|
||||
|
||||
|
||||
def text_of(result):
|
||||
"""The text part of a CallToolResult, which is where the CLI's own
|
||||
output and (on a refusal) the remedy live."""
|
||||
|
||||
for block in result.get("content") or []:
|
||||
if block.get("type") == "text":
|
||||
return block["text"]
|
||||
raise AssertionError(f"no text content in {result!r}")
|
||||
|
||||
|
||||
def succeeded(tool, arguments=None, timeout=180):
|
||||
"""A call that must not be an error, with the reason printed if it is."""
|
||||
|
||||
result = call(tool, arguments, timeout=timeout)
|
||||
if result.get("isError"):
|
||||
print(f"----- {tool} returned an error -----")
|
||||
print(text_of(result))
|
||||
assert not result.get("isError"), f"{tool} failed: {text_of(result)}"
|
||||
return result
|
||||
|
||||
|
||||
start_all()
|
||||
bring_up("the compositor and broker")
|
||||
|
||||
with subtest("the as-user wrapper propagates exit status"):
|
||||
# Every assertion below reads machine.succeed through it, so a
|
||||
# wrapper that swallowed the status would make them all vacuous.
|
||||
machine.succeed(as_user(TRUE), timeout=120)
|
||||
machine.fail(as_user(FALSE), timeout=120)
|
||||
|
||||
with subtest("mcp.enable put the server on the system PATH"):
|
||||
# The whole of the module option: no unit, no socket, no state dir.
|
||||
path = machine.succeed(as_user("/run/current-system/sw/bin/sh -c 'command -v hhstream-mcp'")).strip()
|
||||
print(f"hhstream-mcp resolves to {path}")
|
||||
assert path.startswith("/run/current-system/sw/bin"), path
|
||||
machine.fail("systemctl is-active hhstream-mcp.service")
|
||||
|
||||
with subtest("the stdio handshake completes and names this server"):
|
||||
try:
|
||||
handshake = mcp("--initialize")
|
||||
print(json.dumps(handshake, indent=2)[:600])
|
||||
assert handshake["serverInfo"]["name"] == "hhstream", handshake
|
||||
assert handshake["capabilities"].get("tools") is not None, handshake
|
||||
# The two rules, in the instructions a client is handed once.
|
||||
instructions = handshake["instructions"]
|
||||
assert "`session_touch` is the ONLY" in instructions
|
||||
assert "POINTER lease" in instructions
|
||||
except Exception:
|
||||
dump("the mcp handshake")
|
||||
raise
|
||||
|
||||
with subtest("tools/list carries every verb, with its annotations"):
|
||||
listed = mcp("--list")["tools"]
|
||||
names = sorted(tool["name"] for tool in listed)
|
||||
print(f"{len(names)} tools: {', '.join(names)}")
|
||||
assert len(names) == 26, names
|
||||
for expected in (
|
||||
"status", "shot", "record", "run",
|
||||
"session_start", "session_touch", "session_kill",
|
||||
"pointer_acquire", "pointer_click", "keyboard_type",
|
||||
):
|
||||
assert expected in names, f"{expected} is missing from the listing"
|
||||
by_name = {tool["name"]: tool for tool in listed}
|
||||
assert by_name["session_list"]["annotations"]["readOnlyHint"] is True
|
||||
assert by_name["session_kill"]["annotations"]["destructiveHint"] is True
|
||||
# The rule has to be on the tool, not only in the instructions.
|
||||
assert "session_touch" in by_name["session_list"]["description"]
|
||||
assert "POINTER lease" in by_name["keyboard_type"]["description"]
|
||||
|
||||
with subtest("a read-only tool reaches the broker and returns its object"):
|
||||
try:
|
||||
result = succeeded("session_list")
|
||||
assert result["structuredContent"]["ok"] is True, result
|
||||
# The text part is the CLI's own `--json` output, unmodified.
|
||||
assert json.loads(text_of(result)) == result["structuredContent"]
|
||||
except Exception:
|
||||
dump("session_list over mcp")
|
||||
raise
|
||||
|
||||
with subtest("a session started over mcp is the broker's own session"):
|
||||
try:
|
||||
started = succeeded("session_start", {"app": "foot"}, timeout=300)
|
||||
session_id = started["structuredContent"]["id"]
|
||||
print(f"started {session_id}")
|
||||
assert text_of(started).startswith(f"session id: {session_id}")
|
||||
# The broker is the authority on whether that happened, so it
|
||||
# is asked directly rather than through the server under test.
|
||||
listing = cli_json("list --json")
|
||||
assert session_id in [s["id"] for s in listing["sessions"]], listing
|
||||
except Exception:
|
||||
dump("session_start over mcp")
|
||||
raise
|
||||
|
||||
with subtest("touch is the verb that moves the idle deadline"):
|
||||
try:
|
||||
before = succeeded("session_status", {"id": session_id})
|
||||
before_expiry = before["structuredContent"]["session"]["expires_in_s"]
|
||||
# `status` must NOT have moved it; `touch` must.
|
||||
after_status = succeeded("session_status", {"id": session_id})
|
||||
assert (
|
||||
after_status["structuredContent"]["session"]["expires_in_s"]
|
||||
<= before_expiry
|
||||
), "reading status reset the idle clock"
|
||||
touched = succeeded("session_touch", {"id": session_id})
|
||||
assert (
|
||||
touched["structuredContent"]["session"]["expires_in_s"]
|
||||
>= after_status["structuredContent"]["session"]["expires_in_s"]
|
||||
), touched
|
||||
except Exception:
|
||||
dump("touch over mcp", session_id)
|
||||
raise
|
||||
|
||||
with subtest("shot returns image content, sized as the publisher serves it"):
|
||||
# MEASURED, and not what this check first assumed: the frame is
|
||||
# 2560x720 on a guest configured for 1280x720. The publisher's
|
||||
# default capture is `grim -t png -` with no `-o`, which takes the
|
||||
# whole compositor layout -- and with containers on (the module's
|
||||
# default) a session's own output sits beside the main one, so one
|
||||
# session doubles the width. The geometry the module configures is
|
||||
# the geometry of an *output*, which is what the next subtest
|
||||
# captures by name.
|
||||
try:
|
||||
shot = succeeded("shot", timeout=300)
|
||||
kinds = [block["type"] for block in shot["content"]]
|
||||
assert kinds == ["text", "image"], kinds
|
||||
image = shot["content"][1]
|
||||
frame = base64.b64decode(image["data"])
|
||||
print(f"{image['mimeType']}, {len(frame)} bytes: {text_of(shot)}")
|
||||
assert image["mimeType"] in ("image/png", "image/jpeg")
|
||||
# The media type is read off the bytes, never off the extension
|
||||
# this server asked the CLI for -- so the two must agree.
|
||||
magic = {"image/png": b"\x89PNG", "image/jpeg": b"\xff\xd8\xff"}
|
||||
assert frame.startswith(magic[image["mimeType"]]), frame[:8]
|
||||
reported = shot["structuredContent"]
|
||||
assert reported["height"] == HEIGHT, reported
|
||||
assert reported["width"] >= WIDTH, reported
|
||||
assert reported["bytes"] == len(frame), reported
|
||||
assert len(frame) > 1024, "a frame that small is not a desktop"
|
||||
except Exception:
|
||||
dump("shot over mcp", session_id)
|
||||
raise
|
||||
|
||||
with subtest("shot of the session's own container is one output, exactly"):
|
||||
# Which is also the only subtest that reads `session_container`, and
|
||||
# the one that joins the two tools an agent uses together: the
|
||||
# output name that verb reports is what `shot` takes as `output`.
|
||||
try:
|
||||
container = succeeded("session_container", {"id": session_id})
|
||||
output = container["structuredContent"]["container"]["output"]
|
||||
print(f"container output: {output}")
|
||||
assert output == f"hh-{session_id}", container
|
||||
|
||||
framed = succeeded("shot", {"output": output}, timeout=300)
|
||||
reported = framed["structuredContent"]
|
||||
print(text_of(framed))
|
||||
assert reported["output"] == output, reported
|
||||
assert (reported["width"], reported["height"]) == (WIDTH, HEIGHT), reported
|
||||
assert framed["content"][1]["type"] == "image"
|
||||
except Exception:
|
||||
dump("shot --output over mcp", session_id)
|
||||
raise
|
||||
|
||||
with subtest("the pointer lease is granted, used and given back"):
|
||||
try:
|
||||
acquired = succeeded("pointer_acquire", {"id": session_id})
|
||||
lease = acquired["structuredContent"]["lease_id"]
|
||||
print(f"lease {lease}: {text_of(acquired).splitlines()[0]}")
|
||||
assert text_of(acquired).startswith(f"lease_id: {lease}")
|
||||
|
||||
# Every gesture takes the lease as an explicit argument: the
|
||||
# server caches nothing, which is what makes a transcript of
|
||||
# tool calls readable.
|
||||
succeeded("pointer_move", {"id": session_id, "lease": lease, "to": [0.5, 0.5]})
|
||||
succeeded("pointer_click", {"id": session_id, "lease": lease})
|
||||
succeeded("keyboard_type", {"id": session_id, "lease": lease, "text": "hello"})
|
||||
typed = succeeded(
|
||||
"keyboard_key", {"id": session_id, "lease": lease, "name": "Return"}
|
||||
)
|
||||
assert typed["structuredContent"]["ok"] is True, typed
|
||||
|
||||
released = succeeded("pointer_release", {"id": session_id, "lease": lease})
|
||||
assert released["structuredContent"]["ok"] is True, released
|
||||
# And the broker agrees that nobody holds it now.
|
||||
assert cli_json("pointer state --json")["holder"] is None
|
||||
except Exception:
|
||||
dump("the pointer lease over mcp", session_id)
|
||||
raise
|
||||
|
||||
with subtest("a gesture after the release is exit 9 and says to acquire"):
|
||||
# The frozen NO_LEASE code, arriving as a tool error an agent can
|
||||
# act on rather than as a success with a sad message in it.
|
||||
refused = call("pointer_click", {"id": session_id, "lease": lease})
|
||||
assert refused["isError"] is True, refused
|
||||
assert refused["structuredContent"]["exit_code"] == 9, refused
|
||||
said = text_of(refused)
|
||||
print(said.splitlines()[0])
|
||||
assert "exit 9 (NO_LEASE)" in said
|
||||
assert "pointer_acquire" in said
|
||||
|
||||
with subtest("an unknown session is exit 3, not a success"):
|
||||
refused = call("session_status", {"id": "no-such-session"})
|
||||
assert refused["isError"] is True, refused
|
||||
assert refused["structuredContent"]["exit_code"] == 3, refused
|
||||
assert "session_list" in text_of(refused)
|
||||
|
||||
with subtest("an argument the schema refuses never reaches the broker"):
|
||||
# No `lease`, so the server's own schema refuses before an argv list
|
||||
# exists. The CLI is not run at all, which is the point.
|
||||
refused = call("keyboard_type", {"id": session_id, "text": "x"})
|
||||
assert refused["isError"] is True, refused
|
||||
assert "lease" in text_of(refused), refused
|
||||
|
||||
with subtest("kill over mcp ends the session the broker was tracking"):
|
||||
try:
|
||||
killed = succeeded("session_kill", {"id": session_id}, timeout=300)
|
||||
print(text_of(killed))
|
||||
assert killed["structuredContent"]["ok"] is True, killed
|
||||
live = [s["id"] for s in cli_json("list --json")["sessions"]]
|
||||
assert session_id not in live, live
|
||||
except Exception:
|
||||
dump("session_kill over mcp", session_id)
|
||||
raise
|
||||
'';
|
||||
}
|
||||
)
|
||||
Reference in new issue
Block a user