test(stream): both ends of live.sock, the broker's verbs, and a real viewer in a guest
Three layers, because the stream has three and each one can be wrong on its own. `tests/live/test_live_control.py` (20 tests) runs `Control` against a fake capture: the four ops, `attach` idempotent on the phrase, the same phrase aimed at another output refused, a malformed phrase refused *without the value in the message*, and `fps` defaulting to the daemon's when the broker sends none. `tests/sessions/unit/test_stream_verbs.py` (24 tests) runs the broker against a fake daemon, and its subjects are the decisions rather than the plumbing: no container output is a refusal and never a fallback to the main output; a re-`enable` returns the same phrase and a different one is exit 8; a down daemon turns `attached` false rather than failing a read; the sweep touches a watched session and leaves an unwatched one alone; teardown detaches before the container is destroyed. `tests/unit/test_mcp_*` grow the two tools, the two resources and the three `session_start` arguments, with the read-only half asserted *absent* as a tool by name -- re-adding one should be a failure, not a silent widening. `checks/vm-stream.nix` is the new guest check, and it exists for the two things none of the above can say: 1. **That `grim` captures a real compositor from inside a unit.** It reads actual JPEGs off `/<phrase>/stream.mjpg` with `checks/lib/mjpeg_read.py`, a hand-rolled multipart reader that shares no code with the server -- a parser that did could not tell you the boundary was wrong. It framed on SOI/EOI rather than `Content-Length` for the same reason. This is what caught the unit's missing `XDG_RUNTIME_DIR`: `frames: [25827, 25827, 25827]` now, ten capture failures and a stream that gave up before. 2. **That a connected viewer moves the idle clock.** That needs the broker's real sweep, on a real timer, with a client genuinely still there a minute later -- hence `--hold`, and hence a check that spends ~90 seconds twice. The negative control runs first, because if the sweep touched an unwatched stream the positive case would pass for the wrong reason. Measured in the guest: with nobody watching `580s -> 502s` remaining, with a viewer `499s -> 596s`. It also asserts the phrase is in neither the audit log nor a 404 body, that the old URL stops serving after `disable`, that `disable` then `enable` rotates it, that `kill` takes the stream down and leaves the daemon up, and that `start --stream` yields an id and a URL that serves frames. No clock is injected anywhere in this check. An injected one would prove the arithmetic, which the unit tests already do, and not the daemon. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011ZeH65zd6pFTgBSu52W9PE
This commit is contained in:
1 parent
5f5726c02e
commit
b4abd085cc
10 files changed
+1626
-7
No files matched your search
@@ -42,11 +42,13 @@ EXPECTED_FIXED = {
|
||||
"hhstream://sessions",
|
||||
"hhstream://pointer",
|
||||
"hhstream://containers",
|
||||
"hhstream://streams",
|
||||
"hhstream://screen",
|
||||
}
|
||||
EXPECTED_TEMPLATES = {
|
||||
"hhstream://sessions/{id}{?events}",
|
||||
"hhstream://sessions/{id}/logs{?lines}",
|
||||
"hhstream://sessions/{id}/stream",
|
||||
"hhstream://outputs/{name}",
|
||||
}
|
||||
|
||||
@@ -76,7 +78,8 @@ def test_a_template_is_exactly_an_entry_with_a_capture():
|
||||
def test_the_three_reads_the_activity_rule_names_say_so_themselves():
|
||||
"""The rule has to be where it is read, and these are read, not called."""
|
||||
|
||||
for name in ("sessions", "session", "session-logs", "pointer", "containers"):
|
||||
for name in ("sessions", "session", "session-logs", "pointer", "containers",
|
||||
"streams", "session-stream"):
|
||||
description = resources.BY_NAME[name].description
|
||||
assert "does NOT reset" in description, name
|
||||
assert "`session_touch`" in description, name
|
||||
@@ -109,6 +112,7 @@ def test_each_fixed_resource_runs_the_verb_it_names(read):
|
||||
"hhstream://sessions": ["session", "list", "--json"],
|
||||
"hhstream://pointer": ["session", "pointer", "state", "--json"],
|
||||
"hhstream://containers": ["session", "container", "--json"],
|
||||
"hhstream://streams": ["session", "stream", "list", "--json"],
|
||||
}
|
||||
for uri, argv in cases.items():
|
||||
_, calls = read(uri, stdout=mcp_fake.reply(ok=True))
|
||||
@@ -290,6 +294,48 @@ def test_a_refused_capture_carries_the_cli_s_own_words(read):
|
||||
assert "hh-gone" in str(caught.value)
|
||||
|
||||
|
||||
# ── the stream ─────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_one_session_s_stream_reads_through_its_own_route(read):
|
||||
_, calls = read("hhstream://sessions/foot-abc/stream", stdout=mcp_fake.reply(ok=True))
|
||||
assert calls == [["session", "stream", "status", "--json", "--", "foot-abc"]]
|
||||
|
||||
|
||||
def test_the_stream_routes_do_not_collide_with_the_logs_route():
|
||||
"""Three templates under `sessions/{id}`; each must resolve to its own."""
|
||||
|
||||
assert resources.resolve("hhstream://sessions/x")[0].name == "session"
|
||||
assert resources.resolve("hhstream://sessions/x/logs")[0].name == "session-logs"
|
||||
assert resources.resolve("hhstream://sessions/x/stream")[0].name == "session-stream"
|
||||
|
||||
|
||||
def test_reading_a_stream_never_enables_one(read):
|
||||
"""The read is `status`, not `enable` -- switching one on is a tool."""
|
||||
|
||||
_, calls = read("hhstream://sessions/x/stream", stdout=mcp_fake.reply(ok=True))
|
||||
assert "enable" not in calls[0]
|
||||
_, calls = read("hhstream://streams", stdout=mcp_fake.reply(ok=True))
|
||||
assert "enable" not in calls[0]
|
||||
|
||||
|
||||
def test_both_stream_reads_warn_that_the_url_carries_its_credential():
|
||||
"""The URL is in these bodies, so the warning has to be on them."""
|
||||
|
||||
for name in ("streams", "session-stream"):
|
||||
description = resources.BY_NAME[name].description
|
||||
assert "PHRASE" in description, name
|
||||
assert "only thing protecting" in resources.BY_NAME["streams"].description
|
||||
|
||||
|
||||
def test_the_streams_listing_says_a_viewer_is_the_activity_exception():
|
||||
"""The one exception to `session_touch`, where somebody will read it."""
|
||||
|
||||
description = resources.BY_NAME["streams"].description
|
||||
assert "does NOT reset" in description
|
||||
assert "connected viewer" in description
|
||||
|
||||
|
||||
# ── the one read that must not be a resource ───────────────────────────────
|
||||
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ def test_tools_list_returns_every_tool_with_a_title_and_a_description(session):
|
||||
|
||||
listed = run(go).tools
|
||||
assert {tool.name for tool in listed} == set(tools.BY_NAME)
|
||||
assert len(listed) == 21
|
||||
assert len(listed) == 23
|
||||
for tool in listed:
|
||||
assert tool.title, tool.name
|
||||
assert tool.description and len(tool.description) > 40, tool.name
|
||||
@@ -524,6 +524,53 @@ def test_a_tool_outside_the_selection_cannot_be_called_by_name(session):
|
||||
assert fake.calls == []
|
||||
|
||||
|
||||
def test_the_stream_url_round_trips_through_a_resource_read(session):
|
||||
"""The phrase reaching an agent is the point of the whole feature."""
|
||||
|
||||
reply = {
|
||||
"ok": True,
|
||||
"id": "s-1",
|
||||
"stream": {
|
||||
"enabled": True,
|
||||
"attached": True,
|
||||
"output": "hh-s-1",
|
||||
"fps": 5,
|
||||
"viewers": 1,
|
||||
"url": "http://127.0.0.1:9988/amber-otter-lantern-harbor-7k2qp/",
|
||||
},
|
||||
}
|
||||
fake, connected = session(stdout=json.dumps(reply, indent=2))
|
||||
|
||||
async def go():
|
||||
async with connected as client:
|
||||
await client.initialize()
|
||||
return await client.read_resource(AnyUrl("hhstream://sessions/s-1/stream"))
|
||||
|
||||
content = run(go).contents[0]
|
||||
assert json.loads(content.text)["stream"]["url"] == reply["stream"]["url"]
|
||||
assert fake.calls == [["session", "stream", "status", "--json", "--", "s-1"]]
|
||||
|
||||
|
||||
def test_enabling_a_stream_through_a_tool_call_returns_its_url(session):
|
||||
reply = {
|
||||
"ok": True,
|
||||
"id": "s-1",
|
||||
"stream": {"attached": True, "url": "http://127.0.0.1:9988/a-b-c-d-12345/"},
|
||||
}
|
||||
fake, connected = session(stdout=json.dumps(reply, indent=2))
|
||||
|
||||
async def go():
|
||||
async with connected as client:
|
||||
await client.initialize()
|
||||
return await client.call_tool("session_stream_enable", {"id": "s-1"})
|
||||
|
||||
result = run(go)
|
||||
assert not result.isError
|
||||
assert result.content[0].text.startswith(reply["stream"]["url"])
|
||||
assert result.structuredContent == reply
|
||||
assert fake.calls == [["session", "stream", "enable", "--json", "--", "s-1"]]
|
||||
|
||||
|
||||
def test_content_for_a_text_only_result_has_no_image_block():
|
||||
blocks = server.content_for(tools.Result(text="hello"))
|
||||
assert [block.type for block in blocks] == ["text"]
|
||||
|
||||
@@ -51,6 +51,7 @@ EXPECTED = {
|
||||
# lifecycle. The read-only verbs are resources; see MOVED_TO_RESOURCES.
|
||||
"session_start", "session_touch", "session_kill", "session_gc",
|
||||
"session_adopt", "session_container", "session_reconcile",
|
||||
"session_stream_enable", "session_stream_disable",
|
||||
# pointer
|
||||
"pointer_acquire", "pointer_move", "pointer_click", "pointer_scroll",
|
||||
"pointer_down", "pointer_up", "pointer_release",
|
||||
@@ -72,7 +73,7 @@ MOVED_TO_RESOURCES = {
|
||||
|
||||
def test_the_acting_verbs_are_tools_and_nothing_else_is():
|
||||
assert set(tools.BY_NAME) == EXPECTED
|
||||
assert len(tools.TOOLS) == 21
|
||||
assert len(tools.TOOLS) == 23
|
||||
|
||||
|
||||
def test_the_read_only_verbs_are_not_tools():
|
||||
@@ -108,7 +109,12 @@ def test_only_the_verbs_that_read_are_marked_read_only():
|
||||
# `record` writes a file, so it is not read-only either.
|
||||
assert not tools.BY_NAME["record"].read_only
|
||||
assert tools.BY_NAME["session_kill"].destructive
|
||||
assert [t.name for t in tools.TOOLS if t.destructive] == ["session_kill"]
|
||||
# `stream_disable` is destructive too, and for a reason worth naming: it
|
||||
# forgets the phrase, so anybody holding the URL loses it.
|
||||
assert [t.name for t in tools.TOOLS if t.destructive] == [
|
||||
"session_kill",
|
||||
"session_stream_disable",
|
||||
]
|
||||
|
||||
|
||||
# ── the two rules an agent gets wrong ──────────────────────────────────────
|
||||
@@ -448,6 +454,151 @@ def test_a_broker_that_is_down_is_reported_as_the_operator_s_problem(call):
|
||||
assert "frame tools" in result.text
|
||||
|
||||
|
||||
# ── the live stream ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def test_enabling_a_stream_puts_the_url_where_a_caller_will_look(call):
|
||||
reply = {
|
||||
"ok": True,
|
||||
"id": "s-1",
|
||||
"stream": {
|
||||
"enabled": True,
|
||||
"output": "hh-s-1",
|
||||
"fps": 5,
|
||||
"attached": True,
|
||||
"url": "http://127.0.0.1:9988/amber-otter-lantern-harbor-7k2qp/",
|
||||
"viewers": 0,
|
||||
},
|
||||
}
|
||||
result, calls = call(
|
||||
"session_stream_enable", {"id": "s-1"}, stdout=json.dumps(reply, indent=2)
|
||||
)
|
||||
|
||||
assert calls == [["session", "stream", "enable", "--json", "--", "s-1"]]
|
||||
# The URL on the first line: it is the whole point of the call.
|
||||
assert result.text.startswith(reply["stream"]["url"])
|
||||
assert result.data == reply
|
||||
# ...and the one thing a caller must know about it.
|
||||
assert "only thing protecting it" in result.text
|
||||
|
||||
|
||||
def test_enabling_a_stream_passes_a_phrase_and_a_rate_through(call):
|
||||
_, calls = call(
|
||||
"session_stream_enable",
|
||||
{"id": "s-1", "phrase": "amber-otter-lantern-harbor-7k2qp", "fps": 3},
|
||||
stdout=mcp_fake.reply(ok=True, id="s-1", stream={"url": "http://x/"}),
|
||||
)
|
||||
assert calls == [
|
||||
[
|
||||
"session", "stream", "enable", "--json",
|
||||
"--phrase=amber-otter-lantern-harbor-7k2qp",
|
||||
"--fps=3",
|
||||
"--", "s-1",
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
def test_an_enable_the_daemon_did_not_confirm_does_not_invent_a_url(call):
|
||||
"""A printed URL that serves nothing is worse than saying there is none."""
|
||||
|
||||
result, _ = call(
|
||||
"session_stream_enable",
|
||||
{"id": "s-1"},
|
||||
stdout=mcp_fake.reply(ok=True, id="s-1", stream={"attached": False}),
|
||||
)
|
||||
assert "did not confirm" in result.text
|
||||
assert "http" not in result.text.split("\n\n")[0]
|
||||
|
||||
|
||||
def test_a_stream_on_a_session_with_no_container_is_exit_ten(call):
|
||||
"""It must never fall back to the main output -- that one carries everyone."""
|
||||
|
||||
result, _ = call(
|
||||
"session_stream_enable",
|
||||
{"id": "s-1"},
|
||||
stderr=mcp_fake.reply(
|
||||
ok=False,
|
||||
error="stream_unavailable",
|
||||
reason="session s-1 has no container output to publish",
|
||||
),
|
||||
exit_code=int(ExitCode.COMPOSITOR),
|
||||
)
|
||||
assert result.is_error
|
||||
assert result.data["exit_code"] == int(ExitCode.COMPOSITOR)
|
||||
assert "not lost" in result.text # the exit-10 remedy: degraded, not dead
|
||||
|
||||
|
||||
def test_a_phrase_that_is_not_this_session_s_is_exit_eight(call):
|
||||
result, _ = call(
|
||||
"session_stream_enable",
|
||||
{"id": "s-1", "phrase": "other-words-go-here-99999"},
|
||||
stderr=mcp_fake.reply(ok=False, error="stream_busy", reason="already has a stream"),
|
||||
exit_code=int(ExitCode.BUSY),
|
||||
)
|
||||
assert result.is_error
|
||||
assert result.text.startswith("exit 8 (BUSY)")
|
||||
|
||||
|
||||
def test_disabling_a_stream_is_marked_destructive_and_forgets_the_phrase(call):
|
||||
_, calls = call(
|
||||
"session_stream_disable", {"id": "s-1"}, stdout=mcp_fake.reply(ok=True, detached=True)
|
||||
)
|
||||
assert calls == [["session", "stream", "disable", "--json", "--", "s-1"]]
|
||||
assert tools.BY_NAME["session_stream_disable"].destructive
|
||||
assert "forget" in tools.BY_NAME["session_stream_disable"].description
|
||||
|
||||
|
||||
def test_start_can_ask_for_a_stream_and_gets_its_url_back(call):
|
||||
"""One call, because a stream needs the container output that start makes."""
|
||||
|
||||
reply = {
|
||||
"ok": True,
|
||||
"id": "s-9",
|
||||
"stream": {"url": "http://127.0.0.1:9988/amber-otter-lantern-harbor-7k2qp/"},
|
||||
}
|
||||
result, calls = call(
|
||||
"session_start",
|
||||
{"app": "chromium", "stream": True, "stream_fps": 4},
|
||||
stdout=json.dumps(reply, indent=2),
|
||||
)
|
||||
assert calls == [
|
||||
["session", "start", "--json", "--stream", "--stream-fps=4", "--", "chromium"]
|
||||
]
|
||||
assert "session id: s-9" in result.text
|
||||
assert reply["stream"]["url"] in result.text
|
||||
|
||||
|
||||
def test_a_start_whose_stream_failed_says_so_although_it_succeeded(call):
|
||||
"""Exit 0 with a running session; a caller must not read that as a stream."""
|
||||
|
||||
result, _ = call(
|
||||
"session_start",
|
||||
{"app": "chromium", "stream": True},
|
||||
stdout=mcp_fake.reply(
|
||||
ok=True, id="s-9", stream_error="session s-9 has no container output to publish"
|
||||
),
|
||||
)
|
||||
assert not result.is_error
|
||||
assert "stream: NOT enabled" in result.text
|
||||
assert "session_stream_enable" in result.text
|
||||
|
||||
|
||||
def test_a_start_that_asked_for_no_stream_says_nothing_about_one(call):
|
||||
result, calls = call(
|
||||
"session_start", {"app": "foot"}, stdout=mcp_fake.reply(ok=True, id="s-1")
|
||||
)
|
||||
assert "--stream" not in calls[0]
|
||||
assert "stream" not in result.text.lower().split("\n\n")[0]
|
||||
|
||||
|
||||
def test_the_stream_note_carries_both_facts_it_exists_for():
|
||||
assert "CREDENTIAL" in tools.STREAM_NOTE
|
||||
assert "only protection" in tools.STREAM_NOTE
|
||||
# The exception to the activity rule has to be where the stream is.
|
||||
assert "A CONNECTED VIEWER DOES" in tools.STREAM_NOTE
|
||||
assert tools.STREAM_NOTE in tools.BY_NAME["session_stream_enable"].description
|
||||
|
||||
|
||||
# ── shot and record ────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user