Library only; nothing is mounted. Adds minimal DeviceContext/NoticeContext stubs (owned by W0-breakpoints / W0-registry-notices) that expose the agreed interfaces. Refs #39 #49 #34 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
201 lines
6.9 KiB
TypeScript
201 lines
6.9 KiB
TypeScript
import { describe, test, expect, vi, afterEach, beforeEach } from 'vitest';
|
|
import React from 'react';
|
|
import { renderEditorHarness, EditorHarness } from '../test-utils/editorHarness';
|
|
import { buildNodeTree } from '../utils/craft-tree';
|
|
import {
|
|
registerCommand,
|
|
registerCommands,
|
|
getCommands,
|
|
getCommand,
|
|
subscribeCommands,
|
|
useCommandContext,
|
|
useCommandShortcuts,
|
|
useRunCommand,
|
|
useRegisterCommands,
|
|
__resetCommandsForTests,
|
|
type CommandContext,
|
|
type Command,
|
|
} from './commands';
|
|
|
|
let h: EditorHarness;
|
|
let ctxRef: { current: CommandContext | null };
|
|
let runRef: { current: ((id: string) => Promise<boolean>) | null };
|
|
|
|
const Probe: React.FC = () => {
|
|
useCommandShortcuts();
|
|
ctxRef.current = useCommandContext();
|
|
runRef.current = useRunCommand();
|
|
return null;
|
|
};
|
|
|
|
function key(init: KeyboardEventInit, target: EventTarget = document.body) {
|
|
const e = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, ...init });
|
|
h.act(() => {
|
|
target.dispatchEvent(e);
|
|
});
|
|
return e;
|
|
}
|
|
|
|
const cmd = (over: Partial<Command> & { id: string }): Command => ({
|
|
title: over.id,
|
|
group: 'edit',
|
|
run: () => {},
|
|
...over,
|
|
});
|
|
|
|
beforeEach(() => {
|
|
__resetCommandsForTests();
|
|
ctxRef = { current: null };
|
|
runRef = { current: null };
|
|
h = renderEditorHarness();
|
|
h.mountChild(<Probe />);
|
|
});
|
|
afterEach(() => {
|
|
h.unmount();
|
|
__resetCommandsForTests();
|
|
document.body.innerHTML = '';
|
|
});
|
|
|
|
describe('registry', () => {
|
|
test('order, replace by id, unregister, subscribe', () => {
|
|
const cb = vi.fn();
|
|
const off = subscribeCommands(cb);
|
|
const offA = registerCommand(cmd({ id: 'a' }));
|
|
registerCommands([cmd({ id: 'b' }), cmd({ id: 'c' })]);
|
|
expect(getCommands().map((c) => c.id)).toEqual(['a', 'b', 'c']);
|
|
expect(getCommands()).toBe(getCommands()); // stable snapshot
|
|
const a2 = cmd({ id: 'a', title: 'A2' });
|
|
registerCommand(a2);
|
|
expect(getCommand('a')).toBe(a2);
|
|
expect(getCommands().map((c) => c.id)).toEqual(['a', 'b', 'c']);
|
|
offA(); // stale unregister must not remove the replacement
|
|
expect(getCommand('a')).toBe(a2);
|
|
expect(cb).toHaveBeenCalled();
|
|
off();
|
|
});
|
|
|
|
test('useRegisterCommands registers on mount and unregisters on unmount', () => {
|
|
const Feature: React.FC = () => {
|
|
useRegisterCommands([cmd({ id: 'feat' })], []);
|
|
return null;
|
|
};
|
|
h.mountChild(<><Probe /><Feature /></>);
|
|
expect(getCommand('feat')).toBeDefined();
|
|
h.mountChild(<Probe />);
|
|
expect(getCommand('feat')).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('useCommandShortcuts', () => {
|
|
test('mod+d runs the matching command once, preventDefault called', () => {
|
|
const dup = vi.fn();
|
|
const other = vi.fn();
|
|
registerCommands([
|
|
cmd({ id: 'edit.other', shortcut: 'mod+e', run: other }),
|
|
cmd({ id: 'edit.duplicate', shortcut: 'mod+d', run: dup }),
|
|
]);
|
|
const e = key({ key: 'd', ctrlKey: true });
|
|
expect(dup).toHaveBeenCalledTimes(1);
|
|
expect(other).not.toHaveBeenCalled();
|
|
expect(e.defaultPrevented).toBe(true);
|
|
expect(dup.mock.calls[0][0].query).toBeDefined();
|
|
});
|
|
|
|
test('not run when a focused input is the target', () => {
|
|
const run = vi.fn();
|
|
registerCommand(cmd({ id: 'x', shortcut: 'mod+d', run }));
|
|
const input = document.createElement('input');
|
|
document.body.appendChild(input);
|
|
input.focus();
|
|
const e = key({ key: 'd', ctrlKey: true }, input);
|
|
expect(run).not.toHaveBeenCalled();
|
|
expect(e.defaultPrevented).toBe(false);
|
|
});
|
|
|
|
test('allowWhileTyping commands still fire in an input', () => {
|
|
const run = vi.fn();
|
|
registerCommand(cmd({ id: 'x', shortcut: 'mod+k', allowWhileTyping: true, run }));
|
|
const input = document.createElement('input');
|
|
document.body.appendChild(input);
|
|
input.focus();
|
|
key({ key: 'k', ctrlKey: true }, input);
|
|
expect(run).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test('when() false: not run, event not prevented, next match may run', () => {
|
|
const blocked = vi.fn();
|
|
const fallback = vi.fn();
|
|
registerCommands([
|
|
cmd({ id: 'blocked', shortcut: 'mod+d', when: () => false, run: blocked }),
|
|
cmd({ id: 'fallback', shortcut: 'mod+d', run: fallback }),
|
|
]);
|
|
const e = key({ key: 'd', ctrlKey: true });
|
|
expect(blocked).not.toHaveBeenCalled();
|
|
expect(fallback).toHaveBeenCalledTimes(1);
|
|
expect(e.defaultPrevented).toBe(true);
|
|
|
|
__resetCommandsForTests();
|
|
registerCommand(cmd({ id: 'only', shortcut: 'mod+d', when: () => false, run: blocked }));
|
|
const e2 = key({ key: 'd', ctrlKey: true });
|
|
expect(blocked).not.toHaveBeenCalled();
|
|
expect(e2.defaultPrevented).toBe(false);
|
|
});
|
|
|
|
test('unregister stops it', () => {
|
|
const run = vi.fn();
|
|
const off = registerCommand(cmd({ id: 'x', shortcut: 'mod+d', run }));
|
|
key({ key: 'd', ctrlKey: true });
|
|
expect(run).toHaveBeenCalledTimes(1);
|
|
off();
|
|
const e = key({ key: 'd', ctrlKey: true });
|
|
expect(run).toHaveBeenCalledTimes(1);
|
|
expect(e.defaultPrevented).toBe(false);
|
|
});
|
|
|
|
test('run sees the LIVE selection even before the collector re-renders', () => {
|
|
const tree = buildNodeTree(h.query, { type: { resolvedName: 'Container' }, props: { node_id: 'n1' }, nodes: [] } as any);
|
|
h.act(() => h.actions.addNodeTree(tree, 'ROOT'));
|
|
let seen: string[] = [];
|
|
registerCommand(cmd({ id: 'x', shortcut: 'mod+d', run: (c) => { seen = c.selectedIds; } }));
|
|
// Same act() batch: React has not re-rendered, so only a live read can see it.
|
|
h.act(() => {
|
|
h.actions.selectNode('n1');
|
|
document.body.dispatchEvent(new KeyboardEvent('keydown', { key: 'd', ctrlKey: true, bubbles: true, cancelable: true }));
|
|
});
|
|
expect(seen).toEqual(['n1']);
|
|
});
|
|
});
|
|
|
|
describe('useCommandContext / useRunCommand', () => {
|
|
test('selectedIds reflects actions.selectNode', () => {
|
|
const tree = buildNodeTree(h.query, { type: { resolvedName: 'Container' }, props: { node_id: 'n1' }, nodes: [] } as any);
|
|
h.act(() => h.actions.addNodeTree(tree, 'ROOT'));
|
|
expect(ctxRef.current!.selectedIds).toEqual([]);
|
|
h.act(() => h.actions.selectNode('n1'));
|
|
expect(ctxRef.current!.selectedIds).toEqual(['n1']);
|
|
h.act(() => h.actions.clearEvents());
|
|
expect(ctxRef.current!.selectedIds).toEqual([]);
|
|
});
|
|
|
|
test('exposes the composed contexts', () => {
|
|
const c = ctxRef.current!;
|
|
expect(c.device).toBe('desktop');
|
|
for (const fn of ['setDevice', 'notify', 'openSitesmith', 'openTemplates', 'openHeadCode'] as const) {
|
|
expect(typeof c[fn]).toBe('function');
|
|
}
|
|
expect(c.pages).toBeDefined();
|
|
});
|
|
|
|
test('useRunCommand: runs by id; false when missing or when() fails', async () => {
|
|
const run = vi.fn();
|
|
registerCommands([
|
|
cmd({ id: 'ok', run }),
|
|
cmd({ id: 'off', when: () => false, run }),
|
|
]);
|
|
expect(await runRef.current!('ok')).toBe(true);
|
|
expect(await runRef.current!('off')).toBe(false);
|
|
expect(await runRef.current!('missing')).toBe(false);
|
|
expect(run).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|