Files
Announcements/client/e2e/stack.js
T
MediaandClaude Opus 5 ba678d08d5 Complete Phase 5B offline display resilience
Make the /display kiosk survive losing its infrastructure, then surrender
authority cleanly when it returns. The Phase 5A renderer is unchanged: this
builds on its two permanent scene layers, explicit Cache Storage, IndexedDB,
Service Worker, timing anchors, and structured errors.

While connected the server is authoritative. While disconnected the display
becomes a deterministic local continuation of the last authoritative state
it persisted, and the moment the server speaks again the local cursor is
abandoned rather than reconciled or reported back.

A small client evaluator resolves offline playback from the persisted
manifest and clock anchor. Catch-up is arithmetic rather than iterative:
whole rotations are skipped between eligibility boundaries, so a month
offline resolves in milliseconds. Mixed durations, wraparound, [start, end)
schedules, and expirations are evaluated locally, and a null expiration
stays permanent. Mode off keeps the hidden cursor advancing, pause holds
its slide and remainder, static holds until the cached metadata says the
asset genuinely expired, and an activation queued for an unknown playlist
holds the last good frame instead of continuing into a playlist that is
being replaced. Contract tests run this evaluator and the real Phase 3B
engine over identical scenarios and assert the same slide, start, and
deadline, so the client tracks the server without refactoring it.

Cold start restores signage from the Service Worker shell, IndexedDB, and
Cache Storage with the entire stack down, keeping the same persistent
display identity.

Validated display error codes now become Home Assistant incidents through
the Phase 4 machinery, keyed per display and condition so a persisting
error reports once and recovers once. Arbitrary browser text stays
telemetry.

Corrects the deployment assumption: the kiosk runs on a separate machine,
so production requires a trusted HTTPS origin and localhost is development
only. Verifying on a real trusted certificate exposed two production bugs
that localhost had hidden — behind a reverse proxy an outage arrives as a
502 rather than a refused connection, and an unbounded shell fetch left a
restarting kiosk blank for the proxy's whole connect timeout. The worker
now treats a gateway error as an outage and bounds that wait.

Verified with 200/200 backend, 20 client unit, and 30 Playwright tests on
a trusted HTTPS origin with no browser security flags weakened.

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-08-17 20:36:13 -05:00

75 lines
2.4 KiB
JavaScript

import http from "node:http";
// The offline suite must genuinely remove infrastructure, not simulate it.
// Talking to the Docker Engine socket directly avoids installing a CLI into
// the browser image and keeps every scenario inside one Playwright run.
const SOCKET = "/var/run/docker.sock";
function engine(method, path) {
return new Promise((resolve, reject) => {
const request = http.request({ socketPath: SOCKET, method, path }, (response) => {
let body = "";
response.on("data", (chunk) => (body += chunk));
response.on("end", () => resolve({ status: response.statusCode, body }));
});
request.on("error", reject);
request.end();
});
}
const PROJECT = process.env.VERIFY_PROJECT || "announcements-p5b-verify";
export const CONTAINERS = {
web: `${PROJECT}-web-1`,
api: `${PROJECT}-api-1`,
storage: `${PROJECT}-seaweedfs-1`,
};
export async function stop(...names) {
for (const name of names) await engine("POST", `/containers/${name}/stop?t=2`);
}
export async function start(...names) {
for (const name of names) await engine("POST", `/containers/${name}/start`);
}
export async function isRunning(name) {
const { status, body } = await engine("GET", `/containers/${name}/json`);
if (status !== 200) return false;
return Boolean(JSON.parse(body).State?.Running);
}
export async function waitUntilHealthy(baseUrl, timeoutMs = 120_000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
const response = await fetch(`${baseUrl}/api/playback`, {
signal: AbortSignal.timeout(2_000),
});
if (response.ok) return true;
} catch {
// Still coming up.
}
await new Promise((resolve) => setTimeout(resolve, 500));
}
throw new Error("stack did not become reachable in time");
}
// With the API stopped but Nginx still running, the proxy answers 502. That is
// a response, not a reachable API, so a gateway error counts as unreachable
// just like a refused connection does.
export async function waitUntilUnreachable(baseUrl, timeoutMs = 60_000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
const response = await fetch(`${baseUrl}/api/playback`, {
signal: AbortSignal.timeout(1_000),
});
if (!response.ok) return true;
} catch {
return true;
}
await new Promise((resolve) => setTimeout(resolve, 250));
}
throw new Error("stack stayed reachable");
}