screentinker/server/test/pip-overlay.test.js
screentinker 965920cd17
PiP overlay MVP: push image/web overlays to a device or group (#109) (#127)
* PiP overlay MVP: push image/web overlays to a device or group (#109)

Implements the #109 MVP from docs proposal: a floating overlay PUSHED to a device or
group in real time, rendered above the playlist without disturbing it. Scope is the
MVP only — video/RTSP, MQTT, offline-queue, and the priority/stacking system are
deferred to follow-up PRs as the proposal specifies.

Protocol (/device socket, player-agnostic):
- device:pip-show { pip_id, type:image|web, uri, position, width, height, duration,
  title?, title_color?, background_color?, opacity?, border_radius?, close_button? }
- device:pip-clear { pip_id? }
The player fetches uri itself (same trust model as remote_url content; server never
proxies). type:web is full-trust by design, hence the 'full' token scope.

Server (server/routes/pip.js, new; mounted in config/api-surface.js PUBLIC_ROUTERS):
- POST /api/pip and POST /api/pip/clear + DELETE /api/pip, all requireScope('full').
- Resolves device_id to a device OR a group, expands a group to members, and emits
  per-device — reusing the group command route's room-size online check and
  {device_id, name, status: sent|offline} result shape. Generates pip_id.
- Validates type/position allowlists, uri http(s), numeric bounds on
  width/height/duration/opacity/border_radius, colors via the existing VALID_COLOR
  (#RRGGBB; transparency is the separate opacity field).
- Workspace-isolated: every target query is scoped to req.workspaceId, so a token
  bound to workspace A can't address workspace B (404). Offline devices are reported,
  never queued (PiP is ephemeral).

Player overlay layer (Tizen; tizen/js/pip-overlay.js, new):
- A #pip sibling ABOVE #stage that PlaylistPlayer/ZoneRenderer never touch.
- applyOrientation now applies the SAME transform to #pip as #stage, so corner
  positions track the visible CONTENT in all four orientations.
- image -> <img>, web -> <iframe> (muted by default: empty allow= denies autoplay),
  sized/positioned/styled per payload, optional title bar.
- Single overlay slot, last-show-wins; duration timer (0 = until cleared); pip-clear
  (id-aware) or timer tears down; teardown wrapped so a malformed payload can't wedge
  the layer. Reports show/clear over device:log (tag 'pip').

Dashboard: a minimal "Send overlay" / "Clear overlay" tester on the device-detail
controls (device/group via the open device, type, uri, position, duration), calling
POST /api/pip through the api helper.

Tests (server suite green, 161/161):
- api.test.js: PiP tier — authz (read/write 403, full passes), workspace isolation
  (wsA token -> wsB device 404), payload validation, device + group targeting, clear;
  plus the PUBLIC_ROUTERS snapshot-firewall updated for /api/pip.
- pip-overlay.test.js: loads the real player.js + pip-overlay.js in a vm with a DOM
  shim; proves the overlay shows, auto-dismisses on the duration timer, and never
  changes the playlist signature / touches #stage; web->iframe, last-show-wins,
  id-aware clear, malformed-payload safety.

Not in this PR (intentional):
- Android player overlay — fast-follow. Protocol + server are player-agnostic; the
  Android layer (an overlay View above the player, orientation-matched to MainActivity's
  rootView rotation) is the same shape and lands next.
- OpenAPI docs for POST /api/pip — the contract test's scope heuristic only treats
  'command' paths as full-scope, so documenting a full-scope non-command route there
  needs that heuristic extended first; deferred with the docs item (proposal §8.6).
- video/rtsp types, MQTT, offline queue-on-reconnect, priority/stacking, arbitrary
  (x,y)/selector positioning (proposal §6).

Refs #109

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* PiP overlay: add Android + web players (#109)

Extends the #109 PiP MVP to the other two players so the protocol (device:pip-show /
device:pip-clear) is honored fleet-wide, not just on Tizen. No server/protocol changes —
the route and socket messages are player-agnostic; these are the two missing surfaces.

Web player (server/player/index.html):
- New #pipContainer layer above #playerContainer, pointer-transparent, that the playlist
  render never touches. The same orientation transform is applied to it as to
  #playerContainer (extended to also reset width/height on landscape so a
  portrait->landscape switch realigns), so corner positions track the visible content.
- Inline PiP logic mirroring tizen/js/pip-overlay.js: image -> <img>, web -> <iframe>
  (muted by default via empty allow=), position/size/bg/opacity/radius/title, single slot
  last-show-wins, duration timer (0 = until cleared), id-aware clear, wrapped teardown.
- device:pip-show/clear handlers; reports show/clear over device:log (tag "pip").

Android player:
- activity_main.xml: a pipLayout FrameLayout as the LAST child of rootLayout — it draws
  above the content AND inherits rootView's orientation rotation/translation, so corner
  positioning is orientation-matched for free.
- PipOverlay.kt (new): builds the overlay box into pipLayout. image -> ImageView (decoded
  off-thread via ImageLoader, dropped if torn down mid-decode); web -> WebView with
  mediaPlaybackRequiresUserGesture=true (mute-by-default). Gravity-based corner/center
  placement with a 4% inset, GradientDrawable bg + corner radius, alpha=opacity, optional
  title bar. Single slot last-show-wins; duration timer; id-aware clear; teardown wrapped
  and also run on activity destroy (WebView cleanup).
- WebSocketService: onPipShow/onPipClear callbacks + safeOn handlers posted to the main
  thread (they build Views) + a sendLog(tag, level, message) emitter for device:log.
- MainActivity: instantiate PipOverlay (log -> wsService.sendLog("pip", ...)), wire the
  callbacks, tear down on destroy.

Verified: Android assembleDebug builds clean; web player inline JS parses; server suite
still 161/161 (no server changes this commit). Not yet validated on real hardware —
four-orientation corner positioning mirrors the player container/rootView transform but
should be eyeballed on a panel.

Refs #109

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 14:54:44 -05:00

136 lines
6.6 KiB
JavaScript

'use strict';
// #109 PiP player-layer test. Loads the REAL tizen/js/player.js + tizen/js/pip-overlay.js
// into a vm context with a minimal DOM shim (the repo has no jsdom; node --test only).
// Proves the overlay shows and auto-dismisses WITHOUT changing the playlist signature
// underneath — i.e. PipOverlay writes only to #pip and never to #stage / PlaylistPlayer.
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
// --- minimal DOM element shim: only what PlaylistPlayer.renderImage + PipOverlay use ---
function makeEl() {
const el = {
tag: '', style: {}, className: '', attrs: {}, children: [], _html: '', _src: '', _text: '',
appendChild(c) { this.children.push(c); this._html = '<children>'; return c; },
querySelector(sel) { return this.children.find(c => c.tag === sel) || null; },
setAttribute(k, v) { this.attrs[k] = v; },
removeAttribute(k) { delete this.attrs[k]; },
addEventListener() {}, removeEventListener() {},
classList: { add() {}, remove() {}, contains() { return false; } },
load() {}, pause() {}, play() { return { catch() {} }; },
};
Object.defineProperty(el, 'innerHTML', { get() { return this._html; }, set(v) { this._html = v; if (v === '') this.children = []; } });
Object.defineProperty(el, 'src', { get() { return this._src; }, set(v) { this._src = v; } });
Object.defineProperty(el, 'textContent', { get() { return this._text; }, set(v) { this._text = v; } });
return el;
}
function loadPlayerContext() {
// Controllable timer so the duration teardown is deterministic (no wall-clock waits).
const timers = {};
let seq = 0;
const sandbox = {
console,
Date,
setTimeout: (fn) => { const id = ++seq; timers[id] = fn; return id; },
clearTimeout: (id) => { delete timers[id]; },
setInterval: () => 0,
clearInterval: () => {},
localStorage: { getItem: () => null, setItem() {}, removeItem() {} },
navigator: { language: 'en' },
};
sandbox.document = { createElement: (tag) => { const e = makeEl(); e.tag = tag; return e; } };
sandbox.window = sandbox;
vm.createContext(sandbox);
const read = (p) => fs.readFileSync(path.join(__dirname, '..', '..', 'tizen', 'js', p), 'utf8');
vm.runInContext(read('player.js'), sandbox, { filename: 'player.js' });
vm.runInContext(read('pip-overlay.js'), sandbox, { filename: 'pip-overlay.js' });
return { sandbox, timers };
}
test('pip: overlay shows in #pip and never touches #stage / the playlist signature', () => {
const { sandbox } = loadPlayerContext();
const stage = makeEl();
const pip = makeEl();
// A 1-item image playlist; capture the signature the renderer computes.
const player = new sandbox.PlaylistPlayer(stage, () => 'http://server');
player.load([{ content_id: 'c1', mime_type: 'image/png', sort_order: 0, duration_sec: 10 }]);
const sigBefore = player.sig;
const stageChildrenBefore = stage.children.length;
assert.ok(sigBefore, 'player computed a playlist signature');
assert.ok(stageChildrenBefore >= 1, 'playlist rendered into #stage');
const logs = [];
const overlay = new sandbox.PipOverlay(pip, { document: sandbox.document, log: (lvl, msg) => logs.push([lvl, msg]) });
overlay.show({ pip_id: 'p1', type: 'image', uri: 'http://img/x.png', position: 'top-right', width: 480, height: 360, duration: 30 });
assert.equal(pip.children.length, 1, 'overlay box rendered into #pip');
assert.equal(player.sig, sigBefore, 'playlist signature unchanged by pip show');
assert.equal(stage.children.length, stageChildrenBefore, '#stage untouched by pip show');
assert.ok(logs.some(l => l[1].indexOf('pip show') === 0), 'show reported over the log channel');
});
test('pip: duration timer auto-dismisses without disturbing the playlist', () => {
const { sandbox, timers } = loadPlayerContext();
const stage = makeEl();
const pip = makeEl();
const player = new sandbox.PlaylistPlayer(stage, () => 'http://server');
player.load([{ content_id: 'c1', mime_type: 'image/png', sort_order: 0, duration_sec: 10 }]);
const sigBefore = player.sig;
const overlay = new sandbox.PipOverlay(pip, { document: sandbox.document });
overlay.show({ pip_id: 'p1', type: 'image', uri: 'http://img/x.png', duration: 5 });
assert.equal(pip.children.length, 1, 'overlay shown');
// Fire the scheduled duration timer (deterministic: the sandbox setTimeout captured it).
const ids = Object.keys(timers);
assert.equal(ids.length, 1, 'a single duration timer was scheduled');
timers[ids[0]]();
assert.equal(pip.children.length, 0, 'overlay auto-dismissed at duration');
assert.equal(player.sig, sigBefore, 'playlist signature still unchanged after dismiss');
});
test('pip: web type renders an iframe; last-show-wins; targeted clear is id-aware', () => {
const { sandbox } = loadPlayerContext();
const pip = makeEl();
const overlay = new sandbox.PipOverlay(pip, { document: sandbox.document });
overlay.show({ pip_id: 'web1', type: 'web', uri: 'https://example.com', duration: 0 });
assert.equal(pip.children.length, 1);
const box = pip.children[0];
assert.ok(box.children.some(c => c.tag === 'iframe'), 'web overlay uses an <iframe>');
assert.equal(box.children.find(c => c.tag === 'iframe').attrs.allow, '', 'web audio muted by default (empty allow)');
// last-show-wins: a second show replaces the first (still a single slot).
overlay.show({ pip_id: 'web2', type: 'image', uri: 'http://img/y.png', duration: 0 });
assert.equal(pip.children.length, 1, 'single overlay slot after a replacing show');
// a clear for a STALE pip_id is a no-op; the matching id clears.
overlay.clear('web1');
assert.equal(pip.children.length, 1, 'stale-id clear ignored');
overlay.clear('web2');
assert.equal(pip.children.length, 0, 'matching-id clear tore down the overlay');
});
test('pip: a malformed payload cannot wedge the layer', () => {
const { sandbox } = loadPlayerContext();
const pip = makeEl();
const overlay = new sandbox.PipOverlay(pip, { document: sandbox.document });
// doc.createElement throws -> show must swallow it, tear down, and stay usable.
const boom = { createElement: () => { throw new Error('boom'); } };
const broken = new sandbox.PipOverlay(pip, { document: boom });
broken.show({ pip_id: 'x', type: 'image', uri: 'http://img/x.png', duration: 0 });
assert.equal(pip.children.length, 0, 'no half-built overlay left behind');
// the healthy overlay still works afterwards
overlay.show({ pip_id: 'ok', type: 'image', uri: 'http://img/ok.png', duration: 0 });
assert.equal(pip.children.length, 1, 'layer still usable after a malformed payload');
});