Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

2.9 KB
Raw Blame
/**
 * Loaded-Y.Doc rewrite path through a real Hocuspocus server: a connected client's Y.Text mutates
 * from `[[old]]` to `[[new]]`, the disk file matches, and Y.Text and XmlFragment converge through
 * the server-authoritative Observer A path (precedent #14).
 */

import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
import { afterEach, describe, expect, test } from 'vitest';
import { createInspectableServer, createMultiClientContext, pollUntil } from './test-harness';

const cleanups: Array<() => Promise<void> | void> = [];

afterEach(async () => {
  while (cleanups.length > 0) {
    await cleanups.pop()?.();
  }
});

describe('Managed rename — loaded-Y.Doc rewrite path (QA-040 / QA-008)', () => {
  test('open editor on doc with [[old]] link → file rename → Y.Text observably becomes [[new]]', async () => {
    const server = await createInspectableServer();
    cleanups.push(() => server.shutdown());

    writeFileSync(join(server.contentDir, 'old.md'), '# Old doc\n', 'utf-8');
    writeFileSync(join(server.contentDir, 'host.md'), '# Host doc\n\nLink: [[old]]\n', 'utf-8');
    await wait(300);

    const ctx = await createMultiClientContext({
      server,
      docName: 'host',
      clientCount: 1,
    });
    cleanups.push(() => ctx.cleanup());

    await pollUntil(
      () =>
        ctx.pools[0]
          .getActive()
          ?.provider.document.getText('source')
          .toString()
          .includes('[[old]]') ?? false,
      8000,
      50,
    );

    const entry = ctx.pools[0].getActive();
    if (!entry) throw new Error('pool[0] has no active entry');
    const doc = entry.provider.document;
    const ytext = doc.getText('source');
    const fragment = doc.getXmlFragment('default');

    expect(ytext.toString()).toContain('[[old]]');
    expect(ytext.toString()).not.toContain('[[new]]');

    const res = await fetch(`http://127.0.0.1:${server.port}/api/rename-path`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ kind: 'file', fromPath: 'old', toPath: 'new' }),
    });
    expect(res.status).toBe(200);
    const body = (await res.json()) as {
      renamed: unknown[];
      rewrittenDocs: unknown[];
    };
    expect(body.renamed).toHaveLength(1);
    expect(body.rewrittenDocs.length).toBeGreaterThan(0);

    await pollUntil(() => ytext.toString().includes('[[new|old]]'), 5000, 25);
    expect(ytext.toString()).toContain('Link: [[new|old]]');
    expect(ytext.toString()).not.toContain('[[old]]');

    await wait(800);
    const hostDisk = readFileSync(join(server.contentDir, 'host.md'), 'utf-8');
    expect(hostDisk).toBe('# Host doc\n\nLink: [[new|old]]\n');

    const fragmentText = fragment.toString();
    expect(fragmentText).toContain('new');
    expect(fragmentText).not.toContain('[[old]]');
  }, 30_000);
});