Files
LexAI/tests/unit/actions.test.ts
john kevin asprec d0c4cc947d refactor: declarative context-menu registry; named selection threshold
CONTEXT_MENU_ENTRIES maps every menu id to {action, style, parentId,
title}, replacing the split('-') id parsing that only worked because no
name contained a hyphen. Foreign menu ids are now ignored explicitly.
Parity pinned by tests enumerating all 30 previous ids. The 10-char
toolbar trigger threshold is now the named MIN_SELECTION_LENGTH.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-14 21:44:16 +08:00

49 lines
1.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
ACTIONS,
ACTION_LABELS,
WRITING_STYLES,
CONTEXT_MENU_STYLES,
CONTEXT_MENU_ENTRIES,
findContextMenuEntry,
} from '@lib/actions';
describe('context-menu registry', () => {
it('contains 5 parents + 5x5 style children = 30 entries (parity with old loops)', () => {
expect(CONTEXT_MENU_ENTRIES).toHaveLength(30);
expect(CONTEXT_MENU_ENTRIES.filter((e) => !e.parentId)).toHaveLength(ACTIONS.length);
});
it('every parent precedes its children (contextMenus.create ordering)', () => {
CONTEXT_MENU_ENTRIES.forEach((entry, i) => {
if (entry.parentId) {
const parentIndex = CONTEXT_MENU_ENTRIES.findIndex((e) => e.id === entry.parentId);
expect(parentIndex).toBeGreaterThanOrEqual(0);
expect(parentIndex).toBeLessThan(i);
}
});
});
it('maps every previous menu id to the same action/style as the old split("-") parser', () => {
// The old parser: strip 'lexai-', split on '-', capitalize the style part.
for (const action of ACTIONS) {
const parent = findContextMenuEntry(`lexai-${action}`);
expect(parent).toMatchObject({ action, style: 'Default' });
expect(parent!.title).toBe(`⚡ LexAI: ${ACTION_LABELS[action]}`);
for (const style of CONTEXT_MENU_STYLES) {
const child = findContextMenuEntry(`lexai-${action}-${style.toLowerCase()}`);
expect(child).toMatchObject({ action, style, parentId: `lexai-${action}`, title: style });
}
}
});
it('returns undefined for foreign menu ids', () => {
expect(findContextMenuEntry('some-other-extension-item')).toBeUndefined();
});
it('style lists stay consistent', () => {
expect(CONTEXT_MENU_STYLES).toEqual(WRITING_STYLES.filter((s) => s !== 'Default'));
});
});