aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/tests
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/tests')
-rw-r--r--packages/meshbay-hub/tests/harness/cast_session_probe.py166
-rw-r--r--packages/meshbay-hub/tests/test_android_cast.py8
-rw-r--r--packages/meshbay-hub/tests/test_cast_files.py372
-rw-r--r--packages/meshbay-hub/tests/test_cast_photos.py240
4 files changed, 524 insertions, 262 deletions
diff --git a/packages/meshbay-hub/tests/harness/cast_session_probe.py b/packages/meshbay-hub/tests/harness/cast_session_probe.py
index 2904860..573dd95 100644
--- a/packages/meshbay-hub/tests/harness/cast_session_probe.py
+++ b/packages/meshbay-hub/tests/harness/cast_session_probe.py
@@ -40,9 +40,13 @@ FRAME = r"""<!doctype html><html><head><meta charset=utf-8>
<script>
// A module that fails to load never reaches its own catch: say so.
const report = (error) => parent.postMessage({ error }, '*');
-addEventListener('error', (e) => report(
- `${e.message || 'load error'} ${e.filename || (e.target && e.target.src) || ''}`
- + `:${e.lineno || ''}`), true);
+// Scripts only: the music case's stub track is three bytes no <audio>
+// element can decode, which is expected and is not the page failing.
+addEventListener('error', (e) => {
+ if (!(e instanceof ErrorEvent) && !(e.target && e.target.tagName === 'SCRIPT')) return;
+ report(`${e.message || 'load error'} ${e.filename || (e.target && e.target.src) || ''}`
+ + `:${e.lineno || ''}`);
+}, true);
addEventListener('unhandledrejection',
(e) => report(String((e.reason && e.reason.stack) || e.reason)));
const WITH_BRIDGE = !location.search.includes('nobridge');
@@ -111,6 +115,8 @@ window.MeshBayTransport = function () {
// The native side, as the Electron preload and the Android shim present it,
// recording every cast call.
window.CALLS = [];
+// What the stub receiver reports: the music case moves it along.
+window.TV = { state: 'IDLE', position: 0, idle: null };
if (WITH_BRIDGE) {
let connected = false, v = 0;
const rec = (name, arg) => { CALLS.push({ name, arg }); };
@@ -127,13 +133,19 @@ if (WITH_BRIDGE) {
stop: async () => { rec('stop'); return true; },
subtitle: async () => null,
finish: async () => true,
- image: async (img) => {
- const bmp = await createImageBitmap(new Blob([img.bytes], { type: img.type }));
- rec('image', { type: img.type, width: bmp.width, height: bmp.height,
- jpeg: img.bytes[0] === 0xff && img.bytes[1] === 0xd8 });
- return { url: 'http://tv/image?t=x&v=' + (++v) };
+ file: async (f) => {
+ let info = { type: f.type, size: f.bytes.length, title: f.title || null,
+ artist: f.artist || null, album: f.album || null, cover: !!f.cover };
+ if (f.type.startsWith('image/')) {
+ const bmp = await createImageBitmap(new Blob([f.bytes], { type: f.type }));
+ info = { ...info, width: bmp.width, height: bmp.height,
+ jpeg: f.bytes[0] === 0xff && f.bytes[1] === 0xd8 };
+ }
+ rec('file', info);
+ return { url: 'http://tv/file?t=x&v=' + (++v) };
},
- status: async () => ({ active: true, url: null, chromecast: { connected } }),
+ status: async () => ({ active: true, url: null, chromecast: {
+ connected, playerState: TV.state, position: TV.position, idleReason: TV.idle } }),
scan: async () => { rec('scan'); return true; },
devices: async () => ({ devices: [{ id: 'tv1', name: 'Salon' }], scanning: false }),
chromecastConnect: async (o) => {
@@ -142,8 +154,9 @@ if (WITH_BRIDGE) {
},
chromecastReload: async (o) => { rec('reload', o); return { playerState: 'IDLE' }; },
chromecastDisconnect: async () => { rec('disconnect'); connected = false; return true; },
- chromecastPause: async () => ({}),
- chromecastPlay: async () => ({}),
+ chromecastPause: async () => { rec('pause'); TV.state = 'PAUSED'; return {}; },
+ chromecastPlay: async () => { rec('play'); TV.state = 'PLAYING'; return {}; },
+ chromecastSeek: async (t) => { rec('seek', t); TV.position = t; return {}; },
},
};
}
@@ -161,12 +174,91 @@ async function until(pred, what, tries = 200) {
throw new Error('timed out waiting for ' + what);
}
const calls = (name) => CALLS.filter((c) => c.name === name);
+const files = () => calls('file').map((c) => c.arg);
// Files is always first; then the enabled apps in their registry order.
const videosTab = () => $$('.group-tab')[1];
const count = () => (($('.photo-lightbox-count') || {}).textContent || '').trim();
+// The music bar, with a television already chosen: two tracks of one album.
+async function music() {
+ const { MusicPlayerBar } = await import('/music-player.js');
+ const { castSession } = await import('/cast-session.js');
+ const out = {};
+ castSession.choose({ id: 'tv1', name: 'Salon' });
+ // Which hashes were fetched, so a cover can be traced to where it came from.
+ window.FETCHED = [];
+ const transport = { connected: true, async waitForReconnect() {},
+ async fetchChunk(id) {
+ FETCHED.push(id);
+ return { nonce: 'n', ct: new Uint8Array([1, 2, 3]) };
+ },
+ // The node's MusicBrainz answer: a cover for the other album only.
+ async fetchMusicMeta(id) {
+ CALLS.push({ name: 'musicMeta', arg: id });
+ return id === 't3' ? { confidence: 1, cover_thumb_hash: 'mb-cover' }
+ : { confidence: 0 };
+ } };
+ const getConnection = async () => ({ transport, gek: 'gek' });
+ // One has no cover of its own but Two, of the same album, has; Three is of
+ // another album whose only cover is the one the node found.
+ const track = (n, title, album, hash) => ({ id: 't' + n, name: title + '.mp3', size: 3,
+ groupId: 'g1', display_title: title, artist: 'Some Band', album, duration: 200,
+ thumb_hash: hash });
+ const queue = { tracks: [track(1, 'One', 'An Album', null), track(2, 'Two', 'An Album', 'cover2'),
+ track(3, 'Three', 'Another Album', null)], startIndex: 0, nonce: 1 };
+ render(html`<${MusicPlayerBar} getConnection=${getConnection} queue=${queue}
+ userPrefs=${{}} onClose=${() => {}} />`, document.getElementById('root'));
+
+ await until(() => calls('connect').length === 1, 'the first track on the television');
+ out.first = { file: files()[0], connect: calls('connect')[0].arg,
+ coverFetched: FETCHED.includes('cover2') };
+ TV.state = 'PLAYING'; TV.position = 12;
+ await until(() => ($$('.music-player-time')[0] || {}).textContent === '0:12',
+ "the television's clock in the bar");
+ out.barButton = !!$('.music-player-bar .cast-btn.casting');
+ out.volumeDisabled = $('.music-player-volume input').disabled;
+
+ $('.music-player-play').click();
+ await until(() => calls('pause').length === 1, 'pause on the television');
+ const slider = $('.music-player-seek input');
+ slider.value = '30';
+ slider.dispatchEvent(new Event('input', { bubbles: true }));
+ await until(() => calls('seek').length === 1, 'a seek on the television');
+ out.seek = calls('seek')[0].arg;
+
+ // The television finishes the track: the next one goes, once.
+ TV.state = 'IDLE'; TV.idle = 'FINISHED';
+ await until(() => calls('reload').length === 1, 'the next track on the television');
+ out.second = { file: files()[1], reload: calls('reload')[0].arg,
+ title: $('.music-player-title').textContent.trim() };
+ TV.state = 'PLAYING'; TV.idle = null; TV.position = 1;
+ await sleep(2500);
+ out.filesAfterSettling = files().length;
+
+ // Three: the cover comes from the node's answer for its album.
+ TV.state = 'IDLE'; TV.idle = 'FINISHED';
+ await until(() => calls('reload').length === 2, 'the third track on the television');
+ out.third = { file: files()[2], mbCover: FETCHED.includes('mb-cover'),
+ metaAsked: calls('musicMeta').map((c) => c.arg) };
+ TV.state = 'PLAYING'; TV.idle = null;
+
+ // Stopping the cast lets the television go.
+ await castSession.end();
+ await sleep(200);
+ out.afterStop = { disconnects: calls('disconnect').length,
+ barCasting: !!$('.music-player-bar .cast-btn.casting') };
+ parent.postMessage(out, '*');
+}
+
(async () => {
const out = { bridge: !!window.meshbay };
+ if (location.search.includes('music')) {
+ try { await initLocale(); await music(); }
+ catch (err) {
+ parent.postMessage({ calls: CALLS, error: String((err && err.stack) || err) }, '*');
+ }
+ return;
+ }
try {
await initLocale();
render(html`<${GroupPage} groupId="g1" token="t" username="me" userId="u1"
@@ -195,13 +287,13 @@ const count = () => (($('.photo-lightbox-count') || {}).textContent || '').trim(
$$('.cast-menu .ctx-menu-item').find((b) => b.textContent.includes('Salon')).click();
await until(() => $('.photo-lightbox'), 'the lightbox to open on choosing');
await until(() => calls('connect').length === 1, 'the first photo to reach the television');
- out.first = { count: count(), image: calls('image')[0], connect: calls('connect')[0].arg,
+ out.first = { count: count(), image: files()[0], connect: calls('connect')[0].arg,
lightboxCasting: !!$('.photo-lightbox .cast-btn.casting') };
// Next photo: the receiver is connected, so it is reloaded.
$('.photo-nav-next').click();
await until(() => calls('reload').length === 1, 'the second photo');
- out.second = { count: count(), reload: calls('reload')[0].arg, images: calls('image').length };
+ out.second = { count: count(), reload: calls('reload')[0].arg, images: files().length };
// Slideshow: on by itself to the last photo, then off.
const play = $$('.photo-lightbox .video-top-bar .video-close')
@@ -209,7 +301,7 @@ const count = () => (($('.photo-lightbox-count') || {}).textContent || '').trim(
play.click();
await until(() => count() === '3 / 3', 'the slideshow to reach the last photo', 160);
await until(() => play.getAttribute('aria-pressed') === 'false', 'the slideshow to end', 160);
- out.slideshow = { count: count(), images: calls('image').length,
+ out.slideshow = { count: count(), images: files().length,
reloads: calls('reload').length };
// Close the lightbox; Videos shows the same television; a film opens as its remote.
@@ -341,16 +433,17 @@ def check(engine: str, cast: dict, plain: dict) -> list[str]:
first = cast["first"]
if first["count"] != "1 / 3":
bad.append(f"choosing a television did not open the first photo: {first['count']}")
- img = first["image"]["arg"]
+ img = first["image"]
if not (img["type"] == "image/jpeg" and img["jpeg"] and img["width"] == 1440
and img["height"] == 1080):
bad.append(f"the photo did not leave as a 1440x1080 JPEG: {img}")
- if first["connect"] != {"deviceId": "tv1", "mediaUrl": "http://tv/image?t=x&v=1"}:
+ if first["connect"] != {"deviceId": "tv1", "mediaUrl": "http://tv/file?t=x&v=1",
+ "startAt": 0}:
bad.append(f"connect: {first['connect']}")
if not first["lightboxCasting"]:
bad.append("the lightbox's cast button does not say it is casting")
if cast["second"]["count"] != "2 / 3" or cast["second"]["reload"] != {
- "mediaUrl": "http://tv/image?t=x&v=2"}:
+ "mediaUrl": "http://tv/file?t=x&v=2", "startAt": 0}:
bad.append(f"next photo: {cast['second']}")
if cast["slideshow"]["count"] != "3 / 3" or cast["slideshow"]["images"] != 3:
bad.append(f"slideshow: {cast['slideshow']}")
@@ -367,6 +460,39 @@ def check(engine: str, cast: dict, plain: dict) -> list[str]:
return [f"{engine}: {b}" for b in bad]
+def check_music(engine: str, m: dict) -> list[str]:
+ if "error" in m:
+ return [f"{engine}: music: {m['error']}"]
+ bad = []
+ f = m["first"]["file"]
+ if not (f["type"] == "audio/mpeg" and f["title"] == "One" and f["artist"] == "Some Band"
+ and f["album"] == "An Album" and f["cover"] and m["first"]["coverFetched"]):
+ bad.append(f"the first track left without its album's cover: {m['first']}")
+ third = m["third"]
+ if not (third["file"]["title"] == "Three" and third["file"]["cover"] and third["mbCover"]):
+ bad.append(f"the node's cover did not reach the television: {third}")
+ if "t1" in third["metaAsked"] or "t2" in third["metaAsked"]:
+ bad.append(f"the node was asked for a cover a track already had: {third['metaAsked']}")
+ if m["first"]["connect"] != {"deviceId": "tv1", "mediaUrl": "http://tv/file?t=x&v=1",
+ "startAt": 0}:
+ bad.append(f"connect: {m['first']['connect']}")
+ if not m["barButton"]:
+ bad.append("the music bar's cast button does not say it is casting")
+ if not m["volumeDisabled"]:
+ bad.append("the volume slider still acts while casting")
+ if m["seek"] != 30:
+ bad.append(f"seek: {m['seek']}")
+ s = m["second"]
+ if s["file"]["title"] != "Two" or s["title"] != "Two" or s["reload"] != {
+ "mediaUrl": "http://tv/file?t=x&v=2", "startAt": 0}:
+ bad.append(f"the end of a track did not move the queue on: {s}")
+ if m["filesAfterSettling"] != 2:
+ bad.append(f"a finished track moved the queue on more than once: {m['filesAfterSettling']}")
+ if m["afterStop"]["disconnects"] < 1 or m["afterStop"]["barCasting"]:
+ bad.append(f"stopping: {m['afterStop']}")
+ return [f"{engine}: music: {b}" for b in bad]
+
+
def main() -> int:
failures = []
with socketserver.ThreadingTCPServer(("127.0.0.1", PORT), H) as srv:
@@ -377,11 +503,13 @@ def main() -> int:
continue
cast = browse(engine, "")
plain = browse(engine, "?nobridge")
- if cast is None or plain is None:
+ music = browse(engine, "?music")
+ if cast is None or plain is None or music is None:
failures.append(f"{engine}: the page never reported")
continue
- print(f"{engine}:", json.dumps(cast, indent=1))
+ print(f"{engine}:", json.dumps({"photos-videos": cast, "music": music}, indent=1))
failures += check(engine, cast, plain)
+ failures += check_music(engine, music)
for f in failures:
print("FAIL", f, file=sys.stderr)
if not failures:
diff --git a/packages/meshbay-hub/tests/test_android_cast.py b/packages/meshbay-hub/tests/test_android_cast.py
index b537df7..ebcdec4 100644
--- a/packages/meshbay-hub/tests/test_android_cast.py
+++ b/packages/meshbay-hub/tests/test_android_cast.py
@@ -103,12 +103,14 @@ def test_the_receiver_is_pointed_at_the_relay_and_nothing_else():
def test_relay_calls_keep_their_order():
"""The page does not await each push; on a pool they could overtake."""
channels = _read(CAST / "CastChannels.kt")
- assert '"cast:start", "cast:push", "cast:subtitle", "cast:image", "cast:finish", "cast:stop"' in channels
+ assert '"cast:start", "cast:push", "cast:subtitle",' in channels
+ assert ('"cast:file:begin", "cast:file:write", "cast:file:end", "cast:finish", "cast:stop"'
+ in channels)
bridge = _read(SRC / "bridge" / "Bridge.kt")
assert "Executors.newSingleThreadExecutor()" in bridge
assert "(if (ordered) serial else work).execute" in bridge
- assert "fun ordered(frame: BinaryFrame) = frame.channel == BinaryFrame.CAST_PUSH" in _read(
- SRC / "bridge" / "Channels.kt")
+ assert ("fun ordered(frame: BinaryFrame) = frame.channel == BinaryFrame.CAST_PUSH"
+ " || frame.channel == BinaryFrame.CAST_FILE") in _read(SRC / "bridge" / "Channels.kt")
def test_screen_off_survival_is_switched_on_only_while_casting_or_playing():
diff --git a/packages/meshbay-hub/tests/test_cast_files.py b/packages/meshbay-hub/tests/test_cast_files.py
new file mode 100644
index 0000000..8f51d80
--- /dev/null
+++ b/packages/meshbay-hub/tests/test_cast_files.py
@@ -0,0 +1,372 @@
+"""
+Whole files on a cast receiver — photos and music tracks — and the television
+a session casts to.
+
+A file takes a different road to the television than a film: no stream, one
+file at a time, handed to the relay in pieces (begin, writes, end), served at
+`/file` with its cover at `/cover`, and loaded by the default media receiver
+as a picture or as a music track. The page scales and re-encodes a photo
+first (cast-session.js); a track goes as it is stored.
+
+The first half runs the desktop relay itself, as test_cast_subtitles.py does:
+it is plain Node with no dependencies. The file is behind the same token as
+the stream, its address changes with every file — a receiver handed the same
+URL twice shows what it already has — ranges are honoured, because that is how
+a receiver seeks in a track, and only a picture or a sound is served as one.
+
+The second half holds the wiring: what the receiver loads is the shell's to
+say, never the page's; Android accepts no URL but the relay's own; a
+television chosen anywhere is the one a film opens on, with the player as its
+remote from the start; and the music bar drives the television once one is
+chosen.
+"""
+
+import json
+import re
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+PACKAGES = Path(__file__).resolve().parents[2]
+STATIC = PACKAGES / "meshbay-hub" / "src" / "meshbay_hub" / "static"
+DESKTOP = PACKAGES / "meshbay-client" / "src"
+RELAY = DESKTOP / "cast-relay.js"
+CHROMECAST = DESKTOP / "cast-chromecast.js"
+ANDROID_MAIN = PACKAGES / "meshbay-android" / "app" / "src" / "main"
+ANDROID_SRC = ANDROID_MAIN / "kotlin"
+CLIENT = ANDROID_SRC / "org" / "meshbay" / "client"
+CAST = CLIENT / "cast"
+SHIM = ANDROID_MAIN / "assets" / "bridge" / "meshbay-bridge.js"
+
+
+def _read(path: Path) -> str:
+ return path.read_text(encoding="utf-8")
+
+
+RELAY_SCRIPT = r"""
+const http = require('node:http');
+const CastRelay = require(process.argv[2]);
+const get = (url, headers = {}) => new Promise((resolve, reject) => {
+ http.get(url, { headers }, (res) => {
+ const parts = [];
+ res.on('data', (c) => parts.push(c));
+ res.on('end', () => resolve({ status: res.statusCode, headers: res.headers,
+ body: Buffer.concat(parts).toString('hex') }));
+ }).on('error', reject);
+});
+const refused = async (f) => { try { await f(); return false; } catch { return true; } };
+const send = async (relay, bytes, opts) => {
+ await relay.beginFile({ size: bytes.length, ...opts });
+ for (let at = 0; at < bytes.length; at += 3) relay.writeFile(bytes.subarray(at, at + 3));
+ return relay.endFile();
+};
+(async () => {
+ const relay = new CastRelay();
+ const out = {};
+ out.activeBefore = relay.active;
+ out.htmlRefused = await refused(() => relay.beginFile({ type: 'text/html', size: 3 }));
+ out.activeAfterRefusal = relay.active;
+
+ const jpeg = Buffer.from([0xff, 0xd8, 1, 2, 0xff, 0xd9]);
+ const first = await send(relay, jpeg, { type: 'image/jpeg' });
+ out.activeAfterPhoto = relay.active;
+ out.firstUrl = first.url;
+ out.photo = await get(first.url);
+ out.badToken = (await get(first.url.replace(/t=[0-9a-f]+/, 't=x'))).status;
+ out.photoMeta = relay.fileMeta;
+
+ // A track, its cover and what to show with it; read back whole and by range.
+ const track = Buffer.from([...Array(20).keys()]);
+ const second = await send(relay, track, {
+ type: 'audio/flac', title: 'A Song', artist: 'Some Band', album: 'An Album',
+ cover: { bytes: Buffer.from([0xff, 0xd8, 7, 0xff, 0xd9]), type: 'image/jpeg' } });
+ out.secondUrl = second.url;
+ out.trackType = relay.fileType;
+ out.trackMeta = relay.fileMeta;
+ out.whole = await get(second.url);
+ out.range = await get(second.url, { Range: 'bytes=5-9' });
+ out.tail = await get(second.url, { Range: 'bytes=15-' });
+ out.beyond = (await get(second.url, { Range: 'bytes=50-60' })).status;
+ out.cover = await get(relay.fileMeta.cover);
+
+ // Fewer bytes than announced is not a file; more is refused as they come.
+ await relay.beginFile({ type: 'audio/mpeg', size: 10 });
+ relay.writeFile(Buffer.from([1, 2, 3]));
+ out.shortRefused = await refused(() => relay.endFile());
+ await relay.beginFile({ type: 'audio/mpeg', size: 2 });
+ out.longRefused = await refused(() => relay.writeFile(Buffer.from([1, 2, 3])));
+ out.stillServed = (await get(second.url)).status;
+
+ await relay.start({});
+ out.fileAfterFilm = relay.fileUrl;
+ await send(relay, jpeg, { type: 'image/jpeg' });
+ await relay.stop();
+ out.fileAfterStop = relay.fileUrl;
+ console.log(JSON.stringify(out));
+})().catch((e) => { console.error(e); process.exit(1); });
+"""
+
+
+@pytest.fixture(scope="module")
+def relay_run(tmp_path_factory):
+ if shutil.which("node") is None or not RELAY.exists():
+ pytest.skip("node or the desktop client sources are not available")
+ script = tmp_path_factory.mktemp("cf") / "relay.cjs"
+ script.write_text(RELAY_SCRIPT, encoding="utf-8")
+ proc = subprocess.run(["node", str(script), str(RELAY)],
+ capture_output=True, text=True, encoding="utf-8", timeout=30)
+ assert proc.returncode == 0, proc.stderr
+ return json.loads(proc.stdout)
+
+
+def test_a_file_starts_the_relay_and_is_served_behind_its_token(relay_run):
+ assert relay_run["activeBefore"] is False
+ assert relay_run["activeAfterPhoto"] is True
+ photo = relay_run["photo"]
+ assert photo["status"] == 200
+ assert photo["headers"]["content-type"] == "image/jpeg"
+ assert photo["headers"]["cache-control"] == "no-store"
+ assert photo["body"] == "ffd80102ffd9"
+ assert relay_run["badToken"] == 403
+ assert relay_run["photoMeta"]["cover"] is None
+
+
+def test_every_file_has_its_own_address(relay_run):
+ assert re.search(r"/file\?t=[0-9a-f]{32}&v=\d+$", relay_run["firstUrl"])
+ assert relay_run["secondUrl"] != relay_run["firstUrl"]
+
+
+def test_a_track_is_served_with_its_cover_and_what_it_is(relay_run):
+ assert relay_run["trackType"] == "audio/flac"
+ meta = relay_run["trackMeta"]
+ assert (meta["title"], meta["artist"], meta["album"]) == ("A Song", "Some Band", "An Album")
+ assert re.search(r"/cover\?t=[0-9a-f]{32}&v=\d+$", meta["cover"])
+ assert relay_run["cover"]["status"] == 200
+ assert relay_run["cover"]["body"] == "ffd807ffd9"
+ assert relay_run["whole"]["body"] == bytes(range(20)).hex()
+ assert relay_run["whole"]["headers"]["accept-ranges"] == "bytes"
+
+
+def test_a_receiver_seeks_in_a_track_by_range(relay_run):
+ r = relay_run["range"]
+ assert r["status"] == 206
+ assert r["headers"]["content-range"] == "bytes 5-9/20"
+ assert r["body"] == bytes(range(5, 10)).hex()
+ tail = relay_run["tail"]
+ assert tail["status"] == 206 and tail["body"] == bytes(range(15, 20)).hex()
+ assert relay_run["beyond"] == 416
+
+
+def test_only_a_whole_picture_or_sound_is_served(relay_run):
+ assert relay_run["htmlRefused"] is True
+ assert relay_run["activeAfterRefusal"] is False, "a refused file starts nothing"
+ assert relay_run["shortRefused"] is True
+ assert relay_run["longRefused"] is True
+ assert relay_run["stillServed"] == 200, "a failed send leaves the shown file alone"
+
+
+def test_a_film_or_a_stop_takes_the_file_down(relay_run):
+ assert relay_run["fileAfterFilm"] is None
+ assert relay_run["fileAfterStop"] is None
+
+
+def test_the_receiver_loads_a_photo_as_a_picture_and_a_track_as_music():
+ if shutil.which("node") is None or not CHROMECAST.exists():
+ pytest.skip("node or the desktop client sources are not available")
+ script = (
+ "const Module = require('node:module');"
+ "const real = Module._load;"
+ "Module._load = function (r, ...a) {"
+ " if (r === 'bonjour-service') return { Bonjour: class {} };"
+ " if (r === 'castv2-client') return { Client: class {}, DefaultMediaReceiver: {} };"
+ " return real.call(this, r, ...a); };"
+ f"const {{ mediaFor, loadOptionsFor }} = require({json.dumps(str(CHROMECAST))});"
+ "const meta = { title: 'A Song', artist: 'Some Band', album: 'An Album',"
+ " cover: 'http://x/cover?t=a&v=2' };"
+ "console.log(JSON.stringify({"
+ " photo: mediaFor('http://x/file?t=a&v=1', null,"
+ " { contentType: 'image/jpeg', meta: { cover: null } }),"
+ " track: mediaFor('http://x/file?t=a&v=2', null, { contentType: 'audio/flac', meta }),"
+ " from: loadOptionsFor(null, 42.5),"
+ " film: mediaFor('http://x/stream.mp4?t=a', null, null),"
+ " filmOptions: loadOptionsFor(null, 0),"
+ " legacy: mediaFor('http://x/stream.mp4?t=a', null) }));")
+ proc = subprocess.run(["node", "-e", script], capture_output=True, text=True,
+ encoding="utf-8", timeout=30)
+ assert proc.returncode == 0, proc.stderr
+ out = json.loads(proc.stdout)
+ assert out["photo"]["contentType"] == "image/jpeg"
+ assert out["photo"]["streamType"] == "NONE"
+ assert "tracks" not in out["photo"]
+ track = out["track"]
+ assert track["contentType"] == "audio/flac" and track["streamType"] == "BUFFERED"
+ assert track["metadata"] == {"metadataType": 3, "title": "A Song", "artist": "Some Band",
+ "albumName": "An Album",
+ "images": [{"url": "http://x/cover?t=a&v=2"}]}
+ assert out["from"]["currentTime"] == 42.5
+ assert "currentTime" not in out["filmOptions"]
+ assert out["film"] == out["legacy"], "a film is loaded exactly as before"
+ assert out["film"]["streamType"] == "LIVE" and out["film"]["contentType"] == "video/mp4"
+
+
+def test_the_shell_not_the_page_says_what_the_receiver_loads():
+ """The page names a URL; what it is, is the relay's to say. A page that
+ could name the content type could have a television load the stream as
+ anything at all."""
+ main = _read(DESKTOP / "main.js")
+ load_of = main.split("const loadOf = ", 1)[1].split("\n\n", 1)[0]
+ assert "mediaUrl === castRelay.fileUrl" in load_of
+ assert "contentType: castRelay.fileType, meta: castRelay.fileMeta" in load_of
+ assert "subtitle: null" in load_of, "a file carries no subtitle"
+ assert main.count("loadOf(mediaUrl, subtitle)") == 2, "connect and reload both"
+ # A start position means something for a file only; a film starts where
+ # its relay does.
+ assert main.count("l.file ? startAt : 0") == 2
+
+
+def test_android_accepts_the_relays_file_and_nothing_else():
+ if not CAST.exists():
+ pytest.skip("android sources not present")
+ channels = _read(CAST / "CastChannels.kt")
+ check = channels.split("private fun relayUrl(", 1)[1].split("\n }", 1)[0]
+ assert "asked != ours && asked != relay.fileUrl" in check
+ assert 'put("contentType", relay.fileType).put("meta", relay.fileMeta()' in channels
+ assert "url == relay.fileUrl -> null" in channels, "a file carries no subtitle"
+ assert 'if (url == relay.fileUrl) maxOf(0.0, o.optDouble("startAt", 0.0)) else 0.0' in channels
+ # A file is not a cast that has to survive the screen going off.
+ begin = channels.split('"cast:file:begin" ->', 1)[1].split('"cast:file:end"', 1)[0]
+ assert "onCasting" not in begin
+ relay = _read(CAST / "CastRelay.kt")
+ assert '"/file" -> serveFile(out, socket, range)' in relay
+ assert '"/cover" -> serveCover(out, socket)' in relay
+ serve = relay.split("private fun serve(", 1)[1]
+ assert serve.index('query["t"] != token') < serve.index('"/file"'), "behind the token"
+ # On disk, never held: a long lossless track is a few hundred megabytes.
+ assert 'File(spoolDir, "file-${fileSeq.incrementAndGet()}.bin")' in relay
+ assert "java.io.FileOutputStream(slot.path)" in relay
+ control = _read(CAST / "CastControl.kt")
+ assert "MediaInfo.STREAM_TYPE_NONE" in control
+ assert "MediaMetadata.MEDIA_TYPE_PHOTO" in control
+ assert "MediaMetadata.MEDIA_TYPE_MUSIC_TRACK" in control
+ assert "MediaInfo.STREAM_TYPE_BUFFERED" in control
+ assert "setCurrentTime((startAt * 1000).toLong())" in control
+ assert "MediaSeekOptions.Builder().setPosition(" in control
+ # Loaded is shown: a photo never reaches PLAYING, and waiting for it
+ # would hold every photo for the full fifteen seconds.
+ load = control.split("private fun load(", 1)[1]
+ assert load.index("if (photo) return") < load.index("PLAY_WAIT_MS")
+
+
+def test_a_file_crosses_the_android_bridge_in_binary_pieces():
+ """One message the size of a lossless track would be the WebView's to
+ refuse; a file goes in pieces of a megabyte, as binary frames where the
+ WebView has them."""
+ if not SHIM.exists():
+ pytest.skip("android sources not present")
+ shim = _read(SHIM)
+ assert "const CAST_FILE = 3;" in shim
+ assert "const FILE_PIECE = 1024 * 1024;" in shim
+ assert ("BINARY ? sendBinary(CAST_FILE, 0, piece) : call('cast:file:write', base64Of(piece))"
+ in shim)
+ assert "const val CAST_FILE = 3" in _read(CLIENT / "save" / "SaveNames.kt")
+ channels = _read(CLIENT / "bridge" / "Channels.kt")
+ assert "BinaryFrame.CAST_FILE -> " in channels and ".writeFile(frame.bytes" in channels
+
+
+def test_a_television_chosen_anywhere_takes_the_next_film():
+ """The player opens as the remote of a television chosen before it, the
+ cast pending until the first segment exactly as a restart is; choosing one
+ in the player keeps it for the session, and stopping there forgets it."""
+ player = _read(STATIC / "video-player.js")
+ assert ("const [autoCast] = useState(() => "
+ "(castSession.supported ? castSession.device : null));") in player
+ assert "useState(Boolean(autoCast))" in player
+ assert "castActiveRef = useRef(Boolean(autoCast))" in player
+ assert "castRestartPendingRef = useRef(Boolean(autoCast))" in player
+ assert "castDeviceRef = useRef(autoCast)" in player
+ assert "useState(autoCast ? autoCast.name : null)" in player
+ stop = player.split("const stopCast = async () => {", 1)[1].split("};", 1)[0]
+ assert "castSession.forget()" in stop
+ assert "castSession.choose(d);" in player
+ assert "castSession.claim('video')" in player
+
+
+def test_the_cast_button_is_where_the_media_are():
+ """Videos' toolbar, the Photos landing, a photo album's bar and Music's
+ toolbar are drawn by the group page and the search page alike, so one
+ button in each covers both. On the Photos landing the button keeps a
+ toolbar Search would otherwise hide. The music bar has its own, opening
+ upward from the bottom of the window."""
+ videos = _read(STATIC / "video-app.js")
+ assert "<${CastButton} />" in videos.split('<div class="video-toolbar">', 1)[1]
+ photos = _read(STATIC / "photos-app.js")
+ landing = photos.split("function PhotosApp(", 1)[1]
+ assert "(!hideFilter || castSession.supported) && html`" in landing
+ assert "<${CastButton} />" in landing.split('<div class="photo-toolbar">', 1)[1]
+ album = photos.split("function AlbumView(", 1)[1]
+ assert "<${CastButton}" in album
+ lightbox = photos.split("function Lightbox(", 1)[1].split("function AlbumView(", 1)[0]
+ assert '<${CastButton} variant="overlay"' in lightbox
+ assert "if (castDevice && blobUrl && blob) showPhoto(blob);" in lightbox
+ music = _read(STATIC / "music-app.js")
+ assert "<${CastButton} />" in music.split('<div class="video-toolbar">', 1)[1]
+ bar = _read(STATIC / "music-player.js")
+ assert '<${CastButton} cls="music-player-btn" up />' in bar
+ # The group page draws them through the app registry, the search page
+ # imports them: the same components either way.
+ registry = _read(STATIC / "apps.js")
+ search = _read(STATIC / "search-page.js")
+ for page in (registry, search):
+ for app in ("VideoApp", "PhotosApp", "MusicApp"):
+ assert app in page
+
+
+def test_the_music_bar_drives_the_television():
+ """With a television chosen, the decrypted track goes to the relay instead
+ of the element, and the bar's controls and clock are the receiver's. The
+ receiver keeps saying FINISHED until it is given something else, so the
+ end of a track moves the queue on once; and a television a film or a
+ photo has taken is not the bar's to read."""
+ bar = _read(STATIC / "music-player.js")
+ assert "const remote = Boolean(castDevice) && castSession.canSendFiles;" in bar
+ send = bar.split("const sendToTv = useCallback(", 1)[1].split("}, [fetchCover]);", 1)[0]
+ assert "owner: 'music', startAt," in send
+ assert "type: cached.mime, cover," in send
+ assert "sendingRef.current = true;" in send
+ load = bar.split("if (remoteRef.current) {\n // To the television", 1)[1]
+ assert "audio.pause();" in load.split("await audio.play();", 1)[0]
+ tick = bar.split("const tick = async () => {", 1)[1].split("const id = setInterval", 1)[0]
+ assert "castSession.owner !== 'music'" in tick
+ assert "sendingRef.current" in tick
+ assert "finishedRef.current !== remoteTrackRef.current" in tick
+ assert "String(c.idleReason || '').toLowerCase() === 'finished'" in tick
+ # The cover is found as the album card finds it, not only on the track.
+ cover = bar.split("const fetchCover = useCallback(", 1)[1]
+ cover = cover.split("}, [getConnection, tracks]);", 1)[0]
+ assert "entry.thumb_hash || (sibling && sibling.thumb_hash)" in cover
+ assert "transport.fetchMusicMeta(entry.id)" in cover
+ assert "meta.cover_thumb_hash" in cover
+ assert "castBridge.chromecastSeek(to)" in bar
+ assert "castBridge.chromecastPause() : castBridge.chromecastPlay()" in bar
+ # Stopping the cast carries the track on here; a film taking the
+ # television pauses the bar.
+ assert "if (castSession.device && castSession.owner !== 'music') setPlaying(false);" in bar
+
+
+def test_a_photo_leaves_scaled_and_upright():
+ session = _read(STATIC / "cast-session.js")
+ assert "imageOrientation: 'from-image'" in session
+ assert "const TV_WIDTH = 1920;" in session and "const TV_HEIGHT = 1080;" in session
+ assert "Math.min(1, TV_WIDTH / bitmap.width, TV_HEIGHT / bitmap.height)" in session
+ assert "await loadFile({ owner: 'photo', bytes, type: 'image/jpeg' });" in session
+ # Only the newest photo asked for is sent.
+ assert session.count("gen !== photoGen") >= 2
+
+
+def test_every_locale_names_the_slideshow():
+ for f in sorted((STATIC / "locales").glob("*.js")):
+ text = f.read_text(encoding="utf-8")
+ for key in ("photo.slideshow", "photo.slideshow_stop"):
+ assert f"'{key}':" in text, f"{f.name} lacks {key}"
diff --git a/packages/meshbay-hub/tests/test_cast_photos.py b/packages/meshbay-hub/tests/test_cast_photos.py
deleted file mode 100644
index 699e663..0000000
--- a/packages/meshbay-hub/tests/test_cast_photos.py
+++ /dev/null
@@ -1,240 +0,0 @@
-"""
-Photos on a cast receiver, and the television a session casts to.
-
-A photo takes a different road to the television than a film: no stream, one
-picture at a time, served by the relay at `/image` and loaded as a picture by
-the default media receiver. The page scales and re-encodes it first
-(cast-session.js), so what reaches the relay is a JPEG the receiver can show.
-
-The first half runs the desktop relay itself, as test_cast_subtitles.py does:
-it is plain Node with no dependencies. The photo is behind the same token as
-the stream, its address changes with every photo — a receiver handed the same
-URL twice shows what it already has — and only a picture is served as one.
-
-The second half holds the wiring: the receiver is told a photo is a photo by
-the shell, never by the page; Android accepts no URL but the relay's own; and
-a television chosen anywhere is the one a film opens on, with the player as
-its remote from the start.
-"""
-
-import json
-import re
-import shutil
-import subprocess
-from pathlib import Path
-
-import pytest
-
-PACKAGES = Path(__file__).resolve().parents[2]
-STATIC = PACKAGES / "meshbay-hub" / "src" / "meshbay_hub" / "static"
-DESKTOP = PACKAGES / "meshbay-client" / "src"
-RELAY = DESKTOP / "cast-relay.js"
-CHROMECAST = DESKTOP / "cast-chromecast.js"
-ANDROID_SRC = PACKAGES / "meshbay-android" / "app" / "src" / "main" / "kotlin"
-CAST = ANDROID_SRC / "org" / "meshbay" / "client" / "cast"
-
-
-def _read(path: Path) -> str:
- return path.read_text(encoding="utf-8")
-
-
-RELAY_SCRIPT = r"""
-const http = require('node:http');
-const CastRelay = require(process.argv[2]);
-const get = (url) => new Promise((resolve, reject) => {
- http.get(url, (res) => {
- const parts = [];
- res.on('data', (c) => parts.push(c));
- res.on('end', () => resolve({ status: res.statusCode, headers: res.headers,
- body: Buffer.concat(parts).toString('hex') }));
- }).on('error', reject);
-});
-(async () => {
- const relay = new CastRelay();
- const out = {};
- out.activeBefore = relay.active;
- try {
- await relay.showImage({ bytes: Buffer.from('<p>'), type: 'text/html' });
- out.htmlRefused = false;
- } catch { out.htmlRefused = true; }
- out.activeAfterRefusal = relay.active;
-
- const jpeg = Buffer.from([0xff, 0xd8, 1, 2, 0xff, 0xd9]);
- const first = await relay.showImage({ bytes: jpeg, type: 'image/jpeg' });
- out.activeAfterPhoto = relay.active;
- out.firstUrl = first.url;
- out.imageType = relay.imageType;
- out.photo = await get(first.url);
- out.badToken = (await get(first.url.replace(/t=[0-9a-f]+/, 't=x'))).status;
- const second = await relay.showImage({ bytes: Buffer.from([9]), type: 'image/jpeg' });
- out.secondUrl = second.url;
- out.second = await get(second.url);
-
- await relay.start({});
- out.imageAfterFilm = relay.imageUrl;
- await relay.stop();
- out.imageAfterStop = relay.imageUrl;
- console.log(JSON.stringify(out));
-})().catch((e) => { console.error(e); process.exit(1); });
-"""
-
-
-@pytest.fixture(scope="module")
-def relay_run(tmp_path_factory):
- if shutil.which("node") is None or not RELAY.exists():
- pytest.skip("node or the desktop client sources are not available")
- script = tmp_path_factory.mktemp("cp") / "relay.cjs"
- script.write_text(RELAY_SCRIPT, encoding="utf-8")
- proc = subprocess.run(["node", str(script), str(RELAY)],
- capture_output=True, text=True, encoding="utf-8", timeout=30)
- assert proc.returncode == 0, proc.stderr
- return json.loads(proc.stdout)
-
-
-def test_a_photo_starts_the_relay_and_is_served_behind_its_token(relay_run):
- assert relay_run["activeBefore"] is False
- assert relay_run["activeAfterPhoto"] is True
- photo = relay_run["photo"]
- assert photo["status"] == 200
- assert photo["headers"]["content-type"] == "image/jpeg"
- assert photo["headers"]["cache-control"] == "no-store"
- assert photo["body"] == "ffd80102ffd9"
- assert relay_run["badToken"] == 403
- assert relay_run["imageType"] == "image/jpeg"
-
-
-def test_every_photo_has_its_own_address(relay_run):
- assert re.search(r"/image\?t=[0-9a-f]{32}&v=\d+$", relay_run["firstUrl"])
- assert relay_run["secondUrl"] != relay_run["firstUrl"]
- assert relay_run["second"]["body"] == "09"
-
-
-def test_only_a_picture_is_served_as_one(relay_run):
- assert relay_run["htmlRefused"] is True
- assert relay_run["activeAfterRefusal"] is False, "a refused photo starts nothing"
-
-
-def test_a_film_or_a_stop_takes_the_photo_down(relay_run):
- assert relay_run["imageAfterFilm"] is None
- assert relay_run["imageAfterStop"] is None
-
-
-def test_the_receiver_loads_a_photo_as_a_picture():
- if shutil.which("node") is None or not CHROMECAST.exists():
- pytest.skip("node or the desktop client sources are not available")
- script = (
- "const Module = require('node:module');"
- "const real = Module._load;"
- "Module._load = function (r, ...a) {"
- " if (r === 'bonjour-service') return { Bonjour: class {} };"
- " if (r === 'castv2-client') return { Client: class {}, DefaultMediaReceiver: {} };"
- " return real.call(this, r, ...a); };"
- f"const {{ mediaFor }} = require({json.dumps(str(CHROMECAST))});"
- "console.log(JSON.stringify({"
- " photo: mediaFor('http://x/image?t=a&v=1', null, 'image/jpeg'),"
- " film: mediaFor('http://x/stream.mp4?t=a', null, 'video/mp4'),"
- " legacy: mediaFor('http://x/stream.mp4?t=a', null) }));")
- proc = subprocess.run(["node", "-e", script], capture_output=True, text=True,
- encoding="utf-8", timeout=30)
- assert proc.returncode == 0, proc.stderr
- out = json.loads(proc.stdout)
- assert out["photo"]["contentType"] == "image/jpeg"
- assert out["photo"]["streamType"] == "NONE"
- assert "tracks" not in out["photo"]
- assert out["film"] == out["legacy"], "a film is loaded exactly as before"
- assert out["film"]["streamType"] == "LIVE" and out["film"]["contentType"] == "video/mp4"
-
-
-def test_the_shell_not_the_page_says_what_the_receiver_loads():
- """The page names a URL; whether it is a photo is the relay's to say. A
- page that could name the content type could have a television load the
- stream URL as anything at all."""
- main = _read(DESKTOP / "main.js")
- load_of = main.split("const loadOf = ", 1)[1].split("\n\n", 1)[0]
- assert "mediaUrl === castRelay.imageUrl" in load_of
- assert "contentType: castRelay.imageType" in load_of
- assert "contentType: 'video/mp4'" in load_of
- assert "subtitle: null" in load_of, "a photo carries no subtitle"
- assert main.count("loadOf(mediaUrl, subtitle)") == 2, "connect and reload both"
-
-
-def test_android_accepts_the_relays_photo_and_nothing_else():
- if not CAST.exists():
- pytest.skip("android sources not present")
- channels = _read(CAST / "CastChannels.kt")
- check = channels.split("private fun relayUrl(", 1)[1].split("\n }", 1)[0]
- assert "asked != ours && asked != relay.imageUrl" in check
- assert "if (url == relay.imageUrl) relay.imageType" in channels
- assert "url == relay.imageUrl -> null" in channels, "a photo carries no subtitle"
- # A photo is not a cast that has to survive the screen going off.
- image = channels.split('"cast:image" ->', 1)[1].split('\n "', 1)[0]
- assert "onCasting" not in image
- relay = _read(CAST / "CastRelay.kt")
- assert '"/image" -> serveImage(out, socket)' in relay
- serve = relay.split("private fun serve(", 1)[1]
- assert serve.index('query["t"] != token') < serve.index('"/image"'), "behind the token"
- control = _read(CAST / "CastControl.kt")
- assert "MediaInfo.STREAM_TYPE_NONE" in control
- assert "MediaMetadata.MEDIA_TYPE_PHOTO" in control
- # Loaded is shown: a photo never reaches PLAYING, and waiting for it
- # would hold every photo for the full fifteen seconds.
- load = control.split("private fun load(", 1)[1]
- assert load.index("if (photo) return") < load.index("PLAY_WAIT_MS")
-
-
-def test_a_television_chosen_anywhere_takes_the_next_film():
- """The player opens as the remote of a television chosen before it, the
- cast pending until the first segment exactly as a restart is; choosing one
- in the player keeps it for the session, and stopping there forgets it."""
- player = _read(STATIC / "video-player.js")
- assert ("const [autoCast] = useState(() => "
- "(castSession.supported ? castSession.device : null));") in player
- assert "useState(Boolean(autoCast))" in player
- assert "castActiveRef = useRef(Boolean(autoCast))" in player
- assert "castRestartPendingRef = useRef(Boolean(autoCast))" in player
- assert "castDeviceRef = useRef(autoCast)" in player
- assert "useState(autoCast ? autoCast.name : null)" in player
- stop = player.split("const stopCast = async () => {", 1)[1].split("};", 1)[0]
- assert "castSession.forget()" in stop
- assert "castSession.choose(d);" in player
-
-
-def test_the_cast_button_is_where_the_media_are():
- """Videos' toolbar, the Photos landing and a photo album's bar are drawn by
- the group page and the search page alike, so one button in each covers
- both. On the landing the button keeps a toolbar Search would otherwise
- hide."""
- videos = _read(STATIC / "video-app.js")
- assert "<${CastButton} />" in videos.split('<div class="video-toolbar">', 1)[1]
- photos = _read(STATIC / "photos-app.js")
- landing = photos.split("function PhotosApp(", 1)[1]
- assert "(!hideFilter || castSession.supported) && html`" in landing
- assert "<${CastButton} />" in landing.split('<div class="photo-toolbar">', 1)[1]
- album = photos.split("function AlbumView(", 1)[1]
- assert "<${CastButton}" in album
- lightbox = photos.split("function Lightbox(", 1)[1].split("function AlbumView(", 1)[0]
- assert '<${CastButton} variant="overlay"' in lightbox
- assert "if (castDevice && blobUrl && blob) showPhoto(blob);" in lightbox
- # The group page draws them through the app registry, the search page
- # imports them: the same components either way.
- registry = _read(STATIC / "apps.js")
- search = _read(STATIC / "search-page.js")
- for page in (registry, search):
- assert "VideoApp" in page and "PhotosApp" in page
-
-
-def test_a_photo_leaves_scaled_and_upright():
- session = _read(STATIC / "cast-session.js")
- assert "imageOrientation: 'from-image'" in session
- assert "const TV_WIDTH = 1920;" in session and "const TV_HEIGHT = 1080;" in session
- assert "Math.min(1, TV_WIDTH / bitmap.width, TV_HEIGHT / bitmap.height)" in session
- assert "'image/jpeg'" in session
- # Only the newest photo asked for is sent.
- assert session.count("gen !== photoGen") >= 2
-
-
-def test_every_locale_names_the_slideshow():
- for f in sorted((STATIC / "locales").glob("*.js")):
- text = f.read_text(encoding="utf-8")
- for key in ("photo.slideshow", "photo.slideshow_stop"):
- assert f"'{key}':" in text, f"{f.name} lacks {key}"