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 { PluginApi } from '../../../src/lib/plugins/sdk.ts'; import type { BackgroundFrame } from '../../../src/lib/plugins/backgroundContracts.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('plugins get no background API, and the frames stay with the host', async () => { let api!: PluginApi; 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() }, }, () => {}, () => {}); await session.start({ activate(value) { api = value; } }); // Project Management was the last plugin user of `api.backgrounds`. It reads `backgroundHub` as a native feature. assert.equal('backgrounds' in api, false); const owner = Symbol(); backgroundHub.set(owner, 'pm-all', frame); await session.stop(); assert.deepEqual(backgroundHub.get('pm-all'), frame); backgroundHub.removeOwner(owner); }); test('the Background commands are allowed in the windows that use them, and the old plugin commands are gone', () => { const build = readFileSync(new URL('../../../src-tauri/build.rs', import.meta.url), 'utf8'); const all = ['background_choose_folder', 'background_folder', 'background_clear_folder', 'background_list', 'background_read', 'background_read_sidecar', 'background_write_sidecar', 'background_migrate']; const scopes: Record = { default: all, settings: all, browser: ['background_folder', 'background_list', 'background_read', 'background_read_sidecar'] }; for (const [scope, commands] of Object.entries(scopes)) { const capability = JSON.parse(readFileSync(new URL(`../../../src-tauri/capabilities/${scope}.json`, import.meta.url), 'utf8')); for (const command of all) { assert(build.includes(`"${command}"`), `${command} needs a generated desktop permission`); assert.equal(capability.permissions.includes(`allow-${command.replaceAll('_', '-')}`), commands.includes(command), `${scope} and ${command}`); } assert.equal(capability.permissions.some((permission: string) => permission.startsWith('allow-backgrounds-')), false, `${scope} has no old plugin command`); } assert.equal(/"backgrounds_/.test(build), false); }); test('a new app frame changes only the custom properties with a new value', () => { const writes: string[] = []; const properties = new Map(); const style = { getPropertyValue: (key: string) => properties.get(key) ?? '', getPropertyPriority: () => '', setProperty: (key: string, value: string) => { writes.push(key); 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); assert.equal(writes.length, 19); writes.length = 0; hub.set(owner, 'app', { ...frame, url: 'blob:http://localhost/second' }); assert.deepEqual(writes, ['--rs-background-image']); writes.length = 0; hub.set(owner, 'app', { ...frame, url: 'blob:http://localhost/second' }); assert.deepEqual(writes, [], 'The same frame writes nothing'); hub.removeOwner(owner); assert.equal(properties.size, 0); writes.length = 0; hub.set(owner, 'app', frame); assert.equal(writes.length, 19, 'After a removal, the next frame writes every property again'); hub.removeOwner(owner); });