Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

3.2 KB
Raw Blame
import { existsSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import { HARNESS_BOOT_TIMEOUT_MS } from './harness-boot-timeout';
import { createTestClient, createTestServer, type TestServer } from './test-harness';

let server: TestServer;
let tmpHome: string;
const base = () => `http://127.0.0.1:${server.port}`;

beforeAll(async () => {
  tmpHome = mkdtempSync(join(tmpdir(), 'ok-scope-roundtrip-home-'));
  mkdirSync(join(tmpHome, '.claude', 'skills'), { recursive: true });
  server = await createTestServer({ configHomedirOverride: tmpHome });
}, HARNESS_BOOT_TIMEOUT_MS);
afterAll(async () => {
  await server.cleanup();
  rmSync(tmpHome, { recursive: true, force: true });
});

describe('scope-move round-trip keeps the skill openable', () => {
  test('project -> global -> project: listed, readable, and doc servable at every hop', async () => {
    const name = 'roundtrip-skill';
    const marker = 'Roundtrip body marker';
    const put = await fetch(`${base()}/api/skill`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        scope: 'project',
        name,
        frontmatter: { name, description: 'Round-trip coverage.' },
        body: `# ${marker}\n`,
      }),
    });
    expect(put.status).toBe(200);
    const createdPath = ((await put.json()) as { path: string }).path;
    const projectDocName = createdPath.replace(/\.md$/, '');

    const move = async (fromScope: string, toScope: string) => {
      const res = await fetch(`${base()}/api/skill/move-scope`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ name, fromScope, toScope }),
      });
      expect(res.status, await res.clone().text()).toBe(200);
    };

    await move('project', 'global');
    const listedGlobal = (await (await fetch(`${base()}/api/skills`)).json()) as {
      skills: Array<{ name: string; scope: string }>;
    };
    expect(listedGlobal.skills.some((s) => s.name === name && s.scope === 'global')).toBe(true);
    expect(listedGlobal.skills.some((s) => s.name === name && s.scope === 'project')).toBe(false);

    await move('global', 'project');
    const listedBack = (await (await fetch(`${base()}/api/skills`)).json()) as {
      skills: Array<{ name: string; scope: string; path: string }>;
    };
    const backEntry = listedBack.skills.find((s) => s.name === name && s.scope === 'project');
    expect(backEntry).toBeDefined();
    expect(listedBack.skills.some((s) => s.name === name && s.scope === 'global')).toBe(false);

    const detail = await fetch(`${base()}/api/skill?name=${name}&scope=project`);
    expect(detail.status).toBe(200);

    expect(existsSync(join(server.contentDir, backEntry?.path ?? ''))).toBe(true);

    const backDocName = (backEntry?.path ?? '').replace(/\.md$/, '');
    expect(backDocName).toBe(projectDocName);
    const client = await createTestClient(server.port, backDocName);
    try {
      await expect
        .poll(() => client.ytext.toString(), { timeout: 15_000, interval: 250 })
        .toContain(marker);
    } finally {
      await client.cleanup();
    }
  }, 60_000);
});