Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

11.1 KB
Raw Blame
import * as actualLinguiMacro from '@lingui/react/macro';
import { cleanup, render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { renderLinguiTemplate } from '@/test-utils/lingui-mock';

vi.doMock('@lingui/react/macro', () => ({
  ...actualLinguiMacro,
  useLingui: () => ({ t: renderLinguiTemplate }),
}));

type DispatchMock = ReturnType<typeof vi.fn>;

function installBridge(
  platform: string,
  mode: 'editor' | 'note' = 'editor',
  options: { ptyAvailable?: boolean; queryResult?: unknown } = {},
): DispatchMock {
  const dispatch = vi.fn((request: { kind: string }) =>
    Promise.resolve(request.kind === 'query' ? (options.queryResult ?? null) : undefined),
  );
  (window as unknown as { okDesktop?: unknown }).okDesktop = {
    platform,
    config: { mode, ptyAvailable: options.ptyAvailable ?? false },
    menu: { dispatch },
  };
  return dispatch;
}

async function openMenu(name: string) {
  const { AppMenubar } = await import('./AppMenubar');
  const { SingleFileModeProvider } = await import('@/lib/single-file-mode');
  render(
    <SingleFileModeProvider>
      <AppMenubar />
    </SingleFileModeProvider>,
  );
  await userEvent.click(screen.getByRole('menuitem', { name }));
}

async function openHelpMenu() {
  await openMenu('Help');
}

describe('AppMenubar Help menu', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  afterEach(() => {
    cleanup();
    (window as unknown as { okDesktop?: unknown }).okDesktop = undefined;
  });

  test('returns null on darwin, where the native menu bar owns Help', async () => {
    installBridge('darwin');
    const { AppMenubar } = await import('./AppMenubar');
    const { container } = render(<AppMenubar />);
    expect(container.firstChild).toBeNull();
  });

  test('returns null on the web host', async () => {
    const { AppMenubar } = await import('./AppMenubar');
    const { container } = render(<AppMenubar />);
    expect(container.firstChild).toBeNull();
  });

  test.each(['win32', 'linux'] as const)(
    'returns null in a %s note window, where app-wide menu chrome is intentionally absent',
    async (platform) => {
      installBridge(platform, 'note');
      const { AppMenubar } = await import('./AppMenubar');
      const { container } = render(<AppMenubar />);
      expect(container.firstChild).toBeNull();
    },
  );

  test('Send feedback dispatches the send-feedback menu action', async () => {
    const dispatch = installBridge('win32');
    await openHelpMenu();

    await userEvent.click(screen.getByRole('menuitem', { name: 'Send feedback…' }));

    expect(dispatch).toHaveBeenCalledWith({ kind: 'menu-action', action: 'send-feedback' });
  });

  test('Report a bug dispatches the report-bug menu action', async () => {
    const dispatch = installBridge('win32');
    await openHelpMenu();

    await userEvent.click(screen.getByRole('menuitem', { name: /^Report a bug…/ }));

    expect(dispatch).toHaveBeenCalledWith({ kind: 'menu-action', action: 'report-bug' });
  });

  test('the open Help menu is marked as a bug-report launcher', async () => {
    installBridge('win32');
    await openHelpMenu();

    expect(screen.getByRole('menu').hasAttribute('data-bug-report-launcher')).toBe(true);
  });

  test('Help entries read identically to the native menu, ellipsis included', async () => {
    installBridge('linux');
    await openHelpMenu();

    expect(screen.getByRole('menuitem', { name: /^Report a bug…/ })).not.toBeNull();
    expect(screen.getByRole('menuitem', { name: 'Send feedback…' })).not.toBeNull();
    expect(screen.getByRole('menuitem', { name: 'OpenKnowledge on GitHub' })).not.toBeNull();
  });

  test('lists the resource links after GitHub and ends with About', async () => {
    installBridge('win32');
    await openHelpMenu();

    const labels = within(screen.getByRole('menu'))
      .getAllByRole('menuitem')
      .map((item) => item.textContent);
    expect(labels).toEqual([
      'OpenKnowledge on GitHub',
      'Documentation',
      'Join us on Discord',
      'Report a bug…Ctrl+Shift+D',
      'Send feedback…',
      'About OpenKnowledge',
    ]);
  });

  test.each([
    ['Documentation', 'open-docs'],
    ['Join us on Discord', 'open-discord'],
  ] as const)('%s dispatches the %s command', async (name, command) => {
    const dispatch = installBridge('win32');
    await openHelpMenu();

    await userEvent.click(screen.getByRole('menuitem', { name }));

    expect(dispatch).toHaveBeenCalledWith({ kind: 'command', command });
  });

  test.each([
    ['win32', 'About OpenKnowledge'],
    ['linux', 'About'],
  ] as const)('About on %s reads "%s" and dispatches the about role', async (platform, name) => {
    const dispatch = installBridge(platform);
    await openHelpMenu();

    await userEvent.click(screen.getByRole('menuitem', { name }));

    expect(dispatch).toHaveBeenCalledWith({ kind: 'role', role: 'about' });
  });
});

describe('AppMenubar View navigation history', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  afterEach(() => {
    cleanup();
    (window as unknown as { okDesktop?: unknown }).okDesktop = undefined;
  });

  test('does not add a standalone Go menu on Windows/Linux', async () => {
    installBridge('linux');
    const { AppMenubar } = await import('./AppMenubar');
    render(<AppMenubar />);

    const labels = within(screen.getByTestId('app-menubar'))
      .getAllByRole('menuitem')
      .map((item) => item.textContent);
    expect(labels).toEqual(['File', 'Edit', 'View', 'Window', 'Help']);
  });

  test('starts View with Back then Forward and Alt-arrow shortcut hints', async () => {
    installBridge('win32');
    await openMenu('View');

    const rows = within(screen.getByRole('menu')).getAllByRole('menuitem');
    expect(rows.slice(0, 2).map((row) => row.textContent)).toEqual([
      'BackAlt+Left',
      'ForwardAlt+Right',
    ]);
  });

  test('dispatches each navigation-history action once', async () => {
    const dispatch = installBridge('linux');
    await openMenu('View');
    expect(dispatch).toHaveBeenCalledTimes(1);
    expect(dispatch).toHaveBeenCalledWith({ kind: 'query' });
    dispatch.mockClear();

    await userEvent.click(screen.getByRole('menuitem', { name: /Back/ }));
    expect(dispatch).toHaveBeenCalledTimes(1);
    expect(dispatch).toHaveBeenCalledWith({
      kind: 'menu-action',
      action: 'navigate-back',
    });
    dispatch.mockClear();

    await userEvent.click(screen.getByRole('menuitem', { name: 'View' }));
    expect(dispatch).toHaveBeenCalledTimes(1);
    expect(dispatch).toHaveBeenCalledWith({ kind: 'query' });
    dispatch.mockClear();

    await userEvent.click(screen.getByRole('menuitem', { name: /Forward/ }));
    expect(dispatch).toHaveBeenCalledTimes(1);
    expect(dispatch).toHaveBeenCalledWith({
      kind: 'menu-action',
      action: 'navigate-forward',
    });
  });
});

describe('AppMenubar View terminal toggle', () => {
  const snapshot = (terminalVisible: boolean) => ({
    recentProjects: [],
    spellCheckEnabled: true,
    showDevToolsMenu: false,
    canCheckForUpdates: false,
    canReconfigureMcpWiring: false,
    activeTarget: { kind: null },
    viewMenuState: {
      showHiddenFiles: false,
      showOkFolders: false,
      showOnlyMarkdownFiles: false,
      showSkillsSection: true,
      canExpandAll: true,
      canCollapseAll: true,
      sidebarVisible: true,
      docPanelVisible: true,
      terminalVisible,
    },
  });

  beforeEach(() => {
    vi.clearAllMocks();
  });

  afterEach(() => {
    cleanup();
    (window as unknown as { okDesktop?: unknown }).okDesktop = undefined;
  });

  test('Linux shows the terminal toggle and dispatches it through the shared menu action', async () => {
    const dispatch = installBridge('linux', 'editor', {
      ptyAvailable: true,
      queryResult: snapshot(false),
    });
    await openMenu('View');

    const item = await screen.findByRole('menuitem', { name: /Show Terminal/ });
    expect(item.textContent).toContain('Ctrl+J');
    dispatch.mockClear();

    await userEvent.click(item);
    expect(dispatch).toHaveBeenCalledWith({
      kind: 'menu-action',
      action: 'toggle-terminal',
    });
  });

  test('Linux labels a visible terminal with the inverse action', async () => {
    installBridge('linux', 'editor', {
      ptyAvailable: true,
      queryResult: snapshot(true),
    });
    await openMenu('View');

    expect(await screen.findByRole('menuitem', { name: /Hide Terminal/ })).not.toBeNull();
    expect(screen.queryByRole('menuitem', { name: /Show Terminal/ })).toBeNull();
  });

  test('a project window offers Hide Editor, then Show Editor once hidden, through the shared menu action', async () => {
    const dispatch = installBridge('linux', 'editor', { queryResult: snapshot(false) });
    await openMenu('View');

    const item = await screen.findByRole('menuitem', { name: 'Hide Editor' });
    dispatch.mockClear();
    await userEvent.click(item);
    expect(dispatch).toHaveBeenCalledWith({ kind: 'menu-action', action: 'toggle-editor' });

    cleanup();
    const hidden = snapshot(false);
    installBridge('linux', 'editor', {
      queryResult: { ...hidden, viewMenuState: { ...hidden.viewMenuState, editorVisible: false } },
    });
    await openMenu('View');
    expect(await screen.findByRole('menuitem', { name: 'Show Editor' })).not.toBeNull();
  });

  test('a single-file window offers no editor toggle', async () => {
    const dispatch = installBridge('win32', 'editor', { queryResult: snapshot(false) });
    const bridge = (window as unknown as { okDesktop: { config: Record<string, unknown> } })
      .okDesktop;
    bridge.config.singleFile = true;
    await openMenu('View');
    await screen.findByRole('menuitem', { name: /document panel/ });

    expect(screen.queryByRole('menuitem', { name: /Editor/ })).toBeNull();
    expect(dispatch).not.toHaveBeenCalledWith({ kind: 'menu-action', action: 'toggle-editor' });
  });

  test('Windows keeps the terminal toggle absent while PTYs are unavailable', async () => {
    installBridge('win32', 'editor', {
      ptyAvailable: false,
      queryResult: snapshot(false),
    });
    await openMenu('View');

    expect(screen.queryByRole('menuitem', { name: /Show Terminal/ })).toBeNull();
    expect(screen.queryByRole('menuitem', { name: /Hide Terminal/ })).toBeNull();
  });

  test('Windows exposes the terminal toggle when the build supports PTYs', async () => {
    installBridge('win32', 'editor', {
      ptyAvailable: true,
      queryResult: snapshot(false),
    });
    await openMenu('View');

    expect(await screen.findByRole('menuitem', { name: /Show Terminal/ })).not.toBeNull();
  });
});

describe('AppMenubar drag-suspension attributes', () => {
  afterEach(() => {
    cleanup();
    (window as unknown as { okDesktop?: unknown }).okDesktop = undefined;
  });

  test('open menu content matches the globals.css drag-suspension selector', async () => {
    installBridge('win32');
    await openMenu('View');

    const content = document.querySelector('[data-slot="menubar-content"]');
    expect(content).toBeTruthy();
    expect(content?.getAttribute('data-state')).toBe('open');
  });
});