import assert from 'node:assert/strict'; import { test } from 'node:test'; import { planNativeMenu, runNativeMenu, safeId, type NativeMenuActions, type NativeMenuState } from '../src/frame/nativeMenuModel.ts'; import { MENU_MESSAGES } from '../src/frame/menuModel.ts'; const words = (name: string, params: Record = {}) => (MENU_MESSAGES[`wb.menu.${name}`] ?? name).replace(/\{(\w+)\}/g, (_, key) => String(params[key] ?? '')); const text = (key: string) => key.split('.').pop() ?? key; const state: NativeMenuState = { product: 'wayBetter for X', hasPanel: true, canSplit: true, canFind: true, safeActive: false, features: { find: true, zoom: true, deck: true, store: true, safeMode: true, workspaces: true, updates: true }, themes: [{ id: 'dark', label: 'Dark', checked: true }, { id: 'my theme!', label: 'Mine', checked: false }], iconPacks: [{ id: 'theme', label: 'Follow theme', checked: true }], modes: [{ id: 'basic', label: 'Basic', checked: true }, { id: 'power', label: 'Power User', checked: false }], accounts: [{ id: 'default', label: '@one', checked: true }], plugins: [{ id: 'chat', label: 'Chat+', checked: true }, { id: 'media', label: 'Media+', checked: false }], workspaces: [{ id: 'w1', label: 'Work', checked: false }], installFromFile: true, shareSetup: true, docs: true, website: 'waybetter.fyi', }; /** Every node of the description, flattened. */ type AnyNode = { kind: string; id?: string; label?: string; items?: AnyNode[]; accelerator?: string }; const walk = (nodes: AnyNode[]): AnyNode[] => nodes.flatMap(node => [node, ...walk(node.items ?? [])]); test('the bar has the eight menus and every item in the owner\'s list', () => { const spec = planNativeMenu(state, words, text); assert.deepEqual(spec.menus.map(menu => (menu as AnyNode).label), ['wayBetter for X', 'File', 'Edit', 'View', 'Account', 'Plugins', 'Window', 'Help']); const ids = walk(spec.menus as AnyNode[]).filter(node => node.kind === 'item').map(node => node.id); for (const id of ['app.about', 'app.updates', 'app.settings', 'app.safe', 'file.new-tab', 'file.split-right', 'file.split-down', 'file.close', 'file.install', 'file.share', 'edit.find', 'edit.find-next', 'edit.find-previous', 'view.zoom-in', 'view.zoom-out', 'view.zoom-reset', 'view.deck', 'acct.add', 'plugins.store', 'plugins.folder', 'ws.save', 'help.docs', 'help.report', 'help.website']) { assert.ok(ids.includes(id), id); } assert.ok(ids.includes('theme:dark') && ids.includes('mode:power') && ids.includes('icons:theme') && ids.includes('acct:default') && ids.includes('plugin:chat') && ids.includes('ws:w1')); const roles = walk(spec.menus as AnyNode[]).filter(node => node.kind === 'predefined').map(node => (node as unknown as { role: string }).role); for (const role of ['services', 'hide', 'quit', 'undo', 'cut', 'copy', 'paste', 'select_all', 'minimize', 'fullscreen']) assert.ok(roles.includes(role), role); }); test('what native code accepts: ids and labels', () => { const spec = planNativeMenu(state, words, text); for (const node of walk(spec.menus as AnyNode[])) { if (node.kind === 'item') assert.match(node.id!, /^[A-Za-z0-9_.:/-]{1,96}$/, node.id); if (node.label !== undefined) assert.ok(node.label.length > 0 && node.label.length <= 120 && !/[\u0000-\u001f]/.test(node.label), node.label); } assert.equal(safeId('my theme!'), 'my_theme_'); }); test('shortcuts follow the standard places', () => { const items = new Map(walk(planNativeMenu(state, words, text).menus as AnyNode[]).filter(node => node.kind === 'item').map(node => [node.id, node.accelerator])); assert.equal(items.get('app.settings'), 'CmdOrCtrl+,'); assert.equal(items.get('file.new-tab'), 'CmdOrCtrl+T'); assert.equal(items.get('file.close'), 'CmdOrCtrl+W'); assert.equal(items.get('edit.find'), 'CmdOrCtrl+F'); assert.equal(items.get('edit.find-next'), 'CmdOrCtrl+G'); assert.equal(items.get('edit.find-previous'), 'CmdOrCtrl+Shift+G'); }); test('what the app does not serve leaves its items out; nothing is empty', () => { const bare: NativeMenuState = { ...state, themes: undefined, iconPacks: undefined, modes: undefined, plugins: undefined, workspaces: undefined, accounts: [], features: { find: false, zoom: false, deck: false, store: false, safeMode: false, workspaces: false, updates: false }, installFromFile: false, shareSetup: false, docs: false, website: undefined }; const ids = walk(planNativeMenu(bare, words, text).menus as AnyNode[]).filter(node => node.kind === 'item').map(node => node.id); for (const gone of ['app.safe', 'app.updates', 'edit.find', 'view.zoom-in', 'view.deck', 'plugins.store', 'file.install', 'help.docs', 'help.website']) assert.ok(!ids.includes(gone), gone); assert.ok(ids.includes('acct.none') && ids.includes('acct.add')); assert.ok(!ids.some(id => id?.startsWith('theme:'))); }); test('a chosen id runs exactly its action; unknown ids run nothing', () => { const calls: string[] = []; const act = new Proxy({}, { get: (_target, name: string) => (...args: unknown[]) => { calls.push(`${name}(${args.join(',')})`); } }) as NativeMenuActions; const run = (id: string) => runNativeMenu(id, state, act); assert.ok(run('file.new-tab') && run('edit.find-previous') && run('theme:my_theme_') && run('mode:power') && run('acct:default') && run('plugin:media') && run('plugin:chat') && run('ws:w1') && run('icons:theme')); assert.deepEqual(calls, ['newTab()', 'findPrevious()', 'theme(my theme!)', 'mode(power)', 'account(default)', 'plugin(media,true)', 'plugin(chat,false)', 'workspace(w1)', 'iconPack(theme)']); calls.length = 0; assert.equal(run('theme:nope'), false); assert.equal(run('evil:thing'), false); assert.equal(run('app.unknown'), false); assert.deepEqual(calls, []); });