Files
hyprland-headless-stream-pl…/tests/live/test_live_publisher.py
T
Hermenix d770bcd298 feat(live): one port, a router, and a phrase per session (stage 1)
The session-stream web app: a single HTTP listener (9988) that serves a page
per enabled session, keyed by a spoken phrase.

  src/hhstream_live/phrase.py     four EFF-large words + a 5-char base-36 tail
                                  (~77.5 bits); the phrase is the password
  src/hhstream_live/publisher.py  one capture thread + one frame slot per
                                  output, one slot/no queue, capture paused at
                                  zero viewers
  src/hhstream_live/server.py     the router: /, /<phrase>/, /stream.mjpg,
                                  /frame.jpg, /status.json; unknown phrase is
                                  an empty 404, rate-limited
  src/hhstream_live/pages.py      the built-in dark pages (no CDN, no build)
  src/hhstream_live/__main__.py   hhstream-live serve|phrase, standalone

The root page is a form, never a list: the phrase is known only by whoever
enabled the stream. Tests: tests/live (51), all green with the full suite
(958). Not yet wired to the broker -- that is stage 2.
2026-10-06 13:26:02 +00:00

126 lines
3.9 KiB
Python

"""The publisher: no capture while unwatched, one slot, and giving up when dead."""
from __future__ import annotations
import time
import pytest
from live_helpers import PHRASE, StubCapture
from hhstream_live.publisher import (FPS_CAP, FAILURE_LIMIT, Stream, StreamRegistry,
StreamSpec)
def make_stream(capture, *, fps=10, phrase=PHRASE):
return Stream(StreamSpec(phrase=phrase, output="hh-demo", fps=fps), capture)
@pytest.mark.parametrize("bad", [0, -1, True, 1.5, FPS_CAP + 1])
def test_fps_must_be_a_positive_int_within_the_cap(bad):
with pytest.raises(ValueError):
make_stream(StubCapture(), fps=bad)
def test_no_capture_while_nobody_is_watching():
"""An enabled stream costs nothing until a request arrives."""
capture = StubCapture()
stream = make_stream(capture)
stream.start()
try:
time.sleep(0.4)
assert capture.calls == 0
finally:
stream.stop()
def test_capture_starts_when_a_viewer_arrives_and_stops_when_it_leaves():
capture = StubCapture()
stream = make_stream(capture)
stream.start()
try:
stream.add_viewer()
frame, seq = stream.wait_frame(0, timeout=5)
assert frame and seq >= 1
stream.remove_viewer()
time.sleep(0.3)
settled = capture.calls
time.sleep(0.4)
assert capture.calls == settled # idle again
finally:
stream.stop()
def test_snapshot_is_none_before_anyone_watches_and_bytes_after():
stream = make_stream(StubCapture())
stream.start()
try:
assert stream.snapshot() is None
stream.add_viewer()
frame, _ = stream.wait_frame(0, timeout=5)
stream.remove_viewer()
assert frame is not None
assert stream.snapshot() == frame
finally:
stream.stop()
def test_a_slow_viewer_does_not_queue_frames():
"""One slot: a viewer that sleeps sees the latest frame, never a backlog."""
stream = make_stream(StubCapture(delay=0.01), fps=10)
stream.start()
try:
stream.add_viewer()
first, seq = stream.wait_frame(0, timeout=5)
time.sleep(0.3) # let many frames go by unsent
later, seq2 = stream.wait_frame(seq, timeout=5)
assert later is not None
# seq jumped by more than one: the frames in between were dropped, not
# buffered for us.
assert seq2 > seq + 1
finally:
stream.stop()
def test_a_dead_capture_gives_up_and_closes_viewers():
capture = StubCapture(fail_first=10_000) # never succeeds
stream = make_stream(capture, fps=10)
stream.start()
try:
stream.add_viewer()
frame, _ = stream.wait_frame(0, timeout=10)
assert frame is None
assert stream.stopped
assert capture.calls >= FAILURE_LIMIT
finally:
stream.stop()
# ---- registry --------------------------------------------------------------
def test_registry_adds_and_finds_a_phrase_case_insensitively():
registry = StreamRegistry()
try:
registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture())
assert registry.get("Amber-Otter-Lantern-Harbor-7K2QP") is not None
assert registry.get("nope-not-here-at-all") is None
finally:
registry.stop_all()
def test_registry_refuses_a_duplicate_phrase():
registry = StreamRegistry()
try:
registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture())
with pytest.raises(ValueError):
registry.add(StreamSpec(phrase=PHRASE, output="hh-other"), StubCapture())
finally:
registry.stop_all()
def test_registry_remove_stops_the_stream():
registry = StreamRegistry()
stream = registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture())
assert registry.remove(PHRASE) is stream
assert registry.get(PHRASE) is None
assert stream.stopped
assert registry.remove(PHRASE) is None