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 one parent per action + style children for all actions except prompt', () => {
    const styledActions = ACTIONS.filter((a) => a !== 'prompt');
    expect(CONTEXT_MENU_ENTRIES).toHaveLength(ACTIONS.length + styledActions.length * CONTEXT_MENU_STYLES.length);
    expect(CONTEXT_MENU_ENTRIES.filter((e) => !e.parentId)).toHaveLength(ACTIONS.length);
    // prompt is a single item — its parameters live in the Prompt Builder dialog
    expect(CONTEXT_MENU_ENTRIES.filter((e) => e.parentId === 'lexai-prompt')).toHaveLength(0);
  });

  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]}`);

      if (action === 'prompt') continue; // no style children — see registry comment
      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'));
  });
});
