import test from "node:test"; import assert from "node:assert/strict"; import { watchBackground, type BackgroundFrame, type PMBackgroundApi } from "../../../../src/lib/features/pm/model/transient-background.ts"; const frame = (url: string): BackgroundFrame => ({ url, overlay: 0, position: "center", size: "cover" }); function fixture() { const frames = new Map(); const listeners = new Set<(target: string) => void>(); // The shape of `backgroundHub` in `src/lib/plugins/backgrounds.ts`. const api: PMBackgroundApi = { get: target => frames.get(target) ?? null, subscribe(listener) { listeners.add(listener); return () => { listeners.delete(listener); }; }, }; const set = (target: string, value: BackgroundFrame | null) => { if (value) frames.set(target, value); else frames.delete(target); for (const listener of listeners) listener(target); }; return { api, set, listeners }; } test("slides override saved images and removal restores them without writes", async () => { const { api, set, listeners } = fixture(); const seen: (BackgroundFrame | null)[] = []; let savedReads = 0; const watcher = watchBackground("pm-board:one", api, async () => { savedReads++; return frame("saved"); }, value => seen.push(value)); await watcher.refresh(); set("pm-board:one", frame("slide")); assert.equal(seen.at(-1)?.url, "slide"); assert.equal(savedReads, 1); set("pm-board:one", null); await Promise.resolve(); assert.equal(seen.at(-1)?.url, "saved"); watcher.dispose(); watcher.dispose(); assert.equal(listeners.size, 0); }); test("pending saved reads cannot replace a new slide or change a closed mount", async () => { const { api, set } = fixture(); let resolve!: (value: BackgroundFrame) => void; const pending = new Promise(done => { resolve = done; }); const seen: (BackgroundFrame | null)[] = []; const watcher = watchBackground("pm-all", api, () => pending, value => seen.push(value)); const refresh = watcher.refresh(); set("pm-all", frame("slide")); resolve(frame("stale")); await refresh; assert.deepEqual(seen.map(value => value?.url), ["slide"]); set("pm-all", null); watcher.dispose(); await Promise.resolve(); await watcher.refresh(); assert.equal(seen.length, 1); }); test("late failures cannot clear a slide", async () => { const { api, set } = fixture(); let reject!: (error: Error) => void; const pending = new Promise( (_resolve, fail) => { reject = fail; }); const seen: (BackgroundFrame | null)[] = []; const watcher = watchBackground("pm-all", api, () => pending, value => seen.push(value)); const refresh = watcher.refresh(); set("pm-all", frame("slide")); reject(new Error("Missing image")); await refresh; assert.deepEqual(seen.map(value => value?.url), ["slide"]); watcher.dispose(); }); test("split mounts subscribe separately and filter board and all-task targets", async () => { const { api, set, listeners } = fixture(); const seen = [[], [], []] as (BackgroundFrame | null)[][]; const mounts = ["pm-board:one", "pm-board:two", "pm-all"].map((target, index) => watchBackground(target, api, async () => null, value => seen[index].push(value))); set("pm-board:one", frame("one")); assert.deepEqual(seen.map(values => values.length), [1, 0, 0]); mounts[0].dispose(); set("pm-board:two", frame("two")); set("pm-all", frame("all")); assert.deepEqual(seen.map(values => values.at(-1)?.url), ["one", "two", "all"]); mounts[1].dispose(); mounts[2].dispose(); assert.equal(listeners.size, 0); }); test("all-board slides override saved boards and specific slides retain priority", async () => { const { api, set } = fixture(); const seen: (BackgroundFrame | null)[] = []; const watcher = watchBackground("pm-board:one", api, async () => frame("saved"), value => seen.push(value)); await watcher.refresh(); set("pm-all", frame("all")); assert.equal(seen.at(-1)?.url, "all"); set("pm-board:one", frame("specific")); set("pm-all", frame("next-all")); assert.equal(seen.at(-1)?.url, "specific"); set("pm-board:one", null); assert.equal(seen.at(-1)?.url, "next-all"); set("pm-all", null); await Promise.resolve(); assert.equal(seen.at(-1)?.url, "saved"); watcher.dispose(); }); test("older hosts use saved images and failures clear the background", async () => { const seen: (BackgroundFrame | null)[] = []; let fails = false; const watcher = watchBackground("pm-all", undefined, async () => { if (fails) throw new Error("Missing image"); return frame("saved"); }, value => seen.push(value)); await watcher.refresh(); fails = true; await watcher.refresh(); assert.deepEqual(seen.map(value => value?.url ?? null), ["saved", null]); watcher.dispose(); }); test("shared app slides use one host layer and react to either target changing", async () => { const { api, set } = fixture(); const seen: (BackgroundFrame | null)[] = []; const watcher = watchBackground("pm-board:one", api, async () => frame("saved"), value => seen.push(value)); set("pm-all", frame("shared")); assert.equal(seen.at(-1)?.url, "shared"); set("app", frame("shared")); assert.equal(seen.at(-1), null); set("app", frame("next")); assert.equal(seen.at(-1)?.url, "shared"); set("pm-all", frame("next")); assert.equal(seen.at(-1), null); set("pm-board:one", frame("specific")); assert.equal(seen.at(-1)?.url, "specific"); set("pm-board:one", null); assert.equal(seen.at(-1), null); set("app", null); assert.equal(seen.at(-1)?.url, "next"); watcher.dispose(); }); test("different image appearance and transitions keep an independent PM layer", () => { const { api, set } = fixture(); const seen: (BackgroundFrame | null)[] = []; const watcher = watchBackground("pm-all", api, async () => null, value => seen.push(value)); set("app", frame("same")); for (const difference of [{ blurredUrl: "blob:blur" }, { overlay: 0.4 }, { size: "contain" as const }, { position: "top left" as const }, { previous: frame("old"), transitionMs: 500 }]) { set("pm-all", { ...frame("same"), ...difference }); assert.equal(seen.at(-1)?.url, "same"); } set("app", { ...frame("same"), previous: frame("old"), transitionMs: 500 }); assert.equal(seen.at(-1), null); watcher.dispose(); });