import assert from 'node:assert/strict'; import test from 'node:test'; import { JSDOM } from 'jsdom'; // Exercise the real Radix portal/focus lifecycle, rather than a mocked Dialog. test('plugin manager is single-instance, closes with Escape, restores focus and remounts on reopen', async () => { const dom = new JSDOM('
', { pretendToBeVisual: true, url: 'http://localhost' }); const previous = new Map(); const install = (key: string, value: unknown) => { previous.set(key, Object.getOwnPropertyDescriptor(globalThis, key)); Object.defineProperty(globalThis, key, { configurable: true, writable: true, value }); }; for (const key of ['window', 'document', 'navigator', 'HTMLElement', 'HTMLInputElement', 'Element', 'Node', 'NodeFilter', 'MutationObserver', 'CustomEvent', 'Event', 'KeyboardEvent', 'MouseEvent']) { install(key, key === 'window' ? dom.window : dom.window[key as keyof typeof dom.window]); } install('getComputedStyle', dom.window.getComputedStyle.bind(dom.window)); install('requestAnimationFrame', dom.window.requestAnimationFrame.bind(dom.window)); install('cancelAnimationFrame', dom.window.cancelAnimationFrame.bind(dom.window)); install('IS_REACT_ACT_ENVIRONMENT', true); const { default: React, act, useEffect, useRef, useState } = await import('react'); const { createRoot } = await import('react-dom/client'); const { I18nProvider } = await import('../../../application/i18n/I18nProvider'); const { PluginManagementDialog } = await import('./PluginManagementDialog'); const root = createRoot(document.getElementById('root')!); let mounts = 0; let unmounts = 0; function Content() { useEffect(() => { mounts++; return () => { unmounts++; }; }, []); return ; } const settle = () => new Promise((resolve) => setTimeout(resolve, 20)); const click = async (element: HTMLElement) => { await act(async () => { element.click(); await settle(); }); }; try { await act(async () => { root.render(); }); const trigger = document.querySelector('button')!; assert.equal(document.querySelector('[role="dialog"]'), null); assert.equal(mounts, 0, 'closed settings entry does not mount manager subscriptions'); trigger.focus(); await click(trigger); assert.equal(document.querySelectorAll('[role="dialog"]').length, 1); assert.equal(mounts, 1); assert.equal(document.activeElement?.getAttribute('aria-label'), 'Plugin search'); assert.equal(document.querySelector('[role="dialog"]')?.contains(document.querySelector('[data-settings-scroll-pane]')), true); await act(async () => { root.render(); }); assert.equal(document.querySelectorAll('[role="dialog"]').length, 1); assert.equal(mounts, 1, 'rerender does not spawn another manager'); const closeButton = document.querySelector('[role="dialog"] button[data-dialog-close]:not([aria-hidden])')!; await act(async () => { closeButton.focus(); closeButton.dispatchEvent(new KeyboardEvent('keydown', { key: 'Tab', bubbles: true, cancelable: true })); }); assert.equal(document.activeElement?.getAttribute('aria-label'), 'Plugin search', 'Tab wraps inside the modal'); await act(async () => { document.activeElement!.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); await settle(); }); assert.equal(document.querySelector('[role="dialog"]'), null); assert.equal(document.activeElement, trigger); assert.equal(unmounts, 1); await click(trigger); assert.equal(mounts, 2); await click(document.querySelector('[role="dialog"] button[data-dialog-close]:not([aria-hidden])')!); assert.equal(document.querySelector('[role="dialog"]'), null); assert.equal(document.activeElement, trigger); assert.equal(unmounts, 2); let handedOff = false; await act(async () => root.render({(handoff) => })); await click(trigger); await click(document.querySelector('[role="dialog"] button:not([data-dialog-close])')!); assert.equal(handedOff, true, 'external view opens only after modal ownership is released'); function ControlledEntry() { const [open, setOpen] = useState(false); const browse = useRef(null); const opener = useRef(null); return <> ; } await act(async () => root.render()); const external = [...document.querySelectorAll('button')].find((item) => item.textContent === 'External plugin settings')!; external.focus(); await click(external); assert.equal(document.querySelectorAll('[role=dialog]').length, 1); await act(async () => { document.activeElement!.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); await settle(); }); assert.equal(document.activeElement, external, 'controlled entry restores the actual row opener'); await click(external); external.disabled = true; await act(async () => { document.activeElement!.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); await settle(); }); assert.equal(document.activeElement?.textContent, 'Fallback browse', 'a disabled or removed opener uses the stable browse action'); } finally { await act(async () => root.unmount()); dom.window.close(); for (const [key, descriptor] of previous) { if (descriptor) Object.defineProperty(globalThis, key, descriptor); else Reflect.deleteProperty(globalThis, key); } } });