Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

7.8 KB
Raw Blame
import { describe, expect, test } from 'vitest';
import {
  buildSendToAiInputForActiveTarget,
  resolveActiveTargetAbsPath,
  resolveActiveTargetRelativePath,
} from './file-menu-target-resolvers';
import type { Workspace } from './workspace-paths';

const WORKSPACE: Workspace = {
  contentDir: '/Users/test/project',
  pathSeparator: '/',
};

describe('resolveActiveTargetAbsPath', () => {
  test('doc scope joins contentDir + markdown relative path', () => {
    expect(
      resolveActiveTargetAbsPath(
        { kind: 'doc', target: 'specs/foo', docName: 'specs/foo' },
        WORKSPACE,
      ),
    ).toBe('/Users/test/project/specs/foo.md');
  });

  test('folder-index scope routes through the doc path (folder note is a doc)', () => {
    expect(
      resolveActiveTargetAbsPath(
        {
          kind: 'folder-index',
          target: 'specs',
          folderPath: 'specs',
          docName: 'specs/index',
          noteKind: 'canonical-index',
        },
        WORKSPACE,
      ),
    ).toBe('/Users/test/project/specs/index.md');
  });

  test('extension-qualified doc scope uses active target docName', () => {
    expect(
      resolveActiveTargetAbsPath(
        { kind: 'doc', target: 'specs/foo.mdx', docName: 'specs/foo.mdx' },
        WORKSPACE,
      ),
    ).toBe('/Users/test/project/specs/foo.mdx');
  });

  test('folder scope joins contentDir + folderPath (no trailing slash)', () => {
    expect(
      resolveActiveTargetAbsPath(
        { kind: 'folder', target: 'reports', folderPath: 'reports' },
        WORKSPACE,
      ),
    ).toBe('/Users/test/project/reports');
  });

  test('null scope (project) returns contentDir verbatim', () => {
    expect(resolveActiveTargetAbsPath(null, WORKSPACE)).toBe('/Users/test/project');
  });

  test('asset scope joins contentDir + assetPath', () => {
    expect(
      resolveActiveTargetAbsPath(
        { kind: 'asset', target: 'media/foo.png', assetPath: 'media/foo.png', mediaKind: null },
        WORKSPACE,
      ),
    ).toBe('/Users/test/project/media/foo.png');
  });

  test('missing scope falls back to contentDir', () => {
    expect(resolveActiveTargetAbsPath({ kind: 'missing', target: 'gone' }, WORKSPACE)).toBe(
      '/Users/test/project',
    );
  });
});

describe('resolveActiveTargetRelativePath', () => {
  test('doc scope returns markdown relative path', () => {
    expect(
      resolveActiveTargetRelativePath({ kind: 'doc', target: 'specs/foo', docName: 'specs/foo' }),
    ).toBe('specs/foo.md');
  });

  test('folder-index scope returns the doc-relative path', () => {
    expect(
      resolveActiveTargetRelativePath({
        kind: 'folder-index',
        target: 'specs',
        folderPath: 'specs',
        docName: 'specs/index',
        noteKind: 'canonical-index',
      }),
    ).toBe('specs/index.md');
  });

  test('extension-qualified doc scope returns active target relative path', () => {
    expect(
      resolveActiveTargetRelativePath({
        kind: 'doc',
        target: 'specs/foo.mdx',
        docName: 'specs/foo.mdx',
      }),
    ).toBe('specs/foo.mdx');
  });

  test('folder scope returns the folder path with no trailing slash', () => {
    expect(
      resolveActiveTargetRelativePath({ kind: 'folder', target: 'reports', folderPath: 'reports' }),
    ).toBe('reports');
  });

  test('asset scope returns the asset-relative path', () => {
    expect(
      resolveActiveTargetRelativePath({
        kind: 'asset',
        target: 'x.png',
        assetPath: 'x.png',
        mediaKind: null,
      }),
    ).toBe('x.png');
  });

  test('null / missing scopes return empty string (contentDir convention)', () => {
    expect(resolveActiveTargetRelativePath(null)).toBe('');
    expect(resolveActiveTargetRelativePath({ kind: 'missing', target: 'gone' })).toBe('');
  });
});

describe('buildSendToAiInputForActiveTarget', () => {
  test('null scope (project) returns project-scoped handoff input', () => {
    const input = buildSendToAiInputForActiveTarget(null, WORKSPACE);
    expect(input).not.toBeNull();
    expect(input?.docContext).toBeNull();
    expect(input?.projectDir).toBe('/Users/test/project');
    expect(input?.docPath).toBe('');
  });

  test('folder scope returns folder-scoped handoff input with contentDir projectDir + relative path', () => {
    const input = buildSendToAiInputForActiveTarget(
      { kind: 'folder', target: 'reports', folderPath: 'reports' },
      WORKSPACE,
    );
    expect(input).not.toBeNull();
    expect(input?.docContext).toBeNull();
    expect(input?.folderRelativePath).toBe('reports');
    expect(input?.projectDir).toBe('/Users/test/project');
  });

  test('doc + folder-index scopes return file-scoped handoff input', () => {
    const docInput = buildSendToAiInputForActiveTarget(
      { kind: 'doc', target: 'specs/foo', docName: 'specs/foo' },
      WORKSPACE,
    );
    expect(docInput?.docContext?.relativePath).toBe('specs/foo.md');
    expect(docInput?.docPath).toBe('/Users/test/project/specs/foo.md');

    const folderIndexInput = buildSendToAiInputForActiveTarget(
      {
        kind: 'folder-index',
        target: 'specs',
        folderPath: 'specs',
        docName: 'specs/index',
        noteKind: 'canonical-index',
      },
      WORKSPACE,
    );
    expect(folderIndexInput?.docContext?.relativePath).toBe('specs/index.md');
  });

  test('extension-qualified doc scope keeps the selected file extension', () => {
    const docInput = buildSendToAiInputForActiveTarget(
      { kind: 'doc', target: 'specs/foo.mdx', docName: 'specs/foo.mdx' },
      WORKSPACE,
    );
    expect(docInput?.docContext?.relativePath).toBe('specs/foo.mdx');
    expect(docInput?.docPath).toBe('/Users/test/project/specs/foo.mdx');
  });

  test('asset / missing scopes return null (no meaningful dispatch)', () => {
    expect(
      buildSendToAiInputForActiveTarget(
        { kind: 'asset', target: 'x.png', assetPath: 'x.png', mediaKind: null },
        WORKSPACE,
      ),
    ).toBeNull();
    expect(
      buildSendToAiInputForActiveTarget({ kind: 'missing', target: 'gone' }, WORKSPACE),
    ).toBeNull();
  });

  test('returns null when workspace is unresolved on doc / folder scopes', () => {
    expect(
      buildSendToAiInputForActiveTarget({ kind: 'doc', target: 'foo', docName: 'foo' }, null),
    ).toBeNull();
    expect(
      buildSendToAiInputForActiveTarget({ kind: 'folder', target: 'foo', folderPath: 'foo' }, null),
    ).toBeNull();
  });
});

describe('buildSendToAiInputForActiveTarget — skill targets', () => {
  const workspace = { contentDir: '/proj', workspaceRoot: '/proj' } as never;

  test('a skill preview hands off the SKILL, not nothing', () => {
    const input = buildSendToAiInputForActiveTarget(
      {
        kind: 'skill-preview',
        target: 'linked//abs/dir/write-skill',
        flavor: 'linked',
        source: '/abs/dir/write-skill',
        name: 'write-skill',
        subtitle: '',
        level: 'project',
      } as never,
      workspace,
    );
    expect(input?.skill).toEqual({ name: 'write-skill', scope: 'project' });
  });

  test('a skill-file target hands off its skill at the file tab scope', () => {
    const input = buildSendToAiInputForActiveTarget(
      {
        kind: 'skill-file',
        target: 'global/grill-me/references/x.md',
        scope: 'global',
        name: 'grill-me',
        path: 'references/x.md',
      } as never,
      workspace,
    );
    expect(input?.skill).toEqual({ name: 'grill-me', scope: 'global' });
  });
});

test('a skill preview without a level falls back to project scope', () => {
  const input = buildSendToAiInputForActiveTarget(
    {
      kind: 'skill-preview',
      target: 'detected//x/ai-sdk',
      flavor: 'detected',
      source: '/x',
      name: 'ai-sdk',
      subtitle: '',
    } as never,
    { contentDir: '/proj', workspaceRoot: '/proj' } as never,
  );
  expect(input?.skill).toEqual({ name: 'ai-sdk', scope: 'project' });
});