Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

3.0 KB
Raw Blame
import { cleanup, render } from '@testing-library/react';
import type { Editor } from '@tiptap/core';
import type { ReactNode } from 'react';
import { afterEach, describe, expect, test, vi } from 'vitest';
import { TooltipProvider } from '@/components/ui/tooltip';
import { buildPagesByBasenameIndex, buildPagesBySlugIndex } from '../page-list-cache';
import { toWikiLinkSlug } from './wiki-link-helpers';

const PAGES = new Set(['index', 'notes/acp.daemon', 'notes/roadmap']);
const ASSET_PATHS = new Set(['files/meeting.pdf']);

vi.doMock('../link-preview/use-internal-doc-preview.ts', () => ({
  useInternalDocPreview: () => null,
}));

vi.doMock('../../components/InteractionPropPanel', () => ({
  InteractionPropPanel: ({ children }: { children: ReactNode }) => (
    <div data-testid="prop-panel">{children}</div>
  ),
}));

vi.doMock('../../components/PageListContext', () => ({
  usePageList: () => ({
    addPage: () => {},
    assetPaths: ASSET_PATHS,
    filePaths: new Set<string>(),
    folderPaths: new Set<string>(['notes', 'files']),
    pages: PAGES,
    pagesBySlug: buildPagesBySlugIndex(PAGES, toWikiLinkSlug),
    pagesByBasename: buildPagesByBasenameIndex(PAGES, toWikiLinkSlug),
    pageTitles: new Map<string, string>(),
    pageMeta: new Map(),
    loading: false,
    error: null,
    refetch: () => {},
  }),
}));

afterEach(cleanup);

function makeEditor(target: string): Editor {
  const node = { attrs: { target, alias: null, anchor: null }, nodeSize: 1 };
  return {
    state: { doc: { nodeAt: () => node } },
    view: { dom: document.createElement('div') },
  } as unknown as Editor;
}

async function renderPanel(target: string) {
  const { WikiLinkPropPanel } = await import('./WikiLinkPropPanel');
  const { container } = render(
    <TooltipProvider>
      <WikiLinkPropPanel
        editor={makeEditor(target)}
        getPos={() => 1}
        onClose={() => {}}
        onNavigate={() => true}
      />
    </TooltipProvider>,
  );
  const destination = container.querySelector('[data-slot="wiki-link-prop-panel-text"]');
  return {
    container,
    href: destination?.getAttribute('href') ?? null,
    ariaLabel: container.querySelector('[data-testid="prop-panel"]')?.getAttribute('aria-label'),
  };
}

describe('WikiLinkPropPanel destination', () => {
  test('a target naming a dotted-filename document points at that document', async () => {
    const { href } = await renderPanel('acp.daemon');

    expect(href).toBe('#/notes/acp.daemon');
  });

  test('a dot-free bare name still points at its subfolder document', async () => {
    const { href } = await renderPanel('roadmap');

    expect(href).toBe('#/notes/roadmap');
  });

  test('a target naming a real asset still points at the asset viewer', async () => {
    const { href } = await renderPanel('meeting.pdf');

    expect(href).toBe('#/__asset__/files/meeting.pdf');
  });

  test('a target naming nothing at all still points at the asset viewer', async () => {
    const { href } = await renderPanel('absent.pdf');

    expect(href).toBe('#/__asset__/absent.pdf');
  });
});