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
@@ -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