import test from 'node:test'; import { readFileSync } from 'node:fs'; import assert from 'node:assert/strict'; import { BackgroundHub, backgroundHub } from '../../../src/lib/plugins/backgrounds.ts'; import { ExternalSession } from '../../../src/lib/plugins/externalSession.ts'; import type { BackgroundFrame, PluginApi } from '../../../src/lib/plugins/sdk.ts'; const frame: BackgroundFrame = { url: 'blob:http://localhost/example', overlay: 0.3, position: 'center', size: 'cover' }; test('frames retain ownership, reveal earlier owners, and cannot mutate hub state', () => { const hub = new BackgroundHub(() => undefined); const first = Symbol(), second = Symbol(); const events: string[] = []; hub.subscribe(target => events.push(target)); hub.set(first, 'pm-all', frame); const received = hub.get('pm-all')!; received.url = 'blob:changed'; assert.equal(hub.get('pm-all')!.url, frame.url); hub.set(second, 'pm-all', { ...frame, overlay: -0.4 }); hub.set(first, 'app', frame); assert.equal(hub.get('pm-all')!.overlay, -0.4); hub.removeOwner(second); assert.equal(hub.get('pm-all')!.overlay, 0.3); hub.removeOwner(first); assert.equal(hub.get('pm-all'), null); assert.equal(hub.get('app'), null); assert.deepEqual(events, ['pm-all', 'pm-all', 'app', 'pm-all', 'pm-all', 'app']); }); test('app override uses body properties and removes them without changing current root appearance', () => { const root = new Map([['--rs-background-image', 'saved image']]); const properties = new Map(); const style = { getPropertyValue: (key: string) => properties.get(key) ?? '', getPropertyPriority: () => '', setProperty: (key: string, value: string) => properties.set(key, value), removeProperty: (key: string) => { properties.delete(key); }, }; const hub = new BackgroundHub(() => ({ style }) as unknown as HTMLElement); const owner = Symbol(); hub.set(owner, 'app', frame); assert.match(properties.get('--rs-background-image')!, /blob:http/); assert.equal(properties.get('--rs-workarea-surface-opacity'), 'var(--rs-surface-opacity, 82%)'); assert.equal(properties.get('--rs-workarea-plugin-bg'), 'var(--rs-transparent-plugin-bg, transparent)'); root.set('--rs-background-image', 'new saved image'); hub.set(owner, 'app', { ...frame, overlay: -0.5, size: 'contain' }); assert.match(properties.get('--rs-background-image')!, /255,255,255,0.5/); hub.removeOwner(owner); assert.equal(properties.size, 0); assert.equal(root.get('--rs-background-image'), 'new saved image'); }); test('invalid frames and targets do not enter the hub', () => { const hub = new BackgroundHub(() => undefined), owner = Symbol(); for (const target of ['pm-board:../../x', 'unknown', 'pm-board:']) assert.throws(() => hub.set(owner, target, frame), /target/); for (const invalid of [{ ...frame, url: 'https://example.com/image.png' }, { ...frame, blurredUrl: 'https://example.com/image.png' }, { ...frame, blurredUrl: 'blob:bad")' }, { ...frame, overlay: NaN }, { ...frame, overlay: 2 }, { ...frame, url: 'blob:bad")' }]) { assert.throws(() => hub.set(owner, 'app', invalid), /frame/); } hub.set(owner, 'pm-board:0c18bcdc-f41e-4a31-87f4-87c369f398c1', frame); hub.removeOwner(owner); }); test('portrait frames retain a contained foreground and cover underlay through transitions and cleanup', () => { const properties = new Map(); const style = { getPropertyValue: (key: string) => properties.get(key) ?? '', getPropertyPriority: () => '', setProperty: (key: string, value: string) => properties.set(key, value), removeProperty: (key: string) => properties.delete(key) }; const hub = new BackgroundHub(() => ({ style }) as unknown as HTMLElement), owner = Symbol(); hub.set(owner, 'app', { ...frame, size: 'contain', blurredUrl: 'blob:blurred', previous: frame, transitionMs: 2000 }); assert.equal(properties.get('--rs-background-layer-sizes'), '100% 100%, contain, cover'); assert.equal(properties.get('--rs-background-layer-positions'), 'center, center, center'); assert.equal(properties.get('--rs-background-previous-layer-sizes'), '100% 100%, cover'); assert.match(properties.get('--rs-background-image')!, /blob:blurred/); const received = hub.get('app')!; received.blurredUrl = 'blob:changed'; assert.equal(hub.get('app')!.blurredUrl, 'blob:blurred'); hub.removeOwner(owner); assert.equal(properties.size, 0); }); test('fade frames validate previous images and keep nested data private', () => { const hub = new BackgroundHub(() => undefined), owner = Symbol(); const fading = { ...frame, previous: { ...frame, url: 'blob:previous' }, transitionMs: 800 }; hub.set(owner, 'pm-all', fading); fading.previous.url = 'blob:mutated'; const received = hub.get('pm-all')!; assert.equal(received.previous!.url, 'blob:previous'); received.previous!.url = 'blob:changed'; assert.equal(hub.get('pm-all')!.previous!.url, 'blob:previous'); for (const invalid of [ { ...frame, previous: { ...frame, url: 'https://example.com/image.png' } }, { ...frame, transitionMs: NaN }, { ...frame, transitionMs: -1 }, { ...frame, transitionMs: 5001 }, ]) assert.throws(() => hub.set(owner, 'pm-all', invalid), /frame/); }); test('subscriber errors do not prevent frame cleanup or remaining notifications', () => { const hub = new BackgroundHub(() => undefined), owner = Symbol(); let events = 0; hub.subscribe(() => { throw new Error('Subscriber failure'); }); hub.subscribe(() => { events++; }); hub.set(owner, 'pm-all', frame); hub.removeOwner(owner); assert.equal(events, 2); assert.equal(hub.get('pm-all'), null); }); test('disable removes frames and subscriptions before cleanup and rejects pending file results', async () => { let api!: PluginApi; let finishRead!: (value: Uint8Array) => void; let finishList!: (value: { name: string }[]) => void; let finishPicker!: (value: string | null) => void; const session = new ExternalSession('org.test.backgrounds', { workspace: { list: async () => [], read: async () => '', write: async () => {} }, settings: { read: async () => null, write: async () => {} }, assets: { read: async () => new Uint8Array() }, backgrounds: { chooseFolder: () => new Promise(resolve => { finishPicker = resolve; }), list: () => new Promise(resolve => { finishList = resolve; }), read: () => new Promise(resolve => { finishRead = resolve; }), }, }, () => {}, () => {}); let notifications = 0; await session.start({ activate(value) { api = value; api.backgrounds!.subscribe(() => { notifications++; }); api.backgrounds!.set('app', frame); api.backgrounds!.set('pm-all', frame); api.onDispose(() => { assert.equal(backgroundHub.get('app'), null); assert.equal(backgroundHub.get('pm-all'), null); assert.throws(() => api.backgrounds!.get('app'), /disabled/); }); } }); const pending = [api.backgrounds!.read('folder', 'file.png'), api.backgrounds!.list('folder'), api.backgrounds!.chooseFolder()]; const rejected = pending.map(request => assert.rejects(request, /disabled/)); await session.stop(); finishRead(new Uint8Array([1])); finishList([{ name: 'file.png' }]); finishPicker('folder'); await Promise.all(rejected); assert.equal(notifications, 2); assert.throws(() => api.backgrounds!.set('app', frame), /disabled/); assert.throws(() => api.backgrounds!.read('folder', 'file.png'), /disabled/); assert.throws(() => api.backgrounds!.subscribe(() => {}), /disabled/); assert.equal(backgroundHub.get('app'), null); }); test('background commands are allowed in main and settings windows', () => { const build = readFileSync(new URL('../../../src-tauri/build.rs', import.meta.url), 'utf8'); for (const scope of ['default', 'settings']) { const capability = JSON.parse(readFileSync(new URL(`../../../src-tauri/capabilities/${scope}.json`, import.meta.url), 'utf8')); for (const command of ['backgrounds_choose_folder', 'backgrounds_default_folder', 'backgrounds_list', 'backgrounds_read', 'backgrounds_read_image_settings', 'backgrounds_write_image_settings']) { assert(build.includes(`"${command}"`), `${command} needs a generated desktop permission`); assert(capability.permissions.includes(`allow-${command.replaceAll('_', '-')}`), `${scope} must allow ${command}`); } } });