Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

3.4 KB
Raw Blame
/**
 * Marked inline leaf nodes survive the live server bridge. Losing the mark on the PM to
 * Y.XmlFragment hop breaks precedent #38: the fragment stops deriving from Y.Text, so the
 * write-back reports a bridge-invariant violation and the doc never converges.
 */
import { randomUUID } from 'node:crypto';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import { HARNESS_BOOT_TIMEOUT_MS } from './harness-boot-timeout';
import {
  agentWriteMd,
  assertBridgeInvariant,
  awaitDocQuiescence,
  createTestServer,
  getServerState,
  pollUntil,
  readTestDoc,
  type TestServer,
} from './test-harness';

let server: TestServer;
const warnLines: string[] = [];
const origWarn = console.warn;

beforeAll(async () => {
  console.warn = (...args: unknown[]) => {
    warnLines.push(args.map((a) => (typeof a === 'string' ? a : JSON.stringify(a))).join(' '));
    origWarn(...args);
  };
  server = await createTestServer();
}, HARNESS_BOOT_TIMEOUT_MS);

afterAll(async () => {
  console.warn = origWarn;
  await server.cleanup();
});

async function writeAndSettle(docName: string, markdown: string) {
  await agentWriteMd(server.port, markdown, { docName, position: 'replace' });

  const doc = server.instance.hocuspocus.documents.get(docName);
  if (!doc) throw new Error(`server never opened ${docName}`);
  await awaitDocQuiescence(doc, { timeoutMs: 5000 });
  await pollUntil(() => readTestDoc(server.contentDir, docName).length > 0);

  const state = getServerState(server, docName);
  if (!state) throw new Error(`server has no state for ${docName} after the flush`);
  return state;
}

function bridgeEventsFor(docName: string): string[] {
  return warnLines.filter((line) => {
    if (!line.startsWith('{')) return false;
    let parsed: unknown;
    try {
      parsed = JSON.parse(line);
    } catch {
      return false;
    }
    const event = parsed as { event?: unknown; 'doc.name'?: unknown };
    return (
      (event.event === 'bridge-invariant-violation' ||
        event.event === 'bridge-split-brain-rederive') &&
      event['doc.name'] === docName
    );
  });
}

describe('marked inline leaf nodes through the live bridge', () => {
  test('a strong-marked wikilink keeps its mark', async () => {
    const docName = `marked-inline-leaf-${randomUUID()}`;
    const state = await writeAndSettle(docName, '**[[a]]**\n');

    expect(state.ytext.toString()).toBe('**[[a]]**\n');
    expect(state.md).toBe('**[[a]]**\n');
    assertBridgeInvariant(state.ytext, state.fragment);
    expect(bridgeEventsFor(docName)).toEqual([]);
  }, 30000);

  test('a strong-marked inline JSX node keeps its mark', async () => {
    const docName = `marked-inline-leaf-${randomUUID()}`;
    const state = await writeAndSettle(docName, '**<Icon />**\n');

    expect(state.ytext.toString()).toBe('**<Icon />**\n');
    expect(state.md).toBe('**<Icon />**\n');
    assertBridgeInvariant(state.ytext, state.fragment);
    expect(bridgeEventsFor(docName)).toEqual([]);
  }, 30000);

  test('marked text and an unmarked wikilink are unaffected', async () => {
    const docName = `marked-inline-leaf-control-${randomUUID()}`;
    const state = await writeAndSettle(docName, '**bold text**\n\n[[a]]\n');

    expect(state.ytext.toString()).toBe('**bold text**\n\n[[a]]\n');
    expect(state.md).toBe('**bold text**\n\n[[a]]\n');
    assertBridgeInvariant(state.ytext, state.fragment);
    expect(bridgeEventsFor(docName)).toEqual([]);
  }, 30000);
});