Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
/**
* Per precedent #57, an agent-authored byte the human never touched must survive that settlement.
*/
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import * as Y from 'yjs';
import { HARNESS_BOOT_TIMEOUT_MS } from './harness-boot-timeout';
import {
agentWriteMd,
awaitDocQuiescence,
createTestClient,
createTestServer,
pollUntil,
type TestClient,
type TestServer,
} from './test-harness';
const NBSP = '\u00A0';
let server: TestServer;
beforeAll(async () => {
server = await createTestServer();
}, HARNESS_BOOT_TIMEOUT_MS);
afterAll(async () => {
await server.cleanup();
});
function findXmlTextContaining(
node: Y.XmlFragment | Y.XmlElement,
marker: string,
): Y.XmlText | null {
const len = node.length;
for (let i = 0; i < len; i++) {
const child = node.get(i);
if (child instanceof Y.XmlText) {
if (child.toString().includes(marker)) return child;
} else if (child instanceof Y.XmlElement) {
const found = findXmlTextContaining(child, marker);
if (found) return found;
}
}
return null;
}
async function seedThenMaybeEdit(opts: { body: string; editMarker?: string }): Promise<string> {
const docName = `nbsp-${crypto.randomUUID()}`;
const client: TestClient = await createTestClient(server.port, docName, {
skipInvariantWatcher: true,
});
try {
await agentWriteMd(server.port, opts.body, { docName, position: 'replace' });
await pollUntil(() => client.ytext.toString().includes('bar'), 5000);
await awaitDocQuiescence(client.doc);
expect(client.ytext.toString()).toContain(NBSP);
if (opts.editMarker !== undefined) {
const target = findXmlTextContaining(client.fragment, opts.editMarker);
if (!target) {
throw new Error(`edit marker not found in fragment: ${opts.editMarker}`);
}
target.insert(target.length, ' EDITWORD');
await pollUntil(() => client.ytext.toString().includes('EDITWORD'), 5000);
await awaitDocQuiescence(client.doc);
expect(client.ytext.toString()).toContain('EDITWORD');
}
return client.ytext.toString();
} finally {
await client.cleanup();
}
}
describe('document NBSP survives a WYSIWYG edit', () => {
test('document NBSP survives a WYSIWYG edit in the SAME block', async () => {
const ytext = await seedThenMaybeEdit({
body: `Alpha foo${NBSP}bar keepme\n`,
editMarker: 'keepme',
});
expect(ytext).toContain(NBSP);
}, 25_000);
test('document NBSP survives a WYSIWYG edit in a DIFFERENT block', async () => {
const ytext = await seedThenMaybeEdit({
body: `Kept foo${NBSP}bar paragraph.\n\nEditable paragraph editme.\n`,
editMarker: 'editme',
});
expect(ytext).toContain(NBSP);
}, 25_000);
test('document NBSP survives verbatim with no WYSIWYG edit (byte-sacred control)', async () => {
const ytext = await seedThenMaybeEdit({
body: `Alpha foo${NBSP}bar keepme\n`,
});
expect(ytext).toContain(NBSP);
}, 25_000);
});