import { allocateEntity } from '@flighthq/entity/contract'; import type { RectangleLike } from '@flighthq/geometry/contract'; import { createSignal } from '@flighthq/signals/contract'; import type { Signal } from '@flighthq/signals/contract'; import { describe, expect, it, vi } from 'vitest'; import { createTrayIcon, destroyTrayIcon, displayTrayBalloon, getTrayIconBounds, getTrayIconTitle, getTrayIconTooltip, getTrayIcons, initializeTrayCreateFailedResult, initializeTrayCreateProviderFailureResult, initializeTrayCreateSuccessResult, isTrayDestroyed, isTrayIconAnimating, onTrayBalloonEvent, onTrayDrop, onTrayInteraction, onTrayMenuSelection, popupTrayContextMenu, removeTrayBalloon, setTrayAnimationGuard, setTrayIcon, setTrayIconContextMenu, setTrayIconTemplate, setTrayIconTitle, setTrayIconTooltip, setTrayIgnoreDoubleClickEvents, setTrayPressedIcon, startTrayIconAnimation, stopTrayIconAnimation, } from './tray.ts'; import type { HostTrayBalloonCapability, HostTrayBalloonEventsCapability, HostTrayBoundsCapability, HostTrayDoubleClickPolicyCapability, HostTrayDropEventsCapability, HostTrayImageCapability, HostTrayInteractionEventsCapability, HostTrayLifecycleCapability, HostTrayMenuCapability, HostTrayMenuSelectionEventsCapability, HostTrayPopupMenuCapability, HostTrayPressedImageCapability, HostTrayTemplateImageCapability, HostTrayTitleCapability, HostTrayTooltipCapability, TrayBalloonEvent, TrayDropEvent, TrayIcon, TrayInteractionEvent, TrayMenuSelectionEvent, } from './types.ts'; type TestHost = { readonly tray: { readonly lifecycle: HostTrayLifecycleCapability } } & { readonly tray: { readonly image: HostTrayImageCapability }; } & { readonly tray: { readonly title: HostTrayTitleCapability } } & { readonly tray: { readonly tooltip: HostTrayTooltipCapability }; } & { readonly tray: { readonly menu: HostTrayMenuCapability } } & { readonly tray: { readonly templateImage: HostTrayTemplateImageCapability }; } & { readonly tray: { readonly bounds: HostTrayBoundsCapability } } & { readonly tray: { readonly popupMenu: HostTrayPopupMenuCapability }; } & { readonly tray: { readonly doubleClickPolicy: HostTrayDoubleClickPolicyCapability } } & { readonly tray: { readonly pressedImage: HostTrayPressedImageCapability }; } & { readonly tray: { readonly balloon: HostTrayBalloonCapability } } & { readonly tray: { readonly interactionEvents: HostTrayInteractionEventsCapability }; } & { readonly tray: { readonly menuSelectionEvents: HostTrayMenuSelectionEventsCapability; }; } & { readonly tray: { readonly balloonEvents: HostTrayBalloonEventsCapability } } & { readonly tray: { readonly dropEvents: HostTrayDropEventsCapability }; }; interface TestState { balloonEvents: WeakMap>>; destroyFailures: number; destroyed: WeakSet; dropEvents: WeakMap>>; images: string[]; interactionEvents: WeakMap>>; live: TrayIcon[]; menuSelectionEvents: WeakMap>>; title: string; tooltip: string; } function testHost(): { host: TestHost; state: TestState } { const state: TestState = { balloonEvents: new WeakMap(), destroyFailures: 0, destroyed: new WeakSet(), dropEvents: new WeakMap(), images: [], interactionEvents: new WeakMap(), live: [], menuSelectionEvents: new WeakMap(), title: '', tooltip: '', }; const signalFor = ( map: WeakMap>>, tray: TrayIcon, ): Signal> | null => map.get(tray) ?? null; const host = { tray: { lifecycle: (() => { const out = allocateEntity(); out.create = async (tray: TrayIcon) => { state.live.push(tray); state.balloonEvents.set(tray, createSignal()); state.dropEvents.set(tray, createSignal()); state.interactionEvents.set(tray, createSignal()); state.menuSelectionEvents.set(tray, createSignal()); return { outcome: 'created' as const }; }; out.destroy = async (tray: TrayIcon) => { if (state.destroyFailures-- > 0) { return { failures: [{ step: 'native-resource' as const }], outcome: 'tray-destroy-failed' as const }; } state.destroyed.add(tray); state.live = state.live.filter((item) => item !== tray); return { outcome: 'destroyed' as const }; }; out.isDestroyed = (tray: TrayIcon) => state.destroyed.has(tray); out.list = () => state.live.slice(); return out; })(), image: (() => { const out = allocateEntity(); out.set = async (_tray: TrayIcon, icon: string) => { state.images.push(icon); return { outcome: 'updated' as const }; }; return out; })(), title: (() => { const out = allocateEntity(); out.get = async () => { return { outcome: 'available' as const, title: state.title }; }; out.set = async (_tray: TrayIcon, title: string) => { state.title = title; return { outcome: 'updated' as const }; }; return out; })(), tooltip: (() => { const out = allocateEntity(); out.get = async () => { return { outcome: 'available' as const, tooltip: state.tooltip }; }; out.set = async (_tray: TrayIcon, tooltip: string) => { state.tooltip = tooltip; return { outcome: 'updated' as const }; }; return out; })(), menu: (() => { const out = allocateEntity(); out.set = async () => { return { outcome: 'updated' as const }; }; return out; })(), templateImage: (() => { const out = allocateEntity(); out.set = async () => { return { outcome: 'updated' as const }; }; return out; })(), bounds: (() => { const out = allocateEntity(); out.get = async () => { return { bounds: { height: 2, width: 1, x: 3, y: 4 } satisfies RectangleLike, outcome: 'available' as const }; }; return out; })(), popupMenu: (() => { const out = allocateEntity(); out.popup = async () => { return { outcome: 'shown' as const }; }; return out; })(), doubleClickPolicy: (() => { const out = allocateEntity(); out.setIgnore = async () => { return { outcome: 'updated' as const }; }; return out; })(), pressedImage: (() => { const out = allocateEntity(); out.set = async () => { return { outcome: 'updated' as const }; }; return out; })(), balloon: (() => { const out = allocateEntity(); out.display = async () => { return { outcome: 'displayed' as const }; }; out.remove = async () => { return { outcome: 'removed' as const }; }; return out; })(), interactionEvents: (() => { const out = allocateEntity(); out.getSignal = (tray: TrayIcon) => signalFor(state.interactionEvents, tray); return out; })(), menuSelectionEvents: (() => { const out = allocateEntity(); out.getSignal = (tray: TrayIcon) => signalFor(state.menuSelectionEvents, tray); return out; })(), balloonEvents: (() => { const out = allocateEntity(); out.getSignal = (tray: TrayIcon) => signalFor(state.balloonEvents, tray); return out; })(), dropEvents: (() => { const out = allocateEntity(); out.getSignal = (tray: TrayIcon) => signalFor(state.dropEvents, tray); return out; })(), }, }; return { host: host as TestHost, state }; } async function acquire(host: TestHost): Promise { const result = await createTrayIcon(host.tray.lifecycle); if (result.outcome !== 'created') throw new Error(result.outcome); return result.tray; } describe('createTrayIcon', () => { it('publishes only after provider acquisition and exposes no public id', async () => { const { host, state } = testHost(); const pending = createTrayIcon(host.tray.lifecycle); expect(state.live).toHaveLength(1); const result = await pending; expect(result.outcome).toBe('created'); if (result.outcome === 'created') expect('id' in result.tray).toBe(false); }); }); describe('destroyTrayIcon', () => { it('retries only a failed native release and is idempotent after success', async () => { const { host, state } = testHost(); state.destroyFailures = 1; const tray = await acquire(host); expect((await destroyTrayIcon(tray)).outcome).toBe('tray-destroy-failed'); expect((await destroyTrayIcon(tray)).outcome).toBe('destroyed'); expect((await destroyTrayIcon(tray)).outcome).toBe('already-destroyed'); }); }); describe('displayTrayBalloon', () => { it('owns balloon display on the Tray entity', async () => { const { host } = testHost(); const tray = await acquire(host); expect((await displayTrayBalloon(host.tray.balloon, tray, { text: 'Done', title: 'Flight' })).outcome).toBe( 'displayed', ); }); }); describe('getTrayIconBounds', () => { it('returns provider bounds', async () => { const { host } = testHost(); expect(await getTrayIconBounds(host.tray.bounds, await acquire(host))).toMatchObject({ outcome: 'available', bounds: { width: 1 }, }); }); }); describe('getTrayIcons', () => { it('preserves acquisition order', async () => { const { host } = testHost(); const first = await acquire(host); const second = await acquire(host); expect(getTrayIcons(host.tray.lifecycle)).toEqual([first, second]); }); }); describe('getTrayIconTitle', () => { it('reads the title', async () => { const { host } = testHost(); const tray = await acquire(host); await setTrayIconTitle(host.tray.title, tray, 'Flight'); expect(await getTrayIconTitle(host.tray.title, tray)).toEqual({ outcome: 'available', title: 'Flight' }); }); }); describe('getTrayIconTooltip', () => { it('reads the tooltip', async () => { const { host } = testHost(); const tray = await acquire(host); await setTrayIconTooltip(host.tray.tooltip, tray, 'Ready'); expect(await getTrayIconTooltip(host.tray.tooltip, tray)).toEqual({ outcome: 'available', tooltip: 'Ready' }); }); }); describe('initializeTrayCreateFailedResult', () => { it('is the construction initializer of createTrayCreateFailedResult', () => { expect(typeof initializeTrayCreateFailedResult).toBe('function'); }); }); describe('initializeTrayCreateProviderFailureResult', () => { it('is the construction initializer of createTrayCreateProviderFailureResult', () => { expect(typeof initializeTrayCreateProviderFailureResult).toBe('function'); }); }); describe('initializeTrayCreateSuccessResult', () => { it('is the construction initializer of createTrayCreateSuccessResult', () => { expect(typeof initializeTrayCreateSuccessResult).toBe('function'); }); }); describe('isTrayDestroyed', () => { it('does not treat an unknown tray as destroyed', () => { const tray = allocateEntity(); expect(isTrayDestroyed(tray)).toBe(false); }); it('tracks the pinned lifecycle', async () => { const { host } = testHost(); const tray = await acquire(host); expect(isTrayDestroyed(tray)).toBe(false); await destroyTrayIcon(tray); expect(isTrayDestroyed(tray)).toBe(true); }); }); describe('isTrayIconAnimating', () => { it('tracks the current animation generation', async () => { vi.useFakeTimers(); const { host } = testHost(); const tray = await acquire(host); await startTrayIconAnimation(host.tray.image, tray, ['a'], 10); expect(isTrayIconAnimating(tray)).toBe(true); stopTrayIconAnimation(tray); expect(isTrayIconAnimating(tray)).toBe(false); vi.useRealTimers(); }); }); describe('onTrayBalloonEvent', () => { it('delivers balloon lifecycle independently', async () => { const { host, state } = testHost(); const tray = await acquire(host); const listener = vi.fn(); expect(onTrayBalloonEvent(host.tray.balloonEvents, tray, listener).outcome).toBe('attached'); state.balloonEvents.get(tray)!.emit({ type: 'show' }); expect(listener).toHaveBeenCalledWith({ type: 'show' }); }); }); describe('onTrayDrop', () => { it('delivers drop data independently', async () => { const { host, state } = testHost(); const tray = await acquire(host); const listener = vi.fn(); expect(onTrayDrop(host.tray.dropEvents, tray, listener).outcome).toBe('attached'); state.dropEvents.get(tray)!.emit({ files: ['a'], type: 'files' }); expect(listener).toHaveBeenCalledWith({ files: ['a'], type: 'files' }); }); }); describe('onTrayInteraction', () => { it('stops delivering to a released listener because its slot is disconnected', async () => { const { host, state } = testHost(); const tray = await acquire(host); const listener = vi.fn(); const attached = onTrayMenuSelection(host.tray.menuSelectionEvents, tray, listener); expectTypeOf(state.menuSelectionEvents.get(tray)!).toEqualTypeOf>>(); state.menuSelectionEvents.get(tray)!.emit({ id: 'first' }); if (attached.outcome === 'attached') await attached.release.release(); state.menuSelectionEvents.get(tray)!.emit({ id: 'second' }); expect(listener).toHaveBeenCalledOnce(); expect(listener).toHaveBeenCalledWith({ id: 'first' }); }); it('releases once and destroy releases a remaining subscription', async () => { const { host, state } = testHost(); const tray = await acquire(host); const listener = vi.fn(); const attached = onTrayInteraction(host.tray.interactionEvents, tray, listener); expect(attached.outcome).toBe('attached'); state.interactionEvents.get(tray)!.emit({ altKey: false, bounds: null, ctrlKey: false, metaKey: false, position: null, shiftKey: false, type: 'click', }); expect(listener).toHaveBeenCalledOnce(); if (attached.outcome === 'attached') { expect((await attached.release.release()).outcome).toBe('released'); expect((await attached.release.release()).outcome).toBe('already-released'); } const destroyReleased = vi.fn(); onTrayInteraction(host.tray.interactionEvents, tray, destroyReleased); await destroyTrayIcon(tray); state.interactionEvents.get(tray)!.emit({ altKey: false, bounds: null, ctrlKey: false, metaKey: false, position: null, shiftKey: false, type: 'click', }); expect(destroyReleased).not.toHaveBeenCalled(); }); }); describe('onTrayMenuSelection', () => { it('delivers a per-Tray menu id', async () => { const { host, state } = testHost(); const tray = await acquire(host); const listener = vi.fn(); expect(onTrayMenuSelection(host.tray.menuSelectionEvents, tray, listener).outcome).toBe('attached'); state.menuSelectionEvents.get(tray)!.emit({ id: 'open' }); expect(listener).toHaveBeenCalledWith({ id: 'open' }); }); }); describe('popupTrayContextMenu', () => { it('shows the installed menu', async () => { const { host } = testHost(); expect((await popupTrayContextMenu(host.tray.popupMenu, await acquire(host), { x: 1, y: 2 })).outcome).toBe( 'shown', ); }); }); describe('removeTrayBalloon', () => { it('owns balloon removal on the Tray entity', async () => { const { host } = testHost(); const tray = await acquire(host); await displayTrayBalloon(host.tray.balloon, tray, { text: 'Done', title: 'Flight' }); expect((await removeTrayBalloon(host.tray.balloon, tray)).outcome).toBe('removed'); }); }); describe('setTrayAnimationGuard', () => { it('installs and clears the opt-in animation inspection hook', async () => { vi.useFakeTimers(); const guard = vi.fn(); setTrayAnimationGuard(guard); const { host } = testHost(); const tray = await acquire(host); const started = await startTrayIconAnimation(host.tray.image, tray, ['a'], 12); expect(guard).toHaveBeenCalledWith(tray, 1, 12); if (started.outcome === 'started') await started.release.release(); setTrayAnimationGuard(null); vi.useRealTimers(); }); }); describe('setTrayIcon', () => { it('uses the direct image provider', async () => { const { host, state } = testHost(); expect((await setTrayIcon(host.tray.image, await acquire(host), 'icon')).outcome).toBe('updated'); expect(state.images).toEqual(['icon']); }); }); describe('setTrayIconContextMenu', () => { it('installs a plain descriptor', async () => { const { host } = testHost(); expect( (await setTrayIconContextMenu(host.tray.menu, await acquire(host), [{ id: 'quit', label: 'Quit' }])).outcome, ).toBe('updated'); }); }); describe('setTrayIconTemplate', () => { it('updates template treatment', async () => { const { host } = testHost(); expect((await setTrayIconTemplate(host.tray.templateImage, await acquire(host), true)).outcome).toBe('updated'); }); }); describe('setTrayIconTitle', () => { it('updates the title', async () => { const { host } = testHost(); const tray = await acquire(host); expect((await setTrayIconTitle(host.tray.title, tray, 'Flight')).outcome).toBe('updated'); }); }); describe('setTrayIconTooltip', () => { it('updates the tooltip', async () => { const { host } = testHost(); const tray = await acquire(host); expect((await setTrayIconTooltip(host.tray.tooltip, tray, 'Ready')).outcome).toBe('updated'); }); }); describe('setTrayIgnoreDoubleClickEvents', () => { it('updates the policy', async () => { const { host } = testHost(); expect((await setTrayIgnoreDoubleClickEvents(host.tray.doubleClickPolicy, await acquire(host), true)).outcome).toBe( 'updated', ); }); }); describe('setTrayPressedIcon', () => { it('updates the pressed icon', async () => { const { host } = testHost(); expect((await setTrayPressedIcon(host.tray.pressedImage, await acquire(host), 'pressed')).outcome).toBe('updated'); }); }); describe('startTrayIconAnimation', () => { it('writes through the direct image provider', async () => { vi.useFakeTimers(); const { host, state } = testHost(); const tray = await acquire(host); const result = await startTrayIconAnimation(host.tray.image, tray, ['a', 'b'], 10); expect(result.outcome).toBe('started'); await vi.advanceTimersByTimeAsync(10); expect(state.images).toEqual(['a', 'b']); if (result.outcome === 'started') await result.release.release(); vi.useRealTimers(); }); }); describe('stopTrayIconAnimation', () => { it('stops only the current generation', async () => { vi.useFakeTimers(); const { host } = testHost(); const tray = await acquire(host); await startTrayIconAnimation(host.tray.image, tray, ['a'], 10); expect(stopTrayIconAnimation(tray).outcome).toBe('stopped'); expect(stopTrayIconAnimation(tray).outcome).toBe('already-stopped'); vi.useRealTimers(); }); });