import assert from 'node:assert/strict'; import test from 'node:test'; import { JSDOM } from 'jsdom'; test('plugin surface forwards its own input and contains malformed updates and render errors', async () => { const dom = new JSDOM('
', { url: 'http://localhost', pretendToBeVisual: true }); const saved = new Map(); for (const key of ['window', 'document', 'navigator', 'HTMLElement', 'Element', 'Node', 'Event', 'MouseEvent', 'ShadowRoot']) { saved.set(key, Object.getOwnPropertyDescriptor(globalThis, key)); Object.defineProperty(globalThis, key, { configurable: true, writable: true, value: key === 'window' ? dom.window : dom.window[key as keyof typeof dom.window] }); } Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, writable: true, value: true }); let captured: ShadowRoot | undefined; const nativeAttach = dom.window.Element.prototype.attachShadow; dom.window.Element.prototype.attachShadow = function (options) { captured = nativeAttach.call(this, options) as unknown as ShadowRoot; return captured as unknown as globalThis.ShadowRoot; }; const { act } = await import('react'); const { createRoot } = await import('react-dom/client'); const { PluginRemoteSurface } = await import('./PluginRemoteSurface'); const events: unknown[] = []; const tree = { children: [{ id: '1', type: 1, element: 'div', attributes: { class: 'card', style: 'display:grid;gap:8px' }, children: [ { id: '2', type: 1, element: 'button', attributes: { type: 'button', 'data-netcatty-remote-node': 'forged' }, children: [{ id: '3', type: 3, data: 'Hello' }] }, { id: '4', type: 1, element: 'input', attributes: { type: 'text', value: '' }, children: [] }, ] }] }; const root = createRoot(document.getElementById('root')!); try { await act(async () => { root.render( events.push(event)} />); }); assert.ok(captured); assert.equal(document.querySelector('[data-plugin-remote-surface]')?.querySelector('button'), null); assert.equal(captured!.querySelector('button')?.textContent, 'Hello'); assert.equal(captured!.querySelector('button')?.getAttribute('data-netcatty-remote-node'), '2'); await act(async () => { captured!.querySelector('button')!.click(); }); assert.deepEqual(events, [{ nodeId: '2', type: 'click' }]); const input = captured!.querySelector('input')!; input.value = 'typed'; await act(async () => { input.dispatchEvent(new dom.window.Event('input', { bubbles: true })); }); assert.deepEqual(events.at(-1), { nodeId: '4', type: 'input', value: 'typed', selectionStart: 5, selectionEnd: 5 }); await act(async () => { document.getElementById('host')!.dispatchEvent(new dom.window.Event('input', { bubbles: true })); }); assert.equal(events.length, 2); await act(async () => { root.render( {}} />); }); assert.equal(document.querySelector('[role=alert]')?.textContent, 'Unable to display plugin view.'); const optionA = { id: 'a', type: 1, element: 'option', attributes: { selected: '' }, children: [{ id: 'at', type: 3, data: ' First\n option ' }] }; const optionB = { id: 'b', type: 1, element: 'option', attributes: {}, children: [{ id: 'bt', type: 3, data: 'Second' }] }; const controls = { children: [ { id: 'select', type: 1, element: 'select', attributes: { multiple: '' }, children: [optionA, optionB] }, { id: 'textarea', type: 1, element: 'textarea', attributes: {}, children: [{ id: 'ta', type: 3, data: 'Hello' }, { id: 'tb', type: 3, data: ' world' }] }, ] }; const renderControls = async () => act(async () => root.render( events.push(event)} />)); await renderControls(); const select = captured!.querySelector('select')!; assert.deepEqual([...select.selectedOptions].map((option) => option.value), ['First option']); assert.equal(captured!.querySelector('textarea')!.value, 'Hello world'); await act(async () => { select.options[0].selected = false; select.options[1].selected = true; select.dispatchEvent(new dom.window.Event('change', { bubbles: true })); }); assert.deepEqual(events.at(-1), { nodeId: 'select', type: 'change', value: 'Second', values: ['Second'] }); optionA.attributes = {} as typeof optionA.attributes; optionB.attributes = { selected: '' }; await renderControls(); assert.deepEqual([...select.selectedOptions].map((option) => option.value), ['Second'], 'acknowledgement keeps native option text values selected'); let clicks = 0; const renderView = async (nextTree: unknown, viewKey = 'view') => act(async () => root.render(<> {}} /> {}} /> )); await renderView(tree); await renderView({ children: [{ id: 'bad', type: 1, element: 'input', attributes: {}, children: [{ id: 'text', type: 3, data: 'invalid' }] }] }); assert.equal(document.querySelectorAll('[data-plugin-remote-surface]').length, 1); assert.ok(document.querySelector('[role=alert]')); await act(async () => document.getElementById('canary')!.click()); assert.equal(clicks, 1); // Unexpected rendering failures stay within this plugin, including layout effects. const originalError = console.error; const prevAttach = dom.window.Element.prototype.attachShadow; try { console.error = () => {}; dom.window.Element.prototype.attachShadow = () => { throw new Error('Synthetic surface failure'); }; await renderView(tree, 'broken-surface'); } finally { dom.window.Element.prototype.attachShadow = prevAttach; console.error = originalError; } assert.equal(document.querySelectorAll('[data-plugin-remote-surface]').length, 1); assert.ok(document.querySelector('[role=alert]')); await act(async () => document.getElementById('canary')!.click()); assert.equal(clicks, 2); } finally { await act(async () => { root.unmount(); }); for (const [key, value] of saved) { if (value) Object.defineProperty(globalThis, key, value); else Reflect.deleteProperty(globalThis, key); } Reflect.deleteProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT'); dom.window.close(); } }); test('closed-shadow editing keeps app close shortcuts in Vault, sidebar, tabs and popovers', async () => { const dom = new JSDOM('
', { url: 'http://localhost', pretendToBeVisual: true }); const saved = new Map(); for (const [key, value] of Object.entries({ window: dom.window, document: dom.window.document, navigator: dom.window.navigator, HTMLElement: dom.window.HTMLElement, Element: dom.window.Element, Node: dom.window.Node, IS_REACT_ACT_ENVIRONMENT: true })) { saved.set(key, Object.getOwnPropertyDescriptor(globalThis, key)); Object.defineProperty(globalThis, key, { configurable: true, writable: true, value }); } dom.window.HTMLElement.prototype.getBoundingClientRect = () => ({ x: 20, y: 20, width: 240, height: 30, top: 20, left: 20, right: 260, bottom: 50, toJSON() {} }); Object.assign(dom.window, { ResizeObserver: class { observe() {} disconnect() {} } }); const shadows: ShadowRoot[] = []; const nativeAttach = dom.window.Element.prototype.attachShadow; dom.window.Element.prototype.attachShadow = function (options) { const shadow = nativeAttach.call(this, options); shadows.push(shadow as unknown as ShadowRoot); return shadow; }; const { act } = await import('react'); const { createRoot } = await import('react-dom/client'); const { PluginRemoteSurface } = await import('./PluginRemoteSurface'); const { handleGlobalHotkeyKeyDownImpl } = await import('../../application/app/AppHandlers'); const { isPluginViewEditingTarget, isPluginShortcutEditableEvent } = await import('../../application/state/pluginKeybindings'); const { matchesKeyBinding } = await import('../../domain/models'); const { DEFAULT_KEY_BINDINGS } = await import('../../domain/models/keyBindings'); const actions: string[] = []; const capture = (event: KeyboardEvent) => handleGlobalHotkeyKeyDownImpl(() => ({ HOTKEY_DEBUG: false, closeTabKeyStr: 'Ctrl + W', hotkeyScheme: 'pc', keyBindings: DEFAULT_KEY_BINDINGS, matchesKeyBinding, executeHotkeyAction: (action: string) => actions.push(action), }), event); dom.window.addEventListener('keydown', capture as unknown as EventListener, true); const control = (id: string, element: string, attributes = {}) => ({ id, type: 1, element, attributes, children: [] }); const tree = { children: [control('input', 'input'), control('textarea', 'textarea'), control('anchor', 'button', { id: 'anchor' }), { ...control('popover', 'div', { 'data-plugin-popover': 'true', 'data-plugin-anchor': 'anchor', open: '' }), children: [control('pop-input', 'input'), control('pop-textarea', 'textarea')] }, ] }; const root = createRoot(document.getElementById('root')!); try { for (const slot of ['vault-page', 'terminal-sidebar', 'plugin-tab']) { shadows.length = 0; await act(async () => root.render(
{}} />
)); assert.equal(shadows.length, 2, 'includes the portaled popover surface'); for (const shadow of shadows) { for (const element of shadow.querySelectorAll('input,textarea')) { await act(async () => element.focus()); // Native menu IPC sees this same retargeted document.activeElement. assert.equal(isPluginViewEditingTarget(document.activeElement), true, `${slot}: ${element.tagName}`); let editable = false; const observe = (event: Event) => { editable = isPluginShortcutEditableEvent(event); assert.equal(event.target, shadow.host); }; dom.window.addEventListener('keydown', observe, { once: true }); await act(async () => element.dispatchEvent(new dom.window.KeyboardEvent('keydown', { key: 'w', code: 'KeyW', ctrlKey: true, bubbles: true, composed: true, cancelable: true }))); assert.equal(editable, true); assert.deepEqual(actions, []); } } await act(async () => (shadows[0].querySelector('button') as HTMLButtonElement).focus()); assert.equal(isPluginViewEditingTarget(document.activeElement), false, 'focus marker clears when editing ends'); } } finally { dom.window.removeEventListener('keydown', capture as unknown as EventListener, true); await act(async () => root.unmount()); dom.window.close(); for (const [key, value] of saved) { if (value) Object.defineProperty(globalThis, key, value); else Reflect.deleteProperty(globalThis, key); } } }); test('PluginRemoteSurface avoids JSON/form hook work on text and plain elements and preserves unchanged subtrees', async () => { const dom = new JSDOM('
', { url: 'http://localhost', pretendToBeVisual: true }); const saved = new Map(); for (const key of ['window', 'document', 'navigator', 'HTMLElement', 'Element', 'Node', 'Event', 'MouseEvent', 'ShadowRoot']) { saved.set(key, Object.getOwnPropertyDescriptor(globalThis, key)); Object.defineProperty(globalThis, key, { configurable: true, writable: true, value: key === 'window' ? dom.window : dom.window[key as keyof typeof dom.window] }); } Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, writable: true, value: true }); let captured: ShadowRoot | undefined; const nativeAttach = dom.window.Element.prototype.attachShadow; dom.window.Element.prototype.attachShadow = function (options) { captured = nativeAttach.call(this, options) as unknown as ShadowRoot; return captured as unknown as globalThis.ShadowRoot; }; const { act } = await import('react'); const { createRoot } = await import('react-dom/client'); const { PluginRemoteSurface } = await import('./PluginRemoteSurface'); const stableSubtree = { id: 'stable-list', type: 1, element: 'div', attributes: { class: 'list' }, children: Array.from({ length: 40 }, (_, i) => ({ id: `row-${i}`, type: 1, element: 'span', attributes: {}, children: [{ id: `text-${i}`, type: 3, data: `Item ${i}` }], })), }; const onEvent = () => {}; const root = createRoot(document.getElementById('root')!); const origStringify = JSON.stringify; const origParse = JSON.parse; let stringifyCalls = 0; let parseCalls = 0; JSON.stringify = ((...args: Parameters) => { stringifyCalls++; return origStringify(...args); }) as typeof JSON.stringify; JSON.parse = ((...args: Parameters) => { parseCalls++; return origParse(...args); }) as typeof JSON.parse; try { await act(async () => { root.render( , ); }); assert.equal(stringifyCalls, 0, 'initial mount of text, plain, and input elements must not invoke JSON.stringify'); assert.equal(parseCalls, 0, 'initial mount of text, plain, and input elements must not invoke JSON.parse'); const firstListNode = captured!.querySelector('.list'); assert.ok(firstListNode); assert.equal(captured!.querySelector('p')?.textContent, 'Count 0'); assert.equal(captured!.querySelector('input')?.value, 'hello'); await act(async () => { root.render( , ); }); assert.equal(parseCalls, 0, 're-rendering text, plain, and input elements must invoke zero JSON.parse calls'); assert.equal(captured!.querySelector('.list'), firstListNode, 'unchanged plain subtree preserves DOM identity'); assert.equal(captured!.querySelector('p')?.textContent, 'Count 1'); assert.equal(captured!.querySelector('input')?.value, 'world'); } finally { JSON.stringify = origStringify; JSON.parse = origParse; await act(async () => root.unmount()); dom.window.close(); for (const [key, value] of saved) { if (value) Object.defineProperty(globalThis, key, value); else Reflect.deleteProperty(globalThis, key); } Reflect.deleteProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT'); } }); test('PluginRemoteSurface forwards dblclick, contextmenu, throttled scroll, cursor popover, and host-rendered widgets', async () => { const dom = new JSDOM('
', { url: 'http://localhost', pretendToBeVisual: true }); const saved = new Map(); for (const key of ['window', 'document', 'navigator', 'HTMLElement', 'Element', 'Node', 'Event', 'MouseEvent', 'PointerEvent', 'ShadowRoot']) { saved.set(key, Object.getOwnPropertyDescriptor(globalThis, key)); const val = key === 'window' ? dom.window : key === 'PointerEvent' ? dom.window.MouseEvent : dom.window[key as keyof typeof dom.window]; Object.defineProperty(globalThis, key, { configurable: true, writable: true, value: val }); } Object.defineProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT', { configurable: true, writable: true, value: true }); Object.assign(dom.window, { ResizeObserver: class { private cb: (entries: unknown[]) => void; constructor(cb: (entries: unknown[]) => void) { this.cb = cb; } observe(target: Element) { this.cb([{ target, borderBoxSize: [{ inlineSize: 800, blockSize: 600 }], contentRect: { width: 800, height: 600 } }]); } unobserve() {} disconnect() {} }, PointerEvent: dom.window.MouseEvent, }); dom.window.HTMLElement.prototype.getBoundingClientRect = () => ({ x: 100, y: 50, width: 800, height: 600, top: 50, left: 100, right: 900, bottom: 650, toJSON() {}, }); const shadows: ShadowRoot[] = []; const nativeAttach = dom.window.Element.prototype.attachShadow; dom.window.Element.prototype.attachShadow = function (options) { const s = nativeAttach.call(this, options); shadows.push(s as unknown as ShadowRoot); return s; }; const { act } = await import('react'); const { createRoot } = await import('react-dom/client'); const { PluginRemoteSurface } = await import('./PluginRemoteSurface'); const events: Array> = []; const gridProps = { columns: [ { name: 'id', type: 'int4', primaryKey: true }, { name: 'name', type: 'varchar', nullable: true }, ], rows: [ { id: 1, name: 'Alice' }, { id: 2, name: null }, ], dirtyCells: { '0:name': 'Alicia' }, editable: true, }; const erProps = { tables: [ { name: 'users', columns: [{ name: 'id', type: 'int', isPk: true }] }, { name: 'orders', columns: [{ name: 'id', type: 'int', isPk: true }, { name: 'user_id', type: 'int', isFk: true }] }, ], relations: [{ fromTable: 'orders', fromColumn: 'user_id', toTable: 'users', toColumn: 'id' }], }; const tree = { children: [ { id: 'scroller', type: 1, element: 'div', attributes: { class: 'list' }, children: [{ id: 'item', type: 1, element: 'button', attributes: { type: 'button' }, children: [{ id: 'it', type: 3, data: 'Row' }] }], }, { id: 'ctx-popover', type: 1, element: 'div', attributes: { 'data-plugin-popover': 'true', 'data-plugin-anchor-x': '120', 'data-plugin-anchor-y': '80', 'data-plugin-popover-width': '260', 'data-plugin-popover-max-height': '320', open: '', }, children: [{ id: 'pop-item', type: 1, element: 'span', attributes: {}, children: [{ id: 'pt', type: 3, data: 'Menu' }] }], }, { id: 'split', type: 1, element: 'div', attributes: { 'data-plugin-widget': 'split-pane', 'data-plugin-widget-props': JSON.stringify({ direction: 'horizontal', initialSize: 220, minSize: 120, maxSize: 400 }), }, children: [ { id: 'pane-a', type: 1, element: 'div', attributes: {}, children: [{ id: 'pa', type: 3, data: 'Left' }] }, { id: 'pane-b', type: 1, element: 'div', attributes: {}, children: [{ id: 'pb', type: 3, data: 'Right' }] }, ], }, { id: 'grid', type: 1, element: 'div', attributes: { 'data-plugin-widget': 'data-grid', 'data-plugin-widget-props': JSON.stringify(gridProps) }, children: [], }, { id: 'er', type: 1, element: 'div', attributes: { 'data-plugin-widget': 'er-canvas', 'data-plugin-widget-props': JSON.stringify(erProps) }, children: [], }, ], }; const root = createRoot(document.getElementById('root')!); try { await act(async () => { root.render( events.push(e as unknown as Record)} />); }); const mainShadow = shadows[0]; assert.ok(mainShadow, 'main shadow mounted'); // 1. dblclick & contextmenu with surface-relative coordinates + preventDefault const btn = mainShadow.querySelector('button')!; const ctxEvent = new dom.window.MouseEvent('contextmenu', { bubbles: true, cancelable: true, clientX: 250, clientY: 170 }); await act(async () => { btn.dispatchEvent(new dom.window.MouseEvent('dblclick', { bubbles: true, cancelable: true, clientX: 180, clientY: 90 })); btn.dispatchEvent(ctxEvent); }); assert.equal(ctxEvent.defaultPrevented, true, 'contextmenu must call preventDefault'); assert.deepEqual(events[0], { nodeId: 'item', type: 'dblclick', x: 80, y: 40 }); assert.deepEqual(events[1], { nodeId: 'item', type: 'contextmenu', x: 150, y: 120 }); // 2. throttled scroll const scroller = mainShadow.querySelector('.list') as HTMLElement; scroller.scrollTop = 140; scroller.scrollLeft = 20; await act(async () => { scroller.dispatchEvent(new dom.window.Event('scroll', { bubbles: false })); await new Promise((resolve) => setTimeout(resolve, 90)); }); const scrollEvt = events.find((e) => e.type === 'scroll'); assert.ok(scrollEvt, 'scroll event emitted'); assert.equal(scrollEvt!.scrollTop, 140); assert.equal(scrollEvt!.scrollLeft, 20); // 3. cursor-anchored popover positioned within surface bounds with custom width & maxHeight const popoverEl = document.querySelector('[data-plugin-view-popover]') as HTMLElement | null; assert.ok(popoverEl, 'cursor-anchored popover rendered'); assert.equal(popoverEl!.style.width, '260px'); assert.equal(popoverEl!.style.maxHeight, '320px'); assert.equal(popoverEl!.style.left, '220px'); // surface.left (100) + anchorX (120) // 4. SplitPane rendered inside shadow root with two panes and drag handle const splitEl = mainShadow.querySelector('[data-plugin-widget="split-pane"]') as HTMLElement | null; assert.ok(splitEl, 'split-pane rendered in shadow root'); assert.equal(splitEl!.querySelectorAll('[data-split-pane]').length, 2); const handle = splitEl!.querySelector('[data-split-handle]') as HTMLElement; await act(async () => { handle.dispatchEvent(new dom.window.MouseEvent('pointerdown', { bubbles: true, clientX: 220 })); dom.window.dispatchEvent(new dom.window.MouseEvent('pointermove', { bubbles: true, clientX: 280 })); dom.window.dispatchEvent(new dom.window.MouseEvent('pointerup', { bubbles: true, clientX: 280 })); }); assert.deepEqual( events.find((e) => e.nodeId === 'split' && e.widgetEvent === 'resizeEnd'), { nodeId: 'split', type: 'widget', widgetEvent: 'resizeEnd', detail: { size: 280, collapsed: false } }, ); // 5. DataGrid slotted in Light DOM: renders dirty cell, NULL pill, inline edit & sort const gridHost = document.querySelector('[data-plugin-widget-host="data-grid"]') as HTMLElement | null; assert.ok(gridHost, 'data-grid slotted in shadow host Light DOM'); assert.ok(gridHost!.textContent?.includes('Alicia'), 'dirty cell value rendered'); assert.ok(gridHost!.textContent?.includes('NULL'), 'NULL pill rendered'); const nameHeaderBtn = gridHost!.querySelector('[data-col-name="name"] button') as HTMLButtonElement; await act(async () => { nameHeaderBtn.click(); }); assert.deepEqual( events.find((e) => e.nodeId === 'grid' && e.widgetEvent === 'sortChange'), { nodeId: 'grid', type: 'widget', widgetEvent: 'sortChange', detail: { column: 'name', direction: 'asc' } }, ); const cell = gridHost!.querySelector('[role="gridcell"][data-row-index="0"][data-col-name="name"]') as HTMLElement; await act(async () => { cell.dispatchEvent(new dom.window.MouseEvent('dblclick', { bubbles: true })); }); const editorInput = cell.querySelector('input') as HTMLInputElement; assert.ok(editorInput, 'double-click opens inline cell editor'); await act(async () => { const setter = Object.getOwnPropertyDescriptor(dom.window.HTMLInputElement.prototype, 'value')!.set!; setter.call(editorInput, 'Bob'); editorInput.dispatchEvent(new dom.window.Event('input', { bubbles: true })); }); await act(async () => { editorInput.dispatchEvent(new dom.window.KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); }); assert.deepEqual( events.find((e) => e.nodeId === 'grid' && e.widgetEvent === 'cellEdit'), { nodeId: 'grid', type: 'widget', widgetEvent: 'cellEdit', detail: { rowIndex: 0, colName: 'name', value: 'Bob', isNull: false } }, ); // 6. ER Canvas slotted in Light DOM: renders tables + FK curve + emits tableClick const erHost = document.querySelector('[data-plugin-widget-host="er-canvas"]') as HTMLElement | null; assert.ok(erHost, 'er-canvas slotted in shadow host Light DOM'); const usersCard = erHost!.querySelector('[data-er-table="users"]') as Element | null; assert.ok(usersCard, 'users table card rendered'); await act(async () => { usersCard!.dispatchEvent(new dom.window.MouseEvent('pointerdown', { bubbles: true, button: 0, clientX: 10, clientY: 10 })); dom.window.dispatchEvent(new dom.window.MouseEvent('pointerup', { bubbles: true, button: 0, clientX: 10, clientY: 10 })); }); assert.deepEqual( events.find((e) => e.nodeId === 'er' && e.widgetEvent === 'tableClick'), { nodeId: 'er', type: 'widget', widgetEvent: 'tableClick', detail: { table: 'users' } }, ); } finally { await act(async () => root.unmount()); dom.window.close(); for (const [key, value] of saved) { if (value) Object.defineProperty(globalThis, key, value); else Reflect.deleteProperty(globalThis, key); } Reflect.deleteProperty(globalThis, 'IS_REACT_ACT_ENVIRONMENT'); } });