Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(open-knowledge): file tree sidebar scrolls to the last item (#2091)

Andrew Mikofalvya33dc8fa00df
diff --git a/.changeset/file-tree-scroll-to-bottom.md b/.changeset/file-tree-scroll-to-bottom.md
new file mode 100644
index 000000000..066387eec
--- /dev/null
+++ b/.changeset/file-tree-scroll-to-bottom.md
@@ -0,0 +1,5 @@
+---
+'@inkeep/open-knowledge-app': patch
+---
+
+Fix the file-tree sidebar not scrolling all the way to the bottom. The virtualized tree positioned rows on a 24px grid while they rendered at 26px, so the accumulated per-row gap was subtracted from the scroll range and the last rows became unreachable (worse the taller or more deeply nested the tree). The virtualizer row height is now derived from a single source of truth, so it can no longer drift from the rendered row height.
diff --git a/packages/app/src/components/file-tree-density.test.ts b/packages/app/src/components/file-tree-density.test.ts
index 2ed319eb4..cc804190c 100644
--- a/packages/app/src/components/file-tree-density.test.ts
+++ b/packages/app/src/components/file-tree-density.test.ts
@@ -42,6 +42,13 @@ describe('FileTree density configuration', () => {
expect(px).toBeLessThanOrEqual(26);
});
+ test('rendered --trees-item-height is derived from the virtualizer itemHeight (one source of truth, cannot drift)', () => {
+ for (const theme of ['light', 'dark'] as const) {
+ const style = createFileTreeStyle(theme) as Record<string, string>;
+ expect(style['--trees-item-height']).toBe(`${FILE_TREE_DENSITY_OPTIONS.itemHeight}px`);
+ }
+ });
+
test('preserves existing typography + padding overrides alongside density', () => {
const style = createFileTreeStyle('light') as Record<string, string | number>;
expect(style['--trees-font-size-override']).toBe('0.875rem');
diff --git a/packages/app/src/components/file-tree-density.ts b/packages/app/src/components/file-tree-density.ts
index 839be1e5f..173a546ca 100644
--- a/packages/app/src/components/file-tree-density.ts
+++ b/packages/app/src/components/file-tree-density.ts
@@ -3,6 +3,7 @@ import type { CSSProperties } from 'react';
export const FILE_TREE_DENSITY_OPTIONS = {
density: 'compact',
+ itemHeight: 26,
flattenEmptyDirectories: false,
} as const;
@@ -10,7 +11,7 @@ const FILE_TREE_DENSITY_STYLE = {
'--trees-level-gap-override': '4px',
'--trees-item-row-gap-override': '4px',
'--trees-icon-width-override': '14px',
- '--trees-item-height': '26px',
+ '--trees-item-height': `${FILE_TREE_DENSITY_OPTIONS.itemHeight}px`,
'--trees-indent-guide-bg-override': 'color-mix(in oklab, var(--trees-fg-muted) 30%, transparent)',
} as const;
diff --git a/packages/core/src/bridge/apply-diff.test.ts b/packages/core/src/bridge/apply-diff.test.ts
deleted file mode 100644
index 0a6e423c8..000000000
--- a/packages/core/src/bridge/apply-diff.test.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import * as Y from 'yjs';
-import { applyFastDiff } from './apply-diff.ts';
-
-function setup(initial: string): Y.Text {
- const doc = new Y.Doc();
- const ytext = doc.getText('test');
- if (initial) ytext.insert(0, initial);
- return ytext;
-}
-
-describe('applyFastDiff', () => {
- test('identity — no change when currentText === newText', () => {
- const ytext = setup('hello world');
- applyFastDiff(ytext, 'hello world', 'hello world');
- expect(ytext.toString()).toBe('hello world');
- });
-
- test('small middle replacement — outer prefix/suffix preserved', () => {
- const ytext = setup('hello cruel world');
- applyFastDiff(ytext, 'hello cruel world', 'hello kind world');
- expect(ytext.toString()).toBe('hello kind world');
- });
-
- test('pure append', () => {
- const ytext = setup('hello');
- applyFastDiff(ytext, 'hello', 'hello world');
- expect(ytext.toString()).toBe('hello world');
- });
-
- test('pure prepend', () => {
- const ytext = setup('world');
- applyFastDiff(ytext, 'world', 'hello world');
- expect(ytext.toString()).toBe('hello world');
- });
-
- test('empty to nonempty', () => {
- const ytext = setup('');
- applyFastDiff(ytext, '', 'hello');
- expect(ytext.toString()).toBe('hello');
- });
-
- test('nonempty to empty', () => {
- const ytext = setup('hello');
- applyFastDiff(ytext, 'hello', '');
- expect(ytext.toString()).toBe('');
- });
-
- describe('CPU DoS defense — large input fallback', () => {
- const LARGE_BYTES = 300 * 1024;
-
- test('large currentText → fallback still produces correct result', () => {
- const filler = 'a'.repeat(LARGE_BYTES);
- const current = `${filler}MIDDLE${filler}`;
- const next = `${filler}REPLACED${filler}`;
- const ytext = setup(current);
- const start = Date.now();
- applyFastDiff(ytext, current, next);
- const elapsed = Date.now() - start;
- expect(ytext.toString()).toBe(next);
- expect(elapsed).toBeLessThan(1000);
- });
-
- test('large newText → fallback still produces correct result', () => {
- const filler = 'b'.repeat(LARGE_BYTES);
- const current = 'small original';
- const next = `${filler}small original${filler}`;
- const ytext = setup(current);
- applyFastDiff(ytext, current, next);
- expect(ytext.toString()).toBe(next);
- });
-
- test('full wholesale replace at large size — no common prefix/suffix', () => {
- const current = 'a'.repeat(LARGE_BYTES);
- const next = 'b'.repeat(LARGE_BYTES);
- const ytext = setup(current);
- applyFastDiff(ytext, current, next);
- expect(ytext.toString()).toBe(next);
- });
-
- test('large input with full common prefix and suffix collapses to no-op middle', () => {
- const filler = 'c'.repeat(LARGE_BYTES);
- const ytext = setup(filler);
- applyFastDiff(ytext, filler, filler);
- expect(ytext.toString()).toBe(filler);
- });
-
- test('adversarial random middle stays bounded — no CPU pin', () => {
- const ONE_MIB = 1024 * 1024;
- let current = '';
- let next = '';
- for (let i = 0; i < ONE_MIB; i++) {
- current += String.fromCharCode(65 + (i % 26));
- next += String.fromCharCode(97 + ((i * 7) % 26));
- }
- const ytext = setup(current);
- const start = Date.now();
- applyFastDiff(ytext, current, next);
- const elapsed = Date.now() - start;
- expect(ytext.toString()).toBe(next);
- expect(elapsed).toBeLessThan(2000);
- });
- });
-});
diff --git a/packages/core/src/bridge/bind-frontmatter-doc.test.ts b/packages/core/src/bridge/bind-frontmatter-doc.test.ts
deleted file mode 100644
index 65a220e4d..000000000
--- a/packages/core/src/bridge/bind-frontmatter-doc.test.ts
+++ /dev/null
@@ -1,1381 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import * as Y from 'yjs';
-import type { FrontmatterDocProvider, FrontmatterSnapshot } from './bind-frontmatter-doc.ts';
-import { bindFrontmatterDoc, FORM_WRITE_ORIGIN, touchesFmRegion } from './bind-frontmatter-doc.ts';
-import { detectFmRegion, MAX_FM_REGION_BYTES, readFmMap } from './frontmatter-region.ts';
-
-function makeProvider(initial = ''): FrontmatterDocProvider & {
- emitSynced: () => void;
-} {
- const document = new Y.Doc();
- if (initial) {
- document.getText('source').insert(0, initial);
- }
- const handlers = new Set<() => void>();
- return {
- document,
- on(event, listener) {
- if (event === 'synced') handlers.add(listener);
- },
- off(event, listener) {
- if (event === 'synced') handlers.delete(listener);
- },
- emitSynced() {
- for (const h of handlers) h();
- },
- };
-}
-
-function readYTextFm(provider: FrontmatterDocProvider): string {
- return detectFmRegion(provider.document.getText('source').toString()).fenced;
-}
-
-describe('bindFrontmatterDoc — patch()', () => {
- test('valid patch writes the YAML region and reports applied keys', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patch({ title: 'Hello', count: 3, draft: true });
-
- expect(result.ok).toBe(true);
- if (result.ok) {
- expect(result.appliedKeys.sort()).toEqual(['count', 'draft', 'title']);
- }
- const map = readFmMap(provider.document.getText('source').toString());
- expect(map).toEqual({ title: 'Hello', count: 3, draft: true });
- });
-
- test('null value deletes the key', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', tag: 'a' });
-
- const result = binding.patch({ tag: null });
-
- expect(result.ok).toBe(true);
- expect(readFmMap(provider.document.getText('source').toString())).toEqual({ title: 'Hello' });
- });
-
- test('deleting every key removes the FM fences', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', status: 'draft' });
- expect(readYTextFm(provider)).not.toBe('');
-
- const result = binding.patch({ title: null, status: null });
-
- expect(result.ok).toBe(true);
- expect(provider.document.getText('source').toString()).toBe('');
- expect(readYTextFm(provider)).toBe('');
- expect(binding.current().map).toEqual({});
- expect(binding.current().keys).toEqual([]);
- });
-
- test('updating an existing key does NOT reorder properties (FR2)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', cluster: 'X', tags: ['a'] });
-
- binding.patch({ title: 'Goodbye' });
-
- expect(binding.current().keys).toEqual(['title', 'cluster', 'tags']);
- });
-
- test('new keys append at the end (D15 / FR8)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello' });
- binding.patch({ status: 'draft' });
-
- expect(binding.current().keys).toEqual(['title', 'status']);
- });
-
- test('invalid value returns SCHEMA_INVALID without mutating Y.Text', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello' });
-
- const result = binding.patch({ count: Symbol('nope') as unknown as number });
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- }
- expect(readFmMap(provider.document.getText('source').toString())).toEqual({ title: 'Hello' });
- });
-
- test('nested object value is accepted and round-trips through Y.Text', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patch({
- name: 'skill',
- metadata: { version: '1.0.0', author: 'Inkeep' },
- });
-
- expect(result.ok).toBe(true);
- const map = readFmMap(provider.document.getText('source').toString());
- expect(map).toEqual({
- name: 'skill',
- metadata: { version: '1.0.0', author: 'Inkeep' },
- });
- });
-
- test('reserved key "frontmatter" is rejected', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patch({ frontmatter: 'bypass attempt' });
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.path).toEqual(['frontmatter']);
- expect(result.error.issues[0]?.issueCode).toBe('reserved_key');
- }
- }
- expect(readYTextFm(provider)).toBe('');
- });
-
- test('writes are stamped with FORM_WRITE_ORIGIN', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- let observedOrigin: unknown = null;
- provider.document.getText('source').observe((_event, transaction) => {
- observedOrigin = transaction.origin;
- });
-
- binding.patch({ title: 'Hello' });
-
- expect(observedOrigin).toBe(FORM_WRITE_ORIGIN);
- });
-
- test('region exceeding MAX_FM_REGION_BYTES is refused', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const huge = 'x'.repeat(MAX_FM_REGION_BYTES + 1);
- const result = binding.patch({ pad: huge });
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.issueCode).toBe('region_too_large');
- }
- }
- });
-
- test('disposed binding rejects further patches', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.dispose();
-
- const result = binding.patch({ title: 'Hello' });
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('WRITE_ERROR');
- }
- expect(readYTextFm(provider)).toBe('');
- });
-});
-
-describe('bindFrontmatterDoc — rename()', () => {
- test('renames in place, preserving order (FR2)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', cluster: 'X', tags: ['a'] });
-
- const result = binding.rename('cluster', 'group');
-
- expect(result.ok).toBe(true);
- expect(binding.current().keys).toEqual(['title', 'group', 'tags']);
- expect(binding.current().map).toEqual({ title: 'Hello', group: 'X', tags: ['a'] });
- });
-
- test('refuses unknown source key', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello' });
-
- const result = binding.rename('cluster', 'group');
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- }
- });
-
- test('refuses target collision when allowDuplicate is false', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', status: 'draft' });
-
- const result = binding.rename('status', 'title');
-
- expect(result.ok).toBe(false);
- });
-
- test('admits duplicate target when allowDuplicate is true (D17/D18)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', status: 'draft' });
-
- const result = binding.rename('status', 'title', { allowDuplicate: true });
-
- expect(result.ok).toBe(true);
- expect(binding.current().keys).toEqual(['title', 'title']);
- });
-});
-
-describe('bindFrontmatterDoc — reorder()', () => {
- test('reorders to the requested permutation (FR4)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ a: 1, b: 2, c: 3 });
-
- const result = binding.reorder(['c', 'a', 'b']);
-
- expect(result.ok).toBe(true);
- expect(binding.current().keys).toEqual(['c', 'a', 'b']);
- expect(binding.current().map).toEqual({ a: 1, b: 2, c: 3 });
- });
-
- test('refuses non-permutation list', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ a: 1, b: 2 });
-
- const result = binding.reorder(['a', 'b', 'c']);
-
- expect(result.ok).toBe(false);
- });
-});
-
-describe('bindFrontmatterDoc — patchPath() / deletePath() (Q-T9 local addressing)', () => {
- test('patchPath sets a nested leaf, preserving sibling top-level keys', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({
- name: 'skill',
- description: 'd',
- metadata: { version: '1.0', author: 'Inkeep' },
- });
-
- const result = binding.patchPath(['metadata', 'version'], '2.0');
-
- expect(result.ok).toBe(true);
- if (result.ok) {
- expect(result.path).toEqual(['metadata', 'version']);
- }
- expect(binding.current().map).toEqual({
- name: 'skill',
- description: 'd',
- metadata: { version: '2.0', author: 'Inkeep' },
- });
- });
-
- test('patchPath preserves sibling nested keys at the same depth (single-leaf set)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({
- metadata: { version: '1.0', author: 'A', license: 'MIT' },
- });
-
- const result = binding.patchPath(['metadata', 'version'], '2.0');
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({
- metadata: { version: '2.0', author: 'A', license: 'MIT' },
- });
- });
-
- test('patchPath creates missing intermediate maps', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ name: 'skill' });
-
- const result = binding.patchPath(['metadata', 'version'], '1.0');
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({
- name: 'skill',
- metadata: { version: '1.0' },
- });
- });
-
- test('patchPath at depth >=3 creates parent chain', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patchPath(['a', 'b', 'c', 'd'], 'leaf');
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({
- a: { b: { c: { d: 'leaf' } } },
- });
- });
-
- test('patchPath single-element path reduces to a top-level set (append on new key)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello' });
-
- const result = binding.patchPath(['cluster'], 'X');
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ title: 'Hello', cluster: 'X' });
- expect(binding.current().keys).toEqual(['title', 'cluster']);
- });
-
- test('patchPath single-element path updates in place (no reorder on update)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', cluster: 'X' });
-
- binding.patchPath(['title'], 'World');
-
- expect(binding.current().keys).toEqual(['title', 'cluster']);
- expect(binding.current().map.title).toBe('World');
- });
-
- test('patchPath sets an array leaf value', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ name: 'doc' });
-
- const result = binding.patchPath(['tags'], ['alpha', 'beta']);
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ name: 'doc', tags: ['alpha', 'beta'] });
- });
-
- test('patchPath sets nested object leaf value (whole-subtree replace at leaf)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ metadata: { version: '1.0', author: 'A' } });
-
- const result = binding.patchPath(['metadata'], { version: '2.0' });
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ metadata: { version: '2.0' } });
- });
-
- test('patchPath into an existing seq item by index', () => {
- const provider = makeProvider('---\nauthors:\n - name: A\n - name: B\n---\n');
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patchPath(['authors', 0, 'role'], 'lead');
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({
- authors: [{ name: 'A', role: 'lead' }, { name: 'B' }],
- });
- });
-
- test('patchPath through a scalar intermediate returns parse_failed (WRITE_ERROR)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ a: 'string-value' });
-
- const result = binding.patchPath(['a', 'b'], 'x');
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('WRITE_ERROR');
- }
- expect(binding.current().map).toEqual({ a: 'string-value' });
- });
-
- test('patchPath empty path is rejected with SCHEMA_INVALID / invalid_path', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patchPath([], 'x');
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.issueCode).toBe('invalid_path');
- }
- }
- });
-
- test('patchPath rejects reserved top-level key "frontmatter"', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patchPath(['frontmatter', 'nested'], 'x');
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.issueCode).toBe('reserved_key');
- }
- }
- });
-
- test('patchPath admits nested "frontmatter" key (only top-level slot is reserved)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patchPath(['metadata', 'frontmatter'], 'allowed');
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ metadata: { frontmatter: 'allowed' } });
- });
-
- test('patchPath invalid leaf value (Symbol) returns SCHEMA_INVALID without mutating Y.Text', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ metadata: { version: '1.0' } });
-
- const result = binding.patchPath(['metadata', 'count'], Symbol('nope') as unknown as number);
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- }
- expect(binding.current().map).toEqual({ metadata: { version: '1.0' } });
- });
-
- test('patchPath stamps writes with FORM_WRITE_ORIGIN', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- let observedOrigin: unknown = null;
- provider.document.getText('source').observe((_event, transaction) => {
- observedOrigin = transaction.origin;
- });
-
- binding.patchPath(['metadata', 'version'], '1.0');
-
- expect(observedOrigin).toBe(FORM_WRITE_ORIGIN);
- });
-
- test('patchPath preserves flow style on nested map subtree replace', () => {
- const provider = makeProvider('---\nmetadata: {version: 1.0, author: A}\n---\n');
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patchPath(['metadata'], { version: '2.0', author: 'B' });
-
- expect(result.ok).toBe(true);
- const fenced = readYTextFm(provider);
- expect(fenced).toContain('{ version: "2.0"');
- expect(fenced).toContain('author: B }');
- });
-
- test('patchPath preserves sibling comments inside the nested map (single-leaf set)', () => {
- const provider = makeProvider(
- '---\nname: hello\nmetadata:\n # version comment\n version: 1.0\n author: A\n---\n',
- );
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.patchPath(['metadata', 'author'], 'B');
-
- expect(result.ok).toBe(true);
- const fenced = readYTextFm(provider);
- expect(fenced).toContain('# version comment');
- expect(fenced).toContain('version: 1.0');
- expect(fenced).toContain('author: B');
- });
-
- test('patchPath disposed binding returns WRITE_ERROR', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.dispose();
-
- const result = binding.patchPath(['metadata', 'version'], '1.0');
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('WRITE_ERROR');
- }
- });
-
- test('deletePath removes a single nested leaf, leaves sibling siblings + parent map', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({
- name: 'skill',
- metadata: { version: '1.0', author: 'A', license: 'MIT' },
- });
-
- const result = binding.deletePath(['metadata', 'author']);
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({
- name: 'skill',
- metadata: { version: '1.0', license: 'MIT' },
- });
- });
-
- test('deletePath does NOT prune emptied parent maps (documented rule)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ metadata: { version: '1.0' } });
-
- const result = binding.deletePath(['metadata', 'version']);
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ metadata: {} });
- });
-
- test('deletePath single-element path removes a top-level key', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', cluster: 'X' });
-
- const result = binding.deletePath(['cluster']);
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ title: 'Hello' });
- expect(binding.current().keys).toEqual(['title']);
- });
-
- test('deletePath on absent leaf is an idempotent no-op (Result.ok)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ metadata: { version: '1.0' } });
-
- const result = binding.deletePath(['metadata', 'absent']);
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ metadata: { version: '1.0' } });
- });
-
- test('deletePath through a scalar intermediate is an idempotent no-op (getIn returns undefined)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ a: 'string-value' });
-
- const result = binding.deletePath(['a', 'b']);
-
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ a: 'string-value' });
- });
-
- test('deletePath empty path is rejected with SCHEMA_INVALID', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.deletePath([]);
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- }
- });
-
- test('deletePath rejects reserved top-level key "frontmatter"', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- const result = binding.deletePath(['frontmatter']);
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.issueCode).toBe('reserved_key');
- }
- }
- });
-
- test('deletePath stamps writes with FORM_WRITE_ORIGIN', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ metadata: { version: '1.0' } });
-
- let observedOrigin: unknown = null;
- provider.document.getText('source').observe((_event, transaction) => {
- observedOrigin = transaction.origin;
- });
-
- binding.deletePath(['metadata', 'version']);
-
- expect(observedOrigin).toBe(FORM_WRITE_ORIGIN);
- });
-
- test('deletePath disposed binding returns WRITE_ERROR', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.dispose();
-
- const result = binding.deletePath(['metadata', 'version']);
-
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.code).toBe('WRITE_ERROR');
- }
- });
-
- test('patchPath then deletePath round-trip leaves an empty parent map (not pruned)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
-
- binding.patchPath(['metadata', 'version'], '1.0');
- expect(binding.current().map).toEqual({ metadata: { version: '1.0' } });
-
- binding.deletePath(['metadata', 'version']);
- expect(binding.current().map).toEqual({ metadata: {} });
-
- binding.deletePath(['metadata']);
- expect(binding.current().map).toEqual({});
- });
-
- test('patchPath updates do NOT touch siblings or trailing comments (depth-2 leaf set)', () => {
- const provider = makeProvider(
- '---\nname: hello\nmetadata:\n version: 1.0\n author: A\n# trailing\n---\nbody\n',
- );
- const binding = bindFrontmatterDoc(provider);
-
- binding.patchPath(['metadata', 'version'], '2.0');
-
- const fenced = readYTextFm(provider);
- expect(fenced).toContain('# trailing');
- expect(fenced).toContain('author: A');
- expect(fenced).toContain('version: "2.0"');
- expect(fenced).toContain('name: hello');
- });
-});
-
-describe('bindFrontmatterDoc — current()', () => {
- test('returns empty snapshot when there is no FM region', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- expect(binding.current()).toEqual({ map: {}, keys: [], parseError: undefined });
- });
-
- test('reflects the YAML region after writes', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ title: 'Hello', tags: ['a'] });
- expect(binding.current().map).toEqual({ title: 'Hello', tags: ['a'] });
- });
-
- test('surfaces parseError when YAML region is malformed', () => {
- const provider = makeProvider('---\n: : : invalid\n---\nbody\n');
- const binding = bindFrontmatterDoc(provider);
- const snapshot = binding.current();
- expect(snapshot.parseError).toBeDefined();
- });
-
- test('surfaces no parseError when frontmatter has a mixed-scalar array', () => {
- const fixture =
- '---\n' +
- 'title: Sample\n' +
- 'tags: [travel, spain, barcelona, mallorca, palma, balearics, sailing, 2026]\n' +
- '---\nbody\n';
- const provider = makeProvider(fixture);
- const binding = bindFrontmatterDoc(provider);
- const snapshot = binding.current();
- expect(snapshot.parseError).toBeUndefined();
- expect(snapshot.map.tags).toEqual([
- 'travel',
- 'spain',
- 'barcelona',
- 'mallorca',
- 'palma',
- 'balearics',
- 'sailing',
- '2026',
- ]);
- });
-});
-
-describe('bindFrontmatterDoc — subscribe()', () => {
- test('listener fires on patch', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- const calls: FrontmatterSnapshot[] = [];
- binding.subscribe((snapshot) => {
- calls.push(snapshot);
- });
-
- binding.patch({ title: 'Hello' });
-
- expect(calls.length).toBeGreaterThan(0);
- expect(calls.at(-1)?.map).toEqual({ title: 'Hello' });
- });
-
- test('content-equality bailout: body-only edits do not fire (D20)', () => {
- const provider = makeProvider('---\ntitle: Hello\n---\nbody\n');
- const binding = bindFrontmatterDoc(provider);
- let calls = 0;
- binding.subscribe(() => {
- calls += 1;
- });
- const callsBefore = calls;
-
- const ytext = provider.document.getText('source');
- ytext.insert(ytext.length, '\nmore body\n');
-
- expect(calls).toBe(callsBefore);
- });
-
- test('content-equality bailout: body-only edits do not fire with NESTED frontmatter', () => {
- const provider = makeProvider(
- '---\nname: skill\nmetadata:\n version: 1.0.0\n author: Inkeep\n---\nbody\n',
- );
- const binding = bindFrontmatterDoc(provider);
- let calls = 0;
- binding.subscribe(() => {
- calls += 1;
- });
- const callsBefore = calls;
-
- const ytext = provider.document.getText('source');
- ytext.insert(ytext.length, '\nmore body\n');
-
- expect(calls).toBe(callsBefore);
- });
-
- test('listener fires on provider synced', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- let calls = 0;
- binding.subscribe(() => {
- calls += 1;
- });
-
- provider.emitSynced();
-
- expect(calls).toBe(1);
- });
-
- test('unsubscribe stops further fires', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- let calls = 0;
- const unsub = binding.subscribe(() => {
- calls += 1;
- });
-
- binding.patch({ title: 'A' });
- const before = calls;
- unsub();
- binding.patch({ title: 'B' });
-
- expect(calls).toBe(before);
- });
-});
-
-describe('bindFrontmatterDoc — dispose()', () => {
- test('removes Y.Text observer + provider listener', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- let calls = 0;
- binding.subscribe(() => {
- calls += 1;
- });
- binding.dispose();
-
- binding.patch({ title: 'after-dispose' });
- provider.emitSynced();
-
- expect(calls).toBe(0);
- });
-
- test('idempotent', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.dispose();
- expect(() => binding.dispose()).not.toThrow();
- });
-});
-
-describe('touchesFmRegion — pure delta inspector', () => {
- const event = (delta: Array<{ retain?: number; insert?: string; delete?: number }>) =>
- ({ delta }) as Parameters<typeof touchesFmRegion>[0];
-
- test('closed FM present: insert inside region triggers re-parse', () => {
- expect(touchesFmRegion(event([{ retain: 5 }, { insert: 'x' }]), 30, false)).toBe(true);
- });
-
- test('closed FM present: insert past region bails out', () => {
- expect(touchesFmRegion(event([{ retain: 30 }, { insert: 'x' }]), 30, false)).toBe(false);
- });
-
- test('closed FM present: delete inside region triggers re-parse', () => {
- expect(touchesFmRegion(event([{ retain: 10 }, { delete: 5 }]), 30, false)).toBe(true);
- });
-
- test('closed FM present: delete past region bails out', () => {
- expect(touchesFmRegion(event([{ retain: 30 }, { delete: 5 }]), 30, false)).toBe(false);
- });
-
- test('no FM, no open fence: body-only insert bails out (the optimization)', () => {
- expect(touchesFmRegion(event([{ retain: 42 }, { insert: 'x' }]), 0, false)).toBe(false);
- });
-
- test('no FM, no open fence: insert at byte 0 triggers re-parse', () => {
- expect(touchesFmRegion(event([{ insert: '---\n' }]), 0, false)).toBe(true);
- });
-
- test('no FM, no open fence: insert inside the leading-byte window triggers re-parse', () => {
- expect(touchesFmRegion(event([{ retain: 2 }, { insert: '-' }]), 0, false)).toBe(true);
- });
-
- test('no FM, no open fence: delete at byte 0 triggers re-parse', () => {
- expect(touchesFmRegion(event([{ delete: 3 }]), 0, false)).toBe(true);
- });
-
- test('no FM, no open fence: delete past leading window bails out', () => {
- expect(touchesFmRegion(event([{ retain: 50 }, { delete: 5 }]), 0, false)).toBe(false);
- });
-
- test('no FM, no open fence: insert at retain=4 (last byte in window) triggers re-parse', () => {
- expect(touchesFmRegion(event([{ retain: 4 }, { insert: 'x' }]), 0, false)).toBe(true);
- });
-
- test('no FM, no open fence: insert at retain=5 (at threshold) bails out', () => {
- expect(touchesFmRegion(event([{ retain: 5 }, { insert: 'x' }]), 0, false)).toBe(false);
- });
-
- test('open fence prefix: any edit triggers re-parse (could close the fence)', () => {
- expect(touchesFmRegion(event([{ retain: 100 }, { insert: 'x' }]), 0, true)).toBe(true);
- expect(touchesFmRegion(event([{ retain: 100 }, { delete: 1 }]), 0, true)).toBe(true);
- });
-
- test('embedded objects in inserts respect the threshold', () => {
- expect(
- touchesFmRegion(
- event([{ retain: 50 }, { insert: { embed: true } as unknown as string }]),
- 0,
- false,
- ),
- ).toBe(false);
- expect(
- touchesFmRegion(
- event([{ retain: 2 }, { insert: { embed: true } as unknown as string }]),
- 0,
- false,
- ),
- ).toBe(true);
- });
-});
-
-describe('bindFrontmatterDoc — no-FM body-edit perf bailout', () => {
- function spyOnYTextToString(ytext: Y.Text): { reads: () => number; restore: () => void } {
- const original = ytext.toString.bind(ytext);
- let count = 0;
- ytext.toString = function spied() {
- count += 1;
- return original();
- } as typeof ytext.toString;
- return {
- reads: () => count,
- restore: () => {
- ytext.toString = original;
- },
- };
- }
-
- test('body-only edits on a no-FM doc do not call ytext.toString()', () => {
- const provider = makeProvider('plain body content with no frontmatter\n');
- const binding = bindFrontmatterDoc(provider);
- const ytext = provider.document.getText('source');
- const spy = spyOnYTextToString(ytext);
- let calls = 0;
- binding.subscribe(() => {
- calls += 1;
- });
-
- for (let i = 0; i < 25; i++) {
- ytext.insert(ytext.length, String(i % 10));
- }
-
- expect(calls).toBe(0);
- expect(spy.reads()).toBe(0);
- spy.restore();
- binding.dispose();
- });
-
- test('inserting an FM block at byte 0 fires the listener', () => {
- const provider = makeProvider('plain body\n');
- const binding = bindFrontmatterDoc(provider);
- const calls: FrontmatterSnapshot[] = [];
- binding.subscribe((s) => {
- calls.push(s);
- });
-
- const ytext = provider.document.getText('source');
- ytext.insert(0, '---\ntitle: Hello\n---\n');
-
- expect(calls.length).toBeGreaterThan(0);
- expect(calls.at(-1)?.map).toEqual({ title: 'Hello' });
- });
-
- test('open fence prefix without closing fence: body edits still re-parse', () => {
- const provider = makeProvider('---\ntitle: Hello\n');
- const binding = bindFrontmatterDoc(provider);
- const ytext = provider.document.getText('source');
- const spy = spyOnYTextToString(ytext);
- let calls = 0;
- binding.subscribe(() => {
- calls += 1;
- });
-
- ytext.insert(ytext.length, '---\n');
-
- expect(spy.reads()).toBeGreaterThan(0);
- expect(calls).toBeGreaterThan(0);
- expect(binding.current().map).toEqual({ title: 'Hello' });
- spy.restore();
- binding.dispose();
- });
-
- test('transition no-prefix → open-prefix updates the cached flag', () => {
- const provider = makeProvider('hello\n');
- const binding = bindFrontmatterDoc(provider);
- const ytext = provider.document.getText('source');
-
- ytext.insert(0, '---\n');
- expect(binding.current().map).toEqual({});
-
- const spy = spyOnYTextToString(ytext);
- let calls = 0;
- binding.subscribe(() => {
- calls += 1;
- });
- ytext.insert(ytext.length, '---\n');
-
- expect(spy.reads()).toBeGreaterThan(0);
- expect(calls).toBeGreaterThan(0);
- spy.restore();
- binding.dispose();
- });
-
- test('transition open-prefix → no-prefix restores bailout', () => {
- const provider = makeProvider('---\nhello\n');
- const binding = bindFrontmatterDoc(provider);
- const ytext = provider.document.getText('source');
-
- ytext.delete(0, 4);
-
- const spy = spyOnYTextToString(ytext);
- let calls = 0;
- binding.subscribe(() => {
- calls += 1;
- });
- ytext.insert(ytext.length, 'world');
-
- expect(spy.reads()).toBe(0);
- expect(calls).toBe(0);
- spy.restore();
- binding.dispose();
- });
-});
-
-describe('bindFrontmatterDoc — renamePath()', () => {
- function seedNested(): {
- provider: ReturnType<typeof makeProvider>;
- binding: ReturnType<typeof bindFrontmatterDoc>;
- } {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({
- name: 'skill',
- metadata: { version: '1.0.0', author: 'Inkeep', license: 'MIT' },
- });
- return { provider, binding };
- }
-
- test('renames a nested key preserving sibling nested keys + top-level keys', () => {
- const { provider, binding } = seedNested();
- const result = binding.renamePath(['metadata', 'version'], 'semver');
- expect(result.ok).toBe(true);
- const fenced = readYTextFm(provider);
- expect(fenced).toContain('semver: 1.0.0');
- expect(fenced).toContain('author: Inkeep');
- expect(fenced).toContain('license: MIT');
- expect(fenced).toContain('name: skill');
- expect(fenced).not.toContain('version: 1.0.0');
- binding.dispose();
- });
-
- test('single-element path renames a top-level key (matches rename())', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ name: 'skill', description: 'hello' });
-
- const result = binding.renamePath(['name'], 'title');
- expect(result.ok).toBe(true);
- expect(readFmMap(provider.document.getText('source').toString())).toEqual({
- title: 'skill',
- description: 'hello',
- });
- binding.dispose();
- });
-
- test('preserves nested source position so siblings keep their order', () => {
- const { provider, binding } = seedNested();
- binding.renamePath(['metadata', 'author'], 'maintainer');
- const fenced = readYTextFm(provider);
- const versionIdx = fenced.indexOf('version:');
- const maintainerIdx = fenced.indexOf('maintainer:');
- const licenseIdx = fenced.indexOf('license:');
- expect(versionIdx).toBeLessThan(maintainerIdx);
- expect(maintainerIdx).toBeLessThan(licenseIdx);
- binding.dispose();
- });
-
- test('unknown nested key returns SCHEMA_INVALID with unknown_key', () => {
- const { binding } = seedNested();
- const result = binding.renamePath(['metadata', 'missing'], 'x');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.issueCode).toBe('unknown_key');
- }
- binding.dispose();
- });
-
- test('nested target collision returns SCHEMA_INVALID with duplicate_target', () => {
- const { binding } = seedNested();
- const result = binding.renamePath(['metadata', 'version'], 'author');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.issueCode).toBe('duplicate_target');
- }
- binding.dispose();
- });
-
- test('allowDuplicate admits a nested colliding rename', () => {
- const { binding } = seedNested();
- const result = binding.renamePath(['metadata', 'version'], 'author', {
- allowDuplicate: true,
- });
- expect(result.ok).toBe(true);
- binding.dispose();
- });
-
- test('rejects numeric last segment (sequence index) as invalid_path', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ items: ['a', 'b'] });
-
- const result = binding.renamePath(['items', 0], 'x');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.issueCode).toBe('invalid_path');
- }
- binding.dispose();
- });
-
- test('empty path rejected with invalid_path', () => {
- const { binding } = seedNested();
- const result = binding.renamePath([], 'x');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('SCHEMA_INVALID');
- binding.dispose();
- });
-
- test('top-level rename to reserved frontmatter key rejected', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ name: 'skill' });
-
- const result = binding.renamePath(['name'], 'frontmatter');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('SCHEMA_INVALID');
- if (result.error.code === 'SCHEMA_INVALID') {
- expect(result.error.issues[0]?.issueCode).toBe('reserved_key');
- }
- binding.dispose();
- });
-
- test('writes are stamped with FORM_WRITE_ORIGIN', () => {
- const { provider, binding } = seedNested();
-
- let observedOrigin: unknown = null;
- provider.document.getText('source').observe((_event, transaction) => {
- observedOrigin = transaction.origin;
- });
-
- binding.renamePath(['metadata', 'version'], 'semver');
- expect(observedOrigin).toBe(FORM_WRITE_ORIGIN);
- binding.dispose();
- });
-
- test('disposed binding rejects further renamePath calls', () => {
- const { binding } = seedNested();
- binding.dispose();
-
- const result = binding.renamePath(['metadata', 'version'], 'semver');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('WRITE_ERROR');
- });
-});
-
-describe('bindFrontmatterDoc — reorderPath()', () => {
- function seedNested(): {
- provider: ReturnType<typeof makeProvider>;
- binding: ReturnType<typeof bindFrontmatterDoc>;
- } {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({
- name: 'skill',
- metadata: { version: '1.0.0', author: 'Inkeep', license: 'MIT' },
- });
- return { provider, binding };
- }
-
- test('reorders nested children preserving sibling top-level keys', () => {
- const { provider, binding } = seedNested();
- const result = binding.reorderPath(['metadata'], ['license', 'author', 'version']);
- expect(result.ok).toBe(true);
- const fenced = readYTextFm(provider);
- const licenseIdx = fenced.indexOf('license:');
- const authorIdx = fenced.indexOf('author:');
- const versionIdx = fenced.indexOf('version:');
- expect(licenseIdx).toBeLessThan(authorIdx);
- expect(authorIdx).toBeLessThan(versionIdx);
- expect(fenced).toContain('name: skill');
- binding.dispose();
- });
-
- test('empty path reorders top-level keys (matches reorder())', () => {
- const { provider, binding } = seedNested();
- const result = binding.reorderPath([], ['metadata', 'name']);
- expect(result.ok).toBe(true);
- const fenced = readYTextFm(provider);
- const metaIdx = fenced.indexOf('metadata:');
- const nameIdx = fenced.indexOf('name:');
- expect(metaIdx).toBeLessThan(nameIdx);
- binding.dispose();
- });
-
- test('rejects non-permutation with WRITE_ERROR (reorder_mismatch)', () => {
- const { binding } = seedNested();
- const result = binding.reorderPath(['metadata'], ['version', 'missing']);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('WRITE_ERROR');
- binding.dispose();
- });
-
- test('rejects when target is not a map (e.g. scalar leaf)', () => {
- const { binding } = seedNested();
- const result = binding.reorderPath(['name'], ['x']);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('SCHEMA_INVALID');
- binding.dispose();
- });
-
- test('writes are stamped with FORM_WRITE_ORIGIN', () => {
- const { provider, binding } = seedNested();
-
- let observedOrigin: unknown = null;
- provider.document.getText('source').observe((_event, transaction) => {
- observedOrigin = transaction.origin;
- });
-
- binding.reorderPath(['metadata'], ['author', 'version', 'license']);
- expect(observedOrigin).toBe(FORM_WRITE_ORIGIN);
- binding.dispose();
- });
-
- test('disposed binding rejects further reorderPath calls', () => {
- const { binding } = seedNested();
- binding.dispose();
-
- const result = binding.reorderPath(['metadata'], ['version', 'author', 'license']);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('WRITE_ERROR');
- });
-});
-
-describe('bindFrontmatterDoc — reorderSeqPath()', () => {
- function seedAuthors(): {
- provider: ReturnType<typeof makeProvider>;
- binding: ReturnType<typeof bindFrontmatterDoc>;
- } {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({
- title: 'doc',
- authors: [
- { name: 'A', role: 'lead' },
- { name: 'B', role: 'review' },
- { name: 'C', role: 'author' },
- ],
- });
- return { provider, binding };
- }
-
- test('permutes sequence items at path; sibling top-level keys survive', () => {
- const { binding } = seedAuthors();
- const result = binding.reorderSeqPath(['authors'], [2, 0, 1]);
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({
- title: 'doc',
- authors: [
- { name: 'C', role: 'author' },
- { name: 'A', role: 'lead' },
- { name: 'B', role: 'review' },
- ],
- });
- binding.dispose();
- });
-
- test('rejects non-permutation as WRITE_ERROR (reorder_mismatch)', () => {
- const { binding } = seedAuthors();
- const result = binding.reorderSeqPath(['authors'], [0, 1]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('WRITE_ERROR');
- binding.dispose();
- });
-
- test('rejects when target is not a sequence (map at path)', () => {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ metadata: { version: '1.0', author: 'X' } });
- const result = binding.reorderSeqPath(['metadata'], [0, 1]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('SCHEMA_INVALID');
- binding.dispose();
- });
-
- test('writes are stamped with FORM_WRITE_ORIGIN', () => {
- const { provider, binding } = seedAuthors();
- let observedOrigin: unknown = null;
- provider.document.getText('source').observe((_event, transaction) => {
- observedOrigin = transaction.origin;
- });
- binding.reorderSeqPath(['authors'], [2, 1, 0]);
- expect(observedOrigin).toBe(FORM_WRITE_ORIGIN);
- binding.dispose();
- });
-
- test('identity permutation is a no-op without Y.Text mutation', () => {
- const { provider, binding } = seedAuthors();
- const before = provider.document.getText('source').toString();
- const result = binding.reorderSeqPath(['authors'], [0, 1, 2]);
- expect(result.ok).toBe(true);
- const after = provider.document.getText('source').toString();
- expect(after).toBe(before);
- binding.dispose();
- });
-
- test('disposed binding rejects further reorderSeqPath calls', () => {
- const { binding } = seedAuthors();
- binding.dispose();
- const result = binding.reorderSeqPath(['authors'], [2, 0, 1]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.code).toBe('WRITE_ERROR');
- });
-});
-
-describe('bindFrontmatterDoc — array-of-objects item CRUD via patchPath/deletePath', () => {
- function seedAuthors(): {
- provider: ReturnType<typeof makeProvider>;
- binding: ReturnType<typeof bindFrontmatterDoc>;
- } {
- const provider = makeProvider();
- const binding = bindFrontmatterDoc(provider);
- binding.patch({ authors: [{ name: 'A' }, { name: 'B' }] });
- return { provider, binding };
- }
-
- test('add item appends a new empty object at end via patchPath at index = length', () => {
- const { binding } = seedAuthors();
- const result = binding.patchPath(['authors', 2], {});
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({
- authors: [{ name: 'A' }, { name: 'B' }, {}],
- });
- binding.dispose();
- });
-
- test('delete item splices the sequence (length decreases)', () => {
- const { binding } = seedAuthors();
- const result = binding.deletePath(['authors', 0]);
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({ authors: [{ name: 'B' }] });
- binding.dispose();
- });
-
- test('edit a field within an item leaves siblings intact', () => {
- const { binding } = seedAuthors();
- const result = binding.patchPath(['authors', 1, 'role'], 'editor');
- expect(result.ok).toBe(true);
- expect(binding.current().map).toEqual({
- authors: [{ name: 'A' }, { name: 'B', role: 'editor' }],
- });
- binding.dispose();
- });
-});
-
-describe('bindFrontmatterDoc — multi-client convergence', () => {
- test('two clients editing different keys (with shared baseline) converge via Y.Text', () => {
- const providerA = makeProvider();
- const bindingA = bindFrontmatterDoc(providerA);
-
- bindingA.patch({ title: 'Initial', cluster: 'X' });
-
- const providerB = makeProvider();
- Y.applyUpdate(providerB.document, Y.encodeStateAsUpdate(providerA.document));
- const bindingB = bindFrontmatterDoc(providerB);
-
- bindingA.patch({ title: 'Hello' });
- bindingB.patch({ cluster: 'Y' });
-
- Y.applyUpdate(providerA.document, Y.encodeStateAsUpdate(providerB.document));
- Y.applyUpdate(providerB.document, Y.encodeStateAsUpdate(providerA.document));
-
- expect(bindingA.current().map).toEqual(bindingB.current().map);
-
- bindingA.dispose();
- bindingB.dispose();
- });
-});
-
-describe('bindFrontmatterDoc — fence trailing whitespace (fm-delimiter hazard)', () => {
- test('current() recognizes an FM region whose fences carry trailing whitespace', () => {
- const provider = makeProvider('--- \ntitle: Hazard\n--- \n\nBody\n');
- const binding = bindFrontmatterDoc(provider);
-
- const snapshot = binding.current();
-
- expect(snapshot.parseError).toBeUndefined();
- expect(snapshot.map).toEqual({ title: 'Hazard' });
- expect(snapshot.keys).toEqual(['title']);
- binding.dispose();
- });
-
- test('an edit at byte >= 5 that completes a trailing-whitespace fence re-parses and notifies', () => {
- const seed = '--- \ntitle: Hazard\n';
- const provider = makeProvider(seed);
- const binding = bindFrontmatterDoc(provider);
- const snapshots: FrontmatterSnapshot[] = [];
- binding.subscribe((s) => snapshots.push(s));
-
- provider.document.getText('source').insert(seed.length, '---\n');
-
- expect(snapshots.length).toBeGreaterThan(0);
- expect(snapshots.at(-1)?.map).toEqual({ title: 'Hazard' });
- expect(binding.current().map).toEqual({ title: 'Hazard' });
- binding.dispose();
- });
-});
diff --git a/packages/core/src/bridge/diff-lines.test.ts b/packages/core/src/bridge/diff-lines.test.ts
deleted file mode 100644
index d8d45e6f8..000000000
--- a/packages/core/src/bridge/diff-lines.test.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-import { expect, test } from 'bun:test';
-import { diffLinesFast } from './diff-lines.ts';
-
-test('identical strings return single unchanged entry', () => {
- const result = diffLinesFast('foo\n', 'foo\n');
- expect(result).toEqual([{ value: 'foo\n' }]);
-});
-
-test('added line produces added entry', () => {
- const result = diffLinesFast('a\n', 'a\nb\n');
- expect(result.some((c) => c.added && c.value.includes('b'))).toBe(true);
-});
-
-test('removed line produces removed entry', () => {
- const result = diffLinesFast('a\nb\n', 'a\n');
- expect(result.some((c) => c.removed && c.value.includes('b'))).toBe(true);
-});
-
-test('handles unterminated final lines', () => {
- const result = diffLinesFast('a', 'a\nb');
- expect(Array.isArray(result)).toBe(true);
- const reconstructed = result
- .filter((c) => !c.removed)
- .map((c) => c.value)
- .join('');
- expect(reconstructed).toBe('a\nb');
-});
-
-test('empty to content produces single added entry', () => {
- const result = diffLinesFast('', 'hello\nworld\n');
- expect(result.some((c) => c.added)).toBe(true);
-});
-
-test('content to empty produces single removed entry', () => {
- const result = diffLinesFast('hello\nworld\n', '');
- expect(result.some((c) => c.removed)).toBe(true);
-});
-
-test('multiline diff preserves line boundaries', () => {
- const old = 'line1\nline2\nline3\n';
- const next = 'line1\nmodified\nline3\n';
- const result = diffLinesFast(old, next);
- expect(result.some((c) => c.removed && c.value.includes('line2'))).toBe(true);
- expect(result.some((c) => c.added && c.value.includes('modified'))).toBe(true);
-});
-
-test('value strings are exact substrings (character-accurate)', () => {
- const old = 'aaa\nbbb\n';
- const next = 'aaa\nccc\n';
- const result = diffLinesFast(old, next);
- for (const change of result) {
- if (change.removed) {
- expect(old.includes(change.value)).toBe(true);
- } else if (change.added) {
- expect(next.includes(change.value)).toBe(true);
- }
- }
-});
diff --git a/packages/core/src/bridge/doc-boundary-space.test.ts b/packages/core/src/bridge/doc-boundary-space.test.ts
deleted file mode 100644
index 116d93bab..000000000
--- a/packages/core/src/bridge/doc-boundary-space.test.ts
+++ /dev/null
@@ -1,166 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import {
- projectMergeBoundarySpace,
- reattachLeadingDocBoundary,
- splitLeadingDocBoundary,
-} from './doc-boundary-space.ts';
-
-const FM = '---\ntitle: Boundary\n---\n';
-
-describe('splitLeadingDocBoundary', () => {
- test('FM doc with the canonical single blank line', () => {
- const split = splitLeadingDocBoundary(`${FM}\nBody line.\n`);
- expect(split.boundary).toBe('\n');
- expect(split.text).toBe(`${FM}Body line.\n`);
- });
-
- test('FM doc with no boundary is identity', () => {
- const split = splitLeadingDocBoundary(`${FM}Body line.\n`);
- expect(split.boundary).toBe('');
- expect(split.text).toBe(`${FM}Body line.\n`);
- });
-
- test('multi-newline run is captured greedily and verbatim', () => {
- const split = splitLeadingDocBoundary(`${FM}\n\n\nBody.\n`);
- expect(split.boundary).toBe('\n\n\n');
- expect(split.text).toBe(`${FM}Body.\n`);
- });
-
- test('CRLF run', () => {
- const fmCrlf = '---\r\ntitle: x\r\n---\r\n';
- const split = splitLeadingDocBoundary(`${fmCrlf}\r\n\r\nBody.\r\n`);
- expect(split.boundary).toBe('\r\n\r\n');
- expect(split.text).toBe(`${fmCrlf}Body.\r\n`);
- });
-
- test('no FM region: doc-start run is the boundary', () => {
- const split = splitLeadingDocBoundary('\n\nBody only.\n');
- expect(split.boundary).toBe('\n\n');
- expect(split.text).toBe('Body only.\n');
- });
-
- test('FM-only doc: boundary is the run after the close fence, body empty', () => {
- const split = splitLeadingDocBoundary(`${FM}\n`);
- expect(split.boundary).toBe('\n');
- expect(split.text).toBe(FM);
- });
-
- test('empty string is identity', () => {
- expect(splitLeadingDocBoundary('')).toEqual({ boundary: '', text: '' });
- });
-
- test('no-split guard: doc-start run shadowing an FM-shaped block is refused', () => {
- const shadowed = `\n${FM}Body.\n`;
- expect(splitLeadingDocBoundary(shadowed)).toEqual({ boundary: '', text: shadowed });
- });
-});
-
-describe('reattachLeadingDocBoundary', () => {
- test('empty boundary is identity', () => {
- expect(reattachLeadingDocBoundary(`${FM}Body.\n`, '')).toBe(`${FM}Body.\n`);
- });
-
- test('re-inserts between FM region and body', () => {
- expect(reattachLeadingDocBoundary(`${FM}Body.\n`, '\n')).toBe(`${FM}\nBody.\n`);
- });
-
- test('no FM region: attaches at doc start', () => {
- expect(reattachLeadingDocBoundary('Body.\n', '\n\n')).toBe('\n\nBody.\n');
- });
-
- test('FM-only doc: boundary becomes the trailing run', () => {
- expect(reattachLeadingDocBoundary(FM, '\n')).toBe(`${FM}\n`);
- });
-});
-
-describe('projectMergeBoundarySpace', () => {
- test('FM doc with the canonical blank line is unchanged', () => {
- expect(projectMergeBoundarySpace(`${FM}\nBody.\n`)).toBe(`${FM}\nBody.\n`);
- });
-
- test('FM doc without a boundary gains the single separator', () => {
- expect(projectMergeBoundarySpace(`${FM}Body.\n`)).toBe(`${FM}\nBody.\n`);
- });
-
- test('multi-newline boundary normalizes to one separator', () => {
- expect(projectMergeBoundarySpace(`${FM}\n\n\nBody.\n`)).toBe(`${FM}\nBody.\n`);
- });
-
- test('FM-less doc has its doc-start run stripped', () => {
- expect(projectMergeBoundarySpace('\n\nBody only.\n')).toBe('Body only.\n');
- });
-
- test('FM-less doc without a run is unchanged', () => {
- expect(projectMergeBoundarySpace('Body only.\n')).toBe('Body only.\n');
- });
-
- test('empty string is unchanged', () => {
- expect(projectMergeBoundarySpace('')).toBe('');
- });
-
- test('guard-refused shape passes through unchanged', () => {
- const shadowed = `\n${FM}Body.\n`;
- expect(projectMergeBoundarySpace(shadowed)).toBe(shadowed);
- });
-
- test('idempotent and content-preserving outside the boundary slot', () => {
- const inputs = [
- `${FM}\nBody.\n`,
- `${FM}Body.\n`,
- `${FM}\n\n\nBody.\n`,
- '\nBody only.\n',
- 'Body only.\n',
- `${FM}\n|a|b|\n|-|-|\n`,
- ];
- for (const x of inputs) {
- const once = projectMergeBoundarySpace(x);
- expect(projectMergeBoundarySpace(once)).toBe(once);
- expect(splitLeadingDocBoundary(once).text).toBe(splitLeadingDocBoundary(x).text);
- }
- });
-});
-
-describe('round-trip law: reattach(split(x).text, split(x).boundary) === x', () => {
- const fms = [
- '',
- FM,
- '---\n---\n', // empty FM block
- '--- \ntitle: ws fence\n---\t\n', // whitespace-bearing fences (post-#1783 recognition)
- '---\r\ntitle: crlf\r\n---\r\n',
- ];
- const boundaries = ['', '\n', '\n\n', '\n\n\n\n', '\r\n', '\r\n\r\n'];
- const bodies = [
- '',
- 'One paragraph.\n',
- 'First paragraph body. \n\nSecond paragraph stays.\n',
- '|a|b|\n|-|-|\n|c|d|\n', // un-padded NG-class table — storage never sanitizes
- '---\nlooks like FM\n---\nbut is body\n',
- 'no trailing newline',
- ];
-
- test('holds over composed fm × boundary × body shapes', () => {
- for (const fm of fms) {
- for (const boundary of boundaries) {
- for (const body of bodies) {
- const x = fm + boundary + body;
- const split = splitLeadingDocBoundary(x);
- expect(reattachLeadingDocBoundary(split.text, split.boundary)).toBe(x);
- }
- }
- }
- });
-
- test('stripped text never begins its body with a newline (no boundary doubling)', () => {
- for (const fm of fms) {
- for (const boundary of boundaries) {
- for (const body of bodies) {
- const split = splitLeadingDocBoundary(fm + boundary + body);
- const resplit = splitLeadingDocBoundary(split.text);
- if (split.boundary !== '') {
- expect(resplit.boundary).toBe('');
- }
- }
- }
- }
- });
-});
diff --git a/packages/core/src/bridge/fm-partition-invariance.test.ts b/packages/core/src/bridge/fm-partition-invariance.test.ts
deleted file mode 100644
index 258946b29..000000000
--- a/packages/core/src/bridge/fm-partition-invariance.test.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { FM_FENCE_LINE_RE, FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';
-import { normalizeBridge } from './normalize.ts';
-
-const CLEAN = '---\ntitle: x\n---\n\nbody\n';
-
-const FENCE_WHITESPACE_VARIANTS: Array<[string, string]> = [
- ['trailing space on the opening fence', '--- \ntitle: x\n---\n\nbody\n'],
- ['trailing space on the closing fence', '---\ntitle: x\n--- \n\nbody\n'],
- ['trailing tab on the opening fence', '---\t\ntitle: x\n---\n\nbody\n'],
- ['trailing tab on the closing fence', '---\ntitle: x\n---\t\n\nbody\n'],
-];
-
-describe('FM partition invariance under bridge tolerance', () => {
- test('precondition: the clean fixture has a recognized FM region', () => {
- expect(stripFrontmatter(CLEAN).frontmatter).toBe('---\ntitle: x\n---\n');
- });
-
- for (const [name, mutated] of FENCE_WHITESPACE_VARIANTS) {
- test(`${name}: an in-tolerance edit must not break FM recognition`, () => {
- const inTolerance = normalizeBridge(mutated) === normalizeBridge(CLEAN);
- const recognized = stripFrontmatter(mutated).frontmatter !== '';
-
- expect({ inTolerance, recognized }).not.toEqual({
- inTolerance: true,
- recognized: false,
- });
- });
- }
-
- test('control: leading space before the opening fence is beyond tolerance', () => {
- const leading = ' ---\ntitle: x\n---\n\nbody\n';
- expect(normalizeBridge(leading)).not.toBe(normalizeBridge(CLEAN));
- });
-});
-
-describe('FM fence-line predicate agreement (FM_FENCE_LINE_RE vs FRONTMATTER_RE)', () => {
- const FENCE_LINES = ['---', '--- ', '---\t', '--- \t '];
- const NON_FENCE_LINES = [' ---', '----', '--- x', '---x', '-- -', ''];
-
- for (const line of FENCE_LINES) {
- test(`fence line ${JSON.stringify(line)}: both predicates recognize`, () => {
- expect(FM_FENCE_LINE_RE.test(line)).toBe(true);
- expect(FRONTMATTER_RE.test(`${line}\ntitle: x\n---\n`)).toBe(true);
- });
- }
-
- for (const line of NON_FENCE_LINES) {
- test(`non-fence line ${JSON.stringify(line)}: both predicates reject`, () => {
- expect(FM_FENCE_LINE_RE.test(line)).toBe(false);
- expect(FRONTMATTER_RE.test(`${line}\ntitle: x\n---\n`)).toBe(false);
- });
- }
-});
diff --git a/packages/core/src/bridge/frontmatter-region.test.ts b/packages/core/src/bridge/frontmatter-region.test.ts
deleted file mode 100644
index 502f0ce98..000000000
--- a/packages/core/src/bridge/frontmatter-region.test.ts
+++ /dev/null
@@ -1,614 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import {
- applyPatchToFm,
- applyPathDeleteToFm,
- applyPathRenameToFm,
- applyPathReorderSeqToFm,
- applyPathReorderToFm,
- applyPathSetToFm,
- parseFmRegion,
-} from './frontmatter-region.ts';
-
-describe('parseFmRegion — mixed-scalar array coercion', () => {
- test('flow array with integer element coerces to string and surfaces no parseError', () => {
- const yaml = 'tags: [travel, spain, 2026]\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map).not.toBeNull();
- expect(result.map?.tags).toEqual(['travel', 'spain', '2026']);
- });
-
- test('flow array with boolean element coerces to string and surfaces no parseError', () => {
- const yaml = 'tags: [travel, true, spain]\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map).not.toBeNull();
- expect(result.map?.tags).toEqual(['travel', 'true', 'spain']);
- });
-
- test('flow array with float element coerces to string and surfaces no parseError', () => {
- const yaml = 'tags: [travel, 2.5, spain]\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map).not.toBeNull();
- expect(result.map?.tags).toEqual(['travel', '2.5', 'spain']);
- });
-
- test('block array with integer element coerces to string and surfaces no parseError', () => {
- const yaml = 'tags:\n - travel\n - spain\n - 2026\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map).not.toBeNull();
- expect(result.map?.tags).toEqual(['travel', 'spain', '2026']);
- });
-
- test('flow array of all-string elements still parses cleanly (control)', () => {
- const yaml = 'tags: [travel, spain, "2026"]\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map).not.toBeNull();
- expect(result.map?.tags).toEqual(['travel', 'spain', '2026']);
- });
-
- test('null array element is rejected (out of scope per FrontmatterValueSchema)', () => {
- const yaml = 'tags: [travel, ~, spain]\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeDefined();
- expect(result.map).toBeNull();
- });
-
- test("user's reported fixture (8-element flow array with unquoted year) coerces and parses", () => {
- const yaml = 'tags: [travel, spain, barcelona, mallorca, palma, balearics, sailing, 2026]\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map).not.toBeNull();
- expect(result.map?.tags).toEqual([
- 'travel',
- 'spain',
- 'barcelona',
- 'mallorca',
- 'palma',
- 'balearics',
- 'sailing',
- '2026',
- ]);
- });
-});
-
-describe('parseFmRegion — recursive value contract (nested objects + arrays of objects)', () => {
- test('nested mapping parses into a populated map with no parseError', () => {
- const yaml = 'name: skill\nmetadata:\n version: 1.0.0\n author: Inkeep\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map).toEqual({
- name: 'skill',
- metadata: { version: '1.0.0', author: 'Inkeep' },
- });
- });
-
- test('array-of-objects parses without parseError; object elements are not String-coerced', () => {
- const yaml = 'items:\n - title: a\n - title: b\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map?.items).toEqual([{ title: 'a' }, { title: 'b' }]);
- });
-
- test('arbitrarily deep nesting (map in list in map) survives the recursive schema', () => {
- const yaml = 'outer:\n inner:\n - leaf: ok\n';
- const result = parseFmRegion(yaml);
- expect(result.parseError).toBeUndefined();
- expect(result.map).toEqual({ outer: { inner: [{ leaf: 'ok' }] } });
- });
-
- test('genuinely malformed YAML still sets parseError (yaml@2 parse error)', () => {
- const result = parseFmRegion('title: foo: bar');
- expect(result.parseError).toBeDefined();
- expect(result.map).toBeNull();
- });
-
- test('non-mapping top-level value still sets parseError', () => {
- const result = parseFmRegion('- one\n- two');
- expect(result.parseError).toBeDefined();
- expect(result.map).toBeNull();
- });
-});
-
-describe('applyPatchToFm — array style preservation', () => {
- test('flow-style input preserves flow style when patch replaces the array', () => {
- const fenced = '---\ntags: [travel, spain, 2026]\n---\n';
- const result = applyPatchToFm(fenced, { tags: ['travel', 'spain', '2026', 'paris'] });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('tags: [');
- });
-
- test('block-style input preserves block style when patch replaces the array', () => {
- const fenced = '---\ntags:\n - travel\n - spain\n - "2026"\n---\n';
- const result = applyPatchToFm(fenced, { tags: ['travel', 'spain', '2026', 'paris'] });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toMatch(/tags:\s*\n\s*-\s/);
- });
-});
-
-describe('applyPatchToFm — nested write semantics (whole-subtree merge)', () => {
- test('nested object value replaces the subtree, preserving sibling top-level keys', () => {
- const fenced =
- '---\nname: skill\nmetadata:\n version: 1.0.0\n author: Inkeep\ndescription: hello\n---\n';
- const result = applyPatchToFm(fenced, {
- metadata: { version: '2.0.0', author: 'Inkeep' },
- });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(parseFmRegion(result.nextFenced.replace(/^---\n|---\n$/g, '')).map).toEqual({
- name: 'skill',
- metadata: { version: '2.0.0', author: 'Inkeep' },
- description: 'hello',
- });
- });
-
- test('null deletes a nested-object subtree key', () => {
- const fenced =
- '---\nname: skill\nmetadata:\n version: 1.0.0\n author: Inkeep\ndescription: hello\n---\n';
- const result = applyPatchToFm(fenced, { metadata: null });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).not.toContain('metadata');
- expect(result.nextFenced).toContain('name: skill');
- expect(result.nextFenced).toContain('description: hello');
- });
-
- test('non-object scalar replaces an existing object subtree (whole-subtree replace)', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n author: Inkeep\n---\n';
- const result = applyPatchToFm(fenced, { metadata: 'inline' });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toMatch(/metadata:\s*inline/);
- });
-
- test('nested object replaces an existing scalar', () => {
- const fenced = '---\nname: skill\n---\n';
- const result = applyPatchToFm(fenced, {
- name: { kind: 'skill', detail: 'x' },
- });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(parseFmRegion(result.nextFenced.replace(/^---\n|---\n$/g, '')).map).toEqual({
- name: { kind: 'skill', detail: 'x' },
- });
- });
-
- test('arbitrarily-deep nested set builds the full subtree at any depth', () => {
- const fenced = '---\nname: skill\n---\n';
- const result = applyPatchToFm(fenced, {
- outer: { inner: { leaf: 'ok' } },
- });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(parseFmRegion(result.nextFenced.replace(/^---\n|---\n$/g, '')).map).toEqual({
- name: 'skill',
- outer: { inner: { leaf: 'ok' } },
- });
- });
-
- test('preserves comments on untouched sibling keys when editing a nested subtree', () => {
- const fenced =
- '---\n# leading comment\nname: skill\n# metadata block\nmetadata:\n version: 1.0.0\n# trailing comment on description\ndescription: hello\n---\n';
- const result = applyPatchToFm(fenced, {
- metadata: { version: '2.0.0' },
- });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('# leading comment');
- expect(result.nextFenced).toContain('# trailing comment on description');
- expect(result.nextFenced).toContain('name: skill');
- expect(result.nextFenced).toContain('description: hello');
- });
-
- test('flow-style nested map survives a whole-subtree replacement', () => {
- const fenced = '---\nmetadata: {version: 1.0, author: Inkeep}\n---\n';
- const result = applyPatchToFm(fenced, {
- metadata: { version: '2.0', author: 'Inkeep' },
- });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('metadata: {');
- });
-
- test('block-style nested map stays in block style on replacement', () => {
- const fenced = '---\nmetadata:\n version: 1.0\n author: Inkeep\n---\n';
- const result = applyPatchToFm(fenced, {
- metadata: { version: '2.0', author: 'Inkeep' },
- });
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toMatch(/metadata:\s*\n\s+version:/);
- });
-
- test('idempotence: applying the same nested patch twice is byte-stable', () => {
- const fenced = '---\nname: skill\nmetadata:\n version: 1.0.0\n author: Inkeep\n---\n';
- const first = applyPatchToFm(fenced, {
- metadata: { version: '2.0.0', author: 'Inkeep' },
- });
- expect(first.ok).toBe(true);
- if (!first.ok) return;
- const second = applyPatchToFm(first.nextFenced, {
- metadata: { version: '2.0.0', author: 'Inkeep' },
- });
- expect(second.ok).toBe(true);
- if (!second.ok) return;
- expect(second.nextFenced).toBe(first.nextFenced);
- });
-
- test('total function: pathological nested input returns a parse_failed error, never throws', () => {
- const fenced = '---\nname: skill\n---\n';
- const result = applyPatchToFm(fenced, {
- metadata: { stamp: new Date() as unknown as string },
- });
- expect(result.ok).toBe(false);
- });
-});
-
-describe('applyPathRenameToFm — path-addressed nested rename', () => {
- test('renames a top-level key when path has one element', () => {
- const fenced = '---\nname: skill\ndescription: hello\n---\n';
- const result = applyPathRenameToFm(fenced, ['name'], 'title');
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('title: skill');
- expect(result.nextFenced).toContain('description: hello');
- expect(result.nextFenced).not.toContain('name:');
- });
-
- test('renames a nested leaf preserving sibling nested keys + comments', () => {
- const fenced =
- '---\nname: skill\nmetadata:\n # before version\n version: 1.0.0\n author: Inkeep\n repository: https://example.com\n---\n';
- const result = applyPathRenameToFm(fenced, ['metadata', 'version'], 'semver');
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('semver: 1.0.0');
- expect(result.nextFenced).toContain('author: Inkeep');
- expect(result.nextFenced).toContain('repository: https://example.com');
- expect(result.nextFenced).toContain('# before version');
- expect(result.nextFenced).not.toContain('version: 1.0.0');
- });
-
- test('renames a deeply nested leaf at depth 3', () => {
- const fenced = '---\nouter:\n inner:\n leaf: ok\n other: keep\n---\n';
- const result = applyPathRenameToFm(fenced, ['outer', 'inner', 'leaf'], 'tip');
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('tip: ok');
- expect(result.nextFenced).toContain('other: keep');
- });
-
- test('preserves source position of the renamed Pair (siblings stay in order)', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n author: Inkeep\n license: MIT\n---\n';
- const result = applyPathRenameToFm(fenced, ['metadata', 'author'], 'maintainer');
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- const versionIdx = result.nextFenced.indexOf('version:');
- const maintainerIdx = result.nextFenced.indexOf('maintainer:');
- const licenseIdx = result.nextFenced.indexOf('license:');
- expect(versionIdx).toBeLessThan(maintainerIdx);
- expect(maintainerIdx).toBeLessThan(licenseIdx);
- });
-
- test('rejects rename when last path segment is numeric (sequence index)', () => {
- const fenced = '---\nitems:\n - a\n - b\n---\n';
- const result = applyPathRenameToFm(fenced, ['items', 0], 'x');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('invalid_path');
- });
-
- test('rejects unknown leaf', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n---\n';
- const result = applyPathRenameToFm(fenced, ['metadata', 'missing'], 'x');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('unknown_key');
- });
-
- test('rejects nested target collision by default', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n author: Inkeep\n---\n';
- const result = applyPathRenameToFm(fenced, ['metadata', 'version'], 'author');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('duplicate_target');
- });
-
- test('allowDuplicate admits a colliding rename', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n author: Inkeep\n---\n';
- const result = applyPathRenameToFm(fenced, ['metadata', 'version'], 'author', {
- allowDuplicate: true,
- });
- expect(result.ok).toBe(true);
- });
-
- test('top-level rename to reserved "frontmatter" is rejected', () => {
- const fenced = '---\nname: skill\n---\n';
- const result = applyPathRenameToFm(fenced, ['name'], 'frontmatter');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('reserved_key');
- });
-
- test('nested rename to "frontmatter" is allowed (only the top-level slot is reserved)', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n---\n';
- const result = applyPathRenameToFm(fenced, ['metadata', 'version'], 'frontmatter');
- expect(result.ok).toBe(true);
- });
-
- test('renaming through a scalar intermediate fails with unknown_key', () => {
- const fenced = '---\nname: skill\n---\n';
- const result = applyPathRenameToFm(fenced, ['name', 'child'], 'x');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('unknown_key');
- });
-
- test('empty path is rejected', () => {
- const fenced = '---\nname: skill\n---\n';
- const result = applyPathRenameToFm(fenced, [], 'x');
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('invalid_path');
- });
-
- test('no-op rename to same key returns the input unchanged', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n---\n';
- const result = applyPathRenameToFm(fenced, ['metadata', 'version'], 'version');
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toBe(fenced);
- });
-});
-
-describe('applyPathReorderToFm — path-addressed nested reorder', () => {
- test('reorders top-level keys when path is empty (delegates to applyReorderToFm)', () => {
- const fenced = '---\nname: skill\ndescription: hello\nmetadata: {}\n---\n';
- const result = applyPathReorderToFm(fenced, [], ['metadata', 'name', 'description']);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- const metaIdx = result.nextFenced.indexOf('metadata:');
- const nameIdx = result.nextFenced.indexOf('name:');
- const descIdx = result.nextFenced.indexOf('description:');
- expect(metaIdx).toBeLessThan(nameIdx);
- expect(nameIdx).toBeLessThan(descIdx);
- });
-
- test('reorders nested keys preserving sibling top-level keys', () => {
- const fenced =
- '---\nname: skill\nmetadata:\n version: 1.0.0\n author: Inkeep\n license: MIT\n---\n';
- const result = applyPathReorderToFm(fenced, ['metadata'], ['license', 'author', 'version']);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- const licenseIdx = result.nextFenced.indexOf('license:');
- const authorIdx = result.nextFenced.indexOf('author:');
- const versionIdx = result.nextFenced.indexOf('version:');
- expect(licenseIdx).toBeLessThan(authorIdx);
- expect(authorIdx).toBeLessThan(versionIdx);
- expect(result.nextFenced).toContain('name: skill');
- });
-
- test('reorders deeply nested children', () => {
- const fenced = '---\nouter:\n inner:\n a: 1\n b: 2\n c: 3\n---\n';
- const result = applyPathReorderToFm(fenced, ['outer', 'inner'], ['c', 'a', 'b']);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- const cIdx = result.nextFenced.indexOf('c:');
- const aIdx = result.nextFenced.indexOf('a:');
- const bIdx = result.nextFenced.indexOf('b:');
- expect(cIdx).toBeLessThan(aIdx);
- expect(aIdx).toBeLessThan(bIdx);
- });
-
- test('rejects a non-permutation of current keys', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n author: Inkeep\n---\n';
- const result = applyPathReorderToFm(fenced, ['metadata'], ['version', 'missing']);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('reorder_mismatch');
- });
-
- test('rejects when target is not a map', () => {
- const fenced = '---\nname: skill\n---\n';
- const result = applyPathReorderToFm(fenced, ['name'], ['x']);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('invalid_path');
- });
-
- test('rejects reorder targeting a sequence (not a map)', () => {
- const fenced = '---\nitems:\n - a\n - b\n---\n';
- const result = applyPathReorderToFm(fenced, ['items'], ['a', 'b']);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('invalid_path');
- });
-
- test('idempotent reorder to the same order is byte-stable', () => {
- const fenced = '---\nmetadata:\n version: 1.0.0\n author: Inkeep\n---\n';
- const result = applyPathReorderToFm(fenced, ['metadata'], ['version', 'author']);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toBe(fenced);
- });
-
- test('preserves comments on the reordered map', () => {
- const fenced =
- '---\nmetadata:\n # before version\n version: 1.0.0\n # before author\n author: Inkeep\n---\n';
- const result = applyPathReorderToFm(fenced, ['metadata'], ['author', 'version']);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('# before version');
- expect(result.nextFenced).toContain('# before author');
- });
-});
-
-describe('applyPathReorderSeqToFm — array-of-objects reorder semantics', () => {
- test('permutes items at the array path; sibling keys + per-item content survive', () => {
- const fenced =
- '---\ntitle: doc\ncontributors:\n - name: A\n role: lead\n - name: B\n role: review\n - name: C\n role: author\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['contributors'], [2, 0, 1]);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- const parsed = parseFmRegion(result.nextFenced.replace(/^---\n/, '').replace(/---\n$/, ''));
- expect(parsed.parseError).toBeUndefined();
- expect(parsed.map?.contributors).toEqual([
- { name: 'C', role: 'author' },
- { name: 'A', role: 'lead' },
- { name: 'B', role: 'review' },
- ]);
- expect(parsed.map?.title).toBe('doc');
- });
-
- test('identity permutation is a no-op (byte-stable)', () => {
- const fenced = '---\nlist:\n - { x: 1 }\n - { x: 2 }\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['list'], [0, 1]);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toBe(fenced);
- });
-
- test('preserves per-item line comments after reorder', () => {
- const fenced = '---\nitems:\n # before A\n - name: A\n # before B\n - name: B\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['items'], [1, 0]);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('# before A');
- expect(result.nextFenced).toContain('# before B');
- });
-
- test('rejects non-permutation (wrong length)', () => {
- const fenced = '---\nlist:\n - { x: 1 }\n - { x: 2 }\n - { x: 3 }\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['list'], [0, 1]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('reorder_mismatch');
- });
-
- test('rejects non-permutation (duplicate index)', () => {
- const fenced = '---\nlist:\n - { x: 1 }\n - { x: 2 }\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['list'], [0, 0]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('reorder_mismatch');
- });
-
- test('rejects non-permutation (out-of-range index)', () => {
- const fenced = '---\nlist:\n - { x: 1 }\n - { x: 2 }\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['list'], [0, 2]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('reorder_mismatch');
- });
-
- test('rejects when target at path is not a sequence (map instead)', () => {
- const fenced = '---\nmetadata:\n version: 1.0\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['metadata'], [0]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('invalid_path');
- });
-
- test('rejects when target at path is missing (parent does not exist)', () => {
- const fenced = '---\ntitle: doc\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['nope'], [0]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('invalid_path');
- });
-
- test('rejects empty path (whole document)', () => {
- const fenced = '---\ntitle: doc\n---\n';
- const result = applyPathReorderSeqToFm(fenced, [], [0]);
- expect(result.ok).toBe(false);
- if (result.ok) return;
- expect(result.error.kind).toBe('invalid_path');
- });
-
- test('reorder of a deeply-nested array preserves siblings at every depth', () => {
- const fenced =
- '---\nproject:\n name: ok\n authors:\n - name: A\n - name: B\n release: alpha\n---\n';
- const result = applyPathReorderSeqToFm(fenced, ['project', 'authors'], [1, 0]);
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- const parsed = parseFmRegion(result.nextFenced.replace(/^---\n/, '').replace(/---\n$/, ''));
- expect(parsed.map?.project).toEqual({
- name: 'ok',
- authors: [{ name: 'B' }, { name: 'A' }],
- release: 'alpha',
- });
- });
-
- test('reorder result re-applied (twice) is byte-stable (idempotence)', () => {
- const fenced = '---\nlist:\n - { x: 1 }\n - { x: 2 }\n - { x: 3 }\n---\n';
- const once = applyPathReorderSeqToFm(fenced, ['list'], [2, 0, 1]);
- expect(once.ok).toBe(true);
- if (!once.ok) return;
- const twice = applyPathReorderSeqToFm(once.nextFenced, ['list'], [0, 1, 2]);
- expect(twice.ok).toBe(true);
- if (!twice.ok) return;
- expect(twice.nextFenced).toBe(once.nextFenced);
- });
-});
-
-describe('applyPathSetToFm / applyPathDeleteToFm — path validation + value errors', () => {
- const fenced = '---\nname: skill\nitems:\n - a\n - b\n---\n';
-
- test('empty path is rejected as invalid_path', () => {
- const result = applyPathSetToFm(fenced, [], 'x');
- expect(result.ok).toBe(false);
- if (!result.ok) expect(result.error.kind).toBe('invalid_path');
- });
-
- test('negative integer index is rejected as invalid_path', () => {
- const result = applyPathSetToFm(fenced, ['items', -1], 'x');
- expect(result.ok).toBe(false);
- if (!result.ok) expect(result.error.kind).toBe('invalid_path');
- });
-
- test('non-integer (float) index is rejected as invalid_path', () => {
- const result = applyPathSetToFm(fenced, ['items', 1.5], 'x');
- expect(result.ok).toBe(false);
- if (!result.ok) expect(result.error.kind).toBe('invalid_path');
- });
-
- test('reserved top-level key is rejected as reserved_key', () => {
- const result = applyPathSetToFm(fenced, ['frontmatter'], 'x');
- expect(result.ok).toBe(false);
- if (!result.ok) expect(result.error.kind).toBe('reserved_key');
- });
-
- test('a structurally-valid path with a non-conforming value reports invalid_value (not invalid_path)', () => {
- const result = applyPathSetToFm(fenced, ['name'], Symbol('nope') as unknown as string);
- expect(result.ok).toBe(false);
- if (!result.ok) {
- expect(result.error.kind).toBe('invalid_value');
- if (result.error.kind === 'invalid_value') expect(result.error.key).toBe('name');
- }
- });
-
- test('valid nested set adds the leaf and preserves the sibling', () => {
- const result = applyPathSetToFm('---\nmeta:\n a: 1\n---\n', ['meta', 'b'], 'two');
- expect(result.ok).toBe(true);
- if (!result.ok) return;
- expect(result.nextFenced).toContain('a: 1');
- expect(result.nextFenced).toContain('b: two');
- });
-
- test('deletePath validates the path the same way', () => {
- expect(applyPathDeleteToFm(fenced, []).ok).toBe(false);
- const neg = applyPathDeleteToFm(fenced, ['items', -1]);
- expect(neg.ok).toBe(false);
- if (!neg.ok) expect(neg.error.kind).toBe('invalid_path');
- });
-
- test('deletePath on an absent leaf is an idempotent no-op', () => {
- const result = applyPathDeleteToFm(fenced, ['nope']);
- expect(result.ok).toBe(true);
- if (result.ok) expect(result.nextFenced).toBe(fenced);
- });
-});
diff --git a/packages/core/src/bridge/growth-engine.test.ts b/packages/core/src/bridge/growth-engine.test.ts
deleted file mode 100644
index a61768b59..000000000
--- a/packages/core/src/bridge/growth-engine.test.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import {
- loadIndentedJsxFixtures,
- loadLargeEmbedFixtures,
- loadPrd6955Before,
- loadPrd6955CorruptedTriplicated,
-} from '../markdown/fixtures/index.ts';
-import { MarkdownManager } from '../markdown/index.ts';
-import { assertContentPreservation } from './merge-three-way.ts';
-
-const mm = new MarkdownManager({ extensions: sharedExtensions });
-
-function dirtyRoundTrip(md: string): string {
- const json = mm.parse(md);
- const walk = (node: JSONContent): void => {
- if (node.type === 'jsxComponent' && node.attrs) node.attrs.sourceDirty = true;
- if (node.content) for (const c of node.content) walk(c);
- };
- walk(json);
- return mm.serialize(json);
-}
-
-function maxBodyLineOccurrence(doc: string): number {
- const counts = new Map<string, number>();
- for (const raw of doc.split('\n')) {
- const line = raw.trim();
- if (line.length < 16) continue;
- if (line.startsWith('<') || line.startsWith('```') || line.startsWith('|')) continue;
- counts.set(line, (counts.get(line) ?? 0) + 1);
- }
- let max = 0;
- for (const c of counts.values()) if (c > max) max = c;
- return max;
-}
-
-function countJsxComponents(md: string): number {
- let count = 0;
- const walk = (node: JSONContent): void => {
- if (node.type === 'jsxComponent') count++;
- if (node.content) for (const c of node.content) walk(c);
- };
- walk(mm.parse(md));
- return count;
-}
-
-describe('O1 — indented-JSX dirty drain stays within byte budget + no duplication', () => {
- for (const { name, source } of loadIndentedJsxFixtures()) {
- test(`${name}: a dirty drain stays within a small byte budget (no bloat)`, () => {
- const out = dirtyRoundTrip(source);
- expect(Buffer.byteLength(out)).toBeLessThanOrEqual(
- Math.ceil(Buffer.byteLength(source) * 1.5) + 64,
- );
- });
-
- test(`${name}: a dirty drain duplicates no substantive body line`, () => {
- expect(maxBodyLineOccurrence(dirtyRoundTrip(source))).toBeLessThanOrEqual(1);
- });
- }
-});
-
-describe('O1 — large-embed clean round-trip budget (the dirty flip is a no-op here)', () => {
- for (const { name, source } of loadLargeEmbedFixtures()) {
- test(`${name}: has no jsxComponent node, so the dirty flip is a no-op`, () => {
- expect(countJsxComponents(source)).toBe(0);
- expect(dirtyRoundTrip(source)).toBe(mm.serialize(mm.parse(source)));
- });
-
- test(`${name}: a clean round-trip stays within budget and duplicates no body line`, () => {
- const out = mm.serialize(mm.parse(source));
- expect(Buffer.byteLength(out)).toBeLessThanOrEqual(
- Math.ceil(Buffer.byteLength(source) * 1.5) + 64,
- );
- expect(maxBodyLineOccurrence(out)).toBeLessThanOrEqual(1);
- });
- }
-});
-
-describe('O1 static occurrence-count oracle (AC-C3) — pins the PRD-6955 triplication', () => {
- test('the clean BEFORE capture has no triplicated body line', () => {
- expect(maxBodyLineOccurrence(loadPrd6955Before())).toBeLessThan(3);
- });
-
- test('the CORRUPTED capture trips the oracle (a body line occurs >= 3x)', () => {
- const corrupted = maxBodyLineOccurrence(loadPrd6955CorruptedTriplicated());
- expect(corrupted).toBeGreaterThanOrEqual(3);
- expect(corrupted).toBeGreaterThan(maxBodyLineOccurrence(loadPrd6955Before()));
- });
-});
-
-describe('assertContentPreservation is BLIND to pure duplication (documents the deferred B4 gap)', () => {
- test('a 2x-duplicated merge result does NOT throw today (the blindness)', () => {
- const body = 'alpha line one\nbravo line two\ncharlie line three';
- const duplicated = `${body}\n${body}`;
- expect(() => assertContentPreservation(body, body, body, duplicated)).not.toThrow();
- });
-
- test.skip('a 2x-duplicated merge result throws once the B4 growth guard lands', () => {
- const body = 'alpha line one\nbravo line two\ncharlie line three';
- const duplicated = `${body}\n${body}`;
- expect(() => assertContentPreservation(body, body, body, duplicated)).toThrow();
- });
-});
diff --git a/packages/core/src/bridge/merge-three-way.test.ts b/packages/core/src/bridge/merge-three-way.test.ts
deleted file mode 100644
index cb256e8d0..000000000
--- a/packages/core/src/bridge/merge-three-way.test.ts
+++ /dev/null
@@ -1,408 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import DiffMatchPatch from 'diff-match-patch';
-import {
- assertContentPreservation,
- BridgeMergeContentLossError,
- mergeThreeWay,
-} from './merge-three-way.ts';
-
-const BENCH_ENABLED = process.env.RUN_BENCH === '1' || process.env.RUN_BENCH === 'true';
-const describeBench = BENCH_ENABLED ? describe : describe.skip;
-
-const dmp = new DiffMatchPatch();
-dmp.Match_Threshold = 0.5;
-
-function dmpMerge(baseline: string, userText: string, agentText: string): string {
- const patches = dmp.patch_make(baseline, userText);
- const [merged] = dmp.patch_apply(patches, agentText);
- return merged;
-}
-
-describe('T1: Non-overlapping distributed edits', () => {
- const baseline = Array.from({ length: 20 }, (_, i) => `Line ${i + 1}`).join('\n');
-
- const userLines = baseline.split('\n');
- userLines.splice(5, 0, 'USER_INSERT_A');
- userLines.splice(13, 0, 'USER_INSERT_B'); // offset +1 from prior insert
- userLines.splice(20, 0, 'USER_INSERT_C'); // offset +2
- const userText = userLines.join('\n');
-
- const agentLines = baseline.split('\n');
- agentLines.splice(8, 0, 'AGENT_INSERT_A');
- agentLines.splice(16, 0, 'AGENT_INSERT_B'); // offset +1
- const agentText = agentLines.join('\n');
-
- test('hybrid mergeThreeWay preserves all 5 insertions', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- expect(merged).toContain('USER_INSERT_A');
- expect(merged).toContain('USER_INSERT_B');
- expect(merged).toContain('USER_INSERT_C');
- expect(merged).toContain('AGENT_INSERT_A');
- expect(merged).toContain('AGENT_INSERT_B');
- expect(merged).toContain('Line 1');
- expect(merged).toContain('Line 20');
- expect(merged.split('USER_INSERT_A').length - 1).toBe(1);
- expect(merged.split('AGENT_INSERT_A').length - 1).toBe(1);
- });
-
- test('DMP baseline also passes (sanity)', () => {
- const merged = dmpMerge(baseline, userText, agentText);
- expect(merged).toContain('USER_INSERT_A');
- expect(merged).toContain('USER_INSERT_B');
- expect(merged).toContain('USER_INSERT_C');
- expect(merged).toContain('AGENT_INSERT_A');
- expect(merged).toContain('AGENT_INSERT_B');
- });
-});
-
-describe('T2: Same-position concurrent inserts', () => {
- const baseline = 'Line 1\nLine 2\nLine 3';
- const userText = 'Line 1\nUSER PARAGRAPH\nLine 2\nLine 3';
- const agentText = 'Line 1\nAGENT PARAGRAPH\nLine 2\nLine 3';
-
- test('hybrid mergeThreeWay preserves BOTH insertions', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- expect(merged).toContain('USER PARAGRAPH');
- expect(merged).toContain('AGENT PARAGRAPH');
- expect(merged).toContain('Line 1');
- expect(merged).toContain('Line 2');
- expect(merged).toContain('Line 3');
- });
-
- test('DMP baseline also preserves both (sanity)', () => {
- const merged = dmpMerge(baseline, userText, agentText);
- expect(merged).toContain('USER PARAGRAPH');
- expect(merged).toContain('AGENT PARAGRAPH');
- });
-});
-
-describe('T3: D8 — identical concurrent edit', () => {
- const baseline = 'Hello world';
- const userText = 'Hello world!';
- const agentText = 'Hello world!';
-
- test('hybrid mergeThreeWay deduplicates → single "!"', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- expect(merged).toBe('Hello world!');
- });
-
- test('DMP baseline FAILS — produces "Hello world!!"', () => {
- const merged = dmpMerge(baseline, userText, agentText);
- expect(merged).toBe('Hello world!!');
- });
-});
-
-describe('T4: Emoji and Unicode content', () => {
- const baseline = 'Hello 👨‍💻 world';
- const userText = 'Hello 👨‍💻 world! 🎉';
- const agentText = 'Hello 👨‍💻 beautiful world';
-
- test('hybrid mergeThreeWay preserves all emoji and text', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- expect(merged).toContain('👨‍💻');
- expect(merged).toContain('🎉');
- expect(merged).toContain('beautiful');
- expect(merged.length).toBeGreaterThan(0);
- });
-
- test('DMP baseline also handles emoji (sanity)', () => {
- const merged = dmpMerge(baseline, userText, agentText);
- expect(merged).toContain('👨‍💻');
- });
-});
-
-describe('T5: Heavy divergence — many insertions across a 30-line doc', () => {
- const baseLines = Array.from({ length: 30 }, (_, i) => `Original line ${i + 1}`);
- const baseline = baseLines.join('\n');
-
- const userMarkers = Array.from({ length: 10 }, (_, i) => `USER_MARKER_${i}`);
- const userInsertAfter = [1, 4, 7, 10, 13, 16, 19, 22, 25, 28];
- const userLines = [...baseLines];
- for (let i = userInsertAfter.length - 1; i >= 0; i--) {
- userLines.splice(userInsertAfter[i], 0, userMarkers[i]);
- }
- const userText = userLines.join('\n');
-
- const agentMarkers = Array.from({ length: 6 }, (_, i) => `AGENT_MARKER_${i}`);
- const agentInsertAfter = [3, 8, 12, 18, 23, 27];
- const agentLines = [...baseLines];
- for (let i = agentInsertAfter.length - 1; i >= 0; i--) {
- agentLines.splice(agentInsertAfter[i], 0, agentMarkers[i]);
- }
- const agentText = agentLines.join('\n');
-
- test('all 10 user markers present', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- for (const marker of userMarkers) {
- expect(merged).toContain(marker);
- }
- });
-
- test('all 6 agent markers present', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- for (const marker of agentMarkers) {
- expect(merged).toContain(marker);
- }
- });
-
- test('no duplications', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- for (const marker of [...userMarkers, ...agentMarkers]) {
- expect(merged.split(marker).length - 1).toBe(1);
- }
- });
-
- test('all original content intact', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- for (const line of baseLines) {
- expect(merged).toContain(line);
- }
- });
-});
-
-describe('T6: Same-line modification (sub-line conflict)', () => {
- const baseline = 'The quick brown fox jumps over the lazy dog.';
- const userText = 'The fast red fox jumps over the lazy dog.';
- const agentText = 'The quick brown fox jumps over the sleepy cat.';
-
- test('hybrid mergeThreeWay preserves BOTH sub-line edits', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- expect(merged).toBe('The fast red fox jumps over the sleepy cat.');
- });
-
- test('DMP baseline also handles this (DMP is good on single lines)', () => {
- const merged = dmpMerge(baseline, userText, agentText);
- expect(merged).toBe('The fast red fox jumps over the sleepy cat.');
- });
-});
-
-describe('T7: Delete/edit conflict', () => {
- const baseline = 'Para 1\n\nThis will be edited by agent.\n\nPara 3';
- const userText = 'Para 1\n\nPara 3'; // deleted middle paragraph
- const agentText = 'Para 1\n\nAGENT EDITED this paragraph.\n\nPara 3';
-
- test('hybrid mergeThreeWay preserves agent edit (conservative)', () => {
- const merged = mergeThreeWay(baseline, userText, agentText);
- expect(merged).toContain('AGENT EDITED this paragraph.');
- expect(merged).toContain('Para 1');
- expect(merged).toContain('Para 3');
- });
-
- test('DMP baseline FAILS — produces corrupt partial line', () => {
- const merged = dmpMerge(baseline, userText, agentText);
- const hasIntactAgentEdit = merged.includes('AGENT EDITED this paragraph.');
- if (!hasIntactAgentEdit) {
- expect(merged).not.toBe('Para 1\n\nAGENT EDITED this paragraph.\n\nPara 3');
- }
- });
-});
-
-describeBench('T-perf: Performance gate', () => {
- const baseLines = Array.from(
- { length: 200 },
- (_, i) =>
- `Line ${i + 1}: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor.`,
- );
- const baseline = baseLines.join('\n');
-
- const userLines = [...baseLines];
- for (let i = 0; i < 200; i += 20) {
- userLines[i] = `USER_MODIFIED_LINE_${i}: Updated content for this line.`;
- }
- let uOffset = 0;
- for (let i = 0; i < 200; i += 40) {
- userLines.splice(i + uOffset + 1, 0, `USER_INSERTION_AT_${i}`);
- uOffset++;
- }
- const userText = userLines.join('\n');
-
- const agentLines = [...baseLines];
- for (let i = 10; i < 200; i += 33) {
- agentLines[i] = `AGENT_MODIFIED_LINE_${i}: Agent updated this line.`;
- }
- let aOffset = 0;
- for (let i = 25; i < 200; i += 67) {
- agentLines.splice(i + aOffset + 1, 0, `AGENT_INSERTION_AT_${i}`);
- aOffset++;
- }
- const agentText = agentLines.join('\n');
-
- test('p95 < 150ms over 100 iterations', () => {
- const times: number[] = [];
- mergeThreeWay(baseline, userText, agentText);
-
- for (let i = 0; i < 100; i++) {
- const start = performance.now();
- mergeThreeWay(baseline, userText, agentText);
- times.push(performance.now() - start);
- }
-
- times.sort((a, b) => a - b);
- const p50 = times[49];
- const p95 = times[94];
- const max = times[99];
-
- console.log(
- `[T-perf] mergeThreeWay 200-line doc: p50=${p50.toFixed(2)}ms p95=${p95.toFixed(2)}ms max=${max.toFixed(2)}ms`,
- );
-
- expect(p95).toBeLessThan(150);
- });
-});
-
-describe('Post-condition: assertContentPreservation', () => {
- test('happy path: result containing both sides passes', () => {
- expect(() =>
- assertContentPreservation(
- 'Hello\nworld\n',
- 'Hello\nUSER\nworld\n',
- 'Hello\nworld\nAGENT\n',
- 'Hello\nUSER\nworld\nAGENT\n',
- ),
- ).not.toThrow();
- });
-
- test('substring violation: user side missing raises BridgeMergeContentLossError with which=substring/side=user', () => {
- try {
- assertContentPreservation(
- 'Hello\nworld\n',
- 'Hello\nUSER-UNIQUE-MARKER\nworld\n',
- 'Hello\nworld\nAGENT\n',
- 'Hello\nworld\nAGENT\n',
- );
- expect(false).toBe(true); // should not reach
- } catch (err) {
- expect(err).toBeInstanceOf(BridgeMergeContentLossError);
- const e = err as BridgeMergeContentLossError;
- expect(e.info.which).toBe('substring');
- expect(e.info.side).toBe('user');
- expect(e.info.lostSubstrings.some((s) => s.includes('USER-UNIQUE-MARKER'))).toBe(true);
- }
- });
-
- test('substring violation: agent side missing raises with side=agent', () => {
- try {
- assertContentPreservation(
- 'Hello\nworld\n',
- 'Hello\nUSER\nworld\n',
- 'Hello\nworld\nAGENT-UNIQUE-MARKER\n',
- 'Hello\nUSER\nworld\n', // drops agent marker
- );
- expect(false).toBe(true);
- } catch (err) {
- expect(err).toBeInstanceOf(BridgeMergeContentLossError);
- const e = err as BridgeMergeContentLossError;
- expect(e.info.which).toBe('substring');
- expect(e.info.side).toBe('agent');
- expect(e.info.lostSubstrings.some((s) => s.includes('AGENT-UNIQUE-MARKER'))).toBe(true);
- }
- });
-
- test('order violation: user-side segments present but reordered raises which=order', () => {
- const baseline = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\n';
- const mine =
- 'MARKER-ALPHA\na\nb\nc\nd\ne\nf\ng\nh\ni\nj\nMARKER-BETA\nk\nl\nm\nn\no\np\nq\nr\ns\nt\n';
- const theirs = `${baseline}AGENT-MARKER\n`;
- const reordered =
- 'MARKER-BETA\na\nb\nc\nd\ne\nf\ng\nh\ni\nj\nMARKER-ALPHA\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nAGENT-MARKER\n';
- try {
- assertContentPreservation(baseline, mine, theirs, reordered);
- expect(false).toBe(true);
- } catch (err) {
- expect(err).toBeInstanceOf(BridgeMergeContentLossError);
- const e = err as BridgeMergeContentLossError;
- expect(e.info.which).toBe('order');
- expect(e.info.side).toBe('user');
- }
- });
-
- test('empty diffs: when no side introduced unique content, post-condition passes trivially', () => {
- expect(() => assertContentPreservation('same\n', 'same\n', 'same\n', 'same\n')).not.toThrow();
- });
-
- test('toLog() emits structured payload with expected fields; redacted by default', () => {
- try {
- assertContentPreservation(
- 'base\n',
- 'base\nSENSITIVE-CONTENT\n',
- 'base\nTHEIRS\n',
- 'base\nTHEIRS\n',
- );
- expect(false).toBe(true);
- } catch (err) {
- if (!(err instanceof BridgeMergeContentLossError)) throw err;
- const payload = err.toLog();
- expect(payload.event).toBe('bridge-merge-content-loss');
- expect(payload.which).toBe('substring');
- expect(payload.side).toBe('user');
- expect(payload.baselineLen).toBe('base\n'.length);
- expect(payload.userTextLen).toBe('base\nSENSITIVE-CONTENT\n'.length);
- expect(payload.agentTextLen).toBe('base\nTHEIRS\n'.length);
- expect(payload.resultLen).toBe('base\nTHEIRS\n'.length);
- expect(payload.redacted).toBe(true);
- expect(Array.isArray(payload.lostSubstrings)).toBe(true);
- for (const entry of payload.lostSubstrings) {
- if (typeof entry === 'string') throw new Error('default log must not leak raw substrings');
- expect(entry).toHaveProperty('len');
- expect(entry).toHaveProperty('digest');
- expect(entry.digest).toMatch(/^[0-9a-f]{8}$/);
- }
- const again = err.toLog();
- expect(again.lostSubstrings).toEqual(payload.lostSubstrings);
- }
- });
-
- test('toLog({ verbose: true }) surfaces raw substrings for opt-in verbose telemetry', () => {
- try {
- assertContentPreservation(
- 'base\n',
- 'base\nUSER-UNIQUE-MARKER\n',
- 'base\nAGENT-MARKER\n',
- 'base\nAGENT-MARKER\n',
- );
- expect(false).toBe(true);
- } catch (err) {
- if (!(err instanceof BridgeMergeContentLossError)) throw err;
- const payload = err.toLog({ verbose: true });
- expect(payload.redacted).toBe(false);
- const hasRawMarker = payload.lostSubstrings.some(
- (s) => typeof s === 'string' && s.includes('USER-UNIQUE-MARKER'),
- );
- expect(hasRawMarker).toBe(true);
- }
- });
-
- test('mergeThreeWay integration: T1-T7 scenarios satisfy the post-condition (no throw)', () => {
- const base = Array.from({ length: 10 }, (_, i) => `Line ${i + 1}`).join('\n');
- const mine = base.replace('Line 2', 'Line 2\nUSER-X');
- const theirs = base.replace('Line 7', 'Line 7\nAGENT-Y');
- expect(() => mergeThreeWay(base, mine, theirs)).not.toThrow();
-
- expect(() => mergeThreeWay('Hello world', 'Hello world!', 'Hello world!')).not.toThrow();
- });
-
- test('perf: post-condition adds <5ms p99 on a 10KB doc with ~10 segments', () => {
- const base = Array.from(
- { length: 100 },
- (_, i) => `Line ${i + 1}: lorem ipsum dolor sit amet`,
- ).join('\n');
- const mine = base
- .replace('Line 10', 'Line 10\nUSER-MARKER-A')
- .replace('Line 30', 'Line 30\nUSER-MARKER-B')
- .replace('Line 50', 'Line 50\nUSER-MARKER-C');
- const theirs = base
- .replace('Line 20', 'Line 20\nAGENT-MARKER-A')
- .replace('Line 40', 'Line 40\nAGENT-MARKER-B')
- .replace('Line 70', 'Line 70\nAGENT-MARKER-C');
- const merged = mergeThreeWay(base, mine, theirs);
-
- const times: number[] = [];
- for (let i = 0; i < 100; i++) {
- const start = performance.now();
- assertContentPreservation(base, mine, theirs, merged);
- times.push(performance.now() - start);
- }
- times.sort((a, b) => a - b);
- expect(times[99]).toBeLessThan(20); // permissive CI bound; local is <1ms
- });
-});
diff --git a/packages/core/src/bridge/normalize.test.ts b/packages/core/src/bridge/normalize.test.ts
deleted file mode 100644
index 461901f0e..000000000
--- a/packages/core/src/bridge/normalize.test.ts
+++ /dev/null
@@ -1,1114 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { loadIndentedJsxFixtures, loadLargeEmbedFixtures } from '../markdown/fixtures/index.ts';
-import { MarkdownManager } from '../markdown/index.ts';
-import {
- BRIDGE_TOLERANCE_CLASSES,
- detectAppliedToleranceClasses,
- normalizeBridge,
-} from './normalize.ts';
-
-describe('per-line trailing whitespace strip', () => {
- test('trailing spaces on a line are stripped', () => {
- expect(normalizeBridge('foo \nbar')).toBe(normalizeBridge('foo\nbar'));
- });
-
- test('trailing tabs on a line are stripped', () => {
- expect(normalizeBridge('foo\t\t\nbar')).toBe(normalizeBridge('foo\nbar'));
- });
-
- test('mixed trailing whitespace on multiple lines', () => {
- expect(normalizeBridge('a \nb \nc\t\nd')).toBe(normalizeBridge('a\nb\nc\nd'));
- });
-
- test('leading whitespace is preserved (only trailing stripped)', () => {
- expect(normalizeBridge(' foo')).toBe(' foo');
- });
-});
-
-describe('3+ newline collapse to 2 (NG1 comparison-only tolerance)', () => {
- test('three newlines collapse to two', () => {
- expect(normalizeBridge('P1\n\n\nP2')).toBe(normalizeBridge('P1\n\nP2'));
- });
-
- test('four newlines collapse to two', () => {
- expect(normalizeBridge('P1\n\n\n\nP2')).toBe(normalizeBridge('P1\n\nP2'));
- });
-
- test('many newlines collapse to two', () => {
- expect(normalizeBridge('P1\n\n\n\n\n\n\nP2')).toBe(normalizeBridge('P1\n\nP2'));
- });
-
- test('two newlines preserved (not collapsed)', () => {
- expect(normalizeBridge('P1\n\nP2')).toBe('P1\n\nP2');
- });
-
- test('single newline preserved', () => {
- expect(normalizeBridge('P1\nP2')).toBe('P1\nP2');
- });
-});
-
-describe('trailing newline policy', () => {
- test('no trailing newline', () => {
- expect(normalizeBridge('P')).toBe(normalizeBridge('P'));
- });
-
- test('single trailing newline stripped', () => {
- expect(normalizeBridge('P')).toBe(normalizeBridge('P\n'));
- });
-
- test('multiple trailing newlines stripped', () => {
- expect(normalizeBridge('P')).toBe(normalizeBridge('P\n\n\n'));
- });
-
- test('trailing newlines do not affect interior structure', () => {
- expect(normalizeBridge('P1\n\nP2\n\n\n')).toBe(normalizeBridge('P1\n\nP2'));
- });
-});
-
-describe('leading newline policy (NG1 comparison-only tolerance at doc-start)', () => {
- test('no leading newline', () => {
- expect(normalizeBridge('P')).toBe(normalizeBridge('P'));
- });
-
- test('single leading newline stripped', () => {
- expect(normalizeBridge('P')).toBe(normalizeBridge('\nP'));
- });
-
- test('multiple leading newlines stripped', () => {
- expect(normalizeBridge('P')).toBe(normalizeBridge('\n\n\nP'));
- });
-
- test('leading newlines do not affect interior structure', () => {
- expect(normalizeBridge('\n\nP1\n\nP2')).toBe(normalizeBridge('P1\n\nP2'));
- });
-
- test('two leading newlines (the canonical source-mode-burst case) tolerate to zero', () => {
- expect(normalizeBridge('\n\nC6-A-SOURCE\nC6-A-WYSIWYG\n')).toBe(
- normalizeBridge('C6-A-SOURCE\nC6-A-WYSIWYG\n'),
- );
- });
-
- test('leading + trailing newlines both stripped (symmetric edges)', () => {
- expect(normalizeBridge('\n\nfoo\n\n')).toBe(normalizeBridge('foo'));
- });
-});
-
-describe('CRLF ↔ LF normalize', () => {
- test('CRLF line endings equivalent to LF', () => {
- expect(normalizeBridge('Line1\r\nLine2\r\n')).toBe(normalizeBridge('Line1\nLine2\n'));
- });
-
- test('mixed CRLF and LF in same input', () => {
- expect(normalizeBridge('A\r\nB\nC\r\nD')).toBe(normalizeBridge('A\nB\nC\nD'));
- });
-
- test('bare \\r tolerated (Old-Mac line endings stripped)', () => {
- expect(normalizeBridge('Line1\rLine2')).toBe('Line1Line2');
- });
-
- test('CRLF with surrounding content', () => {
- expect(normalizeBridge('# Heading\r\n\r\nbody\r\n')).toBe(
- normalizeBridge('# Heading\n\nbody\n'),
- );
- });
-});
-
-describe('UTF-8 BOM strip', () => {
- test('leading BOM stripped', () => {
- expect(normalizeBridge('Hello\n')).toBe(normalizeBridge('Hello\n'));
- });
-
- test('non-leading BOM preserved (only leading stripped)', () => {
- expect(normalizeBridge('HelloWorld')).toBe('HelloWorld');
- });
-
- test('BOM with content', () => {
- expect(normalizeBridge('# Heading\n\nbody\n')).toBe(normalizeBridge('# Heading\n\nbody\n'));
- });
-
- test('bare BOM normalizes to empty', () => {
- expect(normalizeBridge('')).toBe('');
- });
-});
-
-describe('doc-start thematic break canonical (--- ↔ ***)', () => {
- test('doc-start `---` and `***` equivalent', () => {
- expect(normalizeBridge('---\nP\n')).toBe(normalizeBridge('***\nP\n'));
- });
-
- test('longer thematic breaks at doc start equivalent', () => {
- expect(normalizeBridge('-----\nP\n')).toBe(normalizeBridge('*****\nP\n'));
- });
-
- test('doc-start `---` only (no following content) equivalent to `***` only', () => {
- expect(normalizeBridge('---')).toBe(normalizeBridge('***'));
- });
-
- test('mid-doc thematic break NOT in tolerance class', () => {
- const a = normalizeBridge('P1\n\n***\n\nP2\n');
- const b = normalizeBridge('P1\n\n---\n\nP2\n');
- expect(a).not.toBe(b);
- });
-});
-
-describe('setext underline length (NOT a tolerance class — removed per FR-22 / US-011)', () => {
- test('H1 short vs long underline NOT collapsed', () => {
- expect(normalizeBridge('Title\n=====\n')).not.toBe(normalizeBridge('Title\n=\n'));
- });
-
- test('H1 11-char vs 3-char underline NOT collapsed', () => {
- expect(normalizeBridge('Title\n===========\n')).not.toBe(normalizeBridge('Title\n===\n'));
- });
-
- test('H2 short vs long underline NOT collapsed', () => {
- expect(normalizeBridge('Title\n-----\n')).not.toBe(normalizeBridge('Title\n-\n'));
- });
-
- test('byte-equal underline runs DO compare equal', () => {
- expect(normalizeBridge('Title\n=====\n')).toBe(normalizeBridge('Title\n=====\n'));
- expect(normalizeBridge('Title\n-----\n')).toBe(normalizeBridge('Title\n-----\n'));
- });
-});
-
-describe('combined tolerance classes', () => {
- test('all tolerance classes can stack', () => {
- const noisy = '---\r\nP1\r\n\r\n\r\n\r\nP2\r\n';
- const clean = '***\nP1\n\nP2\n';
- expect(normalizeBridge(noisy)).toBe(normalizeBridge(clean));
- });
-
- test('BOM with CRLF and matching setext underline', () => {
- expect(normalizeBridge('Title\r\n=====\r\n\r\nbody\r\n')).toBe(
- normalizeBridge('Title\n=====\n\nbody'),
- );
- });
-
- test('doc-start *** with CRLF and BOM', () => {
- expect(normalizeBridge('*****\r\n\r\nP\r\n')).toBe(normalizeBridge('---\n\nP'));
- });
-});
-
-describe('negative cases — substantive byte differences NOT collapsed', () => {
- test('different content does not collapse', () => {
- expect(normalizeBridge('foo')).not.toBe(normalizeBridge('bar'));
- });
-
- test('source-form delimiter classes NOT a tolerance class (`__foo__` vs `**foo**`)', () => {
- expect(normalizeBridge('__foo__\n')).not.toBe(normalizeBridge('**foo**\n'));
- });
-
- test('different blank-line structure preserved (1 vs 2)', () => {
- expect(normalizeBridge('P1\nP2')).not.toBe(normalizeBridge('P1\n\nP2'));
- });
-
- test('heading level differences preserved', () => {
- expect(normalizeBridge('# Heading\n')).not.toBe(normalizeBridge('## Heading\n'));
- });
-
- test('mid-doc *** vs mid-doc ---', () => {
- expect(normalizeBridge('A\n\n***\n\nB')).not.toBe(normalizeBridge('A\n\n---\n\nB'));
- });
-
- test('list marker differences preserved (- vs *)', () => {
- expect(normalizeBridge('- item\n')).not.toBe(normalizeBridge('* item\n'));
- });
-});
-
-describe('order of operations — BOM strip BEFORE CRLF normalize', () => {
- test('BOM with CRLF: BOM is stripped before line-based regex anchoring', () => {
- expect(normalizeBridge('---\nP\n')).toBe(normalizeBridge('---\nP\n'));
- });
-
- test('BOM with CRLF doc-start thematic break canonical applies', () => {
- expect(normalizeBridge('***\r\nP\r\n')).toBe(normalizeBridge('---\nP'));
- });
-});
-
-describe('idempotence', () => {
- test('normalizeBridge is idempotent (running twice = once)', () => {
- const inputs = [
- '',
- 'foo',
- 'P1\n\nP2',
- '# H\r\n\r\nbody\r\n',
- 'Title\n======\n',
- '---\nP',
- '***\r\n\r\nP1\r\n\r\n\r\nP2\r\n\r\n\r\n',
- ];
- for (const input of inputs) {
- const once = normalizeBridge(input);
- const twice = normalizeBridge(once);
- expect(twice).toBe(once);
- }
- });
-});
-
-describe('block-separator-collapse — `\\n[marker]` ≡ `\\n\\n[marker]`', () => {
- test('heading: `\\n# H` equivalent to `\\n\\n# H`', () => {
- expect(normalizeBridge('P\n# H\n')).toBe(normalizeBridge('P\n\n# H\n'));
- });
-
- test('blockquote: `\\n> q` equivalent to `\\n\\n> q`', () => {
- expect(normalizeBridge('P\n> q\n')).toBe(normalizeBridge('P\n\n> q\n'));
- });
-
- test('unordered list: `\\n- item` equivalent to `\\n\\n- item`', () => {
- expect(normalizeBridge('P\n- item\n')).toBe(normalizeBridge('P\n\n- item\n'));
- });
-
- test('fenced code: `\\n```ts` equivalent to `\\n\\n```ts`', () => {
- expect(normalizeBridge('P\n```ts\nx\n```\n')).toBe(normalizeBridge('P\n\n```ts\nx\n```\n'));
- });
-
- test('ordered list: `\\n1. item` equivalent to `\\n\\n1. item`', () => {
- expect(normalizeBridge('P\n1. item\n')).toBe(normalizeBridge('P\n\n1. item\n'));
- });
-
- test('plus-marker list: `\\n+ item` equivalent to `\\n\\n+ item`', () => {
- expect(normalizeBridge('P\n+ item\n')).toBe(normalizeBridge('P\n\n+ item\n'));
- });
-
- test('tilde fence: `\\n~~~` equivalent to `\\n\\n~~~`', () => {
- expect(normalizeBridge('P\n~~~ts\nx\n~~~\n')).toBe(normalizeBridge('P\n\n~~~ts\nx\n~~~\n'));
- });
-
- test('NEGATIVE: `text\\nmore` NOT equivalent to `text\\n\\nmore` (paragraph soft break preserved)', () => {
- expect(normalizeBridge('text\nmore')).not.toBe(normalizeBridge('text\n\nmore'));
- });
-
- test('reverse: heading line followed by paragraph (`## H\\nP` ≡ `## H\\n\\nP`)', () => {
- expect(normalizeBridge('## H\nP\n')).toBe(normalizeBridge('## H\n\nP\n'));
- });
-
- test('reverse: blockquote line followed by paragraph (`> q\\nP` ≡ `> q\\n\\nP`)', () => {
- expect(normalizeBridge('> q\nP\n')).toBe(normalizeBridge('> q\n\nP\n'));
- });
-
- test('reverse: list item followed by paragraph (`- item\\nP` ≡ `- item\\n\\nP`)', () => {
- expect(normalizeBridge('- item\nP\n')).toBe(normalizeBridge('- item\n\nP\n'));
- });
-
- test('reverse: ordered list item followed by paragraph (`1. item\\nP` ≡ `1. item\\n\\nP`)', () => {
- expect(normalizeBridge('1. item\nP\n')).toBe(normalizeBridge('1. item\n\nP\n'));
- });
-
- test('reverse: plus-marker list item followed by paragraph (`+ item\\nP` ≡ `+ item\\n\\nP`)', () => {
- expect(normalizeBridge('+ item\nP\n')).toBe(normalizeBridge('+ item\n\nP\n'));
- });
-
- test('reverse: tilde fence open followed by content (`~~~ts\\nx` ≡ `~~~ts\\n\\nx`)', () => {
- expect(normalizeBridge('~~~ts\nx\n~~~\n')).toBe(normalizeBridge('~~~ts\n\nx\n~~~\n'));
- });
-
- test('S6 fixture shape: heading line followed by long paragraph', () => {
- const ytext =
- '## Section 1 — Lorem elit labore minim\nEa reprehenderit pariatur sunt id amet.\n';
- const frag =
- '## Section 1 — Lorem elit labore minim\n\nEa reprehenderit pariatur sunt id amet.\n';
- expect(normalizeBridge(ytext)).toBe(normalizeBridge(frag));
- });
-});
-
-describe('commonmark-escape collapse', () => {
- test('escaped underscore equivalent to plain underscore', () => {
- expect(normalizeBridge('init_spike')).toBe(normalizeBridge('init\\_spike'));
- });
-
- test('escaped tilde equivalent to plain tilde', () => {
- expect(normalizeBridge('~3000')).toBe(normalizeBridge('\\~3000'));
- });
-
- test('multiple escaped tildes equivalent to multiple plain tildes', () => {
- expect(normalizeBridge('~~~')).toBe(normalizeBridge('\\~\\~\\~'));
- });
-
- test('escaped underscore in identifier-like text', () => {
- expect(normalizeBridge('STOP_IF')).toBe(normalizeBridge('STOP\\_IF'));
- });
-
- test('mdast strip-on-inline-code direction: `\\|---\\|` equivalent to `|---|`', () => {
- expect(normalizeBridge('`\\|---\\|---\\|`')).toBe(normalizeBridge('`|---|---|`'));
- });
-
- test('NEGATIVE: backslash before a NON-escapable char is preserved', () => {
- expect(normalizeBridge('a\\b')).toBe('a\\b');
- expect(normalizeBridge('a\\b')).not.toBe(normalizeBridge('ab'));
- });
-
- test('NEGATIVE: plain text without backslashes is preserved', () => {
- expect(normalizeBridge('plain text')).toBe('plain text');
- });
-
- test('idempotent: applying collapse to already-collapsed string is a no-op', () => {
- expect(normalizeBridge(normalizeBridge('init\\_spike'))).toBe(normalizeBridge('init_spike'));
- });
-});
-
-describe('table-align-row-spacing collapse', () => {
- test('unpadded equivalent to padded alignment row', () => {
- expect(normalizeBridge('|---|---|')).toBe(normalizeBridge('| --- | --- |'));
- });
-
- test('three-column alignment row, unpadded vs padded', () => {
- expect(normalizeBridge('|---|---|---|')).toBe(normalizeBridge('| --- | --- | --- |'));
- });
-
- test('colon markers preserved: left-align ↔ left-align', () => {
- expect(normalizeBridge('|:---|:---|')).toBe(normalizeBridge('| :--- | :--- |'));
- });
-
- test('colon markers preserved: right-align ↔ right-align', () => {
- expect(normalizeBridge('|---:|---:|')).toBe(normalizeBridge('| ---: | ---: |'));
- });
-
- test('colon markers preserved: center-align ↔ center-align', () => {
- expect(normalizeBridge('|:---:|:---:|')).toBe(normalizeBridge('| :---: | :---: |'));
- });
-
- test('mixed colon markers across columns', () => {
- expect(normalizeBridge('|:---:|---:|:---|')).toBe(normalizeBridge('| :---: | ---: | :--- |'));
- });
-
- test('whole markdown table with header + alignment row + data rows', () => {
- const unpadded = '|Name|Age|\n|---|---|\n|Alice|30|\n';
- const padded = '|Name|Age|\n| --- | --- |\n|Alice|30|\n';
- expect(normalizeBridge(unpadded)).toBe(normalizeBridge(padded));
- });
-
- test('NEGATIVE: a paragraph line that is NOT an alignment row keeps its whitespace', () => {
- expect(normalizeBridge('a - b | c')).toBe('a - b | c');
- });
-
- test('NEGATIVE: single-column "table" with no separator is NOT collapsed', () => {
- expect(normalizeBridge('|---|')).toBe('|---|');
- });
-});
-
-describe('emphasis-around-code flatten', () => {
- test('strong wrapper around inline code equivalent to bare inline code', () => {
- expect(normalizeBridge('**`text-indent`**')).toBe(normalizeBridge('`text-indent`'));
- });
-
- test('strong wrapper with surrounding whitespace collapses identically', () => {
- expect(normalizeBridge('** `code` **')).toBe(normalizeBridge('`code`'));
- });
-
- test('NEGATIVE: strong wrapping non-code content is preserved', () => {
- expect(normalizeBridge('**bold text**')).toBe('**bold text**');
- expect(normalizeBridge('**bold text**')).not.toBe(normalizeBridge('bold text'));
- });
-
- test('NEGATIVE: strong wrapping multiple inline-code spans is preserved', () => {
- expect(normalizeBridge('**`a` and `b`**')).toBe('**`a` and `b`**');
- });
-});
-
-describe('list-indent canonical collapse', () => {
- test('6-space-indented list item equivalent to 3-space-indented', () => {
- expect(normalizeBridge(' - nested item')).toBe(normalizeBridge(' - nested item'));
- });
-
- test('top-level list item unchanged', () => {
- expect(normalizeBridge('- item')).toBe('- item');
- });
-
- test('whole nested list block collapses identically', () => {
- const sixSpace = '- a\n - nested\n - also nested\n- b\n';
- const threeSpace = '- a\n - nested\n - also nested\n- b\n';
- expect(normalizeBridge(sixSpace)).toBe(normalizeBridge(threeSpace));
- });
-
- test('tab-indented list item equivalent to 3-space-indented', () => {
- expect(normalizeBridge('\t- nested item')).toBe(normalizeBridge(' - nested item'));
- });
-
- test('ordered list with deep indent collapses', () => {
- expect(normalizeBridge(' 1. nested item')).toBe(normalizeBridge(' 1. nested item'));
- });
-
- test('asterisk-marker list with deep indent collapses', () => {
- expect(normalizeBridge(' * nested item')).toBe(normalizeBridge(' * nested item'));
- });
-
- test('plus-marker list with deep indent collapses', () => {
- expect(normalizeBridge(' + nested item')).toBe(normalizeBridge(' + nested item'));
- });
-
- test('Pandoc alphabetic marker with deep indent collapses', () => {
- expect(normalizeBridge(' a. nested item')).toBe(normalizeBridge(' a. nested item'));
- });
-
- test('NEGATIVE: non-list-marker line with leading whitespace is preserved', () => {
- expect(normalizeBridge(' plain paragraph line')).toBe(' plain paragraph line');
- });
-});
-
-describe('ordered-list-marker-number canonical collapse', () => {
- test('lazy `1./1.` equivalent to renumbered `1./2.`', () => {
- expect(normalizeBridge('# Todo\n\n1. first\n1. second\n')).toBe(
- normalizeBridge('# Todo\n\n1. first\n2. second\n'),
- );
- });
-
- test('three-item lazy `1./1./1.` equivalent to renumbered `1./2./3.`', () => {
- expect(normalizeBridge('1. a\n1. b\n1. c\n')).toBe(normalizeBridge('1. a\n2. b\n3. c\n'));
- });
-
- test('start offset preserved: `5./5.` equivalent to `5./6.`', () => {
- expect(normalizeBridge('5. a\n5. b\n')).toBe(normalizeBridge('5. a\n6. b\n'));
- });
-
- test('paren-delimiter `1)` markers renumber-tolerate too', () => {
- expect(normalizeBridge('1) a\n1) b\n')).toBe(normalizeBridge('1) a\n2) b\n'));
- });
-
- test('nested ordered list renumber collapses (mirrors list-indent nesting coverage)', () => {
- const lazy = '1. a\n 1. x\n 1. y\n1. b\n';
- const renum = '1. a\n 1. x\n 2. y\n2. b\n';
- expect(normalizeBridge(lazy)).toBe(normalizeBridge(renum));
- });
-
- test('deep-indented ordered item renumber collapses', () => {
- expect(normalizeBridge(' 1. nested')).toBe(normalizeBridge(' 9. nested'));
- });
-
- test('NEGATIVE: content after the marker differing is NOT collapsed', () => {
- expect(normalizeBridge('1. first\n')).not.toBe(normalizeBridge('1. second\n'));
- expect(normalizeBridge('1. apple\n2. banana\n')).not.toBe(
- normalizeBridge('1. apple\n2. cherry\n'),
- );
- });
-
- test('NEGATIVE: marker-type `.` vs `)` is NOT collapsed', () => {
- expect(normalizeBridge('1. a\n')).not.toBe(normalizeBridge('1) a\n'));
- });
-
- test('NEGATIVE: ordered marker vs bullet marker is NOT collapsed', () => {
- expect(normalizeBridge('1. a\n')).not.toBe(normalizeBridge('- a\n'));
- });
-
- test('NEGATIVE: adding or removing a line is NOT collapsed', () => {
- expect(normalizeBridge('1. a\n1. b\n')).not.toBe(normalizeBridge('1. a\n'));
- expect(normalizeBridge('1. a\n')).not.toBe(normalizeBridge('1. a\n1. b\n'));
- });
-
- test('NEGATIVE: a digit run that is not an ordered-list marker is preserved', () => {
- expect(normalizeBridge('Version 2 shipped\n')).toBe('Version 2 shipped');
- expect(normalizeBridge('Version 2 shipped\n')).not.toBe(normalizeBridge('Version 3 shipped\n'));
- });
-});
-
-describe('detectAppliedToleranceClasses (FR-41)', () => {
- test('exposes the class enum for bounded-cardinality emit consumers', () => {
- expect(BRIDGE_TOLERANCE_CLASSES).toEqual([
- 'bom',
- 'crlf',
- 'commonmark-escape',
- 'emphasis-around-code',
- 'leading-newline',
- 'doc-start-thematic',
- 'block-separator-collapse',
- 'table-align-row-spacing',
- 'row-no-trailing-pipe',
- 'list-indent-canonical',
- 'ordered-list-marker-number',
- 'paragraph-continuation-indent',
- 'jsx-container-boundary-blank',
- 'trailing-whitespace',
- 'blank-line-collapse',
- 'trailing-newline',
- ]);
- });
-
- test('detects ordered-list-marker-number when marker digits differ across inputs', () => {
- expect(detectAppliedToleranceClasses('1. a\n1. b', '1. a\n2. b')).toContain(
- 'ordered-list-marker-number',
- );
- expect(detectAppliedToleranceClasses(' 1. x\n 1. y', ' 1. x\n 2. y')).toContain(
- 'ordered-list-marker-number',
- );
- });
-
- test('does not detect ordered-list-marker-number when no ordered marker is present', () => {
- expect(detectAppliedToleranceClasses('- a\n- b', '- a\n- b')).not.toContain(
- 'ordered-list-marker-number',
- );
- expect(detectAppliedToleranceClasses('foo', 'foo')).not.toContain('ordered-list-marker-number');
- });
-
- test('detects emphasis-around-code when either input contains the tight wrap', () => {
- expect(detectAppliedToleranceClasses('**`text-indent`**', '`text-indent`')).toContain(
- 'emphasis-around-code',
- );
- });
-
- test('detects list-indent-canonical when either input has an indented list item', () => {
- expect(detectAppliedToleranceClasses(' - foo', ' - foo')).toContain(
- 'list-indent-canonical',
- );
- expect(detectAppliedToleranceClasses('\t- foo', '- foo')).toContain('list-indent-canonical');
- });
-
- test('does not detect list-indent-canonical when neither input has indent', () => {
- expect(detectAppliedToleranceClasses('- foo', '- foo')).not.toContain('list-indent-canonical');
- });
-
- test('detects paragraph-continuation-indent when a lazy-continuation indent is present', () => {
- expect(
- detectAppliedToleranceClasses(
- 'para line\n continuation text\n',
- 'para line\ncontinuation text\n',
- ),
- ).toContain('paragraph-continuation-indent');
- });
-
- test('does not detect paragraph-continuation-indent when no continuation indent is present', () => {
- expect(
- detectAppliedToleranceClasses(
- 'para line\ncontinuation text\n',
- 'para line\ncontinuation text\n',
- ),
- ).not.toContain('paragraph-continuation-indent');
- });
-
- test('detects commonmark-escape when present in either input', () => {
- expect(detectAppliedToleranceClasses('init\\_spike', 'init_spike')).toContain(
- 'commonmark-escape',
- );
- expect(detectAppliedToleranceClasses('init_spike', 'init\\_spike')).toContain(
- 'commonmark-escape',
- );
- expect(detectAppliedToleranceClasses('\\~5ms', '~5ms')).toContain('commonmark-escape');
- });
-
- test('does not detect commonmark-escape for unrelated backslashes', () => {
- expect(detectAppliedToleranceClasses('a\\b', 'a\\b')).not.toContain('commonmark-escape');
- });
-
- test('detects table-align-row-spacing when either input contains an alignment row', () => {
- expect(detectAppliedToleranceClasses('|---|---|', '| --- | --- |')).toContain(
- 'table-align-row-spacing',
- );
- expect(detectAppliedToleranceClasses('|:---|---:|', '| :--- | ---: |')).toContain(
- 'table-align-row-spacing',
- );
- });
-
- test('does not detect table-align-row-spacing for paragraph content', () => {
- expect(detectAppliedToleranceClasses('a - b | c', 'a - b | c')).not.toContain(
- 'table-align-row-spacing',
- );
- });
-
- test('detects block-separator-collapse when one side has `\\n\\n[marker]` and the other has `\\n[marker]`', () => {
- expect(detectAppliedToleranceClasses('P\n# H', 'P\n\n# H')).toContain(
- 'block-separator-collapse',
- );
- expect(detectAppliedToleranceClasses('P\n\n- item', 'P\n- item')).toContain(
- 'block-separator-collapse',
- );
- });
-
- test('does not detect block-separator-collapse when both sides have the same separator shape', () => {
- expect(detectAppliedToleranceClasses('P\n\n# H', 'P\n\n# H')).not.toContain(
- 'block-separator-collapse',
- );
- expect(detectAppliedToleranceClasses('P\n# H', 'P\n# H')).not.toContain(
- 'block-separator-collapse',
- );
- });
-
- test('detects BOM when present in either input', () => {
- expect(detectAppliedToleranceClasses('foo', 'foo')).toContain('bom');
- expect(detectAppliedToleranceClasses('foo', 'foo')).toContain('bom');
- });
-
- test('detects CRLF when carriage returns present', () => {
- expect(detectAppliedToleranceClasses('a\r\nb', 'a\nb')).toContain('crlf');
- expect(detectAppliedToleranceClasses('a\nb', 'a\r\nb')).toContain('crlf');
- });
-
- test('detects leading-newline when one input has it and the other does not', () => {
- expect(detectAppliedToleranceClasses('\n\nfoo', 'foo')).toContain('leading-newline');
- expect(detectAppliedToleranceClasses('foo', '\nfoo')).toContain('leading-newline');
- });
-
- test('does not detect leading-newline when both inputs have it equally', () => {
- expect(detectAppliedToleranceClasses('\n\nfoo', '\n\nfoo')).not.toContain('leading-newline');
- });
-
- test('detects doc-start-thematic when one is *** and the other is ---', () => {
- expect(detectAppliedToleranceClasses('***\nP', '---\nP')).toContain('doc-start-thematic');
- expect(detectAppliedToleranceClasses('---\nP', '***\nP')).toContain('doc-start-thematic');
- });
-
- test('does not detect doc-start-thematic when neither input starts with *** or ---', () => {
- expect(detectAppliedToleranceClasses('foo\nP', 'foo\nP')).not.toContain('doc-start-thematic');
- });
-
- test('detects trailing-whitespace via mid-line indicator', () => {
- expect(detectAppliedToleranceClasses('foo \nbar', 'foo\nbar')).toContain('trailing-whitespace');
- expect(detectAppliedToleranceClasses('foo\t\nbar', 'foo\nbar')).toContain(
- 'trailing-whitespace',
- );
- });
-
- test('detects trailing-whitespace via end-of-string indicator (no trailing newline)', () => {
- expect(detectAppliedToleranceClasses('foo ', 'foo')).toContain('trailing-whitespace');
- });
-
- test('does not double-emit trailing-whitespace when both indicators apply', () => {
- const classes = detectAppliedToleranceClasses('foo \nbar ', 'foo\nbar');
- expect(classes.filter((c) => c === 'trailing-whitespace')).toHaveLength(1);
- });
-
- test('detects blank-line-collapse via 3+ newlines', () => {
- expect(detectAppliedToleranceClasses('a\n\n\nb', 'a\n\nb')).toContain('blank-line-collapse');
- expect(detectAppliedToleranceClasses('a\n\n\n\n\nb', 'a\n\nb')).toContain(
- 'blank-line-collapse',
- );
- });
-
- test('detects trailing-newline when exactly one input has trailing \\n', () => {
- expect(detectAppliedToleranceClasses('foo\n', 'foo')).toContain('trailing-newline');
- expect(detectAppliedToleranceClasses('foo', 'foo\n')).toContain('trailing-newline');
- });
-
- test('does not detect trailing-newline when both inputs have trailing \\n', () => {
- expect(detectAppliedToleranceClasses('foo\n', 'foo\n')).not.toContain('trailing-newline');
- });
-
- test('returns empty array for byte-equal inputs', () => {
- expect(detectAppliedToleranceClasses('foo', 'foo')).toEqual([]);
- expect(detectAppliedToleranceClasses('', '')).toEqual([]);
- });
-
- test('returns multiple classes when multiple differences apply', () => {
- const classes = detectAppliedToleranceClasses('\n\nfoo \r\n\r\n\r\nbar ', 'foo\n\nbar');
- expect(classes).toContain('bom');
- expect(classes).toContain('crlf');
- expect(classes).toContain('leading-newline');
- expect(classes).toContain('trailing-whitespace');
- expect(classes).toContain('blank-line-collapse');
- });
-
- test('returns only valid class labels (bounded cardinality)', () => {
- const classes = detectAppliedToleranceClasses(
- '\n\n***\nfoo \r\n\r\n\r\nbar ',
- '---\nfoo\n\nbar',
- );
- for (const cls of classes) {
- expect(BRIDGE_TOLERANCE_CLASSES).toContain(cls);
- }
- });
-
- test('detects jsx-container-boundary-blank when a boundary blank adjacent to a JSX tag is present', () => {
- expect(
- detectAppliedToleranceClasses(
- '<Steps>\n\n <Step>\n a\n </Step>\n</Steps>',
- '<Steps>\n <Step>\n a\n </Step>\n</Steps>',
- ),
- ).toContain('jsx-container-boundary-blank');
- });
-
- test('does not detect jsx-container-boundary-blank for plain blank-separated content', () => {
- expect(detectAppliedToleranceClasses('para\n\ntext', 'para\n\ntext')).not.toContain(
- 'jsx-container-boundary-blank',
- );
- });
-
- test('jsx-container-boundary-blank detection is linear, not quadratic, on large angle-bracket-heavy input', () => {
- const genericHeavy = 'Promise<Array<Map<Set<Record '.repeat(8000); // ~232 KB
- const attributeHeavy = '<Ax '.repeat(32000); // ~128 KB, exercises the \\s[^>]* path
- const start = performance.now();
- detectAppliedToleranceClasses(genericHeavy, genericHeavy);
- detectAppliedToleranceClasses(attributeHeavy, attributeHeavy);
- const elapsedMs = performance.now() - start;
- expect(elapsedMs).toBeLessThan(250);
- });
-});
-
-describe('paragraph lazy-continuation indent (step 7f)', () => {
- test('single leading space on a paragraph continuation line is tolerated', () => {
- expect(normalizeBridge('para two\n continuation text\n')).toBe(
- normalizeBridge('para two\ncontinuation text\n'),
- );
- });
-
- test('multi-space and tab continuations are tolerated', () => {
- expect(normalizeBridge('para two\n continuation\n')).toBe(
- normalizeBridge('para two\ncontinuation\n'),
- );
- expect(normalizeBridge('para two\n\tcontinuation\n')).toBe(
- normalizeBridge('para two\ncontinuation\n'),
- );
- });
-
- test('the observed fuzz construct: continuation born at a chunk boundary', () => {
- const raw = 'lorem ipsum\n\nM2-foxtrot delta\n dolor sit amet lorem\n';
- const canonical = 'lorem ipsum\n\nM2-foxtrot delta\ndolor sit amet lorem\n';
- expect(normalizeBridge(raw)).toBe(normalizeBridge(canonical));
- });
-
- test('does NOT fire under a blank line (indented code is engine-preserved)', () => {
- expect(normalizeBridge('para\n\n indented code\n')).not.toBe(
- normalizeBridge('para\n\nindented code\n'),
- );
- });
-
- test('does NOT fire inside fenced-code interiors', () => {
- expect(normalizeBridge('```\ncode\n indented in fence\n```\n')).not.toBe(
- normalizeBridge('```\ncode\nindented in fence\n```\n'),
- );
- });
-
- test('does NOT fire when de-indenting would mint a setext underline', () => {
- expect(normalizeBridge('foo\n ---\n')).not.toBe(normalizeBridge('foo\n---\n'));
- expect(normalizeBridge('foo\n ===\n')).not.toBe(normalizeBridge('foo\n===\n'));
- });
-
- test('does NOT fire when the previous line is an ATX heading (no paragraph to continue)', () => {
- expect(normalizeBridge('# Heading\n indented text\n')).not.toBe(
- normalizeBridge('# Heading\nindented text\n'),
- );
- });
-
- test('blockquote lazy continuation stays OUT of class (prefix-add, not strip)', () => {
- expect(normalizeBridge('> a\n b\n')).not.toBe(normalizeBridge('> a\n> b\n'));
- });
-
- test('list-marker indents stay owned by step 7c, not stripped as continuations', () => {
- expect(normalizeBridge('para\n - item\n')).toBe('para\n- item');
- });
-});
-
-describe('JSX-container boundary-blank fold (step 7g)', () => {
- test('blank inside a single Step folds to the single-newline form (M2)', () => {
- const authored = '<Steps>\n\n <Step>\n\n Install the package.\n\n </Step>\n\n</Steps>\n';
- const serialized = '<Steps>\n <Step>\n Install the package.\n </Step>\n</Steps>\n';
- expect(normalizeBridge(authored)).toBe(normalizeBridge(serialized));
- });
-
- test('heading-less flush-left multi-Step (github-sync shape) folds', () => {
- const authored =
- '<Steps>\n\n<Step>\n\nClone the repo.\n\n</Step>\n\n<Step>\n\nInstall dependencies.\n\n</Step>\n\n</Steps>\n';
- const serialized =
- '<Steps>\n <Step>\n Clone the repo.\n </Step>\n <Step>\n Install dependencies.\n </Step>\n</Steps>\n';
- expect(normalizeBridge(authored)).toBe(normalizeBridge(serialized));
- });
-
- test('Tabs/Tab container folds (tag name independent)', () => {
- const authored = '<Tabs>\n\n <Tab value="npm">\n\n npm install\n\n </Tab>\n\n</Tabs>\n';
- const serialized = '<Tabs>\n <Tab value="npm">\n npm install\n </Tab>\n</Tabs>\n';
- expect(normalizeBridge(authored)).toBe(normalizeBridge(serialized));
- });
-
- test('depth-2 nested containers fold', () => {
- const authored =
- '<Steps>\n\n <Step>\n\n <Tabs>\n\n <Tab value="a">\n\n body\n\n </Tab>\n\n </Tabs>\n\n </Step>\n\n</Steps>\n';
- const serialized =
- '<Steps>\n <Step>\n <Tabs>\n <Tab value="a">\n body\n </Tab>\n </Tabs>\n </Step>\n</Steps>\n';
- expect(normalizeBridge(authored)).toBe(normalizeBridge(serialized));
- });
-
- test('a closing tag indented after a fenced code block folds (tag de-indent is neighbor-independent)', () => {
- const indentedClose = '<Steps>\n<Step>\n```ts\nconst x = 1;\n```\n </Step>\n</Steps>\n';
- const flushClose = '<Steps>\n<Step>\n```ts\nconst x = 1;\n```\n</Step>\n</Steps>\n';
- expect(normalizeBridge(indentedClose)).toBe(normalizeBridge(flushClose));
- });
-
- test('GUARD (a): a doc-level CommonMark 4.4 indented-code block (depth 0) is NOT folded', () => {
- expect(normalizeBridge('intro\n\n code line\n')).not.toBe(
- normalizeBridge('intro\n\n code line\n'),
- );
- });
-
- test('GUARD (a2): a depth-0 INDENTED line that trims to a JSX tag is doc-level indented code, NOT a container open', () => {
- expect(normalizeBridge('intro\n\n <Step>\n\n body')).not.toBe(
- normalizeBridge('intro\n\n <Step>\n body'),
- );
- });
-
- test('GUARD (d): a self-closing tag does NOT open a container depth (the !endsWith("/>") guard)', () => {
- expect(normalizeBridge('<Icon />\n\nParagraph after icon.\n')).not.toBe(
- normalizeBridge('<Icon />\nParagraph after icon.\n'),
- );
- });
-
- test('GUARD (b): 7g folds the <Step> scaffolding but a nested blockquote continuation indent stays divergent', () => {
- const authored = '<Steps>\n\n<Step>\n\n> quote line\n continuation\n\n</Step>\n\n</Steps>\n';
- const scaffoldFolded = '<Steps>\n<Step>\n> quote line\n continuation\n</Step>\n</Steps>\n';
- expect(normalizeBridge(authored)).toBe(normalizeBridge(scaffoldFolded));
- const tight = '<Steps>\n\n<Step>\n\n> quote line\ncontinuation\n\n</Step>\n\n</Steps>\n';
- expect(normalizeBridge(authored)).not.toBe(normalizeBridge(tight));
- expect(normalizeBridge('> quote line\n continuation\n')).not.toBe(
- normalizeBridge('> quote line\ncontinuation\n'),
- );
- });
-
- test('GUARD (c): 7g folds the <Step> scaffolding but a nested loose-list continuation indent stays divergent', () => {
- const authored = '<Steps>\n\n<Step>\n\n- item\n continuation\n\n</Step>\n\n</Steps>\n';
- const scaffoldFolded = '<Steps>\n<Step>\n- item\n continuation\n</Step>\n</Steps>\n';
- expect(normalizeBridge(authored)).toBe(normalizeBridge(scaffoldFolded));
- const tight = '<Steps>\n\n<Step>\n\n- item\ncontinuation\n\n</Step>\n\n</Steps>\n';
- expect(normalizeBridge(authored)).not.toBe(normalizeBridge(tight));
- expect(normalizeBridge('- item\n continuation\n')).not.toBe(
- normalizeBridge('- item\ncontinuation\n'),
- );
- });
-
- test('lowercase <details> is NOT a JSX container (boundary blanks preserved)', () => {
- expect(normalizeBridge('<details>\n\n<summary>T</summary>\n\nbody\n\n</details>\n')).not.toBe(
- normalizeBridge('<details>\n<summary>T</summary>\nbody\n</details>\n'),
- );
- });
-
- test('does NOT drop a blank line inside a fenced-code interior within a container', () => {
- const withBlank =
- '<Steps>\n\n <Step>\n\n ```ts\n const a = 1;\n\n const b = 2;\n ```\n\n </Step>\n\n</Steps>\n';
- const noBlank =
- '<Steps>\n\n <Step>\n\n ```ts\n const a = 1;\n const b = 2;\n ```\n\n </Step>\n\n</Steps>\n';
- expect(normalizeBridge(withBlank)).not.toBe(normalizeBridge(noBlank));
- });
-
- test('residual is correct: inside-container deep-indented content folds (container consumes relative indent, parses to a paragraph)', () => {
- const eight = '<Steps>\n\n <Step>\n\n code\n\n </Step>\n\n</Steps>\n';
- const twelve = '<Steps>\n\n <Step>\n\n code\n\n </Step>\n\n</Steps>\n';
- expect(normalizeBridge(eight)).toBe(normalizeBridge(twelve));
- });
-
- test('a large consecutive-blank run inside a container folds identically to a single boundary blank (linear scan, no quadratic re-walk)', () => {
- const big = `<Steps>\n${'\n'.repeat(4000)}<Step>\n\nbody paragraph.\n\n</Step>\n\n</Steps>\n`;
- const single = '<Steps>\n\n<Step>\n\nbody paragraph.\n\n</Step>\n\n</Steps>\n';
- expect(normalizeBridge(big)).toBe(normalizeBridge(single));
- });
-});
-
-describe('fence-opener tracking (CommonMark 4.5: closer must match opener char)', () => {
- const FENCED_A = '```ts\n~~~\n| a | b |\n| - | - |\n| 1 | 2\n```\n';
- const FENCED_B = '```ts\n~~~\n| a | b |\n| - | - |\n| 1 | 2 |\n```\n';
-
- test('does not absorb a trailing-pipe divergence inside an interleaved fence', () => {
- expect(normalizeBridge(FENCED_A)).not.toBe(normalizeBridge(FENCED_B));
- });
-
- test('still tolerates the divergence outside any fence (control)', () => {
- const a = '| a | b |\n| - | - |\n| 1 | 2\n';
- const b = '| a | b |\n| - | - |\n| 1 | 2 |\n';
- expect(normalizeBridge(a)).toBe(normalizeBridge(b));
- });
-
- test('matching-char closers still close the fence (control)', () => {
- const a = '```ts\ncode\n```\n\n| a | b |\n| - | - |\n| 1 | 2\n';
- const b = '```ts\ncode\n```\n\n| a | b |\n| - | - |\n| 1 | 2 |\n';
- expect(normalizeBridge(a)).toBe(normalizeBridge(b));
- });
-});
-
-describe('table-row trailing-pipe tolerance (row-no-trailing-pipe)', () => {
- const mm = new MarkdownManager({ extensions: sharedExtensions });
-
- const NON_UNIFORM = '# Notes\n\n| a | b |\n| - | - |\n| 1 | 2\n';
- const UNIFORM = '# Notes\n\n| a | b\n| - | -\n| 1 | 2\n';
-
- function semanticTree(md: string): unknown {
- const strip = (node: unknown): unknown => {
- if (Array.isArray(node)) return node.map(strip);
- if (!node || typeof node !== 'object') return node;
- const out: Record<string, unknown> = {};
- for (const [k, v] of Object.entries(node as Record<string, unknown>)) {
- if (k === 'position' || k === 'data') continue;
- out[k] = strip(v);
- }
- return out;
- };
- return strip(mm.parseToMdast(md));
- }
-
- function stripTableCaptureAttrs(json: unknown): void {
- if (!json || typeof json !== 'object') return;
- const node = json as { type?: string; attrs?: Record<string, unknown>; content?: unknown[] };
- if (node.type === 'table' && node.attrs) {
- node.attrs.sourceOuterPipes = null;
- }
- for (const child of node.content ?? []) stripTableCaptureAttrs(child);
- }
-
- test('non-uniform witness ≡ its real serialize(parse) twin (β-proof: bytes differ, comparator equal, parses structurally equal)', () => {
- const canon = mm.serialize(mm.parse(NON_UNIFORM));
- expect(canon).not.toBe(NON_UNIFORM);
- expect(normalizeBridge(NON_UNIFORM)).toBe(normalizeBridge(canon));
- expect(semanticTree(NON_UNIFORM)).toEqual(semanticTree(canon));
- });
-
- test('uniform witness ≡ the capture-attr-loss canonical twin (every row gains a bare trailing pipe)', () => {
- const stripped = mm.parse(UNIFORM) as unknown as Record<string, unknown>;
- stripTableCaptureAttrs(stripped);
- const canon = mm.serialize(stripped);
- expect(canon).toBe('# Notes\n\n| a | b|\n| - | -|\n| 1 | 2|\n');
- expect(normalizeBridge(UNIFORM)).toBe(normalizeBridge(canon));
- expect(semanticTree(UNIFORM)).toEqual(semanticTree(canon));
- });
-
- test('padded trailing pipe on a data row tolerated in table context', () => {
- expect(normalizeBridge('| a | b |\n| - | - |\n| 1 | 2 |')).toBe(
- normalizeBridge('| a | b |\n| - | - |\n| 1 | 2'),
- );
- });
-
- test('delimiter-row trailing pipe tolerated in table context', () => {
- expect(normalizeBridge('| a | b\n| - | -|\n| 1 | 2')).toBe(
- normalizeBridge('| a | b\n| - | -\n| 1 | 2'),
- );
- });
-
- test('fully piped table ≡ uniformly unpiped-trailing table (per-row class application)', () => {
- expect(normalizeBridge('| a | b |\n| - | - |\n| 1 | 2 |\n| 3 | 4 |')).toBe(
- normalizeBridge('| a | b\n| - | -\n| 1 | 2\n| 3 | 4'),
- );
- });
-
- test('normalizeBridge stays idempotent with the class active', () => {
- for (const input of [
- NON_UNIFORM,
- UNIFORM,
- mm.serialize(mm.parse(NON_UNIFORM)),
- '| a | b |\n| - | - |\n| 1 | 2 |',
- ]) {
- const once = normalizeBridge(input);
- expect(normalizeBridge(once)).toBe(once);
- }
- });
-
- test('detectAppliedToleranceClasses reports the class on the witness pair', () => {
- const canon = mm.serialize(mm.parse(NON_UNIFORM));
- expect(detectAppliedToleranceClasses(NON_UNIFORM, canon)).toContain('row-no-trailing-pipe');
- });
-
- test('detectAppliedToleranceClasses does not report the class on a tableless pair', () => {
- expect(detectAppliedToleranceClasses('plain paragraph', 'plain paragraph\n')).not.toContain(
- 'row-no-trailing-pipe',
- );
- });
-
- test('BRIDGE_TOLERANCE_CLASSES carries the class label', () => {
- expect(BRIDGE_TOLERANCE_CLASSES).toContain('row-no-trailing-pipe');
- });
-
- describe('context gating — pipe-leading lines outside a table are untouched', () => {
- test('paragraph with vs without trailing pipe is a genuine divergence', () => {
- expect(normalizeBridge('| foo')).not.toBe(normalizeBridge('| foo |'));
- });
-
- test('pipe-leading paragraph elsewhere in a table-bearing doc keeps its trailing pipe', () => {
- const withPipe = '| p | q |\n\n| a | b |\n| - | - |\n| 1 | 2 |';
- const withoutPipe = '| p | q\n\n| a | b |\n| - | - |\n| 1 | 2 |';
- expect(normalizeBridge(withPipe)).not.toBe(normalizeBridge(withoutPipe));
- });
-
- test('table-shaped lines inside a fenced code block are untouched', () => {
- const piped = '```\n| a | b |\n| - | - |\n| 1 | 2 |\n```';
- const unpiped = '```\n| a | b |\n| - | - |\n| 1 | 2\n```';
- expect(normalizeBridge(piped)).not.toBe(normalizeBridge(unpiped));
- });
- });
-
- describe('guards — genuine non-β-safe divergence is NOT absorbed by the tolerance set', () => {
- test('embed downcast is a genuine divergence', () => {
- expect(normalizeBridge('![[note]]')).not.toBe(normalizeBridge('[note](note)'));
- });
-
- test('touched-cell edit is a genuine divergence', () => {
- expect(normalizeBridge('| a | b |\n| - | - |\n| 1 | 2 |')).not.toBe(
- normalizeBridge('| a | b |\n| - | - |\n| 1 | 99 |'),
- );
- });
-
- test('trailing EMPTY cell is a genuine divergence (single strip, not fixpoint)', () => {
- expect(normalizeBridge('| a | b |\n| - | - |\n| 1 | 2 | |')).not.toBe(
- normalizeBridge('| a | b |\n| - | - |\n| 1 | 2 |'),
- );
- });
-
- test('unescaped content pipe at cell end is a genuine divergence', () => {
- expect(normalizeBridge('| a | b |\n| - | - |\n| 1 | 2| |')).not.toBe(
- normalizeBridge('| a | b |\n| - | - |\n| 1 | 2 |'),
- );
- });
-
- test('missing LEADING pipe is out of class (genuine divergence)', () => {
- expect(normalizeBridge('| a | b |\n| - | - |\n1 | 2')).not.toBe(
- normalizeBridge('| a | b |\n| - | - |\n| 1 | 2 |'),
- );
- });
- });
-});
-
-const g2Manager = new MarkdownManager({ extensions: sharedExtensions });
-
-function dirtySerialize(md: string): string {
- const json = g2Manager.parse(md);
- const visit = (n: JSONContent): void => {
- if (n.type === 'jsxComponent' && n.attrs) n.attrs.sourceDirty = true;
- n.content?.forEach(visit);
- };
- visit(json);
- return g2Manager.serialize(json);
-}
-
-const JSX_EMBED_CORPUS = [...loadIndentedJsxFixtures(), ...loadLargeEmbedFixtures()];
-
-function assertFoldsWithinTolerance(authored: string, serialized: string): void {
- expect(normalizeBridge(serialized)).toBe(normalizeBridge(authored));
- if (serialized !== authored) {
- expect(detectAppliedToleranceClasses(authored, serialized).length).toBeGreaterThan(0);
- }
-}
-
-describe('G2 forward-regression guard — dirty serializer output is a within-tolerance fixed point folded by a named class', () => {
- for (const { name, source } of JSX_EMBED_CORPUS) {
- test(`${name}: dirty round-trip folds within tolerance by a named class`, () => {
- assertFoldsWithinTolerance(source, dirtySerialize(source));
- });
- }
-
- test('planted beyond-tolerance output trips the guard (discriminating, not tautological)', () => {
- const [first] = loadIndentedJsxFixtures();
- if (!first) throw new Error('indented-jsx corpus is empty');
- const authored = first.source;
- const planted = `${authored}\nAn injected paragraph the serializer never emitted.\n`;
- expect(normalizeBridge(planted)).not.toBe(normalizeBridge(authored));
- expect(() => assertFoldsWithinTolerance(authored, planted)).toThrow();
- });
-});
-
-describe('O7 — normalizeBridge idempotence over the JSX/embed corpus', () => {
- for (const { name, source } of JSX_EMBED_CORPUS) {
- test(`${name}: normalizeBridge is idempotent`, () => {
- const once = normalizeBridge(source);
- expect(normalizeBridge(once)).toBe(once);
- });
- }
-});
-
-describe('O7c — MDX normalizing-construct convergence under the real comparator', () => {
- const mm = new MarkdownManager({ extensions: sharedExtensions });
- const roundTrip = (md: string): string => mm.serialize(mm.parse(md));
-
- test('CommonMark: heading→paragraph tight separator normalizes and converges', () => {
- const source = '## Heading\nFollowing paragraph.\n';
- const serialized = roundTrip(source);
- expect(serialized).not.toBe(source);
- expect(normalizeBridge(serialized)).toBe(normalizeBridge(source));
- });
-
- test('MDX: indented-children <Steps> dirty round-trip normalizes and converges', () => {
- const [first] = loadIndentedJsxFixtures();
- if (!first) throw new Error('indented-jsx corpus is empty');
- const serialized = dirtySerialize(first.source);
- expect(serialized).not.toBe(first.source);
- expect(normalizeBridge(serialized)).toBe(normalizeBridge(first.source));
- });
-});
diff --git a/packages/core/src/bridge/tolerance-telemetry.test.ts b/packages/core/src/bridge/tolerance-telemetry.test.ts
deleted file mode 100644
index 1e9e9b279..000000000
--- a/packages/core/src/bridge/tolerance-telemetry.test.ts
+++ /dev/null
@@ -1,151 +0,0 @@
-import { afterEach, describe, expect, test } from 'bun:test';
-
-import { BRIDGE_TOLERANCE_CLASSES, type BridgeToleranceClass } from './normalize.ts';
-import {
- classifySeverity,
- emitToleranceFire,
- findFirstDivergenceIndex,
- setToleranceTelemetryHook,
- type ToleranceFireRecord,
-} from './tolerance-telemetry.ts';
-
-afterEach(() => {
- setToleranceTelemetryHook(null);
-});
-
-describe('classifySeverity', () => {
- const pmModelCaused: BridgeToleranceClass[] = ['emphasis-around-code'];
- const parserCaused: BridgeToleranceClass[] = [
- 'bom',
- 'crlf',
- 'commonmark-escape',
- 'leading-newline',
- 'doc-start-thematic',
- 'paragraph-continuation-indent',
- ];
- const serializerCaused: BridgeToleranceClass[] = [
- 'block-separator-collapse',
- 'table-align-row-spacing',
- 'row-no-trailing-pipe',
- 'ordered-list-marker-number',
- 'list-indent-canonical',
- 'jsx-container-boundary-blank',
- 'trailing-whitespace',
- 'blank-line-collapse',
- 'trailing-newline',
- ];
-
- test('pm-model-caused classes', () => {
- for (const cls of pmModelCaused) {
- expect(classifySeverity(cls)).toBe('pm-model-caused');
- }
- });
-
- test('parser-caused classes', () => {
- for (const cls of parserCaused) {
- expect(classifySeverity(cls)).toBe('parser-caused');
- }
- });
-
- test('serializer-caused classes', () => {
- for (const cls of serializerCaused) {
- expect(classifySeverity(cls)).toBe('serializer-caused');
- }
- });
-
- test('the severity buckets partition every bridge tolerance class', () => {
- const bucketed = [...pmModelCaused, ...parserCaused, ...serializerCaused];
- expect(new Set(bucketed).size).toBe(bucketed.length);
- expect([...bucketed].sort()).toEqual([...BRIDGE_TOLERANCE_CLASSES].sort());
- });
-});
-
-describe('findFirstDivergenceIndex', () => {
- test('identical strings return -1', () => {
- expect(findFirstDivergenceIndex('hello', 'hello')).toBe(-1);
- });
-
- test('first char differs returns 0', () => {
- expect(findFirstDivergenceIndex('abc', 'xyz')).toBe(0);
- });
-
- test('middle char differs', () => {
- expect(findFirstDivergenceIndex('abcdef', 'abcXef')).toBe(3);
- });
-
- test('length difference at end', () => {
- expect(findFirstDivergenceIndex('abc', 'abcdef')).toBe(3);
- });
-
- test('empty vs non-empty', () => {
- expect(findFirstDivergenceIndex('', 'a')).toBe(0);
- });
-
- test('both empty returns -1', () => {
- expect(findFirstDivergenceIndex('', '')).toBe(-1);
- });
-});
-
-describe('emitToleranceFire', () => {
- test('does nothing when no hook is set', () => {
- emitToleranceFire(['crlf'], 'a\r\n', 'a\n', 'test-doc');
- });
-
- test('fires hook once per class', () => {
- const records: ToleranceFireRecord[] = [];
- setToleranceTelemetryHook((r) => records.push(r));
-
- emitToleranceFire(['bom', 'crlf'], 'a\r\n', 'a\n', 'my-doc');
-
- expect(records).toHaveLength(2);
- expect(records[0]?.className).toBe('bom');
- expect(records[1]?.className).toBe('crlf');
- });
-
- test('record has all required fields', () => {
- const records: ToleranceFireRecord[] = [];
- setToleranceTelemetryHook((r) => records.push(r));
-
- emitToleranceFire(['trailing-newline'], 'hello\n', 'hello', 'notes/foo');
-
- expect(records).toHaveLength(1);
- const r = records[0];
- expect(r).toBeDefined();
- expect(r?.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T/);
- expect(r?.className).toBe('trailing-newline');
- expect(r?.documentName).toBe('notes/foo');
- expect(r?.codeUnitPosition).toBe(5);
- expect(r?.severity).toBe('serializer-caused');
- });
-
- test('empty classes array does not fire hook', () => {
- let called = false;
- setToleranceTelemetryHook(() => {
- called = true;
- });
-
- emitToleranceFire([], 'same', 'same', 'doc');
-
- expect(called).toBe(false);
- });
-
- test('undefined documentName passes through', () => {
- const records: ToleranceFireRecord[] = [];
- setToleranceTelemetryHook((r) => records.push(r));
-
- emitToleranceFire(['crlf'], 'a\r', 'a', undefined);
-
- expect(records[0]?.documentName).toBeUndefined();
- });
-
- test('clearing hook stops emissions', () => {
- const records: ToleranceFireRecord[] = [];
- setToleranceTelemetryHook((r) => records.push(r));
- emitToleranceFire(['crlf'], 'a\r', 'a', 'doc');
- expect(records).toHaveLength(1);
-
- setToleranceTelemetryHook(null);
- emitToleranceFire(['crlf'], 'a\r', 'a', 'doc');
- expect(records).toHaveLength(1);
- });
-});
diff --git a/packages/core/src/markdown/autolink-promotion.test.ts b/packages/core/src/markdown/autolink-promotion.test.ts
deleted file mode 100644
index 0084d34d3..000000000
--- a/packages/core/src/markdown/autolink-promotion.test.ts
+++ /dev/null
@@ -1,152 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';
-import type { JSONContent } from '@tiptap/core';
-
-interface PmMarkJson {
- type: string;
- attrs?: Record<string, unknown>;
-}
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function roundTrip(md: string): string {
- return mdManager.serialize(mdManager.parse(md));
-}
-
-function findLinks(json: JSONContent): PmMarkJson[] {
- const links: PmMarkJson[] = [];
- function walk(node: JSONContent) {
- if (node.marks) {
- for (const m of node.marks) {
- if (m.type === 'link') links.push(m as PmMarkJson);
- }
- }
- if (node.content) {
- for (const child of node.content) walk(child);
- }
- }
- walk(json);
- return links;
-}
-
-describe('autolink promotion: basic shapes', () => {
- test('<https://example.com> produces a PM link mark with linkStyle autolink', () => {
- const json = mdManager.parse('<https://example.com>\n');
- const links = findLinks(json);
- expect(links.length).toBeGreaterThanOrEqual(1);
- expect(links[0].attrs.href).toBe('https://example.com');
- expect(links[0].attrs.linkStyle).toBe('autolink');
- });
-
- test('<mailto:[email protected]> produces autolink-style link', () => {
- const json = mdManager.parse('<mailto:[email protected]>\n');
- const links = findLinks(json);
- expect(links.length).toBeGreaterThanOrEqual(1);
- expect(links[0].attrs.href).toBe('mailto:[email protected]');
- expect(links[0].attrs.linkStyle).toBe('autolink');
- });
-
- test('<ftp://files.example.com/path> produces autolink-style link', () => {
- const json = mdManager.parse('<ftp://files.example.com/path>\n');
- const links = findLinks(json);
- expect(links.length).toBeGreaterThanOrEqual(1);
- expect(links[0].attrs.linkStyle).toBe('autolink');
- });
-
- test('custom scheme <custom+x.y:data> produces autolink-style link', () => {
- const json = mdManager.parse('<custom+x.y:data>\n');
- const links = findLinks(json);
- expect(links.length).toBeGreaterThanOrEqual(1);
- expect(links[0].attrs.linkStyle).toBe('autolink');
- });
-});
-
-describe('autolink promotion: paragraph context', () => {
- test('autolink in middle of text produces 3 PM children', () => {
- const json = mdManager.parse('See <https://example.com> here.\n');
- const para = json.content?.find((n: JSONContent) => n.type === 'paragraph');
- expect(para).toBeDefined();
- const children = para?.content ?? [];
- expect(children.length).toBeGreaterThanOrEqual(3);
- const linkedChild = children.find((c: JSONContent) => c.marks?.some((m) => m.type === 'link'));
- expect(linkedChild).toBeDefined();
- const linkMark = linkedChild?.marks?.[0] as PmMarkJson | undefined;
- expect(linkMark?.attrs?.linkStyle).toBe('autolink');
- });
-
- test('multiple autolinks in one paragraph', () => {
- const json = mdManager.parse('Visit <https://a.com> and <https://b.com> today.\n');
- const links = findLinks(json);
- expect(links.length).toBe(2);
- expect(links[0].attrs.href).toBe('https://a.com');
- expect(links[1].attrs.href).toBe('https://b.com');
- });
-});
-
-describe('autolink promotion: round-trip byte-identity', () => {
- test('<https://example.com> round-trips byte-identically', () => {
- const input = '<https://example.com>\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('<mailto:[email protected]> round-trips byte-identically', () => {
- const input = '<mailto:[email protected]>\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('autolink in paragraph context round-trips', () => {
- const input = 'See <https://example.com> here.\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('multiple autolinks round-trip', () => {
- const input = 'Visit <https://a.com> and <mailto:[email protected]> today.\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('idempotent: second round-trip matches first', () => {
- const input = '<https://example.com>\n';
- const r1 = roundTrip(input);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- });
-});
-
-describe('autolink promotion: negative cases (should NOT promote)', () => {
- test('<Callout> is JSX, not autolink (no scheme colon)', () => {
- const json = mdManager.parse('<Callout>body</Callout>\n');
- const links = findLinks(json);
- const autolinkLinks = links.filter((l) => l.attrs.linkStyle === 'autolink');
- expect(autolinkLinks.length).toBe(0);
- });
-
- test('<br> is void HTML, not autolink', () => {
- const json = mdManager.parse('Line<br>two.\n');
- const links = findLinks(json);
- const autolinkLinks = links.filter((l) => l.attrs.linkStyle === 'autolink');
- expect(autolinkLinks.length).toBe(0);
- });
-
- test('<foo> without colon is not promoted', () => {
- const input = '<foo>\n';
- const json = mdManager.parse(input);
- const links = findLinks(json);
- const autolinkLinks = links.filter((l) => l.attrs.linkStyle === 'autolink');
- expect(autolinkLinks.length).toBe(0);
- });
-
- test('[text](url) inline link keeps linkStyle inline (not autolink)', () => {
- const json = mdManager.parse('[click here](https://example.com)\n');
- const links = findLinks(json);
- expect(links.length).toBe(1);
- expect(links[0].attrs.linkStyle).toBe('inline');
- });
-});
-
-describe('autolink promotion: JSX + autolink coexistence', () => {
- test('<Callout>see <https://x.com></Callout> — autolink inside JSX children', () => {
- const input = '<Callout>see <https://x.com></Callout>\n';
- const r = roundTrip(input);
- expect(r.trim()).toBe(input.trim());
- });
-});
diff --git a/packages/core/src/markdown/autolink-void-html-guard.astral-brace.test.ts b/packages/core/src/markdown/autolink-void-html-guard.astral-brace.test.ts
deleted file mode 100644
index 6d8ff64c4..000000000
--- a/packages/core/src/markdown/autolink-void-html-guard.astral-brace.test.ts
+++ /dev/null
@@ -1,56 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import * as fc from 'fast-check';
-import { protectFromMdx } from './autolink-void-html-guard.ts';
-
-function restoreString(s: string): string {
- return s
- .replaceAll('\u{E000}', '<')
- .replaceAll('\u{E001}', '>')
- .replaceAll('\u{E002}', ':')
- .replaceAll('\u{E003}', '@')
- .replaceAll('\u{E004}', '{');
-}
-
-const roundTrip = (s: string) => restoreString(protectFromMdx(s));
-const countBraces = (s: string) => [...s].filter((c) => c === '{').length;
-
-describe('R23 brace guard: astral codepoint before an unmatched brace', () => {
- const buildInput = (k: number) => `${'\u{1F3A3}'.repeat(k)}a{bcdefgh`;
-
- for (let k = 0; k <= 4; k++) {
- test(`protect->restore is identity with ${k} astral codepoint(s) before the brace`, () => {
- const input = buildInput(k);
- expect(roundTrip(input)).toBe(input);
- });
- }
-
- test('restore mints no brace the input did not contain', () => {
- const input = '\u{1F3A3}\u{1F9AD}\u{1F3A3}\u{1F9AD}a{bcdefghij';
- expect(countBraces(roundTrip(input))).toBe(countBraces(input));
- });
-
- test('astral before a brace flushed at a blockquote boundary', () => {
- const input = '\u{1F3A3}a{\n>}rest';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('protect->restore identity holds for astral-bearing inputs (closes the BMP-only generator gap)', () => {
- const astralOrBmp = fc
- .array(
- fc.oneof(
- fc.constantFrom('\u{1F3A3}', '\u{1F9AD}', '\u{1F600}', '\u{1F9EA}', '\u{1F30A}'),
- fc.constantFrom(...'abc {}>\n'),
- ),
- { maxLength: 60 },
- )
- .map((parts) => parts.join(''));
- for (const seed of [42, 137, 2718, 31415, 99991]) {
- fc.assert(
- fc.property(astralOrBmp, (s) => {
- expect(roundTrip(s)).toBe(s);
- }),
- { numRuns: 200, seed },
- );
- }
- });
-});
diff --git a/packages/core/src/markdown/autolink-void-html-guard.consistency.test.ts b/packages/core/src/markdown/autolink-void-html-guard.consistency.test.ts
deleted file mode 100644
index f5d71c12c..000000000
--- a/packages/core/src/markdown/autolink-void-html-guard.consistency.test.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import * as fc from 'fast-check';
-import { protectFromMdx } from './autolink-void-html-guard.ts';
-
-function restoreString(s: string): string {
- return s
- .replaceAll('\uE000', '<')
- .replaceAll('\uE001', '>')
- .replaceAll('\uE002', ':')
- .replaceAll('\uE003', '@')
- .replaceAll('\uE004', '{');
-}
-
-const nonPuaString = fc
- .array(
- fc.oneof(
- fc.constantFrom(...'abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n'),
- fc.constantFrom(...'<>&[]{}:@`#\\*_$~!/()"\'-=,.;?'),
- ),
- { maxLength: 200 },
- )
- .map((chars) => chars.join(''));
-
-const NUM_RUNS = process.env.STRESS_FIDELITY === '1' ? 10_000 : 1_000;
-const TIMEOUT = process.env.STRESS_FIDELITY === '1' ? 90_000 : 30_000;
-
-describe('Guard self-consistency', () => {
- test(
- 'restoreString(protectFromMdx(s)) === s for non-PUA strings',
- () => {
- fc.assert(
- fc.property(nonPuaString, (s) => {
- const protected_ = protectFromMdx(s);
- const restored = restoreString(protected_);
- expect(restored).toBe(s);
- }),
- { numRuns: NUM_RUNS, seed: 42 },
- );
- },
- TIMEOUT,
- );
-
- test(
- 'protectFromMdx output has no unmatched, unescaped literal {',
- () => {
- fc.assert(
- fc.property(nonPuaString, (s) => {
- const protected_ = protectFromMdx(s);
- let depth = 0;
- for (let i = 0; i < protected_.length; i++) {
- const ch = protected_[i];
- if (ch !== '{' && ch !== '}') continue;
- let bs = 0;
- for (let j = i - 1; j >= 0 && protected_[j] === '\\'; j--) bs++;
- if (bs % 2 === 1) continue;
- if (ch === '{') depth++;
- else {
- depth--;
- if (depth < 0) depth = 0;
- }
- }
- expect(depth).toBe(0);
- }),
- { numRuns: NUM_RUNS, seed: 42 },
- );
- },
- TIMEOUT,
- );
-
- test('braces across paragraph break are treated as unmatched', () => {
- const input = '{\n\n}text';
- const protected_ = protectFromMdx(input);
- expect(protected_[0]).toBe('\uE004');
- expect(protected_.slice(1)).toBe('\n\n}text');
- });
-
- test('braces within same paragraph remain matched', () => {
- const input = '{expr}\nmore text';
- const protected_ = protectFromMdx(input);
- expect(protected_).toBe('{expr}\nmore text');
- });
-
- test(
- 'multi-seed text round-trip (5 seeds)',
- () => {
- for (const seed of [42, 137, 2718, 31415, 99991]) {
- fc.assert(
- fc.property(nonPuaString, (s) => {
- expect(restoreString(protectFromMdx(s))).toBe(s);
- }),
- { numRuns: Math.floor(NUM_RUNS / 5), seed },
- );
- }
- },
- TIMEOUT,
- );
-});
diff --git a/packages/core/src/markdown/autolink-void-html-guard.perf.test.ts b/packages/core/src/markdown/autolink-void-html-guard.perf.test.ts
deleted file mode 100644
index 06af914fb..000000000
--- a/packages/core/src/markdown/autolink-void-html-guard.perf.test.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { protectFromMdx } from './autolink-void-html-guard.ts';
-
-const BENCH_ENABLED = process.env.RUN_BENCH === '1' || process.env.RUN_BENCH === 'true';
-const describeBench = BENCH_ENABLED ? describe : describe.skip;
-
-function pathological(jsxCount: number): string {
- const parts: string[] = [];
- for (let i = 0; i < jsxCount; i++) {
- parts.push(`Para ${i}: <Unclosed${i} text after.`);
- }
- return `${parts.join('\n\n')}\n`;
-}
-
-describe('R15 R23 guard: pathological unclosed-tag correctness (tier-1 smoke)', () => {
- test('10K unique unclosed uppercase tags produce non-empty output', () => {
- const src = pathological(10_000);
- const out = protectFromMdx(src);
- expect(typeof out).toBe('string');
- expect(out.length).toBeGreaterThan(src.length / 2);
- });
-});
-
-describeBench('R15 R23 guard: pathological unclosed-tag wall-clock bounds (tier-2)', () => {
- test('10K unique unclosed uppercase tags complete in under 100ms', () => {
- const src = pathological(10_000);
- protectFromMdx(src);
-
- const iterations = 3;
- const samples: number[] = [];
- for (let i = 0; i < iterations; i++) {
- const t0 = performance.now();
- protectFromMdx(src);
- samples.push(performance.now() - t0);
- }
- samples.sort((a, b) => a - b);
- const median = samples[Math.floor(samples.length / 2)];
- expect(median).toBeLessThan(100);
- });
-
- test('5K unique unclosed uppercase tags complete in under 50ms', () => {
- const src = pathological(5_000);
- protectFromMdx(src); // Warm-up
- const iterations = 3;
- const samples: number[] = [];
- for (let i = 0; i < iterations; i++) {
- const t0 = performance.now();
- protectFromMdx(src);
- samples.push(performance.now() - t0);
- }
- samples.sort((a, b) => a - b);
- const median = samples[Math.floor(samples.length / 2)];
- expect(median).toBeLessThan(50);
- });
-
- test('20K ÷ 5K ratio stays asymptotically sub-quadratic', () => {
- function median(src: string): number {
- protectFromMdx(src); // Warm-up
- const samples: number[] = [];
- for (let i = 0; i < 5; i++) {
- const t0 = performance.now();
- protectFromMdx(src);
- samples.push(performance.now() - t0);
- }
- samples.sort((a, b) => a - b);
- return samples[Math.floor(samples.length / 2)];
- }
- const t5k = median(pathological(5_000));
- const t20k = median(pathological(20_000));
- if (t5k < 0.5) {
- return;
- }
- const ratio = t20k / t5k;
- expect(ratio).toBeLessThan(10);
- });
-});
diff --git a/packages/core/src/markdown/autolink-void-html-guard.precision.test.ts b/packages/core/src/markdown/autolink-void-html-guard.precision.test.ts
deleted file mode 100644
index 6431b3ba8..000000000
--- a/packages/core/src/markdown/autolink-void-html-guard.precision.test.ts
+++ /dev/null
@@ -1,175 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import * as fc from 'fast-check';
-import { protectFromMdx } from './autolink-void-html-guard.ts';
-
-const safeWord = fc.stringMatching(/^[a-zA-Z]{2,8}$/);
-const tagName = safeWord.map((w) => w.charAt(0).toUpperCase() + w.slice(1));
-
-const selfClosingJsx = fc.oneof(
- tagName.map((name) => `<${name} />`),
- fc.tuple(tagName, safeWord, safeWord).map(([name, attr, val]) => `<${name} ${attr}="${val}" />`),
- fc
- .tuple(tagName, fc.constantFrom('src', 'href', 'url', 'data'))
- .map(([name, attr]) => `<${name} ${attr}="https://example.com/path/to/resource?a=1&b=2" />`),
- fc
- .tuple(tagName, safeWord, safeWord, safeWord, safeWord)
- .map(([name, a1, v1, a2, v2]) => `<${name} ${a1}="${v1}" ${a2}="${v2}" />`),
- fc
- .tuple(tagName, safeWord, safeWord)
- .map(([name, attr, expr]) => `<${name} ${attr}={${expr}} />`),
-);
-
-const pairedJsx = fc
- .tuple(tagName, safeWord)
- .map(([name, body]) => `<${name}>\n\n${body}\n\n</${name}>`);
-
-const multiLineSelfClosing = fc
- .tuple(tagName, safeWord, safeWord)
- .map(([name, attr, val]) => `<${name}\n ${attr}="${val}"\n/>`);
-
-const NUM_RUNS = process.env.STRESS_FIDELITY === '1' ? 10_000 : 1_000;
-const TIMEOUT = process.env.STRESS_FIDELITY === '1' ? 90_000 : 30_000;
-
-describe('Guard precision: valid MDX survives protectFromMdx() unchanged', () => {
- test(
- 'self-closing JSX with attrs (including URLs) not guarded',
- () => {
- fc.assert(
- fc.property(selfClosingJsx, (mdx) => {
- const protected_ = protectFromMdx(mdx);
- expect(protected_[0]).toBe('<');
- }),
- { numRuns: NUM_RUNS, seed: 42 },
- );
- },
- TIMEOUT,
- );
-
- test(
- 'paired JSX not guarded',
- () => {
- fc.assert(
- fc.property(pairedJsx, (mdx) => {
- const protected_ = protectFromMdx(mdx);
- expect(protected_[0]).toBe('<');
- }),
- { numRuns: NUM_RUNS, seed: 42 },
- );
- },
- TIMEOUT,
- );
-
- test(
- 'multi-line self-closing JSX not guarded',
- () => {
- fc.assert(
- fc.property(multiLineSelfClosing, (mdx) => {
- const protected_ = protectFromMdx(mdx);
- expect(protected_[0]).toBe('<');
- }),
- { numRuns: NUM_RUNS, seed: 42 },
- );
- },
- TIMEOUT,
- );
-
- test('hardcoded valid MDX patterns — none guarded', () => {
- const valid = [
- '<Callout>body text</Callout>',
- '<Note>text with **bold**</Note>',
- '<Icon />',
- '<Widget title="hello" />',
- '<Image src="https://example.com?a=1&b=2" />',
- '<Chart data="https://api.example.com/v1/data" />',
- '<Link href="/path/to/page" />',
- '<Widget\n variant="large"\n/>',
- '<Widget\n title="hello"\n data="https://api.com/v1"\n/>',
- '<Callout type="warning">\n\nContent here\n\n</Callout>',
- '<Accordion title="First">\n\nContent\n\n</Accordion>',
- '<img src="x.png" alt="test" />',
- '<video src="v.mp4" controls />',
- '<audio src="a.mp3" controls />',
- ];
-
- for (const mdx of valid) {
- const protected_ = protectFromMdx(mdx);
- expect(protected_[0]).toBe('<');
- }
- });
-});
-
-describe('Guard precision: self-closing canonical media tags are length-independent', () => {
- const GUARD_OPEN = '';
- const CANONICAL = ['img', 'video', 'audio'] as const;
-
- test('long alt does not guard self-closing canonical tags', () => {
- for (const tag of CANONICAL) {
- const desc = 'a lone sailboat on calm rippled water under high cirrus '.repeat(16);
- const mdx = `<${tag} src="m.ext" alt="${desc}" />`;
- expect(mdx.length).toBeGreaterThan(256);
- expect(protectFromMdx(mdx)[0]).toBe('<');
- }
- });
-
- test('data-URI src does not guard self-closing <img/>', () => {
- const mdx = `<img src="data:image/png;base64,${'A'.repeat(2000)}" alt="x" />`;
- expect(protectFromMdx(mdx)[0]).toBe('<');
- });
-
- test('bare void form (no slash) stays guarded regardless of length', () => {
- const mdx = `<img src="x.png" alt="${'a'.repeat(900)}">`;
- expect(protectFromMdx(mdx)[0]).toBe(GUARD_OPEN);
- });
-
- test('adjacent long self-closing tags each pass through', () => {
- const alt = 'descriptive caption text '.repeat(20); // ~500 chars each
- const mdx = `<img src="a.png" alt="${alt}" />\n\n<img src="b.png" alt="${alt}" />`;
- const out = protectFromMdx(mdx);
- expect(out[0]).toBe('<'); // first opener not guarded
- expect(out).not.toContain(GUARD_OPEN); // neither opener guarded
- });
-
- test(
- 'PBT: self-closing canonical media tags are never guarded at arbitrary attribute length',
- () => {
- fc.assert(
- fc.property(fc.constantFrom(...CANONICAL), fc.integer({ min: 0, max: 4000 }), (tag, n) => {
- const mdx = `<${tag} src="x.ext" alt="${'a'.repeat(n)}" />`;
- expect(protectFromMdx(mdx)[0]).toBe('<');
- }),
- { numRuns: NUM_RUNS, seed: 42 },
- );
- },
- TIMEOUT,
- );
-});
-
-describe('Guard: `<mark>` paired-JSX exemption paths', () => {
- test('paired `<mark>text</mark>` opener is not PUA-protected', () => {
- const protected_ = protectFromMdx('<mark>text</mark>');
- expect(protected_[0]).toBe('<');
- expect(protected_).toBe('<mark>text</mark>');
- });
-
- test('closer `</mark>` is not PUA-protected', () => {
- const protected_ = protectFromMdx('</mark>');
- expect(protected_).toBe('</mark>');
- });
-
- test('self-closing `<mark/>` reaches remark-mdx unguarded', () => {
- const protected_ = protectFromMdx('<mark/>');
- expect(protected_[0]).toBe('<');
- expect(protected_).toBe('<mark/>');
- });
-
- test('orphan `<mark>` opener (no closer) is PUA-protected', () => {
- const protected_ = protectFromMdx('<mark>text without closer');
- expect(protected_[0]).toBe('');
- });
-
- test('orphan `<mark>` mid-paragraph is PUA-protected', () => {
- const protected_ = protectFromMdx('Some prose with <mark> no closer');
- expect(protected_).toContain('mark>');
- expect(protected_).not.toContain('<mark>');
- });
-});
diff --git a/packages/core/src/markdown/autolink-void-html-guard.test.ts b/packages/core/src/markdown/autolink-void-html-guard.test.ts
deleted file mode 100644
index aa7d29059..000000000
--- a/packages/core/src/markdown/autolink-void-html-guard.test.ts
+++ /dev/null
@@ -1,387 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { protectFromMdx } from './autolink-void-html-guard.ts';
-import { MarkdownManager } from './index.ts';
-import { mdastToHtml } from './mdast-to-html.ts';
-import { assertByteStable } from './round-trip-asserts.test-helper.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function roundTrip(md: string): string {
- return mdManager.serialize(mdManager.parse(md));
-}
-
-function collectText(node: JSONContent): string {
- let out = node.type === 'text' && typeof node.text === 'string' ? node.text : '';
- for (const child of node.content ?? []) out += collectText(child);
- return out;
-}
-
-function renderedAsLiteralText(md: string): boolean {
- const text = collectText(mdManager.parse(md));
- return text.includes('<img') || text.includes('<video') || text.includes('<audio');
-}
-
-describe('R23: autolink regression fix', () => {
- test('autolink <https://example.com> parses without error', () => {
- expect(() => mdManager.parse('Visit <https://example.com>.\n')).not.toThrow();
- });
-
- test('autolink round-trips', () => {
- const md = 'Visit <https://example.com>.\n';
- const output = roundTrip(md);
- expect(output).toContain('https://example.com');
- });
-});
-
-describe('R23: void HTML regression fix', () => {
- test('<br> parses without error', () => {
- expect(() => mdManager.parse('Line one<br>Line two.\n')).not.toThrow();
- });
-
- test('<br> round-trips', () => {
- const md = 'Line one<br>Line two.\n';
- const output = roundTrip(md);
- expect(output).toContain('<br>');
- });
-
- test('<hr> parses without error', () => {
- expect(() => mdManager.parse('Above\n\n<hr>\n\nBelow\n')).not.toThrow();
- });
-
- test('<img> parses without error', () => {
- expect(() => mdManager.parse('<img src="photo.jpg">\n')).not.toThrow();
- });
-
- test('<br/> self-closing parses without error', () => {
- expect(() => mdManager.parse('Line<br/>break.\n')).not.toThrow();
- });
-});
-
-describe('R23: self-closing media tags render at any length', () => {
- const longAlt = 'a lone sailboat on calm rippled water under high cirrus clouds '.repeat(6);
-
- test('long-alt <img/> is not literal text', () => {
- const md = `<img src="./photo.jpg" alt="${longAlt}" width="320" />\n`;
- expect(md.length).toBeGreaterThan(256);
- expect(renderedAsLiteralText(md)).toBe(false);
- });
-
- test('data-URI <img/> is not literal text', () => {
- expect(
- renderedAsLiteralText(`<img src="data:image/png;base64,${'A'.repeat(800)}" alt="x" />\n`),
- ).toBe(false);
- });
-
- test('long <video/> and <audio/> are not literal text', () => {
- expect(renderedAsLiteralText(`<video src="v.mp4" controls aria-label="${longAlt}" />\n`)).toBe(
- false,
- );
- expect(renderedAsLiteralText(`<audio src="a.mp3" controls aria-label="${longAlt}" />\n`)).toBe(
- false,
- );
- });
-
- test('bare void <img> (no slash) stays literal text regardless of length', () => {
- expect(renderedAsLiteralText(`<img src="x.png" alt="${longAlt}">\n`)).toBe(true);
- });
-
- test('two long self-closing <img/> separated by a caption both render', () => {
- const md = `## Photos\n\n<img src="./a.jpg" alt="${longAlt}" width="320" />\n\n*caption*\n\n<img src="./b.jpg" alt="${longAlt}" width="480" />\n`;
- expect(renderedAsLiteralText(md)).toBe(false);
- });
-
- test('long self-closing <img/> round-trips with src and alt intact', () => {
- const md = `<img src="./photo.jpg" alt="${longAlt}" width="320" />\n`;
- const out = roundTrip(md);
- expect(out).toContain('./photo.jpg');
- expect(out).toContain('high cirrus');
- });
-});
-
-describe('R23: invalid JSX opener recovery', () => {
- test('literal <50ms in prose parses without error', () => {
- expect(() => mdManager.parse('Warm replay when nothing changed is <50ms.\n')).not.toThrow();
- });
-
- test('literal <50ms in prose remains literal text in parsed content', () => {
- const json = mdManager.parse('Warm replay when nothing changed is <50ms.\n');
- expect(json.content?.[0]?.type).toBe('paragraph');
- expect(json.content?.[0]?.content?.[0]?.text).toBe(
- 'Warm replay when nothing changed is <50ms.',
- );
- });
-
- test('comparison prose 2 < 5 remains literal text in parsed content', () => {
- const json = mdManager.parse('Comparison: 2 < 5 and 7 > 3.\n');
- expect(json.content?.[0]?.type).toBe('paragraph');
- expect(json.content?.[0]?.content?.[0]?.text).toBe('Comparison: 2 < 5 and 7 > 3.');
- });
-});
-
-describe('R23 guard: exhaustive < context coverage', () => {
- const mustNotThrow: Array<[string, string]> = [
- ['<https://example.com>', 'autolink https'],
- ['<mailto:[email protected]>', 'autolink mailto'],
- ['<ftp://files.x/p>', 'autolink ftp'],
-
- ['<br>', 'void br'],
- ['<hr>', 'void hr'],
- ['<img src="x">', 'void img with attr'],
- ['<br/>', 'self-closing br'],
- ['<br />', 'self-closing br with space'],
-
- ['<div>content</div>', 'div block'],
- ['<span>inline</span>', 'span inline'],
- ['<p>paragraph</p>', 'p tag'],
-
- ['<Callout>body</Callout>', 'paired MDX'],
- ['<Note>text</Note>', 'paired MDX inline'],
- ['<Icon />', 'self-closing MDX'],
- ['<Widget\n title="hello"\n/>', 'multi-line self-closing JSX'],
- ['<Card\n variant="warning"\n />', 'multi-line self-closing with trailing space'],
- ['<Image src="https://example.com?a=1&b=2" />', 'self-closing with URL in attr'],
- ['<Chart data="https://api.example.com/v1" />', 'self-closing with URL path in attr'],
- ['<Link href="/path/to/page" />', 'self-closing with relative path in attr'],
-
- ['<', 'bare < at EOF'],
- ['< ', 'bare < + space'],
- ['<\n', 'bare < + newline'],
- ['a<b', 'inline <letter'],
- ['a < b', '< with spaces (comparison)'],
- ['<foo', 'unclosed <lowercase'],
- ['<foo bar', 'unclosed <lowercase with text'],
- ['<Foo', 'unclosed <Uppercase'],
- ['<foo>', 'lowercase tag (closed)'],
-
- ['<!-- comment -->', 'HTML comment'],
- ['<!-- <nested> -->', 'HTML comment with angle brackets'],
- ['<!--\nmultiline\n-->', 'multiline HTML comment'],
-
- ['<b>bold</b> and <foo unclosed', 'valid HTML + bare <'],
- ['if (x < y) return', 'code-like comparison'],
- ['a < b && c > d', 'double comparison'],
- ['<Callout>see <https://url></Callout>', 'MDX + autolink inside'],
- ['<Note>has <br> inside</Note>', 'MDX + void HTML inside'],
-
- ['<<<', 'triple <'],
- ['<><>', 'empty angle pairs'],
- ['<{expr}>', 'JSX expression-like'],
- ['< Component >', 'space after < (not JSX)'],
- ['<_private>', 'underscore start (mdx claims _)'],
- ['<$special>', 'dollar start (mdx claims $)'],
- ['<_', 'bare underscore-start unclosed'],
- ['<$', 'bare dollar-start unclosed'],
-
- ['The value is <unknown at this time', 'prose with <word'],
- ['Use Ctrl+< to go back', 'keyboard shortcut'],
- ['Template: <placeholder>', 'template-like'],
- ['Compare: 3 <foo> 5', 'comparison with word in angles'],
-
- ['</', 'bare </ at EOF'],
- ['</foo', 'incomplete close tag'],
- ['</Callout', 'incomplete uppercase close tag'],
-
- ['{', 'bare { at EOF'],
- ['{ ', 'bare { + space'],
- ['text {', 'text then bare {'],
- ['{ unclosed', 'unclosed {'],
- ['a{b', 'inline {letter'],
- ['{a', 'bare {letter at EOF'],
- ['{{', 'double {'],
- ['{{{', 'triple {'],
- ['{a{b', 'nested unmatched {'],
-
- ['{expression}', 'valid MDX expression'],
- ['{/* comment */}', 'MDX comment expression'],
- ['{}', 'empty MDX expression'],
- ['{123}', 'numeric MDX expression'],
- ['{true}', 'boolean MDX expression'],
- ['{{}}', 'nested matched braces'],
-
- ['<foo and {bar', 'bare < and bare { together'],
- ['<Callout>{content}</Callout>', 'MDX with expression inside'],
- ['{expression} and <br>', 'expression + void HTML'],
- ];
-
- for (const [input, label] of mustNotThrow) {
- test(`does not throw: ${label}`, () => {
- expect(() => mdManager.parse(input)).not.toThrow();
- });
- }
-});
-
-describe('R6a: safeText idempotency for §2.4 ambiguous chars', () => {
- const CHARS_AC = ['\\', '*', '_', '#', '<', '>', '{', '}'];
-
- for (const c of CHARS_AC) {
- test(`round-trip stable — bare "${c}"`, () => {
- const r1 = roundTrip(c);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- });
-
- test(`round-trip stable — escaped "\\${c}"`, () => {
- const r1 = roundTrip(`\\${c}`);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- });
- }
-
- test('double-escape stable (5 rounds) — "\\{"', () => {
- let s = '\\{';
- const outputs: string[] = [];
- for (let i = 0; i < 5; i++) {
- s = roundTrip(s);
- outputs.push(s);
- }
- expect(new Set(outputs).size).toBe(1);
- });
-
- test('double-escape stable (5 rounds) — "\\\\{"', () => {
- let s = '\\\\{';
- const outputs: string[] = [];
- for (let i = 0; i < 5; i++) {
- s = roundTrip(s);
- outputs.push(s);
- }
- expect(new Set(outputs).size).toBe(1);
- });
-
- test('CDATA-shaped HTML block idempotent (R6 Finding 4)', () => {
- const cdata =
- '<![CDATA[\nfunction matchwo(a,b)\n{\n if (a < b && a < 0) then {\n return 1;\n\n } else {\n\n return 0;\n }\n}\n]]>\nokay\n';
- const r1 = roundTrip(cdata);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- const countBs = (s: string) => (s.match(/\\\\/g) ?? []).length;
- expect(countBs(r2)).toBe(countBs(r1));
- });
-
- test('escaped brace in MDX-adjacent context preserves escape', () => {
- const md = 'prose \\{expression}\n';
- const r1 = roundTrip(md);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- expect(r1).toContain('\\{');
- });
-
- test('unmatched brace still protected (no crash) — fix does not regress R23', () => {
- expect(() => mdManager.parse('prose {unclosed\n')).not.toThrow();
- expect(() => mdManager.parse('unclosed}\nprose\n')).not.toThrow();
- expect(() => mdManager.parse('{nested {unclosed\n')).not.toThrow();
- });
-
- test('literal backslash before matched pair is still a literal', () => {
- expect(() => mdManager.parse('\\\\{a}\n')).not.toThrow();
- });
-});
-
-describe('R23 guard: angle-bracket link destinations', () => {
- function findNodes(node: unknown, type: string): Array<Record<string, unknown>> {
- const out: Array<Record<string, unknown>> = [];
- const rec = node as Record<string, unknown>;
- if (rec && typeof rec === 'object') {
- if (rec.type === type) out.push(rec);
- const children = rec.children;
- if (Array.isArray(children)) {
- for (const child of children) out.push(...findNodes(child, type));
- }
- }
- return out;
- }
-
- const expectByteStable = (md: string): void => assertByteStable(roundTrip, md);
-
- test('[link](</my uri>) parses to a link node with the space-bearing url', () => {
- const tree = mdManager.parseToMdast('[link](</my uri>)\n');
- const links = findNodes(tree, 'link');
- expect(links.length).toBe(1);
- expect(links[0].url).toBe('/my uri');
- });
-
- test('the destination < is not PUA-protected when the resource forms', () => {
- const src = '[link](</my uri>)';
- expect(protectFromMdx(src)).toBe(src);
- });
-
- test('angle destination link renders href /my%20uri', () => {
- const html = mdastToHtml(mdManager.parseToMdast('[link](</my uri>)\n'));
- expect(html).toContain('href="/my%20uri"');
- expect(html).toContain('>link</a>');
- });
-
- test('angle destination link round-trips byte-identically and idempotently', () => {
- expectByteStable('[link](</my uri>)\n');
- });
-
- test('image form ![img](</a b.png>) parses to an image and round-trips', () => {
- const tree = mdManager.parseToMdast('![img](</a b.png>)\n');
- const images = [...findNodes(tree, 'image'), ...findNodes(tree, 'mdxJsxFlowElement')];
- expect(images.length).toBeGreaterThanOrEqual(1);
- expectByteStable('![img](</a b.png>)\n');
- });
-
- test('uppercase-name space-bearing destination forms a link', () => {
- const tree = mdManager.parseToMdast('[x](<My uri>)\n');
- const links = findNodes(tree, 'link');
- expect(links.length).toBe(1);
- expect(links[0].url).toBe('My uri');
- expectByteStable('[x](<My uri>)\n');
- });
-
- const mustStayGuarded: Array<[string, string]> = [
- ['[link](</my uri', 'mid-typing: unclosed destination'],
- ['[link](</my uri>', 'mid-typing: destination closed, resource open'],
- ['[link](</my uri> junk)', 'resource fails after destination'],
- ['x](</a b>)', 'no label-start before ]('],
- ['link](</a b>)', 'label-start deleted mid-edit'],
- ['a](<', 'bare ](< at EOF'],
- ['a](</', 'bare ](</ at EOF'],
- ['[x] y](</a b>)', 'label already closed before ]('],
- ['[[page]](</a b>)', 'wikilink-consumed brackets before ]('],
- ['[a\\](</b c>)', 'escaped ] cannot end a label'],
- ['`a[b` ](</x y>)', 'code-span-consumed [ before ]('],
- ['[^fn](</a b>)', 'footnote-shaped label'],
- ['[a][b](</c d>)', 'reference-link-shaped label'],
- ['[a](</x \\> y>)', 'escaped > inside destination'],
- ['[a](</x <i:j> k>)', 'autolink inside destination'],
- ['[a](<x\ny>)', 'destination spans a line ending'],
- ['[link](</my uri> "t")', 'title after destination'],
- ['[link]( </my uri>)', 'whitespace between ( and <'],
- ['[a](</b c> )', 'whitespace between > and )'],
- [`[a](<${' '.repeat(2000)}x y>)`, 'destination exceeding the scan cap'],
- ];
-
- for (const [input, label] of mustStayGuarded) {
- test(`does not throw: ${label}`, () => {
- expect(() => mdManager.parse(`${input}\n`)).not.toThrow();
- });
- }
-
- test('conservative rejections form no link and stay byte-stable', () => {
- for (const src of [
- '[link](</my uri> "t")\n',
- '[link]( </my uri>)\n',
- '[a](</b c> )\n',
- 'x](</a b>)\n',
- 'link](</a b>)\n',
- ]) {
- const links = findNodes(mdManager.parseToMdast(src), 'link');
- expect(links.length).toBe(0);
- expectByteStable(src);
- }
- });
-
- test('rejected destination < stays PUA-protected', () => {
- expect(protectFromMdx('x](</a b>)')).toContain('\uE000');
- expect(protectFromMdx('[link](</my uri')).toContain('\uE000');
- });
-
- test('no-space angle destinations keep their current byte-stable behavior', () => {
- expectByteStable('[t](<u>)\n');
- expectByteStable('[link](<foo%20bar>)\n');
- expectByteStable('[link](</no-space>)\n');
- });
-});
diff --git a/packages/core/src/markdown/backslash-escape-guard.test.ts b/packages/core/src/markdown/backslash-escape-guard.test.ts
deleted file mode 100644
index daf6662a7..000000000
--- a/packages/core/src/markdown/backslash-escape-guard.test.ts
+++ /dev/null
@@ -1,188 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { Root } from 'mdast';
-import { encodeBackslashEscapes, restoreBackslashEscapesPlugin } from './backslash-escape-guard.ts';
-
-describe('encodeBackslashEscapes — basic behavior', () => {
- test('substitutes the `\\` byte in `\\<` patterns', () => {
- const src = '\\<';
- const out = encodeBackslashEscapes(src);
- expect(out).not.toBe(src);
- });
-
- test('preserves length char-for-char (`\\<` 2 chars → 2 chars)', () => {
- const src = '\\<';
- const out = encodeBackslashEscapes(src);
- expect(out.length).toBe(src.length);
- });
-
- test('keeps the `<` byte after substitution (only `\\` is replaced)', () => {
- const src = '\\<';
- const out = encodeBackslashEscapes(src);
- expect(out.charAt(out.length - 1)).toBe('<');
- });
-
- test('handles multiple `\\<` matches in a single source', () => {
- const src = 'a \\< b \\< c';
- const out = encodeBackslashEscapes(src);
- expect(out.length).toBe(src.length);
- expect(out.slice(0, 2)).toBe('a ');
- expect(out.slice(4, 7)).toBe(' b ');
- expect(out.slice(9, 11)).toBe(' c');
- });
-
- test('leaves unrelated bytes untouched (e.g. `\\>`, `\\:`, `\\@`)', () => {
- const src = '\\> \\: \\@';
- const out = encodeBackslashEscapes(src);
- expect(out).toBe(src);
- });
-
- test('leaves bare `<` (no preceding `\\`) untouched', () => {
- const src = 'a < b';
- const out = encodeBackslashEscapes(src);
- expect(out).toBe(src);
- });
-});
-
-describe('encodeBackslashEscapes — escape awareness', () => {
- test('skips `\\\\<` — `\\\\` is the escape-of-`\\`, leaving bare `<` after', () => {
- const src = '\\\\<';
- expect(src.length).toBe(3);
- const out = encodeBackslashEscapes(src);
- expect(out).toBe(src);
- });
-
- test('substitutes `\\\\\\<` — `\\\\` escapes `\\`, then `\\<` is the §2.4 escape-of-`<`', () => {
- const src = '\\\\\\<';
- expect(src.length).toBe(4);
- const out = encodeBackslashEscapes(src);
- expect(out).not.toBe(src);
- expect(out.length).toBe(src.length);
- });
-
- test('substitutes at start-of-string (zero preceding backslashes is even)', () => {
- const src = '\\<rest';
- const out = encodeBackslashEscapes(src);
- expect(out).not.toBe(src);
- expect(out.length).toBe(src.length);
- });
-});
-
-describe('restoreBackslashEscapesPlugin — mdast walk', () => {
- function runPlugin(tree: Root): Root {
- const transformer = restoreBackslashEscapesPlugin() as unknown as (root: Root) => void;
- transformer(tree);
- return tree;
- }
-
- test('strips the marker from text-node `value` field', () => {
- const encoded = encodeBackslashEscapes('\\<');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: encoded }],
- },
- ],
- };
- runPlugin(tree);
- const para = tree.children[0];
- if (para?.type !== 'paragraph') throw new Error('expected paragraph');
- const text = para.children[0];
- if (text?.type !== 'text') throw new Error('expected text');
- expect(text.value).toBe('<');
- });
-
- test('strips marker from `value` field on inlineCode and other string-valued nodes', () => {
- const encoded = encodeBackslashEscapes('foo \\< bar');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'inlineCode', value: encoded }],
- },
- ],
- };
- runPlugin(tree);
- const code = (tree.children[0] as { children: Array<{ value?: unknown }> }).children[0];
- expect(code?.value).toBe('foo < bar');
- });
-
- test('strips marker from `url` field (link nodes)', () => {
- const encoded = encodeBackslashEscapes('http://x?q=\\<');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [
- {
- type: 'link',
- url: encoded,
- title: null,
- children: [{ type: 'text', value: 'link' }],
- },
- ],
- },
- ],
- };
- runPlugin(tree);
- const link = (tree.children[0] as { children: Array<{ url?: string }> }).children[0];
- expect(link?.url).toBe('http://x?q=<');
- });
-
- test('strips marker from `title` field', () => {
- const encoded = encodeBackslashEscapes('Title \\<sub>');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [
- {
- type: 'link',
- url: 'https://example.com',
- title: encoded,
- children: [{ type: 'text', value: 'link' }],
- },
- ],
- },
- ],
- };
- runPlugin(tree);
- const link = (tree.children[0] as { children: Array<{ title?: string | null }> }).children[0];
- expect(link?.title).toBe('Title <sub>');
- });
-
- test('strips marker from `alt` field (image nodes)', () => {
- const encoded = encodeBackslashEscapes('See \\<img>');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'image', url: 'cat.png', title: null, alt: encoded }],
- },
- ],
- };
- runPlugin(tree);
- const image = (tree.children[0] as { children: Array<{ alt?: string | null }> }).children[0];
- expect(image?.alt).toBe('See <img>');
- });
-
- test('leaves un-encoded values untouched (no tree mutation when no marker present)', () => {
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: 'plain text without escapes' }],
- },
- ],
- };
- runPlugin(tree);
- const text = (tree.children[0] as { children: Array<{ value?: unknown }> }).children[0];
- expect(text?.value).toBe('plain text without escapes');
- });
-});
diff --git a/packages/core/src/markdown/boundary-whitespace-pm-display.test.ts b/packages/core/src/markdown/boundary-whitespace-pm-display.test.ts
deleted file mode 100644
index c6a9e40bf..000000000
--- a/packages/core/src/markdown/boundary-whitespace-pm-display.test.ts
+++ /dev/null
@@ -1,157 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-
-type PmJSON = JSONContent;
-
-function toJSON(node: unknown): PmJSON {
- const n = node as { toJSON?: () => PmJSON };
- return n && typeof n.toJSON === 'function' ? n.toJSON() : (node as PmJSON);
-}
-
-function pmText(node: unknown, out: string[] = []): string {
- const n = toJSON(node);
- if (!n || typeof n !== 'object') return out.join('');
- if (typeof n.text === 'string') out.push(n.text);
- if (Array.isArray(n.content)) for (const c of n.content) pmText(c, out);
- return out.join('');
-}
-
-function pmSourceLiteralSegments(
- node: unknown,
- out: Array<{ text: string; sourceRaw: string }> = [],
-): Array<{ text: string; sourceRaw: string }> {
- const n = toJSON(node);
- if (!n || typeof n !== 'object') return out;
- if (typeof n.text === 'string' && Array.isArray(n.marks)) {
- const mark = n.marks.find((mk) => (mk as { type?: string }).type === 'sourceLiteral') as
- | { attrs?: { sourceRaw?: string } }
- | undefined;
- if (mark) out.push({ text: n.text, sourceRaw: mark.attrs?.sourceRaw ?? '' });
- }
- if (Array.isArray(n.content)) for (const c of n.content) pmSourceLiteralSegments(c, out);
- return out;
-}
-
-describe('boundary-whitespace char-ref displays decoded in the PM doc (RED — the reported bug)', () => {
- test('interior boundary space displays as a real space, not literal &#x20;', () => {
- const shown = pmText(md.parse('a&#x20;b\n'));
- expect(shown).not.toContain('&#x20;');
- expect(shown).toBe('a b');
- });
-
- test('trailing boundary space displays as a real space (trailing-trim risk)', () => {
- const shown = pmText(md.parse('abc&#x20;\n'));
- expect(shown).not.toContain('&#x20;');
- expect(shown).toBe('abc ');
- });
-
- test('leading boundary space displays as a real space', () => {
- const shown = pmText(md.parse('&#x20;abc\n'));
- expect(shown).not.toContain('&#x20;');
- expect(shown).toBe(' abc');
- });
-
- test('decimal space form (&#32;) decodes for display', () => {
- expect(pmText(md.parse('a&#32;b\n'))).toBe('a b');
- });
-
- test('hex tab form (&#x9;) decodes for display', () => {
- expect(pmText(md.parse('a&#x9;b\n'))).toBe('a\tb');
- });
-
- test('decoded boundary space carries sourceLiteral.sourceRaw for byte-fidelity', () => {
- expect(pmSourceLiteralSegments(md.parse('a&#x20;b\n'))).toEqual([
- { text: ' ', sourceRaw: '&#x20;' },
- ]);
- });
-
- test('decoded boundary tab carries sourceLiteral.sourceRaw', () => {
- expect(pmSourceLiteralSegments(md.parse('a&#x9;b\n'))).toEqual([
- { text: '\t', sourceRaw: '&#x9;' },
- ]);
- });
-
- test('adjacent identical refs display as multiple spaces in ONE coalesced segment', () => {
- expect(pmText(md.parse('a&#x20;&#x20;b\n'))).toBe('a b');
- expect(pmSourceLiteralSegments(md.parse('a&#x20;&#x20;b\n'))).toEqual([
- { text: ' ', sourceRaw: '&#x20;&#x20;' },
- ]);
- });
-
- test('adjacent identical tab refs coalesce', () => {
- expect(pmText(md.parse('a&#x9;&#x9;b\n'))).toBe('a\t\tb');
- expect(pmSourceLiteralSegments(md.parse('a&#x9;&#x9;b\n'))).toEqual([
- { text: '\t\t', sourceRaw: '&#x9;&#x9;' },
- ]);
- });
-
- test('mixed adjacent whitespace refs coalesce into one segment', () => {
- expect(pmText(md.parse('a&#x20;&#x9;b\n'))).toBe('a \tb');
- expect(pmSourceLiteralSegments(md.parse('a&#x20;&#x9;b\n'))).toEqual([
- { text: ' \t', sourceRaw: '&#x20;&#x9;' },
- ]);
- });
-
- test('a long run (n>=4) coalesces into ONE segment, not two equal-mark pairs', () => {
- expect(pmText(md.parse('a&#x20;&#x20;&#x20;&#x20;b\n'))).toBe('a b');
- expect(pmSourceLiteralSegments(md.parse('a&#x20;&#x20;&#x20;&#x20;b\n'))).toEqual([
- { text: ' ', sourceRaw: '&#x20;&#x20;&#x20;&#x20;' },
- ]);
- });
-
- test('whitespace refs do NOT coalesce across an interleaved non-whitespace entity', () => {
- expect(pmText(md.parse('a&#x20;&amp;&#x20;b\n'))).toBe('a &amp; b');
- expect(pmSourceLiteralSegments(md.parse('a&#x20;&amp;&#x20;b\n'))).toEqual([
- { text: ' ', sourceRaw: '&#x20;' },
- { text: '&amp;', sourceRaw: '&amp;' },
- { text: ' ', sourceRaw: '&#x20;' },
- ]);
- });
-});
-
-describe('guard: byte-fidelity is unchanged by the display decode', () => {
- const byteStable = [
- 'a&#x20;b\n',
- 'abc&#x20;\n',
- '&#x20;abc\n',
- 'a&#32;b\n',
- 'a&#x9;b\n',
- 'a&#x41;b\n',
- 'a&amp;b\n',
- 'a\\&#x20;b\n',
- 'a&#x20;&#x20;b\n',
- 'a&#x9;&#x9;b\n',
- 'a&#x20;&#x20;&#x20;b\n',
- 'a&#x20;&#x9;b\n',
- 'a&#x20;&#x20;\n',
- '&#x20;&#x20;a\n',
- 'a&#x20;&amp;&#x20;b\n',
- '**a&#x20;&#x20;b**\n',
- 'a&#x20;&#x20;&#x20;&#x20;b\n',
- 'a&#x20;&#x20;&#x20;&#x20;&#x20;b\n',
- 'a&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;b\n',
- 'a&#x9;&#x9;&#x9;&#x9;b\n',
- 'a&#x20;&#x9;&#x20;&#x9;&#x20;b\n',
- ] as const;
- for (const src of byteStable) {
- test(`round-trips byte-identically: ${JSON.stringify(src)}`, () => {
- expect(md.serialize(md.parse(src))).toBe(src);
- });
- }
-});
-
-describe('guard: scope is strictly whitespace numeric refs (locked non-goal)', () => {
- test('non-whitespace numeric ref (&#x41; = "A") stays literal in display', () => {
- expect(pmText(md.parse('a&#x41;b\n'))).toBe('a&#x41;b');
- });
- test('named ref (&amp;) stays literal in display', () => {
- expect(pmText(md.parse('a&amp;b\n'))).toBe('a&amp;b');
- });
- test('a user-escaped \\&#x20; stays literal (escape = literal intent)', () => {
- expect(pmText(md.parse('x\\&#x20;y\n'))).toContain('&#x20;');
- });
-});
diff --git a/packages/core/src/markdown/callout-type-expansion.precision.test.ts b/packages/core/src/markdown/callout-type-expansion.precision.test.ts
deleted file mode 100644
index e97382a95..000000000
--- a/packages/core/src/markdown/callout-type-expansion.precision.test.ts
+++ /dev/null
@@ -1,142 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function findCalloutType(json: JSONContent): string | null {
- if (json.attrs?.componentName === 'Callout' || json.attrs?.componentName === 'GFMCallout') {
- const props = (json.attrs.props as Record<string, unknown> | undefined) ?? {};
- return typeof props.type === 'string' ? props.type : null;
- }
- for (const child of json.content ?? []) {
- const t = findCalloutType(child);
- if (t !== null) return t;
- }
- return null;
-}
-
-function findCalloutAuthoredAs(json: JSONContent): string | null {
- if (json.attrs?.componentName === 'Callout' || json.attrs?.componentName === 'GFMCallout') {
- const props = (json.attrs.props as Record<string, unknown> | undefined) ?? {};
- const v = props['data-authored-as'];
- return typeof v === 'string' ? v : null;
- }
- for (const child of json.content ?? []) {
- const v = findCalloutAuthoredAs(child);
- if (v !== null) return v;
- }
- return null;
-}
-
-const NEW_FIRST_CLASS_TYPES = [
- 'abstract',
- 'info',
- 'todo',
- 'success',
- 'question',
- 'failure',
- 'danger',
- 'bug',
- 'example',
- 'quote',
-] as const;
-
-describe('callout type expansion — first-class round-trip', () => {
- for (const type of NEW_FIRST_CLASS_TYPES) {
- test(`\`> [!${type}]\` parses + round-trips byte-stable`, () => {
- const src = `> [!${type}]\n> body\n`;
- const json = mdManager.parse(src);
- expect(findCalloutType(json)).toBe(type);
- expect(mdManager.serialize(json)).toBe(src);
- });
- }
-
- test('GFM 5 types still round-trip byte-stable (regression guard)', () => {
- for (const type of ['note', 'tip', 'important', 'warning', 'caution']) {
- const src = `> [!${type}]\n> body\n`;
- const json = mdManager.parse(src);
- expect(findCalloutType(json)).toBe(type);
- expect(mdManager.serialize(json)).toBe(src);
- }
- });
-
- test('first-class type with title round-trips', () => {
- const src = '> [!success] Step complete\n> body\n';
- const json = mdManager.parse(src);
- expect(findCalloutType(json)).toBe('success');
- expect(mdManager.serialize(json)).toBe(src);
- });
-});
-
-describe('callout type expansion — alias resolution', () => {
- const ALIAS_CASES: Array<[alias: string, canonical: string]> = [
- ['summary', 'abstract'],
- ['tldr', 'abstract'],
- ['check', 'success'],
- ['done', 'success'],
- ['help', 'question'],
- ['faq', 'question'],
- ['fail', 'failure'],
- ['missing', 'failure'],
- ['error', 'danger'],
- ['cite', 'quote'],
- ['idea', 'tip'],
- ['hint', 'tip'],
- ['warn', 'warning'],
- ['attention', 'warning'],
- ];
-
- for (const [alias, canonical] of ALIAS_CASES) {
- test(`alias \`${alias}\` resolves to \`${canonical}\` on parse`, () => {
- const json = mdManager.parse(`> [!${alias}]\n> body\n`);
- expect(findCalloutType(json)).toBe(canonical);
- expect(findCalloutAuthoredAs(json)).toBe(alias);
- });
-
- test(`alias \`${alias}\` round-trips byte-stable (data-authored-as preserved)`, () => {
- const src = `> [!${alias}]\n> body\n`;
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
- }
-});
-
-describe('callout type expansion — foldable markers on new types', () => {
- for (const type of NEW_FIRST_CLASS_TYPES) {
- test(`\`> [!${type}]+\` (open foldable) round-trips`, () => {
- const src = `> [!${type}]+\n> body\n`;
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test(`\`> [!${type}]-\` (closed foldable) round-trips`, () => {
- const src = `> [!${type}]-\n> body\n`;
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
- }
-
- test('`> [!quote]+ Title` (open foldable + title) round-trips', () => {
- const src = '> [!quote]+ A pull-quote\n> body\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-});
-
-describe('callout type expansion — GFM/Obsidian conflict resolution', () => {
- test('`important` stays first-class (OK keeps GFM semantics, not Obsidian alias to `tip`)', () => {
- const json = mdManager.parse('> [!important]\n> body\n');
- expect(findCalloutType(json)).toBe('important');
- });
-
- test('`caution` stays first-class (OK keeps GFM semantics, not Obsidian alias to `warning`)', () => {
- const json = mdManager.parse('> [!caution]\n> body\n');
- expect(findCalloutType(json)).toBe('caution');
- });
-});
-
-describe('callout type expansion — unknown tokens still fall back', () => {
- test('unknown type `> [!mystery]` falls back to `note` (existing contract)', () => {
- const json = mdManager.parse('> [!mystery]\n> body\n');
- expect(findCalloutType(json)).toBe('note');
- expect(findCalloutAuthoredAs(json)).toBe('mystery');
- });
-});
diff --git a/packages/core/src/markdown/comment-promoter.precision.test.ts b/packages/core/src/markdown/comment-promoter.precision.test.ts
deleted file mode 100644
index cf0d8938a..000000000
--- a/packages/core/src/markdown/comment-promoter.precision.test.ts
+++ /dev/null
@@ -1,655 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function collectCommentTextNodes(json: JSONContent): JSONContent[] {
- const out: JSONContent[] = [];
- const visit = (n: JSONContent) => {
- if (n.type === 'text' && (n.marks ?? []).some((m) => m.type === 'comment')) {
- out.push(n);
- }
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-function plainTextOf(json: JSONContent): string {
- let out = '';
- const visit = (n: JSONContent) => {
- if (n.type === 'text') out += n.text ?? '';
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-function findCommentBlocks(json: JSONContent): JSONContent[] {
- const out: JSONContent[] = [];
- const visit = (n: JSONContent) => {
- if (n.type === 'commentBlock') out.push(n);
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-describe('comment-promoter — acceptance', () => {
- test('basic `%%hello%%`', () => {
- const json = mdManager.parse('%%hello%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hello');
- });
-
- test('mid-paragraph `a %%hidden%% b`', () => {
- const json = mdManager.parse('a %%hidden%% b\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hidden');
- expect(plainTextOf(json)).toBe('a hidden b');
- });
-
- test('TODO-style `%%TODO: fix this%%`', () => {
- const json = mdManager.parse('%%TODO: fix this%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('TODO: fix this');
- });
-
- test('multi-word `%%a longer note here%%`', () => {
- const json = mdManager.parse('%%a longer note here%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('a longer note here');
- });
-
- test('inner `%%` claims close (`%%a %% b%%` matches `%%a %%`)', () => {
- const json = mdManager.parse('%%a %% b%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('a ');
- expect(plainTextOf(json)).toBe('a b%%');
- });
-
- test('with surrounding emphasis `*%%hi%%*`', () => {
- const json = mdManager.parse('*%%hi%%*\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hi');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['comment', 'emphasis']);
- });
-
- test('with surrounding bold `**%%hi%%**`', () => {
- const json = mdManager.parse('**%%hi%%**\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hi');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['comment', 'strong']);
- });
-
- test('with highlight `==%%note==%%` does NOT compose (delimiters cross)', () => {
- const json = mdManager.parse('==%%note==%%\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-
- test('`%% text %%` (whitespace inside delimiters) is a comment', () => {
- const json = mdManager.parse('%% hello %%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe(' hello ');
- });
-
- test('`%% text%%` (leading space only) is a comment', () => {
- const json = mdManager.parse('%% hello%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe(' hello');
- });
-
- test('`%%text %%` (trailing space only) is a comment', () => {
- const json = mdManager.parse('%%hello %%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hello ');
- });
-
- test('cleanly-nested `==%%note%%==` produces highlight outside, comment inside', () => {
- const json = mdManager.parse('==%%note%%==\n');
- const commentMarks = collectCommentTextNodes(json);
- expect(commentMarks.length).toBe(1);
- expect(commentMarks[0].text).toBe('note');
- const markTypes = (commentMarks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['comment', 'highlight']);
- });
-});
-
-describe('comment-promoter — rejection (stay prose)', () => {
- test('single `%text%` is not a delimiter', () => {
- const json = mdManager.parse('%text%\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('%text%');
- });
-
- test('`%%%` (three percents, edge ambiguity)', () => {
- const json = mdManager.parse('%%%\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-
- test('`%%%%` (four percents)', () => {
- const json = mdManager.parse('%%%%\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-
- test('whitespace-only body `%% %%` stays prose (rule 4)', () => {
- const json = mdManager.parse('%% %%\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('%% %%');
- });
-
- test('unmatched `%%text` (no closing delimiter)', () => {
- const json = mdManager.parse('%%text\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-
- test('unmatched `text%%` (no opening delimiter)', () => {
- const json = mdManager.parse('text%%\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-
- test('multi-line `%%a\\n more%%` (body cannot cross newline)', () => {
- const json = mdManager.parse('%%a\nmore%%\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-
- test('percentage prose `100% off` stays prose', () => {
- const json = mdManager.parse('100% off\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('100% off');
- });
-
- test('`50%%` (paired-percent at word end, no closing pair) stays prose', () => {
- const json = mdManager.parse('50%%\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('50%%');
- });
-
- test('URL-encoded characters in URL stay prose', () => {
- const json = mdManager.parse('See https://example.com/%20%40foo\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-});
-
-describe('comment-promoter — multi-match', () => {
- test('two comments on one line `%%a%% %%b%%`', () => {
- const json = mdManager.parse('%%a%% %%b%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(2);
- expect(marks[0].text).toBe('a');
- expect(marks[1].text).toBe('b');
- });
-
- test('chained `%%a%%b%%` comments `a`, leaves `b%%` as text', () => {
- const json = mdManager.parse('%%a%%b%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('a');
- expect(plainTextOf(json)).toBe('ab%%');
- });
-
- test('three comments `%%a%% %%b%% %%c%%`', () => {
- const json = mdManager.parse('%%a%% %%b%% %%c%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(3);
- expect(marks.map((m) => m.text)).toEqual(['a', 'b', 'c']);
- });
-
- test('adjacent no-separator `%%a%%%%b%%` produces ONE comment body=`a%%%%b`', () => {
- const json = mdManager.parse('%%a%%%%b%%\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('a%%%%b');
- expect(mdManager.serialize(json)).toBe('%%a%%%%b%%\n');
- });
-});
-
-describe('comment-promoter — protection from code spans + math', () => {
- test('`%%text%%` inside a code span stays code', () => {
- const json = mdManager.parse('a `%%text%%` b\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('a %%text%% b');
- });
-
- test('`%%text%%` inside an inline-math body stays math', () => {
- const json = mdManager.parse('$$%%a%%$$\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-
- test('`%%text%%` inside a fenced code block stays code', () => {
- const json = mdManager.parse('```\n%%text%%\n```\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- });
-});
-
-describe('comment-promoter — HTML comment `<!-- ... -->` form', () => {
- test('`<!-- secret -->` standalone block parses as commentBlock', () => {
- const json = mdManager.parse('<!-- secret -->\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toBe('secret');
- });
-
- test('mid-paragraph `<!-- x -->` parses as inline comment mark', () => {
- const json = mdManager.parse('a <!-- x --> b\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('x');
- expect(plainTextOf(json)).toBe('a x b');
- });
-
- test('empty inline `<!-- -->` stays prose (preserves CommonMark list-break separator)', () => {
- const json = mdManager.parse('a <!-- --> b\n');
- expect(collectCommentTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('a <!-- --> b');
- });
-
- test('empty standalone `<!-- -->` paragraph stays prose (block-form, list-break idiom)', () => {
- const json = mdManager.parse('<!-- -->\n');
- expect(findCommentBlocks(json).length).toBe(0);
- });
-
- test('list-break separator: `- foo\\n\\n<!-- -->\\n\\n- bar` produces two lists', () => {
- const json = mdManager.parse('- foo\n\n<!-- -->\n\n- bar\n');
- expect(findCommentBlocks(json).length).toBe(0);
- });
-
- test('two `<!-- ... -->` on one line', () => {
- const json = mdManager.parse('<!-- a --> and <!-- b -->\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(2);
- expect(marks.map((m) => m.text)).toEqual(['a', 'b']);
- });
-
- test('`<!-- text -->` and `%%text%%` mixed in same paragraph', () => {
- const json = mdManager.parse('a %%first%% then <!-- second --> end\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(2);
- expect(marks.map((m) => m.text)).toEqual(['first', 'second']);
- });
-
- test('`<!--secret-->` (no spaces) parses as comment', () => {
- const json = mdManager.parse('a <!--x--> b\n');
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('x');
- });
-
- test('preserves `<!--` source form on save (inline)', () => {
- const out = mdManager.serialize(mdManager.parse('a <!-- secret --> b\n'));
- expect(out).toBe('a <!-- secret --> b\n');
- });
-
- test('preserves `<!--` source form on save (block)', () => {
- const out = mdManager.serialize(mdManager.parse('<!-- secret -->\n'));
- expect(out).toBe('<!-- secret -->\n');
- });
-
- test('block `<!-- ... -->` with inline markdown body is recognised (Case E)', () => {
- const json = mdManager.parse('<!-- text with `code` and more -->\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toBe('text with code and more');
- });
-
- test('block `<!-- ... -->` with inline markdown round-trips byte-stable', () => {
- const src = '<!-- text with `code` and more -->\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('block `<!-- ... -->` with body containing `%%` round-trips byte-stable', () => {
- const src = '<!-- has %%inner%% inside -->\n';
- const r1 = mdManager.serialize(mdManager.parse(src));
- expect(r1).toBe(src); // byte-stable
- expect(mdManager.serialize(mdManager.parse(r1))).toBe(r1); // idempotent
- });
-
- test('inline `<!-- text with %%X%% inside -->` mid-paragraph preserves form', () => {
- const src = 'a <!-- text with %%X%% inside --> b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('block `%% text with `code` %%` (multi-child paragraph) is recognised (Case F)', () => {
- const json = mdManager.parse('%% text with `code` more %%\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toBe('text with code more');
- });
-
- test('block `%% text with `code` %%` round-trips byte-stable (sourceLayout: inline)', () => {
- const src = '%% text with `code` more %%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('block `%% a %% b %% c %%` (multiple `%%` pairs) does NOT promote (inline walker handles)', () => {
- const json = mdManager.parse('%%a%% b %%c%%\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(0);
- const marks = collectCommentTextNodes(json);
- expect(marks.length).toBe(2);
- expect(marks.map((m) => m.text)).toEqual(['a', 'c']);
- });
-
- test('block `<!-- ... **bold** ... -->` (markdown formatting in body) is recognised', () => {
- const json = mdManager.parse('<!-- has **bold** inside -->\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toContain('bold');
- });
-});
-
-describe('comment-promoter — round-trip', () => {
- test('`%%hello%%` round-trips byte-stable', () => {
- const src = '%%hello%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`a %%hidden%% b` round-trips byte-stable', () => {
- const src = 'a %%hidden%% b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('two comments round-trip byte-stable', () => {
- const src = '%%a%% %%b%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`%%a %% b%%` (whitespace-flanked inner) round-trips byte-stable', () => {
- const src = '%%a %% b%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`%% hello %%` (whitespace inside delimiters) round-trips byte-stable', () => {
- const src = '%% hello %%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`%% leading space%%` and `%%trailing space %%` round-trip byte-stable', () => {
- expect(mdManager.serialize(mdManager.parse('%% hello%%\n'))).toBe('%% hello%%\n');
- expect(mdManager.serialize(mdManager.parse('%%hello %%\n'))).toBe('%%hello %%\n');
- });
-
- test('`<!-- x -->` round-trips byte-stable (sourceForm preserved)', () => {
- const src = 'a <!-- x --> b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`%%text%%` inside code span round-trips as code (not comment)', () => {
- const src = 'a `%%text%%` b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('comment inside heading `## %%note%%` round-trips', () => {
- const src = '## %%note%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('comment inside blockquote `> %%note%%` round-trips', () => {
- const src = '> %%note%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('comment inside list item `- %%note%%` round-trips', () => {
- const src = '- %%note%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('mid-paragraph `**%%bold comment%%**` round-trips byte-stable (strong outer, comment inner — canonical)', () => {
- const src = 'a **%%bold comment%%** b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('mid-paragraph `*%%italic comment%%*` round-trips byte-stable (emphasis outer, comment inner)', () => {
- const src = 'a *%%italic comment%%* b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`%%**bold**%%` (mark-outside-mark input) — strong claims first, comment NOT recognized', () => {
- const src = 'a %%**bold**%% b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- expect(collectCommentTextNodes(mdManager.parse(src)).length).toBe(0);
- });
-});
-
-describe('comment-promoter — block form (`%%\\n…\\n%%`)', () => {
- test('Case A — `%%\\nhello\\n%%` (single paragraph, no blank lines)', () => {
- const json = mdManager.parse('%%\nhello\n%%\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toBe('hello');
- });
-
- test('Case A — `%%\\nline 1\\nline 2\\n%%` (multi-line single paragraph)', () => {
- const json = mdManager.parse('%%\nline 1\nline 2\n%%\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toContain('line 1');
- expect(plainTextOf(blocks[0])).toContain('line 2');
- });
-
- test('Case B — `%%\\n\\nhello\\n\\n%%` (single intermediate paragraph)', () => {
- const json = mdManager.parse('%%\n\nhello\n\n%%\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toBe('hello');
- });
-
- test('Case B — multi-paragraph `%%\\n\\npara 1\\n\\npara 2\\n\\n%%`', () => {
- const json = mdManager.parse('%%\n\npara 1\n\npara 2\n\n%%\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(blocks[0].content?.length).toBe(2);
- });
-
- test('lone `%%` with no closing fence stays prose', () => {
- const json = mdManager.parse('%%\n\nlone\n');
- expect(findCommentBlocks(json).length).toBe(0);
- });
-
- test('empty block `%%\\n\\n%%` stays prose (block+ schema requires at least one child)', () => {
- const json = mdManager.parse('%%\n\n%%\n');
- expect(findCommentBlocks(json).length).toBe(0);
- });
-
- test('three consecutive `%%` fences — greedy nearest-closer, third stays prose', () => {
- const json = mdManager.parse('%%\n\nbody\n\n%%\n\n%%\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toBe('body');
- });
-
- test('Case C mixed form (`%%` fused with adjacent prose) stays prose', () => {
- const json = mdManager.parse('%%\nfirst\n\nsecond\n%%\n');
- expect(findCommentBlocks(json).length).toBe(0);
- });
-
- test('block `%%\\nhello\\n%%` normalizes to canonical padded form on save', () => {
- const compact = '%%\nhello\n%%\n';
- const canonical = '%%\n\nhello\n\n%%\n';
- expect(mdManager.serialize(mdManager.parse(compact))).toBe(canonical);
- expect(mdManager.serialize(mdManager.parse(canonical))).toBe(canonical);
- });
-
- test('multi-paragraph block round-trips byte-stable (canonical form)', () => {
- const src = '%%\n\npara 1\n\npara 2\n\n%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('block surrounded by prose round-trips byte-stable', () => {
- const src = 'Before\n\n%%\n\nblock\n\n%%\n\nAfter\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('inline `%%text%%` still works alongside block (mixed doc)', () => {
- const src = 'Inline %%hidden%% here.\n\n%%\n\nblock body\n\n%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- const json = mdManager.parse(src);
- expect(collectCommentTextNodes(json).length).toBe(1); // inline mark
- expect(findCommentBlocks(json).length).toBe(1); // block node
- });
-
- test('block holding a nested heading round-trips byte-stable', () => {
- const src = '%%\n\n# heading\n\nbody text\n\n%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('block holding a nested list round-trips byte-stable', () => {
- const src = '%%\n\n- item 1\n- item 2\n\n%%\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`<!-- body -->` standalone block → commentBlock', () => {
- const json = mdManager.parse('<!-- body -->\n');
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(1);
- expect(plainTextOf(blocks[0])).toBe('body');
- });
-
- test('`<!-- body -->` block round-trips byte-stable (sourceForm preserved)', () => {
- const src = '<!-- body -->\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('two consecutive block comments parse + round-trip byte-stable', () => {
- const src = '%%\n\nfirst\n\n%%\n\n%%\n\nsecond\n\n%%\n';
- const json = mdManager.parse(src);
- const blocks = findCommentBlocks(json);
- expect(blocks.length).toBe(2);
- expect(plainTextOf(blocks[0])).toBe('first');
- expect(plainTextOf(blocks[1])).toBe('second');
- expect(mdManager.serialize(json)).toBe(src);
- });
-});
-
-describe('comment-promoter — direct mdast→markdown (sourceForm dispatch)', () => {
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const minimalState: any = {
- enter: () => () => {},
- containerPhrasing: (node: { children?: Array<{ value?: string }> }) =>
- (node.children ?? []).map((c) => c.value ?? '').join(''),
- createTracker: () => ({
- move: (s: string) => s,
- current: () => ({}),
- }),
- options: {},
- unsafe: [],
- safe: (s: string) => s,
- };
-
- test('comment with no sourceForm defaults to `%%children%%`', async () => {
- const { toMarkdownHandlers } = await import('./to-markdown-handlers.ts');
- const node = {
- type: 'comment' as const,
- children: [{ type: 'text' as const, value: 'hi' }],
- };
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const out = (toMarkdownHandlers as any).comment(node, undefined, minimalState, {});
- expect(out).toBe('%%hi%%');
- });
-
- test('comment with sourceForm=`percent` emits `%%children%%`', async () => {
- const { toMarkdownHandlers } = await import('./to-markdown-handlers.ts');
- const node = {
- type: 'comment' as const,
- children: [{ type: 'text' as const, value: 'hi' }],
- data: { sourceForm: 'percent' as const },
- };
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const out = (toMarkdownHandlers as any).comment(node, undefined, minimalState, {});
- expect(out).toBe('%%hi%%');
- });
-
- test('comment with sourceForm=`html` emits `<!-- children -->`', async () => {
- const { toMarkdownHandlers } = await import('./to-markdown-handlers.ts');
- const node = {
- type: 'comment' as const,
- children: [{ type: 'text' as const, value: 'hi' }],
- data: { sourceForm: 'html' as const },
- };
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const out = (toMarkdownHandlers as any).comment(node, undefined, minimalState, {});
- expect(out).toBe('<!-- hi -->');
- });
-
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const minimalBlockState: any = {
- ...minimalState,
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- containerFlow: (node: any) => {
- const para = node.children?.[0];
- if (para?.type === 'paragraph') {
- return (para.children ?? []).map((c: { value?: string }) => c.value ?? '').join('');
- }
- return '';
- },
- };
-
- test('commentBlock with no sourceForm emits `%%\\n\\n…\\n\\n%%`', async () => {
- const { toMarkdownHandlers } = await import('./to-markdown-handlers.ts');
- const node = {
- type: 'commentBlock' as const,
- children: [
- {
- type: 'paragraph' as const,
- children: [{ type: 'text' as const, value: 'hello' }],
- },
- ],
- };
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const out = (toMarkdownHandlers as any).commentBlock(node, undefined, minimalBlockState, {});
- expect(out).toBe('%%\n\nhello\n\n%%');
- });
-
- test('commentBlock with sourceForm=`html` (single paragraph) emits `<!-- body -->`', async () => {
- const { toMarkdownHandlers } = await import('./to-markdown-handlers.ts');
- const node = {
- type: 'commentBlock' as const,
- children: [
- {
- type: 'paragraph' as const,
- children: [{ type: 'text' as const, value: 'hello' }],
- },
- ],
- data: { sourceForm: 'html' as const },
- };
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const out = (toMarkdownHandlers as any).commentBlock(node, undefined, minimalBlockState, {});
- expect(out).toBe('<!-- hello -->');
- });
-
- test('commentBlock with sourceForm=`html` (multi-block body) falls back to `%%`', async () => {
- const { toMarkdownHandlers } = await import('./to-markdown-handlers.ts');
- const node = {
- type: 'commentBlock' as const,
- children: [
- {
- type: 'paragraph' as const,
- children: [{ type: 'text' as const, value: 'first' }],
- },
- {
- type: 'paragraph' as const,
- children: [{ type: 'text' as const, value: 'second' }],
- },
- ],
- data: { sourceForm: 'html' as const },
- };
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const out = (toMarkdownHandlers as any).commentBlock(node, undefined, minimalBlockState, {});
- expect(out.startsWith('%%')).toBe(true);
- expect(out.endsWith('%%')).toBe(true);
- });
-});
diff --git a/packages/core/src/markdown/dedent-block-jsx-close.test.ts b/packages/core/src/markdown/dedent-block-jsx-close.test.ts
deleted file mode 100644
index d7ac56cda..000000000
--- a/packages/core/src/markdown/dedent-block-jsx-close.test.ts
+++ /dev/null
@@ -1,157 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { dedentBlockJsxClose } from './dedent-block-jsx-close.ts';
-
-describe('dedentBlockJsxClose — happy path (list context)', () => {
- test('2-space-indented closing tag preceded by a bullet list is dedented', () => {
- const input = '<Tab label="A">\n\n- item\n\n </Tab>\n';
- expect(dedentBlockJsxClose(input)).toBe('<Tab label="A">\n\n- item\n\n</Tab>\n');
- });
-
- test('3-space-indented closing tag preceded by a bullet list is dedented', () => {
- const input = '<Tab>\n\n- item\n\n </Tab>\n';
- expect(dedentBlockJsxClose(input)).toBe('<Tab>\n\n- item\n\n</Tab>\n');
- });
-
- test('1-space-indented closing tag preceded by a list is dedented', () => {
- expect(dedentBlockJsxClose('- one\n\n </Step>\n')).toBe('- one\n\n</Step>\n');
- });
-
- test('preceded by an ordered list (`1.`) — same trigger', () => {
- expect(dedentBlockJsxClose('1. one\n\n </Tab>\n')).toBe('1. one\n\n</Tab>\n');
- });
-
- test('preceded by an asterisk-marker list — same trigger', () => {
- expect(dedentBlockJsxClose('* one\n\n </Tab>\n')).toBe('* one\n\n</Tab>\n');
- });
-
- test('preceded by a plus-marker list — same trigger', () => {
- expect(dedentBlockJsxClose('+ one\n\n </Tab>\n')).toBe('+ one\n\n</Tab>\n');
- });
-
- test('the full PRD-6759 repro shape: every Tab child closing dedents in one pass', () => {
- const input = [
- '<Tabs>',
- '',
- '<Tab label="X">',
- '',
- '- one',
- '',
- ' </Tab>',
- '',
- '<Tab label="Y">',
- '',
- '- two',
- '',
- ' </Tab>',
- '',
- '</Tabs>',
- '',
- ].join('\n');
- const out = dedentBlockJsxClose(input);
- expect(out.match(/^<\/Tab>$/gm)).toHaveLength(2);
- expect(out).not.toMatch(/^ {1,3}<\/Tab>$/m);
- });
-
- test('multiple blank lines between the list and the close — still dedents', () => {
- expect(dedentBlockJsxClose('- one\n\n\n\n </Tab>\n')).toBe('- one\n\n\n\n</Tab>\n');
- });
-});
-
-describe('dedentBlockJsxClose — leave-alone (non-list context)', () => {
- test('preceded by a plain paragraph — NOT dedented (preserves serializer round-trip)', () => {
- const input = '<Step>\n\nA\n\n </Step>\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('preceded by a heading — NOT dedented (no list-continuation hazard)', () => {
- const input = '<Step>\n\n# Title\n\n </Step>\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('preceded by the opening JSX tag (empty body) — NOT dedented', () => {
- const input = '<Step>\n\n </Step>\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('top-of-document indented close — NOT dedented (no preceding context)', () => {
- expect(dedentBlockJsxClose(' </Tab>\n')).toBe(' </Tab>\n');
- });
-
- test('flush-left closing tag is NOT touched (the regex anchor needs at least one space)', () => {
- const input = '- one\n\n</Tab>\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('4-space indent is NOT dedented (CommonMark indented code block — user-intended code)', () => {
- const input = '- one\n\n </Tab>\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('lowercase closing tag (HTML element) is NOT touched — `</span>` inside a list item is legitimate inline HTML', () => {
- const input = '- one\n\n </span>\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('closing tag NOT on its own line (surrounded by prose) is NOT touched', () => {
- const input = '- one\n\n done. </Tab>\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('inside a fenced code block, an indented ` </Tab>` after a bullet is preserved verbatim (documentation example)', () => {
- const input = '```mdx\n- one\n\n </Tab>\n```\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('inside a tilde-fenced code block, same preservation', () => {
- const input = '~~~mdx\n- one\n\n </Tab>\n~~~\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('no `</` anywhere → fast-reject without scanning fences', () => {
- const input = '# heading\n\n- one\n- two\n\nparagraph.\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-
- test('idempotent — running twice on already-dedented output is a no-op', () => {
- const once = dedentBlockJsxClose('- one\n\n </Tab>\n');
- expect(dedentBlockJsxClose(once)).toBe(once);
- });
-});
-
-describe('dedentBlockJsxClose — fence boundaries', () => {
- test('an indented closing tag OUTSIDE the fence (after a list) dedents; one INSIDE stays put', () => {
- const input = [
- '```mdx',
- '- one',
- ' </InsideFence>',
- '```',
- '',
- '- two',
- '',
- ' </OutsideFence>',
- '',
- ].join('\n');
- const out = dedentBlockJsxClose(input);
- expect(out).toContain(' </InsideFence>');
- expect(out).toMatch(/^<\/OutsideFence>$/m);
- });
-
- test('an unclosed fence (extends to EOF) protects everything after it', () => {
- const input = '```mdx\n- one\n\n </Tab>\n </Accordion>\n';
- expect(dedentBlockJsxClose(input)).toBe(input);
- });
-});
-
-describe('dedentBlockJsxClose — tag-name variants', () => {
- test('tag name with digits and underscores still matches after a list', () => {
- expect(dedentBlockJsxClose('- one\n\n </Step2_Inner>\n')).toBe('- one\n\n</Step2_Inner>\n');
- });
-
- test('whitespace before `>` is preserved through the dedent (`</Tab >`)', () => {
- expect(dedentBlockJsxClose('- one\n\n </Tab >\n')).toBe('- one\n\n</Tab >\n');
- });
-
- test('trailing whitespace on the line is preserved after the dedent', () => {
- expect(dedentBlockJsxClose('- one\n\n </Tab> \n')).toBe('- one\n\n</Tab> \n');
- });
-});
diff --git a/packages/core/src/markdown/embed-script-fidelity.test.ts b/packages/core/src/markdown/embed-script-fidelity.test.ts
deleted file mode 100644
index ff056c295..000000000
--- a/packages/core/src/markdown/embed-script-fidelity.test.ts
+++ /dev/null
@@ -1,95 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { sharedExtensions } from '../extensions/shared.ts';
-import {
- loadLargeEmbedFixtures,
- loadPrd6955Before,
- loadPrd6955CorruptedTriplicated,
-} from './fixtures/index.ts';
-import { MarkdownManager } from './index.ts';
-
-const mm = new MarkdownManager({ extensions: sharedExtensions });
-const mdRoundTrip = (md: string): string => mm.serialize(mm.parse(md));
-
-function extractScriptBodies(doc: string): string[] {
- const bodies: string[] = [];
- const re = /<script[^>]*>([\s\S]*?)<\/script>/g;
- let m: RegExpExecArray | null = re.exec(doc);
- while (m !== null) {
- const body = (m[1] ?? '').trim();
- if (body.length > 0) bodies.push(body);
- m = re.exec(doc);
- }
- return bodies;
-}
-
-function jsParses(code: string): boolean {
- try {
- new Function(code);
- return true;
- } catch {
- return false;
- }
-}
-
-const BRACE_INJECTION_RE = /\{onst|\{on\{|\{ons\{|\{var\{|\{onst\b/;
-
-describe('O6 — large-embed fixtures: every script is valid JS and survives a round-trip', () => {
- const fixtures = loadLargeEmbedFixtures().filter((f) => f.scriptsMustParse);
-
- for (const { name, source } of fixtures) {
- test(`${name}: every <script> body parses as JS`, () => {
- const bodies = extractScriptBodies(source);
- expect(bodies.length).toBeGreaterThan(0);
- for (const body of bodies) expect(jsParses(body)).toBe(true);
- });
-
- test(`${name}: no brace-injection signature in the source`, () => {
- expect(BRACE_INJECTION_RE.test(source)).toBe(false);
- });
-
- test(`${name}: a markdown round-trip preserves every script head (still parses, no injection)`, () => {
- const round = mdRoundTrip(source);
- expect(BRACE_INJECTION_RE.test(round)).toBe(false);
- for (const body of extractScriptBodies(round)) expect(jsParses(body)).toBe(true);
- });
-
- test(`${name}: second-pass round-trip is idempotent (re-render does not drift)`, () => {
- const once = mdRoundTrip(source);
- expect(mdRoundTrip(once)).toBe(once);
- });
- }
-});
-
-describe('O6 negative oracle — the PRD-6955 forensic captures', () => {
- test('BEFORE: every script parses and there is no brace-injection signature', () => {
- const before = loadPrd6955Before();
- const bodies = extractScriptBodies(before);
- expect(bodies.length).toBeGreaterThanOrEqual(1);
- for (const body of bodies) expect(jsParses(body)).toBe(true);
- expect(BRACE_INJECTION_RE.test(before)).toBe(false);
- });
-
- test('CORRUPTED: the brace-injection fingerprint marks heads the clean capture never has', () => {
- const corrupted = loadPrd6955CorruptedTriplicated();
- const before = loadPrd6955Before();
- expect(BRACE_INJECTION_RE.test(before)).toBe(false);
- const injectedHeads = corrupted.split('\n').filter((l) => BRACE_INJECTION_RE.test(l));
- expect(injectedHeads.length).toBeGreaterThanOrEqual(3);
- for (const head of injectedHeads) {
- expect(/^\s*(?:const|let|var)\s+DATA\s*=/.test(head)).toBe(false);
- }
- });
-
- test('CORRUPTED: the brace-injected scripts fail real JS parse; the clean siblings pass', () => {
- const bodies = extractScriptBodies(loadPrd6955CorruptedTriplicated());
- const injected = bodies.filter((b) => BRACE_INJECTION_RE.test(b));
- const clean = bodies.filter((b) => !BRACE_INJECTION_RE.test(b));
- expect(injected.length).toBeGreaterThanOrEqual(3);
- for (const body of injected) expect(jsParses(body)).toBe(false);
- for (const body of clean) expect(jsParses(body)).toBe(true);
- });
-
- test('BEFORE: the DATA declaration keyword is intact (the un-corrupted form the fingerprint contrasts with)', () => {
- expect(/\b(?:const|let|var)\s+DATA\s*=/.test(loadPrd6955Before())).toBe(true);
- });
-});
diff --git a/packages/core/src/markdown/entity-ref-guard.test.ts b/packages/core/src/markdown/entity-ref-guard.test.ts
deleted file mode 100644
index f4daa3fd5..000000000
--- a/packages/core/src/markdown/entity-ref-guard.test.ts
+++ /dev/null
@@ -1,408 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { Root } from 'mdast';
-import { sharedExtensions } from '../extensions/shared.ts';
-import {
- decodeEntityRefs,
- encodeEntityRefs,
- protectPattern,
- restoreEntityRefsPlugin,
-} from './entity-ref-guard.ts';
-import { MarkdownManager } from './index.ts';
-
-describe('protectPattern — length preservation', () => {
- test('encoded form has same length as match for entity refs', () => {
- const src = '&amp; &#65; &#x41;';
- const out = encodeEntityRefs(src);
- expect(out.length).toBe(src.length);
- });
-
- test('substitution is char-for-char so non-matching segments are unchanged', () => {
- const src = 'before &amp; middle &#65; after';
- const out = encodeEntityRefs(src);
- expect(out.slice(0, 7)).toBe('before ');
- expect(out.slice(12, 20)).toBe(' middle ');
- expect(out.slice(25, 31)).toBe(' after');
- });
-});
-
-describe('protectPattern — escape awareness via preceding-backslash parity', () => {
- test('skips substitution when match is preceded by odd backslash count (CommonMark §2.4 escape)', () => {
- const src = '\\&amp;';
- const out = encodeEntityRefs(src);
- expect(out).toBe(src);
- });
-
- test('substitutes when match is preceded by even backslash count (`\\\\` escapes the `\\`, `&` is bare)', () => {
- const src = '\\\\&amp;';
- const out = encodeEntityRefs(src);
- expect(out.length).toBe(src.length);
- expect(out).not.toBe(src);
- });
-
- test('substitutes at start-of-string (zero preceding backslashes is even)', () => {
- const src = '&amp;';
- const out = encodeEntityRefs(src);
- expect(out.length).toBe(src.length);
- expect(out).not.toBe(src);
- });
-});
-
-describe('encodeEntityRefs — supported entity-ref forms', () => {
- test('matches CommonMark §6.4 named form `&body;`', () => {
- const out = encodeEntityRefs('&amp;');
- expect(out).not.toBe('&amp;');
- expect(out.length).toBe(5);
- });
-
- test('matches CommonMark §6.4 decimal numeric form `&#NNN;`', () => {
- const out = encodeEntityRefs('&#65;');
- expect(out).not.toBe('&#65;');
- expect(out.length).toBe(5);
- });
-
- test('matches CommonMark §6.4 hex numeric form `&#xHHH;`', () => {
- const out = encodeEntityRefs('&#x41;');
- expect(out).not.toBe('&#x41;');
- expect(out.length).toBe(6);
- });
-
- test('matches CommonMark §6.4 hex form with capital `X` (`&#X41;`)', () => {
- const out = encodeEntityRefs('&#X41;');
- expect(out).not.toBe('&#X41;');
- expect(out.length).toBe(6);
- });
-
- test('does NOT match malformed entities (`&123foo;` lacks the `#` numeric prefix)', () => {
- const src = '&123foo;';
- const out = encodeEntityRefs(src);
- expect(out).toBe(src);
- });
-
- test('handles multiple matches in a single pass', () => {
- const src = '&amp; and &lt; and &gt;';
- const out = encodeEntityRefs(src);
- expect(out.length).toBe(src.length);
- expect(out.slice(5, 10)).toBe(' and ');
- expect(out.slice(14, 19)).toBe(' and ');
- });
-});
-
-describe('protectPattern — generic contract for FR-14 reuse', () => {
- test('caller can plug in a different pattern with a length-preserving replacement', () => {
- const out = protectPattern('foo bar', /b/g, () => 'X');
- expect(out).toBe('foo Xar');
- expect(out.length).toBe('foo bar'.length);
- });
-
- test('respects escape-awareness for the caller too', () => {
- const out = protectPattern('a \\b c b', /b/g, () => 'X');
- expect(out).toBe('a \\b c X');
- });
-
- test('regex /g flag lastIndex is reset before substitution', () => {
- const re = /b/g;
- re.lastIndex = 5; // simulate stale state
- const out = protectPattern('bbbbb', re, () => 'X');
- expect(out).toBe('XXXXX');
- });
-});
-
-describe('restoreEntityRefsPlugin — mdast walk', () => {
- function runPlugin(tree: Root): Root {
- const transformer = restoreEntityRefsPlugin() as unknown as (root: Root) => void;
- transformer(tree);
- return tree;
- }
-
- test('restores encoded entity in text-node `value` field', () => {
- const encoded = encodeEntityRefs('&amp;');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: encoded }],
- },
- ],
- };
- runPlugin(tree);
- const para = tree.children[0];
- if (para?.type !== 'paragraph') throw new Error('expected paragraph');
- const text = para.children[0];
- if (text?.type !== 'text') throw new Error('expected text');
- expect(text.value).toBe('&amp;');
- });
-
- test('tags text-node `data.entityRefSpans` with offset/length/raw', () => {
- const encoded = encodeEntityRefs('&amp;');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: encoded }],
- },
- ],
- };
- runPlugin(tree);
- const text = (tree.children[0] as { children: Array<{ data?: unknown }> }).children[0];
- const spans = (text.data as { entityRefSpans?: unknown }).entityRefSpans as Array<{
- offset: number;
- length: number;
- raw: string;
- }>;
- expect(Array.isArray(spans)).toBe(true);
- expect(spans).toHaveLength(1);
- const span = spans[0];
- if (!span) throw new Error('expected one span');
- expect(span.offset).toBe(0);
- expect(span.length).toBe('&amp;'.length);
- expect(span.raw).toBe('&amp;');
- });
-
- test('adjacent entity refs (no separator) restore at correct offsets', () => {
- const encoded = encodeEntityRefs('&amp;&lt;');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: encoded }],
- },
- ],
- };
- runPlugin(tree);
- const text = (tree.children[0] as { children: Array<{ data?: unknown; value: string }> })
- .children[0];
- if (!text) throw new Error('expected text node');
- expect(text.value).toBe('&amp;&lt;');
- const spans = (text.data as { entityRefSpans?: unknown }).entityRefSpans as Array<{
- offset: number;
- length: number;
- raw: string;
- }>;
- expect(spans).toEqual([
- { offset: 0, length: 5, raw: '&amp;' },
- { offset: 5, length: 4, raw: '&lt;' },
- ]);
- });
-
- test('restores encoded entity in `url` field (link nodes)', () => {
- const encoded = encodeEntityRefs('http://x?q=&amp;y=1');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [
- {
- type: 'link',
- url: encoded,
- title: null,
- children: [{ type: 'text', value: 'link' }],
- },
- ],
- },
- ],
- };
- runPlugin(tree);
- const para = tree.children[0];
- if (para?.type !== 'paragraph') throw new Error('expected paragraph');
- const link = para.children[0];
- if (link?.type !== 'link') throw new Error('expected link');
- expect(link.url).toBe('http://x?q=&amp;y=1');
- });
-
- test('restores encoded entity in `title` field', () => {
- const encoded = encodeEntityRefs('Title &amp; subtitle');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [
- {
- type: 'link',
- url: 'https://example.com',
- title: encoded,
- children: [{ type: 'text', value: 'link' }],
- },
- ],
- },
- ],
- };
- runPlugin(tree);
- const para = tree.children[0];
- if (para?.type !== 'paragraph') throw new Error('expected paragraph');
- const link = para.children[0];
- if (link?.type !== 'link') throw new Error('expected link');
- expect(link.title).toBe('Title &amp; subtitle');
- });
-
- test('restores encoded entity in `alt` field (image nodes)', () => {
- const encoded = encodeEntityRefs('Tom &amp; Jerry');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'image', url: 'cat.png', title: null, alt: encoded }],
- },
- ],
- };
- runPlugin(tree);
- const para = tree.children[0];
- if (para?.type !== 'paragraph') throw new Error('expected paragraph');
- const image = para.children[0];
- if (image?.type !== 'image') throw new Error('expected image');
- expect(image.alt).toBe('Tom &amp; Jerry');
- });
-
- test('restores encoded entity in code-node `lang` field (fenced-code info string)', () => {
- const encoded = encodeEntityRefs('f&ouml;&ouml;');
- const tree: Root = {
- type: 'root',
- children: [{ type: 'code', lang: encoded, meta: null, value: 'code' }],
- };
- runPlugin(tree);
- const code = tree.children[0];
- if (code?.type !== 'code') throw new Error('expected code');
- expect(code.lang).toBe('f&ouml;&ouml;');
- expect((code as { data?: unknown }).data).toBeUndefined();
- });
-
- test('restores encoded entity in code-node `meta` field (fenced-code info string)', () => {
- const encoded = encodeEntityRefs('title=&quot;x&quot;');
- const tree: Root = {
- type: 'root',
- children: [{ type: 'code', lang: 'js', meta: encoded, value: 'code' }],
- };
- runPlugin(tree);
- const code = tree.children[0];
- if (code?.type !== 'code') throw new Error('expected code');
- expect(code.meta).toBe('title=&quot;x&quot;');
- expect(code.lang).toBe('js');
- });
-
- test('does NOT tag entityRefSpans on non-text nodes (e.g. inlineCode value)', () => {
- const encoded = encodeEntityRefs('foo &amp; bar');
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'inlineCode', value: encoded }],
- },
- ],
- };
- runPlugin(tree);
- const para = tree.children[0];
- if (para?.type !== 'paragraph') throw new Error('expected paragraph');
- const code = para.children[0];
- if (code?.type !== 'inlineCode') throw new Error('expected inlineCode');
- expect(code.value).toBe('foo &amp; bar');
- expect((code as { data?: unknown }).data).toBeUndefined();
- });
-
- test('leaves un-encoded values untouched (no tree mutation when no PUA delimiter present)', () => {
- const tree: Root = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: 'plain text without entities' }],
- },
- ],
- };
- runPlugin(tree);
- const text = (tree.children[0] as { children: Array<{ value?: unknown; data?: unknown }> })
- .children[0];
- expect(text?.value).toBe('plain text without entities');
- expect(text?.data).toBeUndefined();
- });
-});
-
-describe('decodeEntityRefs — CommonMark §2.5 U+FFFD safety net', () => {
- test('NUL numeric reference decodes to U+FFFD', () => {
- expect(decodeEntityRefs('a&#0;b')).toBe('a\uFFFDb');
- });
-
- test('surrogate numeric reference decodes to U+FFFD, not a throw', () => {
- expect(decodeEntityRefs('x&#xD800;y')).toBe('x\uFFFDy');
- });
-
- test('out-of-range numeric reference decodes to U+FFFD', () => {
- expect(decodeEntityRefs('q&#x110000;r')).toBe('q\uFFFDr');
- });
-
- test('valid named and numeric refs still decode beside the net', () => {
- expect(decodeEntityRefs('&quot;&#65;&#x42;')).toBe('"AB');
- });
-});
-
-describe('fenced-code info-string entity refs — real-engine round-trip', () => {
- const mgr = new MarkdownManager({ extensions: sharedExtensions });
- const roundTrip = (src: string) => mgr.serialize(mgr.parse(src));
-
- test('named entity in lang round-trips byte-exact', () => {
- const src = '```f&ouml;&ouml;\ncode\n```\n';
- expect(roundTrip(src)).toBe(src);
- });
-
- test('round-trip is idempotent (second pass emits identical bytes)', () => {
- const src = '```f&ouml;&ouml;\ncode\n```\n';
- const once = roundTrip(src);
- expect(roundTrip(once)).toBe(once);
- });
-
- test('named entity in meta round-trips byte-exact', () => {
- const src = '```js meta&amp;stuff\ncode\n```\n';
- expect(roundTrip(src)).toBe(src);
- });
-
- test('entities in both lang and meta round-trip byte-exact', () => {
- const src = '```f&ouml;&ouml; title=&quot;x&quot;\ncode\n```\n';
- expect(roundTrip(src)).toBe(src);
- });
-
- test('numeric hex entity in lang round-trips byte-exact', () => {
- const src = '```f&#xF6;&#xF6;\ncode\n```\n';
- expect(roundTrip(src)).toBe(src);
- });
-
- test('tilde-fence info-string entity round-trips byte-exact', () => {
- const src = '~~~f&ouml;&ouml;\ncode\n~~~\n';
- expect(roundTrip(src)).toBe(src);
- });
-
- test('plain info strings stay byte-identical (no encode/restore detour)', () => {
- const src = '```typescript title="x"\ncode\n```\n';
- expect(roundTrip(src)).toBe(src);
- });
-});
-
-describe('escape + entity-ref marker interleaving on the same text node', () => {
- test('text node with BOTH escapedChars AND entityRefSpans round-trips through parse → serialize byte-equal', () => {
- const mgr = new MarkdownManager({ extensions: sharedExtensions });
- const src = 'prefix \\\\&amp; suffix\n';
- const parsed = mgr.parse(src);
- const serialized = mgr.serialize(parsed);
- expect(serialized).toBe(src);
- });
-
- test('escape and entity-ref offsets are independently preserved (escape at offset N, entity span starts at offset N+1)', () => {
- const mgr = new MarkdownManager({ extensions: sharedExtensions });
- const src = '\\\\&amp;\n';
- const parsed = mgr.parse(src);
- const serialized = mgr.serialize(parsed);
- expect(serialized).toBe(src);
- });
-
- test('multi-marker interleaving (escape, entity, escape, entity) round-trips byte-equal', () => {
- const mgr = new MarkdownManager({ extensions: sharedExtensions });
- const src = '\\\\foo&amp;bar\\\\baz&lt;qux\n';
- const parsed = mgr.parse(src);
- const serialized = mgr.serialize(parsed);
- expect(serialized).toBe(src);
- });
-});
diff --git a/packages/core/src/markdown/fence-regions.test.ts b/packages/core/src/markdown/fence-regions.test.ts
deleted file mode 100644
index 466f49e39..000000000
--- a/packages/core/src/markdown/fence-regions.test.ts
+++ /dev/null
@@ -1,102 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { findFencedRegions, isInsideFence } from './fence-regions.ts';
-
-describe('findFencedRegions', () => {
- test('backtick fence pair', () => {
- const src = 'before\n```\ncode\n```\nafter';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(1);
- expect(src.slice(regions[0][0], regions[0][1])).toBe('```\ncode\n```');
- });
-
- test('tilde fence pair', () => {
- const src = 'before\n~~~\ncode\n~~~\nafter';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(1);
- expect(src.slice(regions[0][0], regions[0][1])).toBe('~~~\ncode\n~~~');
- });
-
- test('mismatched fence types do not close each other', () => {
- const src = '```\ncode\n~~~\nstill code\n```';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(1);
- expect(src.slice(regions[0][0], regions[0][1])).toBe('```\ncode\n~~~\nstill code\n```');
- });
-
- test('unclosed fence extends to end of source', () => {
- const src = 'before\n```\ncode with no close';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(1);
- expect(regions[0][0]).toBe(7); // offset of ```
- expect(regions[0][1]).toBe(src.length);
- });
-
- test('fence at document start', () => {
- const src = '```\ncode\n```';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(1);
- expect(regions[0][0]).toBe(0);
- });
-
- test('fence at document end (no trailing newline)', () => {
- const src = 'text\n```\ncode\n```';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(1);
- expect(regions[0][1]).toBe(src.length);
- });
-
- test('multiple fenced regions', () => {
- const src = '```\na\n```\n\n~~~\nb\n~~~\n';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(2);
- });
-
- test('longer closing fence matches shorter opening', () => {
- const src = '```\ncode\n````';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(1);
- });
-
- test('shorter closing fence does not match longer opening', () => {
- const src = '````\ncode\n```\nstill inside\n````';
- const regions = findFencedRegions(src);
- expect(regions).toHaveLength(1);
- expect(src.slice(regions[0][0], regions[0][1])).toBe('````\ncode\n```\nstill inside\n````');
- });
-
- test('empty source returns no regions', () => {
- expect(findFencedRegions('')).toHaveLength(0);
- });
-
- test('no fences returns no regions', () => {
- expect(findFencedRegions('just plain text\nwith newlines')).toHaveLength(0);
- });
-});
-
-describe('isInsideFence', () => {
- test('offset inside fence returns true', () => {
- const src = 'before\n```\ncode\n```\nafter';
- const fences = findFencedRegions(src);
- const codeOffset = src.indexOf('code');
- expect(isInsideFence(codeOffset, fences)).toBe(true);
- });
-
- test('offset outside fence returns false', () => {
- const src = 'before\n```\ncode\n```\nafter';
- const fences = findFencedRegions(src);
- const afterOffset = src.indexOf('after');
- expect(isInsideFence(afterOffset, fences)).toBe(false);
- });
-
- test('offset at fence start boundary is inside', () => {
- const src = '```\ncode\n```';
- const fences = findFencedRegions(src);
- expect(isInsideFence(0, fences)).toBe(true);
- });
-
- test('offset at fence end boundary is outside', () => {
- const src = '```\ncode\n```';
- const fences = findFencedRegions(src);
- expect(isInsideFence(src.length, fences)).toBe(false);
- });
-});
diff --git a/packages/core/src/markdown/footnote.precision.test.ts b/packages/core/src/markdown/footnote.precision.test.ts
deleted file mode 100644
index 68130ca51..000000000
--- a/packages/core/src/markdown/footnote.precision.test.ts
+++ /dev/null
@@ -1,225 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function findNodes(json: JSONContent, type: string): JSONContent[] {
- const out: JSONContent[] = [];
- const visit = (n: JSONContent) => {
- if (n.type === type) out.push(n);
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-function plainTextOf(json: JSONContent): string {
- let out = '';
- const visit = (n: JSONContent) => {
- if (n.type === 'text') out += n.text ?? '';
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-describe('footnotes — basic round-trip', () => {
- test('numeric reference + definition round-trip byte-stable', () => {
- const src = 'Here is text with a footnote[^1].\n\n[^1]: First definition.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('named reference (`[^note]`) round-trips', () => {
- const src = 'Named[^note].\n\n[^note]: A named footnote.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('alphanumeric identifier (`[^a1b2]`) round-trips', () => {
- const src = 'Mixed[^a1b2].\n\n[^a1b2]: Mixed alphanumeric.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-});
-
-describe('footnotes — parse correctness', () => {
- test('reference parses as PM `footnoteReference` atom with identifier attr', () => {
- const json = mdManager.parse('Hi[^1].\n\n[^1]: Body.\n');
- const refs = findNodes(json, 'footnoteReference');
- expect(refs.length).toBe(1);
- expect(refs[0].attrs?.identifier).toBe('1');
- });
-
- test('definition parses as PM `footnoteDefinition` block with identifier attr', () => {
- const json = mdManager.parse('Hi[^1].\n\n[^1]: Body.\n');
- const defs = findNodes(json, 'footnoteDefinition');
- expect(defs.length).toBe(1);
- expect(defs[0].attrs?.identifier).toBe('1');
- });
-
- test('definition body preserves nested paragraph content', () => {
- const json = mdManager.parse('Hi[^1].\n\n[^1]: Body text here.\n');
- const defs = findNodes(json, 'footnoteDefinition');
- expect(plainTextOf(defs[0])).toBe('Body text here.');
- });
-
- test('definition with mixed-case identifier preserves casing on `label`', () => {
- const json = mdManager.parse('Hi[^MyNote].\n\n[^MyNote]: Body.\n');
- const refs = findNodes(json, 'footnoteReference');
- const defs = findNodes(json, 'footnoteDefinition');
- expect(refs.length).toBe(1);
- expect(defs.length).toBe(1);
- expect(refs[0].attrs?.identifier).toBe('mynote');
- expect(refs[0].attrs?.label).toBe('MyNote');
- expect(defs[0].attrs?.identifier).toBe('mynote');
- expect(defs[0].attrs?.label).toBe('MyNote');
- const src = 'Hi[^MyNote].\n\n[^MyNote]: Body.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('reference text is NOT corrupted to literal `"footnoteReference"` (regression guard)', () => {
- const json = mdManager.parse('Hi[^1].\n\n[^1]: Body.\n');
- expect(plainTextOf(json)).not.toContain('footnoteReference');
- });
-});
-
-describe('footnotes — multiple references + definitions', () => {
- test('two references in one paragraph round-trip', () => {
- const src = 'First[^1] and second[^2].\n\n[^1]: One.\n\n[^2]: Two.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('three references with named ids round-trip', () => {
- const src = 'A[^a], B[^b], C[^c].\n\n[^a]: alpha\n\n[^b]: beta\n\n[^c]: gamma\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('parse produces one PM atom per reference', () => {
- const json = mdManager.parse('First[^1] and second[^2].\n\n[^1]: One.\n\n[^2]: Two.\n');
- const refs = findNodes(json, 'footnoteReference');
- expect(refs.length).toBe(2);
- expect(refs.map((r) => r.attrs?.identifier)).toEqual(['1', '2']);
- });
-
- test('parse produces one PM block per definition', () => {
- const json = mdManager.parse('First[^1] and second[^2].\n\n[^1]: One.\n\n[^2]: Two.\n');
- const defs = findNodes(json, 'footnoteDefinition');
- expect(defs.length).toBe(2);
- });
-});
-
-describe('footnotes — orphan handling', () => {
- test('reference without matching definition stays prose (GFM upstream behavior)', () => {
- const src = 'Orphan[^missing] reference.\n';
- const json = mdManager.parse(src);
- const refs = findNodes(json, 'footnoteReference');
- expect(refs.length).toBe(0);
- expect(plainTextOf(json)).toContain('[^missing]');
- });
-
- test('definition without matching reference stays a definition (orphan def)', () => {
- const src = 'Just a paragraph.\n\n[^orphan]: Lonely definition.\n';
- const json = mdManager.parse(src);
- const defs = findNodes(json, 'footnoteDefinition');
- expect(defs.length).toBe(1);
- expect(defs[0].attrs?.identifier).toBe('orphan');
- });
-});
-
-describe('footnotes — definition body content', () => {
- test('definition with bold content round-trips', () => {
- const src = 'Hi[^1].\n\n[^1]: Body with **bold** text.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('definition with link content round-trips', () => {
- const src = 'Hi[^1].\n\n[^1]: See [example](https://example.com).\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('definition body containing a list parses as block content', () => {
- const src = 'Hi[^1].\n\n[^1]:\n - item 1\n - item 2\n';
- const json = mdManager.parse(src);
- const defs = findNodes(json, 'footnoteDefinition');
- expect(defs.length).toBe(1);
- expect(findNodes(defs[0], 'list').length).toBeGreaterThan(0);
- });
-});
-
-describe('footnotes — surrounding context', () => {
- test('references mid-paragraph with marks compose', () => {
- const src = 'Some **important**[^1] text.\n\n[^1]: Note.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('reference inside a heading round-trips', () => {
- const src = '## Heading with note[^1]\n\n[^1]: Heading note.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('reference inside a list item round-trips', () => {
- const src = '- Item with footnote[^1]\n\n[^1]: List note.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-});
-
-describe('footnotes — schema-drift defense', () => {
- const driftMgr = new MarkdownManager({
- extensions: sharedExtensions.filter(
- (ext) => ext.name !== 'footnoteReference' && ext.name !== 'footnoteDefinition',
- ),
- });
-
- function paragraphText(json: JSONContent, index: number): string {
- const paragraphs: JSONContent[] = [];
- for (const child of json.content ?? []) {
- if (child.type === 'paragraph') paragraphs.push(child);
- }
- const target = paragraphs[index];
- return target ? plainTextOf(target) : '';
- }
-
- test('reference without extension preserves `[^id]` source instead of corrupting to type-name text', () => {
- const src = 'Hello[^1].\n\n[^1]: Body.\n';
- const json = driftMgr.parse(src);
- const flat = JSON.stringify(json);
- expect(flat).not.toContain('"text":"footnoteReference"');
- expect(flat).not.toContain('HellofootnoteReference');
- expect(paragraphText(json, 0)).toBe('Hello[^1].');
- });
-
- test('reference without extension preserves source casing on label', () => {
- const src = 'Casing[^MyNote] check.\n\n[^MyNote]: Body.\n';
- const json = driftMgr.parse(src);
- expect(paragraphText(json, 0)).toBe('Casing[^MyNote] check.');
- expect(paragraphText(json, 1)).toBe('[^MyNote]: ');
- });
-
- test('reference without extension round-trips so the [^id] markdown survives serialize', () => {
- const src = 'Sentence with[^7] reference.\n\n[^7]: Body.\n';
- const back = driftMgr.serialize(driftMgr.parse(src));
- expect(back.match(/\[\^7\]/g)?.length ?? 0).toBeGreaterThanOrEqual(2);
- expect(back).not.toContain('footnoteReference');
- });
-
- test('definition without extension surfaces as visible `[^id]:` marker rather than silently dropping', () => {
- const src = 'Hi[^x].\n\n[^x]: Lost body text.\n';
- const json = driftMgr.parse(src);
- expect(paragraphText(json, 1)).toBe('[^x]: ');
- expect(JSON.stringify(json)).not.toContain('"text":"footnoteDefinition"');
- expect(JSON.stringify(json)).not.toContain('Lost body text');
- const paragraphCount = (json.content ?? []).filter((c) => c.type === 'paragraph').length;
- expect(paragraphCount).toBe(2);
- });
-
- test('inlineUnknownHandler emits node.value when present, never the bare mdast type name', () => {
- const mgr = new MarkdownManager({
- extensions: sharedExtensions.filter((ext) => ext.name !== 'mathInline'),
- });
- const json = mgr.parse('Wave $\\psi$ here.\n');
- const flat = JSON.stringify(json);
- expect(flat).not.toContain('"text":"inlineMath"');
- expect(flat).not.toContain('Wave inlineMath here');
- expect(plainTextOf(json)).toContain('\\psi');
- });
-});
diff --git a/packages/core/src/markdown/guard-flanking-matrix.test.ts b/packages/core/src/markdown/guard-flanking-matrix.test.ts
deleted file mode 100644
index c7a185819..000000000
--- a/packages/core/src/markdown/guard-flanking-matrix.test.ts
+++ /dev/null
@@ -1,131 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { readFileSync, writeFileSync } from 'node:fs';
-import path from 'node:path';
-import { sharedExtensions } from '../extensions/shared.ts';
-import {
- ATTENTION_DELIMITERS,
- buildGuardFlankingMatrix,
- computeGuardFlankingCells,
- computeGuardSubstitutionRows,
- diffGuardFlankingMatrix,
- type FlankClass,
- type GuardFlankingMatrix,
-} from './guard-flanking-matrix.ts';
-import { MarkdownManager } from './index.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-const roundTrip = (source: string) => md.serialize(md.parse(source));
-
-function textContent(source: string): string {
- let out = '';
- const walk = (node: unknown): void => {
- if (node === null || typeof node !== 'object') return;
- const rec = node as { value?: unknown; children?: unknown[] };
- if (typeof rec.value === 'string') out += rec.value;
- if (Array.isArray(rec.children)) rec.children.forEach(walk);
- };
- walk(md.parseToMdast(source));
- return out;
-}
-
-const MATRIX_PATH = path.join(import.meta.dir, 'guard-flanking-matrix.json');
-
-function readCommitted(): GuardFlankingMatrix {
- return JSON.parse(readFileSync(MATRIX_PATH, 'utf8')) as GuardFlankingMatrix;
-}
-
-if (process.env.OK_UPDATE_GUARD_FLANKING_MATRIX === '1') {
- writeFileSync(MATRIX_PATH, `${JSON.stringify(buildGuardFlankingMatrix(roundTrip), null, 2)}\n`);
-}
-
-describe('guard-flanking matrix - computation', () => {
- test('every guard substitution is classified; the punctuation->PUA rows are class-changing', () => {
- const rows = computeGuardSubstitutionRows();
- const changed = rows.filter((r) => r.classChanged);
- const preserved = rows.filter((r) => !r.classChanged);
- expect(changed.map((r) => `${r.alphabet}:${r.from}`).sort()).toEqual([
- 'backslash-escape:\\',
- 'entity-ref:&',
- 'entity-ref:;',
- 'r23::',
- 'r23:<',
- 'r23:>',
- 'r23:@',
- 'r23:{',
- ]);
- const expectedDelta: { from: FlankClass; to: FlankClass } = {
- from: 'punctuation',
- to: 'other',
- };
- for (const row of changed) {
- expect(row.fromClass).toBe(expectedDelta.from);
- expect(row.toClass).toBe(expectedDelta.to);
- }
- for (const row of preserved) {
- expect(row.alphabet).toBe('r23-sentinel-escape');
- expect(row.fromClass).toBe('other');
- expect(row.toClass).toBe('other');
- }
- });
-
- test('every class-changing substitution x delimiter carries a cell with protect-layer-verified adjacency', () => {
- const cells = computeGuardFlankingCells();
- expect(cells.length).toBe(8 * ATTENTION_DELIMITERS.length);
- const keys = new Set(cells.map((c) => `${c.alphabet}:${c.from} x ${c.delimiter}`));
- for (const row of computeGuardSubstitutionRows().filter((r) => r.classChanged)) {
- for (const delimiter of ATTENTION_DELIMITERS) {
- expect(keys.has(`${row.alphabet}:${row.from} x ${delimiter}`)).toBe(true);
- }
- }
- });
-});
-
-describe('guard-flanking matrix - freshness gate', () => {
- test('committed snapshot matches a live recompute (regen: bun run gen:guard-flanking-matrix)', () => {
- const live = buildGuardFlankingMatrix(roundTrip);
- const mismatches = diffGuardFlankingMatrix(live, readCommitted());
- expect(mismatches).toEqual([]);
- for (const [index, cell] of live.cells.entries()) {
- expect(cell.roundTrip).toBe(readCommitted().cells[index]?.roundTrip ?? '');
- }
- });
-
- test('every committed cell is lossless, idempotent, and preserves the guarded bytes verbatim', () => {
- for (const cell of readCommitted().cells) {
- const rt1 = roundTrip(cell.witness);
- expect(rt1).toBe(cell.roundTrip);
- expect(roundTrip(rt1)).toBe(rt1);
- expect(textContent(rt1)).toBe(textContent(cell.witness));
- if (cell.alphabet === 'entity-ref') {
- expect(rt1).toContain('&#x41;');
- }
- }
- });
-});
-
-describe('guard-flanking matrix - non-vacuity (tamper)', () => {
- test('a dropped committed cell fails the freshness diff', () => {
- const live = buildGuardFlankingMatrix(roundTrip);
- const tampered: GuardFlankingMatrix = { ...live, cells: live.cells.slice(1) };
- const mismatches = diffGuardFlankingMatrix(live, tampered);
- expect(mismatches.some((m) => m.includes('not committed'))).toBe(true);
- });
-
- test('a drifted round-trip pin fails the per-cell byte compare', () => {
- const committed = readCommitted();
- const first = committed.cells[0];
- if (!first) throw new Error('empty committed matrix');
- expect(roundTrip(first.witness)).not.toBe(`${first.roundTrip}tampered`);
- });
-
- test('a substitution removed from a guard alphabet fails the freshness diff', () => {
- const live = buildGuardFlankingMatrix(roundTrip);
- const tampered: GuardFlankingMatrix = {
- ...live,
- substitutions: live.substitutions.filter((r) => r.from !== '&'),
- cells: live.cells.filter((c) => c.from !== '&'),
- };
- const mismatches = diffGuardFlankingMatrix(live, tampered);
- expect(mismatches.some((m) => m.includes('entity-ref:&'))).toBe(true);
- });
-});
diff --git a/packages/core/src/markdown/handler-shadow-audit.test.ts b/packages/core/src/markdown/handler-shadow-audit.test.ts
deleted file mode 100644
index b717ef4d0..000000000
--- a/packages/core/src/markdown/handler-shadow-audit.test.ts
+++ /dev/null
@@ -1,99 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { sharedExtensions } from '../extensions/shared.ts';
-import {
- collectExtensionHandlerKeys,
- computeShadowedHandlerKeys,
- HANDLER_SHADOW_ADJUDICATIONS,
- type HandlerShadowWitness,
-} from './handler-shadow-audit.ts';
-import { MarkdownManager } from './index.ts';
-import { createSerializeProcessor } from './pipeline.ts';
-import { toMarkdownHandlers } from './to-markdown-handlers.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-const roundTrip = (source: string) => md.serialize(md.parse(source));
-
-function computeLiveShadowedKeys(): string[] {
- const processor = createSerializeProcessor({});
- const extensions = processor.data('toMarkdownExtensions');
- if (!Array.isArray(extensions) || extensions.length === 0) {
- throw new Error('serialize processor registered no toMarkdown extensions');
- }
- return computeShadowedHandlerKeys(
- collectExtensionHandlerKeys(extensions),
- Object.keys(toMarkdownHandlers),
- );
-}
-
-describe('handler-shadow audit - closure', () => {
- test('every shadowed handler key carries an adjudication and none is stale', () => {
- const shadowed = computeLiveShadowedKeys();
- expect(shadowed.length).toBeGreaterThan(0);
- const adjudicated = Object.keys(HANDLER_SHADOW_ADJUDICATIONS).sort();
- expect(shadowed).toEqual(adjudicated);
- });
-
- test('the known shadow surface is exactly the six adjudicated keys', () => {
- expect(computeLiveShadowedKeys()).toEqual([
- 'delete',
- 'inlineCode',
- 'inlineMath',
- 'mdxJsxFlowElement',
- 'mdxJsxTextElement',
- 'table',
- ]);
- });
-});
-
-describe('handler-shadow audit - live witnesses', () => {
- test('every adjudication witness round-trips byte-exactly through the real engine', () => {
- const runWitness = (key: string, witness: HandlerShadowWitness): void => {
- const out = roundTrip(witness.input);
- expect(
- out === witness.input || out === `${witness.input}\n`,
- `${key} witness drifted: ${JSON.stringify(witness.input)} -> ${JSON.stringify(out)}`,
- ).toBe(true);
- if (witness.expect === 'byte-and-reparse-type') {
- const reparsed = md.parseToMdast(out) as { children?: Array<{ type: string }> };
- expect(reparsed.children?.[0]?.type).toBe(witness.reparseType ?? '');
- }
- };
- for (const [key, adjudication] of Object.entries(HANDLER_SHADOW_ADJUDICATIONS)) {
- expect(adjudication.witnesses.length).toBeGreaterThan(0);
- for (const witness of adjudication.witnesses) runWitness(key, witness);
- }
- });
-});
-
-describe('handler-shadow audit - non-vacuity (tamper)', () => {
- test('removing the inlineCode (table-cell pipe re-escape) adjudication fails the closure', () => {
- const shadowed = computeLiveShadowedKeys();
- const { inlineCode: _omitted, ...rest } = HANDLER_SHADOW_ADJUDICATIONS;
- expect(shadowed).not.toEqual(Object.keys(rest).sort());
- const missing = shadowed.filter((key) => !(key in rest));
- expect(missing).toEqual(['inlineCode']);
- });
-
- test('a planted adjudication for an unshadowed key is detected as stale', () => {
- const shadowed = new Set(computeLiveShadowedKeys());
- const planted = {
- ...HANDLER_SHADOW_ADJUDICATIONS,
- paragraph: HANDLER_SHADOW_ADJUDICATIONS.delete,
- };
- const stale = Object.keys(planted).filter((key) => !shadowed.has(key));
- expect(stale).toEqual(['paragraph']);
- });
-
- test('the key collector sees through nested extension arrays', () => {
- const keys = collectExtensionHandlerKeys([
- {
- extensions: [
- { handlers: { alpha: () => '' } },
- { extensions: [{ handlers: { beta: () => '' } }] },
- ],
- },
- { handlers: { gamma: () => '' } },
- ]);
- expect(keys).toEqual(['alpha', 'beta', 'gamma']);
- });
-});
diff --git a/packages/core/src/markdown/handlers.mdx.test.ts b/packages/core/src/markdown/handlers.mdx.test.ts
deleted file mode 100644
index 2e3b430f1..000000000
--- a/packages/core/src/markdown/handlers.mdx.test.ts
+++ /dev/null
@@ -1,62 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function roundTrip(md: string): string {
- return mdManager.serialize(mdManager.parse(md));
-}
-
-function findInJson(json: JSONContent, type: string): JSONContent | null {
- if (json.type === type) return json;
- for (const child of json.content ?? []) {
- const found = findInJson(child, type);
- if (found) return found;
- }
- return null;
-}
-
-describe('MDX: flow element (block-level JSX)', () => {
- test('self-closing component round-trips', () => {
- const md = '<Chart />\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('component with string literal attr round-trips', () => {
- const md = '<Callout type="info" />\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('component with expression attr round-trips', () => {
- const md = '<Chart data={values} />\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('component with boolean shorthand round-trips', () => {
- const md = '<Icon disabled />\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('member expression tag round-trips', () => {
- const md = '<Docs.Link />\n';
- expect(roundTrip(md)).toBe(md);
- });
-});
-
-describe('MDX: PM node storage', () => {
- test('self-closing component parsed to jsxComponent with sourceRaw', () => {
- const json = mdManager.parse('<Chart />\n');
- const jsx = findInJson(json, 'jsxComponent');
- expect(jsx).toBeDefined();
- expect(jsx.attrs.sourceRaw).toContain('Chart');
- });
-
- test('component with attrs stored in sourceRaw', () => {
- const json = mdManager.parse('<Callout type="info" />\n');
- const jsx = findInJson(json, 'jsxComponent');
- expect(jsx).toBeDefined();
- expect(jsx.attrs.sourceRaw).toContain('type="info"');
- });
-});
diff --git a/packages/core/src/markdown/handlers.test.ts b/packages/core/src/markdown/handlers.test.ts
deleted file mode 100644
index 635c1b68d..000000000
--- a/packages/core/src/markdown/handlers.test.ts
+++ /dev/null
@@ -1,835 +0,0 @@
-import { describe, expect, mock, spyOn, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-interface PmMarkJson {
- type: string;
- attrs?: Record<string, unknown>;
-}
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function findInJson(json: JSONContent, type: string): JSONContent | null {
- if (json.type === type) return json;
- for (const child of json.content ?? []) {
- const found = findInJson(child, type);
- if (found) return found;
- }
- return null;
-}
-
-function findMarkInJson(json: JSONContent, markType: string): PmMarkJson | null {
- if (json.marks) {
- const mark = json.marks.find((m) => m.type === markType) as PmMarkJson | undefined;
- if (mark) return mark;
- }
- for (const child of json.content ?? []) {
- const found = findMarkInJson(child, markType);
- if (found) return found;
- }
- return null;
-}
-
-function findJsxComponentInJson(json: JSONContent, componentName: string): JSONContent | null {
- if (json.type === 'jsxComponent' && json.attrs?.componentName === componentName) return json;
- for (const child of json.content ?? []) {
- const found = findJsxComponentInJson(child, componentName);
- if (found) return found;
- }
- return null;
-}
-
-describe('Tier B fidelity: emphasis delimiter', () => {
- test('underscore emphasis carries sourceDelimiter = "_"', () => {
- const json = mdManager.parse('_word_\n');
- const emphMark = findMarkInJson(json, 'emphasis');
- expect(emphMark).toBeDefined();
- expect(emphMark.attrs?.sourceDelimiter).toBe('_');
- });
-
- test('asterisk emphasis carries sourceDelimiter = "*"', () => {
- const json = mdManager.parse('*word*\n');
- const emphMark = findMarkInJson(json, 'emphasis');
- expect(emphMark).toBeDefined();
- expect(emphMark.attrs?.sourceDelimiter).toBe('*');
- });
-});
-
-describe('Tier B fidelity: strong delimiter', () => {
- test('double-underscore strong carries sourceDelimiter = "__"', () => {
- const json = mdManager.parse('__word__\n');
- const strongMark = findMarkInJson(json, 'strong');
- expect(strongMark).toBeDefined();
- expect(strongMark.attrs?.sourceDelimiter).toBe('__');
- });
-
- test('double-asterisk strong carries sourceDelimiter = "**"', () => {
- const json = mdManager.parse('**word**\n');
- const strongMark = findMarkInJson(json, 'strong');
- expect(strongMark).toBeDefined();
- expect(strongMark.attrs?.sourceDelimiter).toBe('**');
- });
-});
-
-describe('Tier B fidelity: heading style', () => {
- test('ATX heading carries headingStyle = "atx"', () => {
- const json = mdManager.parse('## Title\n');
- const heading = findInJson(json, 'heading');
- expect(heading).toBeDefined();
- expect(heading.attrs.level).toBe(2);
- expect(heading.attrs.headingStyle).toBe('atx');
- });
-});
-
-describe('Tier B fidelity: code block fence', () => {
- test('backtick fence carries fenceDelimiter and fenceLength', () => {
- const json = mdManager.parse('```js\ncode\n```\n');
- const code = findInJson(json, 'codeBlock');
- expect(code).toBeDefined();
- expect(code.attrs.language).toBe('js');
- expect(code.attrs.fenceDelimiter).toBe('`');
- expect(code.attrs.fenceLength).toBe(3);
- });
-
- test('tilde fence carries fenceDelimiter = "~"', () => {
- const json = mdManager.parse('~~~\ncode\n~~~\n');
- const code = findInJson(json, 'codeBlock');
- expect(code).toBeDefined();
- expect(code.attrs.fenceDelimiter).toBe('~');
- });
-});
-
-describe('Tier B fidelity: thematic break', () => {
- test('--- carries sourceRaw = "---"', () => {
- const json = mdManager.parse('---\n');
- const hr = findInJson(json, 'thematicBreak');
- expect(hr).toBeDefined();
- expect(hr.attrs.sourceRaw).toBe('---');
- });
-
- test('*** carries sourceRaw = "***"', () => {
- const json = mdManager.parse('***\n');
- const hr = findInJson(json, 'thematicBreak');
- expect(hr).toBeDefined();
- expect(hr.attrs.sourceRaw).toBe('***');
- });
-});
-
-describe('Tier B fidelity: hard break', () => {
- test('backslash hard break carries hardBreakStyle = "backslash"', () => {
- const json = mdManager.parse('line\\\nbreak\n');
- const brk = findInJson(json, 'hardBreak');
- expect(brk).toBeDefined();
- expect(brk.attrs.hardBreakStyle).toBe('backslash');
- });
-});
-
-describe('Tier B fidelity: list markers', () => {
- test('dash bullet list carries bulletMarker = "-"', () => {
- const json = mdManager.parse('- item\n');
- const list = findInJson(json, 'list');
- expect(list).toBeDefined();
- expect(list.attrs.bulletMarker).toBe('-');
- });
-
- test('plus bullet list carries bulletMarker = "+"', () => {
- const json = mdManager.parse('+ item\n');
- const list = findInJson(json, 'list');
- expect(list).toBeDefined();
- expect(list.attrs.bulletMarker).toBe('+');
- });
-
- test('ordered list with dot carries listMarkerDelimiter = "."', () => {
- const json = mdManager.parse('1. item\n');
- const list = findInJson(json, 'list');
- expect(list).toBeDefined();
- expect(list.attrs.listMarkerDelimiter).toBe('.');
- });
-});
-
-describe('Tier B fidelity: listItem PM-schema artifact stripping (R6d / US-011)', () => {
- test('listItem with code as first child round-trips byte-identically', () => {
- const input = '1. ```\n foo\n ```\n\n bar\n';
- const r1 = mdManager.serialize(mdManager.parse(input));
- const r2 = mdManager.serialize(mdManager.parse(r1));
- expect(r1).toBe(r2);
- expect(r1).toBe(input);
- });
-
- test('listItem with code as only child round-trips byte-identically', () => {
- const input = '1. ```\n foo\n ```\n';
- const r1 = mdManager.serialize(mdManager.parse(input));
- const r2 = mdManager.serialize(mdManager.parse(r1));
- expect(r1).toBe(r2);
- expect(r1).toBe(input);
- });
-
- test('listItem with paragraph first stays unchanged (no spurious strip)', () => {
- const input = '1. foo\n\n ```\n bar\n ```\n';
- const r1 = mdManager.serialize(mdManager.parse(input));
- const r2 = mdManager.serialize(mdManager.parse(r1));
- expect(r1).toBe(r2);
- expect(r1).toBe(input);
- });
-
- test('genuinely empty listItem (single empty para child) is preserved', () => {
- const input = '1.\n';
- const r1 = mdManager.serialize(mdManager.parse(input));
- const r2 = mdManager.serialize(mdManager.parse(r1));
- expect(r1).toBe(r2);
- const json = mdManager.parse(input);
- const listItem = findInJson(json, 'listItem');
- expect(listItem).toBeDefined();
- });
-
- test('listItem with thematicBreak as first child round-trips', () => {
- const input = '1. ---\n\n foo\n';
- const r1 = mdManager.serialize(mdManager.parse(input));
- const r2 = mdManager.serialize(mdManager.parse(r1));
- expect(r1).toBe(r2);
- });
-
- test('listItem with blockquote as first child round-trips', () => {
- const input = '1. > foo\n > bar\n';
- const r1 = mdManager.serialize(mdManager.parse(input));
- const r2 = mdManager.serialize(mdManager.parse(r1));
- expect(r1).toBe(r2);
- });
-
- test('nested listItem with code block round-trips', () => {
- const input = '1. - ```\n foo\n ```\n\n bar\n';
- const r1 = mdManager.serialize(mdManager.parse(input));
- const r2 = mdManager.serialize(mdManager.parse(r1));
- expect(r1).toBe(r2);
- });
-});
-
-describe('Tier C: link style', () => {
- test('inline link carries linkStyle = "inline"', () => {
- const json = mdManager.parse('[text](https://example.com)\n');
- const linkMark = findMarkInJson(json, 'link');
- expect(linkMark).toBeDefined();
- expect(linkMark.attrs.href).toBe('https://example.com');
- expect(linkMark.attrs.linkStyle).toBe('inline');
- });
-
- test('empty-label inline links stay literal text (no link mark)', () => {
- const json = mdManager.parse('[]()\n');
- const paragraph = findInJson(json, 'paragraph');
- expect(paragraph?.content?.[0]?.type).toBe('text');
- expect(paragraph?.content?.[0]?.text).toBe('[]()');
- expect(findMarkInJson(json, 'link')).toBeNull();
- });
-
- test('empty-label inline link with destination stays literal text (no link mark)', () => {
- const json = mdManager.parse('[](https://example.com)\n');
- const paragraph = findInJson(json, 'paragraph');
- expect(paragraph?.content?.[0]?.type).toBe('text');
- expect(paragraph?.content?.[0]?.text).toBe('[](https://example.com)');
- expect(findMarkInJson(json, 'link')).toBeNull();
- });
-
- test('trailing backslash runs carry sourceLiteral mark', () => {
- const triple = '\\'.repeat(3);
- const json = mdManager.parse(`text ${triple}\n`);
- const paragraph = findInJson(json, 'paragraph');
- expect(paragraph?.content).toHaveLength(1);
- expect(paragraph?.content?.[0]?.type).toBe('text');
- expect(paragraph?.content?.[0]?.text).toBe(`text ${'\\'.repeat(2)}`);
- const sourceLiteral = findMarkInJson(json, 'sourceLiteral');
- expect(sourceLiteral?.attrs?.sourceRaw).toBe(`text ${triple}`);
- });
-
- test('sourceRaw takes priority when escapedChars and trailing backslash coexist', () => {
- const trailing = '\\';
- const json = mdManager.parse(`\\[text${trailing}\n`);
- const paragraph = findInJson(json, 'paragraph');
- expect(paragraph?.content).toHaveLength(1);
- expect(paragraph?.content?.[0]?.type).toBe('text');
- expect(paragraph?.content?.[0]?.text).toBe(`[text${trailing}`);
- const sourceLiteral = findMarkInJson(json, 'sourceLiteral');
- expect(sourceLiteral?.attrs?.sourceRaw).toBe(`\\[text${trailing}`);
- expect(findMarkInJson(json, 'escapeMark')).toBeNull();
- });
-
- test('image with empty alt remains image syntax', () => {
- const json = mdManager.parse('![](https://example.com/img.png)\n');
- expect(findInJson(json, 'image')).toBeDefined();
- });
-});
-
-describe('Tier A: passthrough', () => {
- test('blockquote round-trip', () => {
- const md = '> Quote text.\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('inline code produces code mark', () => {
- const json = mdManager.parse('Use `code` here.\n');
- const codeMark = findMarkInJson(json, 'code');
- expect(codeMark).toBeDefined();
- });
-
- test('paragraph round-trip', () => {
- const md = 'Hello world.\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-});
-
-describe('handlers.wikiLinkEmbed — server-absolute URL contract (Bug B/C)', () => {
- test('image wiki-embed emits server-absolute src when resolveEmbed provides contentDir-relative path', () => {
- const json = mdManager.parse('![[IMG.PNG]]\n', {
- resolveEmbed: (target: string, _source: string) => {
- if (target === 'IMG.PNG') return 'stories/wiki-links-next/IMG.PNG';
- return null;
- },
- sourcePath: 'stories/wiki-links-next/README.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toMatch(/^\//);
- expect(props?.src).toBe('/stories/wiki-links-next/IMG.PNG');
- });
-
- test('PDF wiki-embed emits server-absolute src on jsxComponent(WikiEmbedFile)', () => {
- const json = mdManager.parse('![[doc.pdf]]\n', {
- resolveEmbed: (target: string, _source: string) => {
- if (target === 'doc.pdf') return 'docs/sub/doc.pdf';
- return null;
- },
- sourcePath: 'docs/sub/notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedFile');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/docs/sub/doc.pdf');
- });
-
- test('WikiEmbedFile stamps formatted size when resolveSize returns bytes', () => {
- const json = mdManager.parse('![[doc.pdf]]\n', {
- resolveEmbed: (target: string) => (target === 'doc.pdf' ? 'docs/sub/doc.pdf' : null),
- resolveSize: (target: string) => (target === 'doc.pdf' ? 909_312 : null),
- sourcePath: 'docs/sub/notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedFile');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.size).toBe('888 KiB');
- });
-
- test('WikiEmbedFile size prop is null when resolveSize is omitted (renderer omits the size span)', () => {
- const json = mdManager.parse('![[doc.pdf]]\n', {
- resolveEmbed: (target: string) => (target === 'doc.pdf' ? 'docs/sub/doc.pdf' : null),
- sourcePath: 'docs/sub/notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedFile');
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.size).toBeNull();
- });
-
- test('WikiEmbedFile size prop is null when resolveSize returns null (file moved / unreadable)', () => {
- const json = mdManager.parse('![[doc.pdf]]\n', {
- resolveEmbed: (target: string) => (target === 'doc.pdf' ? 'docs/sub/doc.pdf' : null),
- resolveSize: () => null,
- sourcePath: 'docs/sub/notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedFile');
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.size).toBeNull();
- });
-
- test('video wiki-embed (MP4) emits server-absolute src on jsxComponent(WikiEmbedVideo)', () => {
- const json = mdManager.parse('![[clip.mp4]]\n', {
- resolveEmbed: (target: string, _source: string) => {
- if (target === 'clip.mp4') return 'media/clip.mp4';
- return null;
- },
- sourcePath: 'notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedVideo');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/media/clip.mp4');
- });
-
- test('unresolved embed (resolveEmbed returns null) falls back to bare target', () => {
- const json = mdManager.parse('![[missing.png]]\n', {
- resolveEmbed: () => null,
- sourcePath: 'notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('missing.png');
- });
-
- test('image dispatch lands on WikiEmbedImage compat descriptor with absolute src', () => {
- const json = mdManager.parse('![[photo.png]]\n', {
- resolveEmbed: () => 'assets/photo.png',
- sourcePath: 'notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/assets/photo.png');
- expect(props?.target).toBe('photo.png');
- expect(props?.anchor).toBeNull();
- expect(props?.alias).toBeNull();
- expect(node?.attrs?.kind).toBe('element');
- expect(node?.attrs?.sourceDirty).toBe(false);
- });
-});
-
-describe('handlers.wikiLinkEmbed — WikiEmbedImage dispatch (US-002)', () => {
- test('![[photo.png]] → jsxComponent(WikiEmbedImage) with target/alias/anchor on props', () => {
- const json = mdManager.parse('![[photo.png]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('photo.png');
- expect(props?.alias).toBeNull();
- expect(props?.anchor).toBeNull();
- expect(findInJson(json, 'image')).toBeNull();
- });
-
- test('![[photo.png|caption]] → props.alias === "caption"', () => {
- const json = mdManager.parse('![[photo.png|caption]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('photo.png');
- expect(props?.alias).toBe('caption');
- expect(props?.anchor).toBeNull();
- });
-
- test('![[photo.png#frag]] → props.anchor === "frag"', () => {
- const json = mdManager.parse('![[photo.png#frag]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('photo.png');
- expect(props?.anchor).toBe('frag');
- expect(props?.alias).toBeNull();
- });
-
- test('![[photo.png#frag|caption]] preserves both anchor and alias', () => {
- const json = mdManager.parse('![[photo.png#frag|caption]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.alias).toBe('caption');
- expect(props?.anchor).toBe('frag');
- });
-
- test('PDF extension (![[doc.pdf]]) routes to WikiEmbedFile descriptor', () => {
- const json = mdManager.parse('![[doc.pdf]]\n');
- expect(findJsxComponentInJson(json, 'WikiEmbedImage')).toBeNull();
- expect(findJsxComponentInJson(json, 'WikiEmbedFile')).not.toBeNull();
- });
-
- test('round-trip: ![[photo.png]] is byte-identical', () => {
- const md = '![[photo.png]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('round-trip: ![[photo.png|caption]] preserves alias byte-identical', () => {
- const md = '![[photo.png|caption]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('round-trip: ![[photo.png#frag]] preserves anchor byte-identical', () => {
- const md = '![[photo.png#frag]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('round-trip: ![[photo.png#frag|caption]] preserves anchor + alias byte-identical', () => {
- const md = '![[photo.png#frag|caption]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('inline-position ![[photo.png]] (mid-prose) → text+link-mark chip, no PM image node', () => {
- const json = mdManager.parse('text ![[photo.png]] more text\n');
- expect(findJsxComponentInJson(json, 'WikiEmbedImage')).toBeNull();
- expect(findInJson(json, 'image')).toBeNull();
- const linkMark = findMarkInJson(json, 'link');
- expect(linkMark).not.toBeNull();
- expect(linkMark?.attrs?.sourceForm).toBe('wikiembed');
- expect(linkMark?.attrs?.target).toBe('photo.png');
- });
-
- test('round-trip: inline mid-prose ![[photo.png]] is byte-identical', () => {
- const md = 'text ![[photo.png]] more text\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-});
-
-describe('handlers.wikiLinkEmbed — WikiEmbedVideo dispatch (US-008)', () => {
- test('![[clip.mp4]] → jsxComponent(WikiEmbedVideo) with target/alias/anchor on props', () => {
- const json = mdManager.parse('![[clip.mp4]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedVideo');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('clip.mp4');
- expect(props?.alias).toBeNull();
- expect(props?.anchor).toBeNull();
- expect(findMarkInJson(json, 'link')).toBeNull();
- });
-
- test('![[clip.mp4|introduction]] → props.alias === "introduction"', () => {
- const json = mdManager.parse('![[clip.mp4|introduction]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedVideo');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('clip.mp4');
- expect(props?.alias).toBe('introduction');
- expect(props?.anchor).toBeNull();
- });
-
- test('![[clip.mp4#t=10]] → props.anchor === "t=10"', () => {
- const json = mdManager.parse('![[clip.mp4#t=10]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedVideo');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('clip.mp4');
- expect(props?.anchor).toBe('t=10');
- expect(props?.alias).toBeNull();
- });
-
- test('all video extensions (mp4/webm/mov/m4v/mkv) dispatch to WikiEmbedVideo', () => {
- for (const ext of ['mp4', 'webm', 'mov', 'm4v', 'mkv']) {
- const json = mdManager.parse(`![[clip.${ext}]]\n`);
- const node = findJsxComponentInJson(json, 'WikiEmbedVideo');
- expect(node, `${ext} should dispatch to WikiEmbedVideo`).not.toBeNull();
- }
- });
-
- test('video dispatch carries server-absolute src when resolveEmbed returns a hit', () => {
- const json = mdManager.parse('![[clip.mp4]]\n', {
- resolveEmbed: (target: string) => (target === 'clip.mp4' ? 'media/clip.mp4' : null),
- sourcePath: 'notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedVideo');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/media/clip.mp4');
- expect(props?.target).toBe('clip.mp4');
- });
-
- test('round-trip: ![[clip.mp4]] is byte-identical', () => {
- const md = '![[clip.mp4]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('round-trip: ![[clip.mp4|caption]] preserves alias byte-identical', () => {
- const md = '![[clip.mp4|caption]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('round-trip: ![[clip.mp4#t=10]] preserves anchor byte-identical', () => {
- const md = '![[clip.mp4#t=10]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-});
-
-describe('handlers.wikiLinkEmbed — WikiEmbedAudio dispatch (US-008)', () => {
- test('![[song.mp3]] → jsxComponent(WikiEmbedAudio) with target/alias/anchor on props', () => {
- const json = mdManager.parse('![[song.mp3]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedAudio');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('song.mp3');
- expect(props?.alias).toBeNull();
- expect(props?.anchor).toBeNull();
- expect(findMarkInJson(json, 'link')).toBeNull();
- });
-
- test('![[song.mp3|jingle]] → props.alias === "jingle"', () => {
- const json = mdManager.parse('![[song.mp3|jingle]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedAudio');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('song.mp3');
- expect(props?.alias).toBe('jingle');
- });
-
- test('![[song.mp3#chorus]] → props.anchor === "chorus"', () => {
- const json = mdManager.parse('![[song.mp3#chorus]]\n');
- const node = findJsxComponentInJson(json, 'WikiEmbedAudio');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.target).toBe('song.mp3');
- expect(props?.anchor).toBe('chorus');
- });
-
- test('all audio extensions (mp3/wav/ogg/m4a/flac/aac/opus) dispatch to WikiEmbedAudio', () => {
- for (const ext of ['mp3', 'wav', 'ogg', 'm4a', 'flac', 'aac', 'opus']) {
- const json = mdManager.parse(`![[song.${ext}]]\n`);
- const node = findJsxComponentInJson(json, 'WikiEmbedAudio');
- expect(node, `${ext} should dispatch to WikiEmbedAudio`).not.toBeNull();
- }
- });
-
- test('audio dispatch carries server-absolute src when resolveEmbed returns a hit', () => {
- const json = mdManager.parse('![[song.mp3]]\n', {
- resolveEmbed: (target: string) => (target === 'song.mp3' ? 'media/song.mp3' : null),
- sourcePath: 'notes.md',
- });
- const node = findJsxComponentInJson(json, 'WikiEmbedAudio');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/media/song.mp3');
- expect(props?.target).toBe('song.mp3');
- });
-
- test('round-trip: ![[song.mp3]] is byte-identical', () => {
- const md = '![[song.mp3]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('round-trip: ![[song.mp3|jingle]] preserves alias byte-identical', () => {
- const md = '![[song.mp3|jingle]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('round-trip: ![[song.mp3#chorus]] preserves anchor byte-identical', () => {
- const md = '![[song.mp3#chorus]]\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-});
-
-describe('handlers.image / imagePromoter — server-absolute URL contract (doc-relative image src)', () => {
- test('block-context ![alt](../../path) → CommonMarkImage with server-absolute src resolved against sourcePath', () => {
- const json = mdManager.parse('![Aang](../../assets/images/characters/aang.png)\n', {
- sourcePath: 'characters/air-nomads/aang.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.alt).toBe('Aang');
- expect(props?.src).toMatch(/^\//);
- expect(props?.src).toBe('/assets/images/characters/aang.png');
- });
-
- test('inline ![alt](../../path) mid-prose → PM image node with server-absolute src', () => {
- const json = mdManager.parse(
- 'Some prose with ![Aang](../../assets/images/characters/aang.png) inline.\n',
- { sourcePath: 'characters/air-nomads/aang.md' },
- );
- const image = findInJson(json, 'image');
- expect(image).not.toBeNull();
- expect(image?.attrs?.src).toMatch(/^\//);
- expect(image?.attrs?.src).toBe('/assets/images/characters/aang.png');
- });
-
- test('block-context ./img.png resolves to /<doc-dir>/img.png', () => {
- const json = mdManager.parse('![pic](./img.png)\n', {
- sourcePath: 'characters/air-nomads/aang.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/characters/air-nomads/img.png');
- });
-
- test('block-context bare-basename img.png resolves to /<doc-dir>/img.png', () => {
- const json = mdManager.parse('![pic](img.png)\n', {
- sourcePath: 'characters/air-nomads/aang.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/characters/air-nomads/img.png');
- });
-
- test('absolute path /abs/x.png passes through unchanged', () => {
- const json = mdManager.parse('![x](/abs/x.png)\n', {
- sourcePath: 'characters/air-nomads/aang.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/abs/x.png');
- });
-
- test('http(s) URL passes through unchanged', () => {
- const json = mdManager.parse('![x](https://example.com/x.png)\n', {
- sourcePath: 'characters/air-nomads/aang.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('https://example.com/x.png');
- });
-
- test('data: URI passes through unchanged', () => {
- const dataUri =
- 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNgAAIAAAUAAen63NgAAAAASUVORK5CYII=';
- const json = mdManager.parse(`![pixel](${dataUri})\n`, {
- sourcePath: 'characters/air-nomads/aang.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe(dataUri);
- });
-
- test('relative path to a non-existent file still produces a server-absolute src', () => {
- const json = mdManager.parse('![x](../missing/ghost.png)\n', {
- sourcePath: 'characters/air-nomads/aang.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/characters/missing/ghost.png');
- });
-
- test('no sourcePath in parse context → src left verbatim (no resolution attempted)', () => {
- const json = mdManager.parse('![x](../../assets/x.png)\n');
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('../../assets/x.png');
- });
-
- test('round-trip: block-context relative image src is byte-identical on save', () => {
- const md = '![Aang](../../assets/images/characters/aang.png)\n';
- expect(
- mdManager.serialize(mdManager.parse(md, { sourcePath: 'characters/air-nomads/aang.md' })),
- ).toBe(md);
- });
-
- test('round-trip: inline relative image src is byte-identical on save', () => {
- const md = 'Some prose with ![Aang](../../assets/images/characters/aang.png) inline.\n';
- expect(
- mdManager.serialize(mdManager.parse(md, { sourcePath: 'characters/air-nomads/aang.md' })),
- ).toBe(md);
- });
-
- test('dirty-path block round-trip: edited CommonMarkImage re-emits the authored doc-relative URL', () => {
- const json = mdManager.parse('![Aang](../../assets/images/characters/aang.png)\n', {
- sourcePath: 'characters/air-nomads/aang.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown>;
- expect(props.src).toBe('/assets/images/characters/aang.png');
- expect(props.sourceUrl).toBe('../../assets/images/characters/aang.png');
- if (node?.attrs) node.attrs.sourceDirty = true;
- props.alt = 'Aang kneeling';
- expect(mdManager.serialize(json)).toBe(
- '![Aang kneeling](../../assets/images/characters/aang.png)\n',
- );
- });
-
- test('dirty-path inline round-trip: edited PM image node re-emits the authored doc-relative URL', () => {
- const json = mdManager.parse(
- 'Some prose with ![Aang](../../assets/images/characters/aang.png) inline.\n',
- { sourcePath: 'characters/air-nomads/aang.md' },
- );
- const image = findInJson(json, 'image');
- expect(image).not.toBeNull();
- const attrs = image?.attrs as Record<string, unknown>;
- expect(attrs.src).toBe('/assets/images/characters/aang.png');
- expect(attrs.sourceUrl).toBe('../../assets/images/characters/aang.png');
- attrs.alt = 'Aang kneeling';
- expect(mdManager.serialize(json)).toBe(
- 'Some prose with ![Aang kneeling](../../assets/images/characters/aang.png) inline.\n',
- );
- });
-
- test('escape: block-context relative path climbing above contentDir stays raw', () => {
- const json = mdManager.parse('![x](../../../../etc/passwd)\n', {
- sourcePath: 'docs/page.md',
- });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('../../../../etc/passwd');
- });
-
- test('escape: inline mid-prose relative path climbing above contentDir stays raw', () => {
- const json = mdManager.parse('Hello ![x](../../../etc/passwd) world.\n', {
- sourcePath: 'a/b.md',
- });
- const image = findInJson(json, 'image');
- expect(image).not.toBeNull();
- expect(image?.attrs?.src).toBe('../../../etc/passwd');
- });
-
- test('exact-depth climb to contentDir root normalizes to server-absolute path', () => {
- const json = mdManager.parse('![x](../img.png)\n', { sourcePath: 'a/b.md' });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('/img.png');
- });
-
- test('exact-depth-plus-one climb escapes contentDir and stays raw', () => {
- const json = mdManager.parse('![x](../../img.png)\n', { sourcePath: 'a/b.md' });
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown> | undefined;
- expect(props?.src).toBe('../../img.png');
- });
-
- test('escape diagnostic: emits a console.warn under NODE_ENV=test', () => {
- const warn = spyOn(console, 'warn').mockImplementation(mock(() => {}));
- try {
- mdManager.parse('![x](../../../../etc/passwd)\n', { sourcePath: 'docs/page.md' });
- expect(warn).toHaveBeenCalled();
- const message = String(warn.mock.calls[0]?.[0] ?? '');
- expect(message).toContain('[resolve-image-url]');
- expect(message).toContain('escapes contentDir');
- expect(message).toContain('../../../../etc/passwd');
- } finally {
- warn.mockRestore();
- }
- });
-
- test('escape diagnostic: silent under NODE_ENV=production', () => {
- const warn = spyOn(console, 'warn').mockImplementation(mock(() => {}));
- const prior = process.env.NODE_ENV;
- process.env.NODE_ENV = 'production';
- try {
- mdManager.parse('![x](../../../../etc/passwd)\n', { sourcePath: 'docs/page.md' });
- expect(warn).not.toHaveBeenCalled();
- } finally {
- if (prior === undefined) delete process.env.NODE_ENV;
- else process.env.NODE_ENV = prior;
- warn.mockRestore();
- }
- });
-
- test('alt-preservation: ![](src) emits jsxComponent with props.alt === "" (decorative opt-in)', () => {
- const json = mdManager.parse('![](https://example.com/img.png)\n');
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown>;
- expect(Object.hasOwn(props, 'alt')).toBe(true);
- expect(props.alt).toBe('');
- });
-
- test('alt-preservation: ![text](src) emits jsxComponent with props.alt === "text"', () => {
- const json = mdManager.parse('![hello](https://example.com/img.png)\n');
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- const props = node?.attrs?.props as Record<string, unknown>;
- expect(props.alt).toBe('hello');
- });
-
- test('alt-preservation: ![](src) round-trips byte-identically', () => {
- const md = '![](https://example.com/img.png)\n';
- expect(mdManager.serialize(mdManager.parse(md))).toBe(md);
- });
-
- test('alt-preservation: dirty-path edited CommonMarkImage with original empty alt re-emits ![](src)', () => {
- const json = mdManager.parse('![](https://example.com/img.png)\n');
- const node = findJsxComponentInJson(json, 'CommonMarkImage');
- expect(node).not.toBeNull();
- expect((node?.attrs?.props as Record<string, unknown>).alt).toBe('');
- if (node?.attrs) node.attrs.sourceDirty = true;
- expect(mdManager.serialize(json)).toBe('![](https://example.com/img.png)\n');
- });
-});
diff --git a/packages/core/src/markdown/highlight-promoter.precision.test.ts b/packages/core/src/markdown/highlight-promoter.precision.test.ts
deleted file mode 100644
index c6459e306..000000000
--- a/packages/core/src/markdown/highlight-promoter.precision.test.ts
+++ /dev/null
@@ -1,485 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function collectHighlightedTextNodes(json: JSONContent): JSONContent[] {
- const out: JSONContent[] = [];
- const visit = (n: JSONContent) => {
- if (n.type === 'text' && (n.marks ?? []).some((m) => m.type === 'highlight')) {
- out.push(n);
- }
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-function plainTextOf(json: JSONContent): string {
- let out = '';
- const visit = (n: JSONContent) => {
- if (n.type === 'text') out += n.text ?? '';
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-describe('highlight-promoter — acceptance', () => {
- test('basic `==hello==`', () => {
- const json = mdManager.parse('==hello==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hello');
- });
-
- test('mid-paragraph `a ==hello== b`', () => {
- const json = mdManager.parse('a ==hello== b\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hello');
- expect(plainTextOf(json)).toBe('a hello b');
- });
-
- test('multi-word `==a b c==`', () => {
- const json = mdManager.parse('==a b c==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('a b c');
- });
-
- test('inner whitespace-flanked `==` does NOT close (`==a == b==`)', () => {
- const json = mdManager.parse('==a == b==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('a == b');
- });
-
- test('with embedded math `==a + $x$ + b==`', () => {
- const json = mdManager.parse('==a + $x$ + b==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(0);
- });
-
- test('with surrounding emphasis `*==hi==*`', () => {
- const json = mdManager.parse('*==hi==*\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hi');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['emphasis', 'highlight']);
- });
-
- test('with surrounding bold `**==hi==**`', () => {
- const json = mdManager.parse('**==hi==**\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hi');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['highlight', 'strong']);
- });
-});
-
-describe('highlight-promoter — rejection (stay prose)', () => {
- test('single `=text=` is not a delimiter', () => {
- const json = mdManager.parse('=text=\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('=text=');
- });
-
- test('`===` (three equals, edge ambiguity)', () => {
- const json = mdManager.parse('===\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('`====` (four equals)', () => {
- const json = mdManager.parse('====\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('open with leading space `== text==` (left-flanking violation)', () => {
- const json = mdManager.parse('== text==\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('== text==');
- });
-
- test('close with trailing space `==text ==` (right-flanking violation)', () => {
- const json = mdManager.parse('==text ==\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('==text ==');
- });
-
- test('unmatched `==text` (no closing delimiter)', () => {
- const json = mdManager.parse('==text\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('unmatched `text==` (no opening delimiter)', () => {
- const json = mdManager.parse('text==\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('multi-line `==a\\n more==` (rule 3, body cannot cross newline)', () => {
- const json = mdManager.parse('==a\nmore==\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('equation `a == b` (no closing pair) stays prose', () => {
- const json = mdManager.parse('a == b\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('a == b');
- });
-
- test('escaped opening `\\==text==`', () => {
- const json = mdManager.parse('\\==text==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('text');
- });
-});
-
-describe('highlight-promoter — multi-match', () => {
- test('two highlights on one line `==a== ==b==`', () => {
- const json = mdManager.parse('==a== ==b==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(2);
- expect(marks[0].text).toBe('a');
- expect(marks[1].text).toBe('b');
- });
-
- test('chained `==a==b==` highlights `a`, leaves `b==` as text', () => {
- const json = mdManager.parse('==a==b==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('a');
- expect(plainTextOf(json)).toBe('ab==');
- });
-
- test('three highlights `==a== ==b== ==c==`', () => {
- const json = mdManager.parse('==a== ==b== ==c==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(3);
- expect(marks.map((m) => m.text)).toEqual(['a', 'b', 'c']);
- });
-
- test('adjacent no-separator `==a====b==` produces ONE highlight body=`a====b`', () => {
- const json = mdManager.parse('==a====b==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('a====b');
- expect(mdManager.serialize(json)).toBe('==a====b==\n');
- });
-});
-
-describe('highlight-promoter — cross-children body (==**bold**==, ==*italic*==, ==`code`==)', () => {
- test('`==**bold**==` carries both highlight + strong', () => {
- const json = mdManager.parse('==**bold**==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('bold');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['highlight', 'strong']);
- });
-
- test('`==*italic*==` carries both highlight + emphasis', () => {
- const json = mdManager.parse('==*italic*==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('italic');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['emphasis', 'highlight']);
- });
-
- test('` ==`code`== ` carries both highlight + code', () => {
- const json = mdManager.parse('==`code`==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('code');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['code', 'highlight']);
- });
-
- test('`==~~strike~~==` carries highlight + strike', () => {
- const json = mdManager.parse('==~~strike~~==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('strike');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['highlight', 'strike']);
- });
-
- test('mid-paragraph `a ==**bold**== b` highlights only the bold', () => {
- const json = mdManager.parse('a ==**bold**== b\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('bold');
- expect(plainTextOf(json)).toBe('a bold b');
- });
-
- test('`==**a** **b**==` highlights the full body — both bolds plus separator', () => {
- const json = mdManager.parse('==**a** **b**==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(3);
- expect(marks.map((m) => m.text)).toEqual(['a', ' ', 'b']);
- const markSets = marks.map((m) =>
- (m.marks ?? [])
- .map((mm) => mm.type)
- .sort()
- .join(','),
- );
- expect(markSets).toEqual(['highlight,strong', 'highlight', 'highlight,strong']);
- });
-
- test('flanking rule 2 — `== **bold**==` (open followed by space) stays prose', () => {
- const json = mdManager.parse('a == **bold**== b\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('flanking rule 4 — `==**bold** ==` (close preceded by space) stays prose', () => {
- const json = mdManager.parse('a ==**bold** == b\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('`==Bold: **bold thing** end.==j` — open + close mid-text with formatting in body (user repro)', () => {
- const json = mdManager.parse('==Bold: **bold thing** end.==j\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.map((m) => m.text)).toEqual(['Bold: ', 'bold thing', ' end.']);
- const strongMark = marks.find((m) => m.text === 'bold thing');
- expect((strongMark?.marks ?? []).map((mm) => mm.type).sort()).toEqual(['highlight', 'strong']);
- expect(plainTextOf(json)).toBe('Bold: bold thing end.j');
- const src = '==Bold: **bold thing** end.==j\n';
- expect(mdManager.serialize(json)).toBe(src);
- const second = mdManager.parse(mdManager.serialize(json));
- expect(collectHighlightedTextNodes(second).length).toBe(3);
- });
-
- test('opaque inline (link) inside `==[anchor](url)==` — stays prose', () => {
- const json = mdManager.parse('==[anchor](url)==\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('opaque inline (inline math) inside `==$x$==` — stays prose', () => {
- const json = mdManager.parse('==$x$==\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('close `==` followed by `=` via inlineCode start char — rule 5 cross-boundary rejection', () => {
- const src = '==**bold**==`=code`\n';
- const json = mdManager.parse(src);
- const marks = collectHighlightedTextNodes(json);
- for (const m of marks) {
- expect(m.text).not.toContain('=code');
- }
- });
-
- test('`==prefix *em* and **strong** suffix==` — multi-inline body wrapped by `==…==`', () => {
- const json = mdManager.parse('==prefix *em* and **strong** suffix==\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.map((m) => m.text)).toEqual(['prefix ', 'em', ' and ', 'strong', ' suffix']);
- });
-
- test('chained `==**bold**== and ==*italic*== and ==`code`==` highlights all three runs', () => {
- const src = '==**bold**== and ==*italic*== and ==`code`==\n';
- const json = mdManager.parse(src);
- const marks = collectHighlightedTextNodes(json);
- expect(marks.map((m) => m.text)).toEqual(['bold', 'italic', 'code']);
- });
-
- test('byte-stable round-trip — `==**bold**==` survives parse + serialize + reparse', () => {
- const src = 'a ==**bold**== b\n';
- const first = mdManager.parse(src);
- const round = mdManager.serialize(first);
- expect(round).toBe(src);
- const second = mdManager.parse(round);
- expect(mdManager.serialize(second)).toBe(src);
- expect(collectHighlightedTextNodes(second).length).toBe(1);
- });
-});
-
-describe('highlight-promoter — MDX `<mark>` JSX form', () => {
- test('`<mark>hello</mark>` parses as highlight mark', () => {
- const json = mdManager.parse('<mark>hello</mark>\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('hello');
- });
-
- test('mid-paragraph `<mark>x</mark>` parses as highlight mark', () => {
- const json = mdManager.parse('a <mark>x</mark> b\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('x');
- expect(plainTextOf(json)).toBe('a x b');
- });
-
- test('`<mark>` with surrounding emphasis carries both marks', () => {
- const json = mdManager.parse('*<mark>x</mark>*\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['emphasis', 'highlight']);
- });
-
- test('`<mark>**x**</mark>` carries both highlight + strong', () => {
- const json = mdManager.parse('<mark>**x**</mark>\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('x');
- const markTypes = (marks[0].marks ?? []).map((m) => m.type).sort();
- expect(markTypes).toEqual(['highlight', 'strong']);
- });
-
- test('`<mark color="red">` — attributes silently dropped', () => {
- const json = mdManager.parse('<mark color="red">x</mark>\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(1);
- expect(marks[0].text).toBe('x');
- const out = mdManager.serialize(json);
- expect(out).toBe('==x==\n');
- });
-
- test('two `<mark>` on one line', () => {
- const json = mdManager.parse('<mark>a</mark> and <mark>b</mark>\n');
- const marks = collectHighlightedTextNodes(json);
- expect(marks.length).toBe(2);
- expect(marks.map((m) => m.text)).toEqual(['a', 'b']);
- });
-
- test('capitalized `<Mark>` does NOT promote (lowercase-only by design)', () => {
- const json = mdManager.parse('<Mark>x</Mark>\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-});
-
-describe('highlight-promoter — protection from code spans + math', () => {
- test('`==text==` inside a code span stays code', () => {
- const json = mdManager.parse('a `==text==` b\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- expect(plainTextOf(json)).toBe('a ==text== b');
- });
-
- test('`==text==` inside an inline-math body stays math', () => {
- const json = mdManager.parse('$$==a==$$\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-
- test('`==text==` inside a fenced code block stays code', () => {
- const json = mdManager.parse('```\n==text==\n```\n');
- expect(collectHighlightedTextNodes(json).length).toBe(0);
- });
-});
-
-describe('highlight-promoter — round-trip', () => {
- test('`==hello==` round-trips byte-stable', () => {
- const src = '==hello==\n';
- const json = mdManager.parse(src);
- const out = mdManager.serialize(json);
- expect(out).toBe(src);
- });
-
- test('`a ==hello== b` round-trips byte-stable', () => {
- const src = 'a ==hello== b\n';
- const json = mdManager.parse(src);
- const out = mdManager.serialize(json);
- expect(out).toBe(src);
- });
-
- test('two highlights round-trip byte-stable', () => {
- const src = '==a== ==b==\n';
- const json = mdManager.parse(src);
- const out = mdManager.serialize(json);
- expect(out).toBe(src);
- });
-
- test('`==a == b==` (whitespace-flanked inner) round-trips byte-stable', () => {
- const src = '==a == b==\n';
- const json = mdManager.parse(src);
- const out = mdManager.serialize(json);
- expect(out).toBe(src);
- });
-
- test('`<mark>x</mark>` normalizes to `==x==` on round-trip', () => {
- const json = mdManager.parse('<mark>x</mark>\n');
- const out = mdManager.serialize(json);
- expect(out).toBe('==x==\n');
- });
-
- test('`==text==` inside code span round-trips as code (not highlight)', () => {
- const src = 'a `==text==` b\n';
- const json = mdManager.parse(src);
- const out = mdManager.serialize(json);
- expect(out).toBe(src);
- });
-
- test('mid-paragraph `==**bold highlight**==` round-trips byte-stable (highlight outermost — canonical)', () => {
- const src = 'a ==**bold highlight**== b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('mid-paragraph `==*italic highlight*==` round-trips byte-stable (highlight outermost)', () => {
- const src = 'a ==*italic highlight*== b\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`**==bold==**` (strong-outer input) normalizes to `==**bold**==` (highlight-outer canonical)', () => {
- const src = 'a **==bold==** b\n';
- const out = mdManager.serialize(mdManager.parse(src));
- expect(out).toBe('a ==**bold**== b\n');
- });
-
- test('`*==italic==*` (em-outer input) normalizes to `==*italic*==`', () => {
- const src = 'a *==italic==* b\n';
- const out = mdManager.serialize(mdManager.parse(src));
- expect(out).toBe('a ==*italic*== b\n');
- });
-
- test('highlight inside heading `## ==section==` round-trips', () => {
- const src = '## ==section==\n';
- const json = mdManager.parse(src);
- const out = mdManager.serialize(json);
- expect(out).toBe(src);
- });
-
- test('highlight inside blockquote `> ==quoted==` round-trips', () => {
- const src = '> ==quoted==\n';
- const json = mdManager.parse(src);
- const out = mdManager.serialize(json);
- expect(out).toBe(src);
- });
-
- test('highlight inside list item `- ==item==` round-trips', () => {
- const src = '- ==item==\n';
- const json = mdManager.parse(src);
- const out = mdManager.serialize(json);
- expect(out).toBe(src);
- });
-});
-
-describe('highlight-promoter — direct mdast→markdown', () => {
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation matching the existing precedent
- const minimalState: any = {
- enter: () => () => {},
- containerPhrasing: (node: { children?: Array<{ value?: string }> }) =>
- (node.children ?? []).map((c) => c.value ?? '').join(''),
- createTracker: () => ({
- move: (s: string) => s,
- current: () => ({}),
- }),
- options: {},
- unsafe: [],
- safe: (s: string) => s,
- };
-
- test('mark emits `==…==`', async () => {
- const { toMarkdownHandlers } = await import('./to-markdown-handlers.ts');
- const node = {
- type: 'mark' as const,
- children: [{ type: 'text' as const, value: 'hello' }],
- };
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const out = (toMarkdownHandlers as any).mark(node, undefined, minimalState, {});
- expect(out).toBe('==hello==');
- });
-});
diff --git a/packages/core/src/markdown/html-to-mdast.test.ts b/packages/core/src/markdown/html-to-mdast.test.ts
deleted file mode 100644
index d4d7f8f58..000000000
--- a/packages/core/src/markdown/html-to-mdast.test.ts
+++ /dev/null
@@ -1,316 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type {
- Blockquote,
- Code,
- Emphasis,
- Heading,
- InlineCode,
- Link,
- List,
- Paragraph,
- Root,
- Strong,
- Table,
- Text,
-} from 'mdast';
-import { visit } from 'unist-util-visit';
-import { cleanupPlugins, htmlToMdast, mdastToMarkdown } from './html-to-mdast.ts';
-
-function firstChild(root: Root): Root['children'][number] {
- const first = root.children[0];
- if (!first) throw new Error('expected root to have at least one child');
- return first;
-}
-
-describe('htmlToMdast — basic HTML→mdast conversion', () => {
- test('paragraph with text', () => {
- const root = htmlToMdast('<p>hello world</p>');
- const para = firstChild(root) as Paragraph;
- expect(para.type).toBe('paragraph');
- expect((para.children[0] as Text).value).toBe('hello world');
- });
-
- test('strong inline', () => {
- const root = htmlToMdast('<p>say <strong>hi</strong></p>');
- const para = firstChild(root) as Paragraph;
- const strong = para.children.find((c) => c.type === 'strong') as Strong;
- expect(strong).toBeDefined();
- expect((strong.children[0] as Text).value).toBe('hi');
- });
-
- test('emphasis inline', () => {
- const root = htmlToMdast('<p>say <em>hi</em></p>');
- const para = firstChild(root) as Paragraph;
- const em = para.children.find((c) => c.type === 'emphasis') as Emphasis;
- expect(em).toBeDefined();
- expect((em.children[0] as Text).value).toBe('hi');
- });
-
- test('inline code', () => {
- const root = htmlToMdast('<p>run <code>npm install</code> first</p>');
- const para = firstChild(root) as Paragraph;
- const code = para.children.find((c) => c.type === 'inlineCode') as InlineCode;
- expect(code).toBeDefined();
- expect(code.value).toBe('npm install');
- });
-
- test('headings h1 through h6', () => {
- const html = '<h1>a</h1><h2>b</h2><h3>c</h3><h4>d</h4><h5>e</h5><h6>f</h6>';
- const root = htmlToMdast(html);
- const headings = root.children.filter((c) => c.type === 'heading') as Heading[];
- expect(headings).toHaveLength(6);
- expect(headings.map((h) => h.depth)).toEqual([1, 2, 3, 4, 5, 6]);
- });
-
- test('unordered list with two items', () => {
- const root = htmlToMdast('<ul><li>one</li><li>two</li></ul>');
- const list = firstChild(root) as List;
- expect(list.type).toBe('list');
- expect(list.ordered).toBeFalsy();
- expect(list.children).toHaveLength(2);
- });
-
- test('ordered list', () => {
- const root = htmlToMdast('<ol><li>one</li><li>two</li></ol>');
- const list = firstChild(root) as List;
- expect(list.type).toBe('list');
- expect(list.ordered).toBe(true);
- });
-
- test('link', () => {
- const root = htmlToMdast('<p>visit <a href="https://example.com">site</a></p>');
- const para = firstChild(root) as Paragraph;
- const link = para.children.find((c) => c.type === 'link') as Link;
- expect(link).toBeDefined();
- expect(link.url).toBe('https://example.com');
- expect((link.children[0] as Text).value).toBe('site');
- });
-
- test('blockquote', () => {
- const root = htmlToMdast('<blockquote><p>quoted</p></blockquote>');
- const bq = firstChild(root) as Blockquote;
- expect(bq.type).toBe('blockquote');
- const innerPara = bq.children[0] as Paragraph;
- expect(innerPara.type).toBe('paragraph');
- expect((innerPara.children[0] as Text).value).toBe('quoted');
- });
-
- test('code block with language', () => {
- const root = htmlToMdast('<pre><code class="language-typescript">const x = 1;</code></pre>');
- const code = firstChild(root) as Code;
- expect(code.type).toBe('code');
- expect(code.lang).toBe('typescript');
- expect(code.value).toBe('const x = 1;');
- });
-
- test('table (GFM)', () => {
- const html = `<table>
- <thead><tr><th>a</th><th>b</th></tr></thead>
- <tbody><tr><td>1</td><td>2</td></tr></tbody>
- </table>`;
- const root = htmlToMdast(html);
- const table = root.children.find((c) => c.type === 'table') as Table;
- expect(table).toBeDefined();
- expect(table.children).toHaveLength(2);
- });
-
- test('malformed HTML is tolerated (no throw)', () => {
- expect(() => htmlToMdast('<p>unclosed <strong>bold')).not.toThrow();
- expect(() => htmlToMdast('<<><foo bar=>')).not.toThrow();
- });
-
- test('empty input returns empty root', () => {
- const root = htmlToMdast('');
- expect(root.type).toBe('root');
- expect(root.children).toHaveLength(0);
- });
-
- test('additionalCleanupPlugins are invoked in order', () => {
- const calls: string[] = [];
- const pluginA = () => (tree: unknown) => {
- calls.push('A');
- return tree;
- };
- const pluginB = () => (tree: unknown) => {
- calls.push('B');
- return tree;
- };
- htmlToMdast('<p>x</p>', {
- additionalCleanupPlugins: [pluginA, pluginB],
- });
- expect(calls).toEqual(['A', 'B']);
- });
-
- test('cleanupPlugins is the scaffold-time registration point', () => {
- expect(Array.isArray(cleanupPlugins)).toBe(true);
- });
-
- test('throws HtmlPayloadTooLargeError when input exceeds the size ceiling', async () => {
- const { HtmlPayloadTooLargeError } = await import('./html-to-mdast.ts');
- let caught: unknown;
- try {
- htmlToMdast('<p>x</p>'.repeat(2000), { maxBytes: 100 });
- } catch (err) {
- caught = err;
- }
- expect(caught).toBeInstanceOf(HtmlPayloadTooLargeError);
- const e = caught as { htmlBytes: number; maxBytes: number };
- expect(e.htmlBytes).toBeGreaterThan(100);
- expect(e.maxBytes).toBe(100);
- });
-
- test('passes through when input is at or below the size ceiling', () => {
- const html = '<p>under the cap</p>';
- expect(() => htmlToMdast(html, { maxBytes: html.length })).not.toThrow();
- });
-});
-
-describe('htmlToMdast — Branch D source-form attr defaults (FR-39)', () => {
- function findFirst<T extends Root['children'][number]>(
- tree: Root,
- type: T['type'],
- ): T | undefined {
- let found: T | undefined;
- visit(tree, type, (node) => {
- found ||= node as T;
- });
- return found;
- }
-
- test('plain <strong> gets sourceDelimiter="**"', () => {
- const tree = htmlToMdast('<p>say <strong>hi</strong></p>');
- const strong = findFirst<Strong>(tree, 'strong');
- expect(strong).toBeDefined();
- expect(strong?.data?.sourceDelimiter).toBe('**');
- });
-
- test('plain <em> gets sourceDelimiter="*"', () => {
- const tree = htmlToMdast('<p>say <em>hi</em></p>');
- const em = findFirst<Emphasis>(tree, 'emphasis');
- expect(em).toBeDefined();
- expect(em?.data?.sourceDelimiter).toBe('*');
- });
-
- test('plain inline <code> gets sourceFenceChar="`" + sourceFenceLength=1', () => {
- const tree = htmlToMdast('<p>run <code>npm install</code> now</p>');
- const inlineCode = findFirst<InlineCode>(tree, 'inlineCode');
- expect(inlineCode).toBeDefined();
- expect(inlineCode?.data?.sourceFenceChar).toBe('`');
- expect(inlineCode?.data?.sourceFenceLength).toBe(1);
- });
-
- test('plain <pre><code> gets sourceFenceChar="`" + sourceFenceLength=3', () => {
- const tree = htmlToMdast('<pre><code>const x = 1;</code></pre>');
- const code = findFirst<Code>(tree, 'code');
- expect(code).toBeDefined();
- expect(code?.data?.sourceFenceChar).toBe('`');
- expect(code?.data?.sourceFenceLength).toBe(3);
- });
-
- test('Word HTML <strong> gets canonical default (no vendor branching)', () => {
- const html =
- '<p class="MsoNormal" style="mso-margin-top-alt:auto">say <strong>hello</strong></p>';
- const tree = htmlToMdast(html);
- const strong = findFirst<Strong>(tree, 'strong');
- expect(strong?.data?.sourceDelimiter).toBe('**');
- });
-
- test('Notion HTML <strong> gets canonical default', () => {
- const html = '<p class="notion-text-block"><strong>hi</strong></p>';
- const tree = htmlToMdast(html);
- const strong = findFirst<Strong>(tree, 'strong');
- expect(strong?.data?.sourceDelimiter).toBe('**');
- });
-
- test('Google Docs HTML <strong> gets canonical default', () => {
- const html = '<b id="docs-internal-guid-abc-123"><strong>hi</strong></b>';
- const tree = htmlToMdast(html);
- const strong = findFirst<Strong>(tree, 'strong');
- expect(strong?.data?.sourceDelimiter).toBe('**');
- });
-
- test('GitHub HTML inline <code> gets canonical default', () => {
- const html = '<p>run <code class="notranslate">npm install</code></p>';
- const tree = htmlToMdast(html);
- const inlineCode = findFirst<InlineCode>(tree, 'inlineCode');
- expect(inlineCode?.data?.sourceFenceChar).toBe('`');
- expect(inlineCode?.data?.sourceFenceLength).toBe(1);
- });
-
- test('VS Code <pre><code> gets canonical default (3-fence)', () => {
- const html =
- '<pre style="color:#000"><code><span style="color:#0000ff">const</span> x = 1;</code></pre>';
- const tree = htmlToMdast(html);
- const code = findFirst<Code>(tree, 'code');
- expect(code?.data?.sourceFenceChar).toBe('`');
- expect(code?.data?.sourceFenceLength).toBe(3);
- });
-
- test('round-trip: <strong>foo</strong> serializes to **foo**', () => {
- const md = mdastToMarkdown(htmlToMdast('<p><strong>foo</strong></p>')).trim();
- expect(md).toBe('**foo**');
- });
-
- test('round-trip: <em>foo</em> serializes to *foo*', () => {
- const md = mdastToMarkdown(htmlToMdast('<p><em>foo</em></p>')).trim();
- expect(md).toBe('*foo*');
- });
-
- test('round-trip: inline <code>foo</code> serializes to `foo`', () => {
- const md = mdastToMarkdown(htmlToMdast('<p><code>foo</code></p>')).trim();
- expect(md).toBe('`foo`');
- });
-
- test('round-trip: <pre><code>x</code></pre> serializes to triple-backtick fence', () => {
- const md = mdastToMarkdown(htmlToMdast('<pre><code>x</code></pre>')).trim();
- expect(md).toBe('```\nx\n```');
- });
-
- test('mixed nesting: <strong><em><code>x</code></em></strong> all carry canonical attrs', () => {
- const tree = htmlToMdast('<p><strong><em><code>x</code></em></strong></p>');
- const strong = findFirst<Strong>(tree, 'strong');
- const em = findFirst<Emphasis>(tree, 'emphasis');
- const inlineCode = findFirst<InlineCode>(tree, 'inlineCode');
- expect(strong?.data?.sourceDelimiter).toBe('**');
- expect(em?.data?.sourceDelimiter).toBe('*');
- expect(inlineCode?.data?.sourceFenceChar).toBe('`');
- expect(inlineCode?.data?.sourceFenceLength).toBe(1);
- });
-
- test('multiple strong nodes each get the attr (not just the first)', () => {
- const tree = htmlToMdast('<p><strong>a</strong> and <strong>b</strong></p>');
- const strongs: Strong[] = [];
- visit(tree, 'strong', (node) => {
- strongs.push(node);
- });
- expect(strongs).toHaveLength(2);
- expect(strongs[0]?.data?.sourceDelimiter).toBe('**');
- expect(strongs[1]?.data?.sourceDelimiter).toBe('**');
- });
-
- test('walker is tolerant of nodes without strong/em/code (no crash on prose-only HTML)', () => {
- expect(() => htmlToMdast('<p>just prose, no marks</p>')).not.toThrow();
- const tree = htmlToMdast('<p>just prose, no marks</p>');
- expect(tree.type).toBe('root');
- });
-
- test('walker initializes data field on nodes that lack it (defensive)', () => {
- const tree = htmlToMdast('<p><strong>x</strong> <em>y</em> <code>z</code></p>');
- const strong = findFirst<Strong>(tree, 'strong');
- const em = findFirst<Emphasis>(tree, 'emphasis');
- const code = findFirst<InlineCode>(tree, 'inlineCode');
- expect(strong?.data).toBeDefined();
- expect(em?.data).toBeDefined();
- expect(code?.data).toBeDefined();
- });
-
- test('non-target node types (paragraphs, headings, links) are not touched', () => {
- const tree = htmlToMdast('<h2>title</h2><p>text <a href="https://x">link</a></p>');
- const heading = findFirst<Heading>(tree, 'heading');
- const para = findFirst<Paragraph>(tree, 'paragraph');
- const link = findFirst<Link>(tree, 'link');
- expect(heading?.data?.sourceDelimiter).toBeUndefined();
- expect(para?.data?.sourceDelimiter).toBeUndefined();
- expect(link?.data?.sourceDelimiter).toBeUndefined();
- });
-});
diff --git a/packages/core/src/markdown/indented-code-promoter.test.ts b/packages/core/src/markdown/indented-code-promoter.test.ts
deleted file mode 100644
index 47c332d9b..000000000
--- a/packages/core/src/markdown/indented-code-promoter.test.ts
+++ /dev/null
@@ -1,100 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { Code, Paragraph, Root } from 'mdast';
-import remarkParse from 'remark-parse';
-import { unified } from 'unified';
-import { VFile } from 'vfile';
-import { indentedCodePromoterPlugin } from './indented-code-promoter.ts';
-import { remarkMdxAgnostic } from './remark-mdx-agnostic.ts';
-
-function parseWithPromoter(source: string): Root {
- const processor = unified()
- .use(remarkParse)
- .use(remarkMdxAgnostic)
- .use(indentedCodePromoterPlugin);
- const tree = processor.parse(source);
- processor.runSync(tree, new VFile({ value: source }));
- return tree;
-}
-
-function firstChild(tree: Root): Paragraph | Code | undefined {
- return tree.children[0] as Paragraph | Code | undefined;
-}
-
-describe('indented-code-promoter', () => {
- test('promotes top-level paragraph with 4-space prefix to code node', () => {
- const tree = parseWithPromoter(' code\n line\n');
- const node = firstChild(tree);
- expect(node?.type).toBe('code');
- expect((node as Code)?.value).toBe('code\nline');
- expect(node?.data?.sourceStyle).toBe('indented');
- });
-
- test('promotes single-line indented paragraph', () => {
- const tree = parseWithPromoter(' just one line\n');
- const node = firstChild(tree);
- expect(node?.type).toBe('code');
- expect((node as Code)?.value).toBe('just one line');
- expect(node?.data?.sourceStyle).toBe('indented');
- });
-
- test('promotes tab-indented paragraph', () => {
- const tree = parseWithPromoter('\tcode line\n');
- const node = firstChild(tree);
- expect(node?.type).toBe('code');
- expect((node as Code)?.value).toBe('code line');
- expect(node?.data?.sourceStyle).toBe('indented');
- });
-
- test('does NOT promote a paragraph without leading whitespace', () => {
- const tree = parseWithPromoter('plain prose\nhere\n');
- const node = firstChild(tree);
- expect(node?.type).toBe('paragraph');
- });
-
- test('does NOT promote paragraph indented less than 4 spaces', () => {
- const tree = parseWithPromoter(' short indent\n');
- const node = firstChild(tree);
- expect(node?.type).toBe('paragraph');
- });
-
- test('does NOT promote a fenced code block (already a code node)', () => {
- const tree = parseWithPromoter('```\ncode\n```\n');
- const node = firstChild(tree);
- expect(node?.type).toBe('code');
- expect(node?.data?.sourceStyle).toBeUndefined();
- });
-
- test('does NOT promote paragraph inside a list item (not root)', () => {
- const tree = parseWithPromoter('- item\n\n continuation\n');
- const node = firstChild(tree);
- expect(node?.type).toBe('list');
- });
-
- test('paragraph followed by indented block both round-trip via promoter', () => {
- const tree = parseWithPromoter('P before\n\n code\n');
- expect(tree.children).toHaveLength(2);
- expect(tree.children[0]?.type).toBe('paragraph');
- expect(tree.children[1]?.type).toBe('code');
- expect(tree.children[1]?.data?.sourceStyle).toBe('indented');
- });
-
- test('indented block followed by paragraph both round-trip via promoter', () => {
- const tree = parseWithPromoter(' code\n\nP after\n');
- expect(tree.children).toHaveLength(2);
- expect(tree.children[0]?.type).toBe('code');
- expect(tree.children[0]?.data?.sourceStyle).toBe('indented');
- expect(tree.children[1]?.type).toBe('paragraph');
- });
-
- test('promoted code position covers indent prefix (column 1)', () => {
- const tree = parseWithPromoter(' code\n line\n');
- const node = firstChild(tree) as Code;
- expect(node?.position?.start?.column).toBe(1);
- expect(node?.position?.start?.offset).toBe(0);
- });
-
- test('empty input does not crash', () => {
- const tree = parseWithPromoter('');
- expect(tree.children).toHaveLength(0);
- });
-});
diff --git a/packages/core/src/markdown/inline-promoter-position-preservation.test.ts b/packages/core/src/markdown/inline-promoter-position-preservation.test.ts
deleted file mode 100644
index e99ac4d24..000000000
--- a/packages/core/src/markdown/inline-promoter-position-preservation.test.ts
+++ /dev/null
@@ -1,219 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { Root as MdastRoot, Nodes } from 'mdast';
-import { visit } from 'unist-util-visit';
-
-import { sharedExtensions } from '../extensions/shared.ts';
-import { createParseProcessor, parseMdToMdast } from './pipeline.ts';
-
-const proc = createParseProcessor({ extensions: sharedExtensions });
-
-function parse(source: string): MdastRoot {
- return parseMdToMdast(source, proc);
-}
-
-function findNodesByType(tree: MdastRoot, type: string): Nodes[] {
- const found: Nodes[] = [];
- visit(tree, type, (node) => {
- found.push(node as Nodes);
- });
- return found;
-}
-
-function assertHasPosition(node: Nodes) {
- expect(node.position).toBeDefined();
- expect(typeof node.position?.start.offset).toBe('number');
- expect(typeof node.position?.end?.offset).toBe('number');
-}
-
-function getStartOffset(node: Nodes): number {
- return node.position?.start?.offset ?? -1;
-}
-
-function getEndOffset(node: Nodes): number {
- return node.position?.end?.offset ?? -1;
-}
-
-function slicePosition(source: string, node: Nodes): string {
- const s = getStartOffset(node);
- const e = getEndOffset(node);
- if (s < 0 || e < 0) return '';
- return source.slice(s, e);
-}
-
-describe('inline-promoter position preservation', () => {
- describe('single-dollar-math ($x$)', () => {
- test('inlineMath node has position pointing to $', () => {
- const source = 'Hello $x$ world';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'inlineMath');
- expect(nodes.length).toBe(1);
- assertHasPosition(nodes[0]);
- expect(source[getStartOffset(nodes[0])]).toBe('$');
- });
-
- test('text fragments around $a$ have position', () => {
- const source = 'Hello $a$ world';
- const tree = parse(source);
- const paragraph = tree.children[0] as Nodes & { children: Nodes[] };
- for (const child of paragraph.children) {
- assertHasPosition(child);
- }
- });
-
- test('multiple math nodes preserve ordered positions', () => {
- const source = 'See $a$ and $b$ here';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'inlineMath');
- expect(nodes.length).toBe(2);
- for (const n of nodes) assertHasPosition(n);
- expect(getStartOffset(nodes[0])).toBeLessThan(getStartOffset(nodes[1]));
- });
- });
-
- describe('highlight (==text==)', () => {
- test('mark node has position starting at ==', () => {
- const source = 'Hello ==word== world';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'mark');
- expect(nodes.length).toBe(1);
- assertHasPosition(nodes[0]);
- expect(source.slice(getStartOffset(nodes[0]), getStartOffset(nodes[0]) + 2)).toBe('==');
- });
-
- test('text fragments around ==a== have position', () => {
- const source = 'Hello ==a== world';
- const tree = parse(source);
- const paragraph = tree.children[0] as Nodes & { children: Nodes[] };
- for (const child of paragraph.children) {
- assertHasPosition(child);
- }
- });
- });
-
- describe('comment (%%text%%)', () => {
- test('%%note%% comment has position starting at %%', () => {
- const source = 'Hello %%note%% world';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'comment');
- expect(nodes.length).toBe(1);
- assertHasPosition(nodes[0]);
- expect(source.slice(getStartOffset(nodes[0]), getStartOffset(nodes[0]) + 2)).toBe('%%');
- });
-
- test('<!-- html --> comment has position', () => {
- const source = 'Hello <!-- note --> world';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'comment');
- expect(nodes.length).toBe(1);
- assertHasPosition(nodes[0]);
- });
- });
-
- describe('tag (#tagname)', () => {
- test('#hello tag has position pointing to #', () => {
- const source = 'See #hello there';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'tag');
- expect(nodes.length).toBe(1);
- assertHasPosition(nodes[0]);
- expect(source[getStartOffset(nodes[0])]).toBe('#');
- });
-
- test('multiple tags preserve position', () => {
- const source = '#a and #b';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'tag');
- expect(nodes.length).toBe(2);
- for (const n of nodes) assertHasPosition(n);
- });
- });
-
- describe('autolink (<scheme:uri>)', () => {
- test('autolink link has position', () => {
- const source = 'Visit <https://x.com> now';
- const tree = parse(source);
- const links: Nodes[] = [];
- visit(tree, 'link', (node) => {
- const data = (node as Nodes & { data?: Record<string, unknown> }).data;
- if (data?.sourceStyle === 'autolink') links.push(node as Nodes);
- });
- expect(links.length).toBe(1);
- assertHasPosition(links[0]);
- });
- });
-
- describe('chain: mixed promoters in one paragraph', () => {
- test('$x$ + ==highlight== both have position', () => {
- const source = 'Math $x$ and ==hi== text';
- const tree = parse(source);
- const math = findNodesByType(tree, 'inlineMath');
- const marks = findNodesByType(tree, 'mark');
- expect(math.length).toBe(1);
- expect(marks.length).toBe(1);
- assertHasPosition(math[0]);
- assertHasPosition(marks[0]);
- });
-
- test('#tag + %%comment%% both have position', () => {
- const source = 'See #tag %%note%% end';
- const tree = parse(source);
- const tags = findNodesByType(tree, 'tag');
- const comments = findNodesByType(tree, 'comment');
- expect(tags.length).toBe(1);
- expect(comments.length).toBe(1);
- assertHasPosition(tags[0]);
- assertHasPosition(comments[0]);
- });
- });
-
- describe('escape-aware offset accuracy', () => {
- test('\\* before $x$ — math offset points to $', () => {
- const source = '\\*text\\* and $x$ here';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'inlineMath');
- expect(nodes.length).toBeGreaterThanOrEqual(1);
- expect(source[getStartOffset(nodes[0])]).toBe('$');
- });
-
- test('\\# is not promoted to tag', () => {
- const source = 'See \\#notag here';
- const tree = parse(source);
- const tags = findNodesByType(tree, 'tag');
- expect(tags.length).toBe(0);
- });
- });
-
- describe('position byte accuracy', () => {
- test('mark position spans ==world==', () => {
- const source = 'Hello ==world== there';
- const tree = parse(source);
- const marks = findNodesByType(tree, 'mark');
- expect(marks.length).toBe(1);
- expect(slicePosition(source, marks[0])).toBe('==world==');
- });
-
- test('inlineMath position spans $abc$', () => {
- const source = 'Hello $abc$ there';
- const tree = parse(source);
- const nodes = findNodesByType(tree, 'inlineMath');
- expect(nodes.length).toBe(1);
- expect(slicePosition(source, nodes[0])).toBe('$abc$');
- });
-
- test('tag position spans #hello', () => {
- const source = 'See #hello there';
- const tree = parse(source);
- const tags = findNodesByType(tree, 'tag');
- expect(tags.length).toBe(1);
- expect(slicePosition(source, tags[0])).toBe('#hello');
- });
-
- test('comment position spans %%note%%', () => {
- const source = 'See %%note%% there';
- const tree = parse(source);
- const comments = findNodesByType(tree, 'comment');
- expect(comments.length).toBe(1);
- expect(slicePosition(source, comments[0])).toBe('%%note%%');
- });
- });
-});
diff --git a/packages/core/src/markdown/math-block-guard.precision.test.ts b/packages/core/src/markdown/math-block-guard.precision.test.ts
deleted file mode 100644
index af928eac7..000000000
--- a/packages/core/src/markdown/math-block-guard.precision.test.ts
+++ /dev/null
@@ -1,261 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function findInJson(json: JSONContent, predicate: (n: JSONContent) => boolean): JSONContent | null {
- if (predicate(json)) return json;
- for (const child of json.content ?? []) {
- const found = findInJson(child, predicate);
- if (found) return found;
- }
- return null;
-}
-
-function countInJson(json: JSONContent, predicate: (n: JSONContent) => boolean): number {
- let count = predicate(json) ? 1 : 0;
- for (const child of json.content ?? []) {
- count += countInJson(child, predicate);
- }
- return count;
-}
-
-const isComponent = (name: string) => (n: JSONContent) =>
- n.type === 'jsxComponent' && n.attrs?.componentName === name;
-
-describe('block math (multi-line `$$…$$`) → DollarMath compat', () => {
- test('multi-line `$$\\n…\\n$$` parses to a DollarMath jsxComponent', () => {
- const json = mdManager.parse('$$\na^2 + b^2 = c^2\n$$\n');
- const node = findInJson(json, isComponent('DollarMath'));
- expect(node).toBeDefined();
- });
-
- test('multi-line block math round-trips back to `$$…$$` (γ pristine)', () => {
- const source = '$$\nE = mc^2\n$$\n';
- const json = mdManager.parse(source);
- const out = mdManager.serialize(json);
- expect(out).toBe(source);
- });
-
- test('single-line `$$x$$` does NOT parse to DollarMath (it is inline math now)', () => {
- const json = mdManager.parse('$$E = mc^2$$\n');
- const dollar = findInJson(json, isComponent('DollarMath'));
- expect(dollar).toBeNull();
- });
-});
-
-describe('inline math (single-line `$$x$$` only) → mathInline atom', () => {
- const isMathInline = (n: JSONContent) => n.type === 'mathInline';
-
- test('single-line `$$E = mc^2$$` parses to a mathInline atom', () => {
- const json = mdManager.parse('$$E = mc^2$$\n');
- const node = findInJson(json, isMathInline);
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('E = mc^2');
- });
-
- test('`$$x$$` mid-paragraph parses to a mathInline atom', () => {
- const json = mdManager.parse('A formula $$x$$ in prose.\n');
- const node = findInJson(json, isMathInline);
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('x');
- });
-
- test('single-dollar `$x$` parses as math (D-M5b heuristic)', () => {
- const json = mdManager.parse('A formula $x$ in prose.\n');
- const node = findInJson(json, isMathInline);
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('x');
- });
-
- test('paired-double `$$x^2$$` round-trips byte-identical', () => {
- const source = 'Result: $$x^2$$.\n';
- const json = mdManager.parse(source);
- const out = mdManager.serialize(json);
- expect(out).toBe(source);
- });
-
- test('single-dollar `$x^2$` round-trips byte-identical on serialize', () => {
- const source = 'Result: $x^2$.\n';
- const json = mdManager.parse(source);
- const out = mdManager.serialize(json);
- expect(out).toBe('Result: $x^2$.\n');
- });
-
- test('`<InlineMath formula="x" id="eq-1" />` round-trips byte-identical (id-bearing JSX form)', () => {
- const source = 'Result: <InlineMath formula="x^2" id="eq-1" />\n';
- const json = mdManager.parse(source);
- const node = findInJson(json, (n) => n.type === 'mathInline');
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('x^2');
- expect(node?.attrs?.id).toBe('eq-1');
- const out = mdManager.serialize(json);
- expect(out).toBe(source);
- });
-
- test('`<InlineMath formula="x" />` without id serializes to `$$x$$` (bare-formula branch)', () => {
- const source = 'Result: <InlineMath formula="x^2" />.\n';
- const json = mdManager.parse(source);
- const node = findInJson(json, (n) => n.type === 'mathInline');
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('x^2');
- const out = mdManager.serialize(json);
- expect(out).toContain('$$x^2$$');
- });
-});
-
-describe('fenced math (` ```math `) → MathFence compat', () => {
- test('` ```math `…``` ` fence parses to a MathFence jsxComponent', () => {
- const json = mdManager.parse('```math\nE = mc^2\n```\n');
- const node = findInJson(json, isComponent('MathFence'));
- expect(node).toBeDefined();
- });
-
- test('fenced math round-trips back to ` ```math `…``` `', () => {
- const source = '```math\nE = mc^2\n```\n';
- const json = mdManager.parse(source);
- const out = mdManager.serialize(json);
- expect(out).toBe(source);
- });
-
- test('non-math fenced code (` ```js `) is unchanged — still a code block, NOT MathFence', () => {
- const json = mdManager.parse('```js\nconst x = 1;\n```\n');
- const mathFence = findInJson(json, isComponent('MathFence'));
- expect(mathFence).toBeNull();
- });
-});
-
-describe('D-M5b heuristic — `$…$` ambiguity guard (currency-safe)', () => {
- test('`$$` inside a code span does NOT parse as math', () => {
- const json = mdManager.parse('Use the `$$E=mc^2$$` syntax.\n');
- const dollarMath = findInJson(json, isComponent('DollarMath'));
- expect(dollarMath).toBeNull();
- const inlineMath = findInJson(json, (n) => n.type === 'mathInline');
- expect(inlineMath).toBeNull();
- });
-
- test('`$x$` inside a code span does NOT parse as math', () => {
- const json = mdManager.parse('Inline `$x$` in code.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-
- test('currency `Costs $5.00 plus tax` stays prose (no closing $ on line)', () => {
- const json = mdManager.parse('Costs $5.00 plus tax.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-
- test('shell var `$PATH` stays prose', () => {
- const json = mdManager.parse('Set $PATH and try again.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-
- test('paired-dollar prose `Pay $5 to $10 dollars` stays prose (the regression case)', () => {
- const json = mdManager.parse('Pay $5 to $10 dollars in prose.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-
- test('two currency mentions on same line stay prose', () => {
- const json = mdManager.parse('Tickets cost $20 or $30 each.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-
- test('opening `$` followed by space `$ x$` stays prose (rule 1)', () => {
- const json = mdManager.parse('Bad $ x$ syntax.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-
- test('closing `$` preceded by space `$x $` stays prose (rule 3)', () => {
- const json = mdManager.parse('Bad $x $ syntax.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-
- test('digit-after-close `$x$5` stays prose (rule 4, currency carve-out)', () => {
- const json = mdManager.parse('Item $x$5 listed.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-
- test('`$` with newline between stays prose (rule 2, same-line requirement)', () => {
- const json = mdManager.parse('First $x\nthen $y in next line.\n');
- expect(findInJson(json, (n) => n.type === 'mathInline')).toBeNull();
- });
-});
-
-describe('D-M5b heuristic — single-dollar acceptance', () => {
- const isMathInline = (n: JSONContent) => n.type === 'mathInline';
-
- test('basic `$x$` mid-paragraph', () => {
- const json = mdManager.parse('A formula $x$ here.\n');
- const node = findInJson(json, isMathInline);
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('x');
- });
-
- test('multi-character formula `$E = mc^2$`', () => {
- const json = mdManager.parse('Famously $E = mc^2$ from Einstein.\n');
- const node = findInJson(json, isMathInline);
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('E = mc^2');
- });
-
- test('LaTeX with super/subscript `$x^2 + y_1$`', () => {
- const json = mdManager.parse('Pythagoras: $x^2 + y_1$ here.\n');
- const node = findInJson(json, isMathInline);
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('x^2 + y_1');
- });
-
- test('two `$x$ … $y$` matches in one paragraph each promote independently', () => {
- const json = mdManager.parse('Both $x$ and $y$ are variables.\n');
- const count = countInJson(json, isMathInline);
- expect(count).toBe(2);
- });
-
- test('`$x$` at start of paragraph', () => {
- const json = mdManager.parse('$x$ is a variable.\n');
- const node = findInJson(json, isMathInline);
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('x');
- });
-
- test('`$x$` at end of paragraph followed by punctuation', () => {
- const json = mdManager.parse('We define $x$.\n');
- const node = findInJson(json, isMathInline);
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('x');
- });
-
- test('paired-double `$$x$$` continues to work alongside single-dollar', () => {
- const json = mdManager.parse('Single $x$ vs paired $$y$$ in prose.\n');
- const count = countInJson(json, isMathInline);
- expect(count).toBe(2);
- });
-
- test('known edge: `Cost $5$ each` matches as math (Obsidian-parity edge)', () => {
- const json = mdManager.parse('Cost $5$ each.\n');
- const node = findInJson(json, (n) => n.type === 'mathInline');
- expect(node).toBeDefined();
- expect(node?.attrs?.formula).toBe('5');
- });
-});
-
-describe('coexistence', () => {
- test('block math followed by inline math both render', () => {
- const json = mdManager.parse('$$\nE = mc^2\n$$\n\nThen $$x^2$$ in prose.\n');
- expect(countInJson(json, isComponent('DollarMath'))).toBe(1);
- expect(countInJson(json, (n) => n.type === 'mathInline')).toBe(1);
- });
-
- test('two block math nodes (multi-line) in one document each promote independently', () => {
- const json = mdManager.parse('$$\nx^2\n$$\n\n$$\ny^2\n$$\n');
- const mathCount = countInJson(json, isComponent('DollarMath'));
- expect(mathCount).toBe(2);
- });
-
- test('two inline math nodes in one paragraph both render as mathInline', () => {
- const json = mdManager.parse('Compare $$x^2$$ vs $$y^2$$ in prose.\n');
- const mathCount = countInJson(json, (n) => n.type === 'mathInline');
- expect(mathCount).toBe(2);
- });
-});
diff --git a/packages/core/src/markdown/mdast-to-hast-handlers.test.ts b/packages/core/src/markdown/mdast-to-hast-handlers.test.ts
deleted file mode 100644
index cce07456b..000000000
--- a/packages/core/src/markdown/mdast-to-hast-handlers.test.ts
+++ /dev/null
@@ -1,688 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { FootnoteDefinition, FootnoteReference } from 'mdast';
-import type { MdxJsxFlowElement, MdxJsxTextElement } from 'mdast-util-mdx';
-import type { RawMdxFallbackMdast, WikiLinkMdast } from './mdast-augmentation.ts';
-import { mdastToHtml } from './mdast-to-html.ts';
-
-function html(tree: { type: 'root'; children: unknown[] }): string {
- // biome-ignore lint/suspicious/noExplicitAny: test helpers stay loose to keep fixtures tidy
- return mdastToHtml(tree as any);
-}
-
-function wrap(child: unknown) {
- return { type: 'root' as const, children: [child] };
-}
-
-describe('wikiLink mdast→hast', () => {
- test('renders as <a class="wiki-link"> with href fragment', () => {
- const node: WikiLinkMdast = {
- type: 'wikiLink',
- value: 'Page',
- data: { target: 'Page', anchor: null, alias: null },
- children: [{ type: 'text', value: 'Page' }],
- };
- const out = html(wrap(node));
- expect(out).toContain('<a');
- expect(out).toContain('class="wiki-link"');
- expect(out).toContain('href="#page"');
- expect(out).toContain('data-target="Page"');
- expect(out).toContain('>Page</a>');
- });
-
- test('anchor shows in href as slug fragment', () => {
- const node: WikiLinkMdast = {
- type: 'wikiLink',
- value: 'Page#Heading',
- data: { target: 'Page', anchor: 'Heading', alias: null },
- children: [{ type: 'text', value: 'Page#Heading' }],
- };
- const out = html(wrap(node));
- expect(out).toContain('href="#page-heading"');
- expect(out).toContain('data-anchor="Heading"');
- });
-
- test('alias is used as visible label', () => {
- const node: WikiLinkMdast = {
- type: 'wikiLink',
- value: 'Alias',
- data: { target: 'Page', anchor: null, alias: 'Alias' },
- children: [{ type: 'text', value: 'Alias' }],
- };
- const out = html(wrap(node));
- expect(out).toContain('>Alias</a>');
- expect(out).toContain('data-alias="Alias"');
- });
-
- test('no data-resolved attribute emitted (Q1 intentional drop)', () => {
- const node: WikiLinkMdast = {
- type: 'wikiLink',
- value: 'Page',
- data: { target: 'Page', anchor: null, alias: null },
- children: [{ type: 'text', value: 'Page' }],
- };
- const out = html(wrap(node));
- expect(out).not.toContain('data-resolved');
- });
-
- test('label text content is entity-encoded for display', () => {
- const node: WikiLinkMdast = {
- type: 'wikiLink',
- value: '<script>',
- data: { target: 'Page', anchor: null, alias: '<script>' },
- children: [{ type: 'text', value: '<script>' }],
- };
- const out = html(wrap(node));
- expect(out).toContain('>&#x3C;script></a>');
- });
-});
-
-describe('mdxJsxFlowElement mdast→hast', () => {
- test('renders as <pre class="mdx-component"><code>raw</code></pre>', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: null,
- attributes: [],
- children: [],
- data: { sourceRaw: '<Note type="info">Hi</Note>' },
- };
- const out = html(wrap(node));
- expect(out).toContain('<pre class="mdx-component">');
- expect(out).toContain('<code>');
- expect(out).toContain('</code></pre>');
- });
-
- test('raw source is entity-encoded, not passed through', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: null,
- attributes: [],
- children: [],
- data: { sourceRaw: '<MyComponent prop="value"/>' },
- };
- const out = html(wrap(node));
- expect(out).toContain('&#x3C;MyComponent');
- expect(out).not.toContain('<MyComponent');
- });
-
- test('adversarial <script> is escaped, not emitted as live HTML', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: null,
- attributes: [],
- children: [],
- data: { sourceRaw: '<script>alert(1)</script>' },
- };
- const out = html(wrap(node));
- expect(out).not.toContain('<script>');
- expect(out).toContain('&#x3C;script>');
- });
-});
-
-describe('lowercase HTML-primitive shortcut (img / video / audio)', () => {
- test('img flow element emits native <img> with src + alt, not the <pre> source-as-code shape', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/img.png' },
- { type: 'mdxJsxAttribute', name: 'alt', value: 'A picture' },
- ],
- children: [],
- data: { sourceRaw: '<img src="https://x.example/img.png" alt="A picture" />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<img\b[^>]*src="https:\/\/x\.example\/img\.png"[^>]*>/);
- expect(out).toMatch(/<img\b[^>]*alt="A picture"[^>]*>/);
- expect(out).not.toContain('<pre class="mdx-component">');
- });
-
- test('video flow element emits <video> with explicit close + bare boolean controls attr', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'video',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/v.mp4' },
- { type: 'mdxJsxAttribute', name: 'controls', value: null },
- ],
- children: [],
- data: { sourceRaw: '<video src="https://x.example/v.mp4" controls />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<video\b[^>]*src="https:\/\/x\.example\/v\.mp4"[^>]*>/);
- expect(out).toMatch(/<video\b[^>]*\bcontrols(?=[\s/>])[^>]*>/);
- expect(out).toContain('</video>');
- expect(out).not.toContain('<pre class="mdx-component">');
- });
-
- test('audio flow element emits native <audio>...</audio>', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'audio',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/a.mp3' },
- { type: 'mdxJsxAttribute', name: 'controls', value: null },
- ],
- children: [],
- data: { sourceRaw: '<audio src="https://x.example/a.mp3" controls />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<audio\b[^>]*src="https:\/\/x\.example\/a\.mp3"[^>]*>/);
- expect(out).toMatch(/<audio\b[^>]*\bcontrols(?=[\s/>])[^>]*>/);
- expect(out).toContain('</audio>');
- expect(out).not.toContain('<pre class="mdx-component">');
- });
-
- test('img flow element strips on* event handler attributes (FR-20 defense-in-depth)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/img.png' },
- { type: 'mdxJsxAttribute', name: 'onerror', value: 'alert(1)' },
- { type: 'mdxJsxAttribute', name: 'onload', value: 'fetch("//evil")' },
- { type: 'mdxJsxAttribute', name: 'onclick', value: 'doom()' },
- ],
- children: [],
- data: { sourceRaw: '<img onerror="alert(1)" onload="fetch(...)" src="x.png" />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<img\b[^>]*src="https:\/\/x\.example\/img\.png"[^>]*>/);
- expect(out).not.toContain('onerror');
- expect(out).not.toContain('onload');
- expect(out).not.toContain('onclick');
- expect(out).not.toContain('alert(1)');
- });
-
- test('img flow element preserves `on*`-prefixed safe attributes when length < 3 or non-handler', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/img.png' },
- { type: 'mdxJsxAttribute', name: 'on', value: 'unusual' },
- ],
- children: [],
- data: { sourceRaw: '<img src="x.png" on="unusual" />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/\bon="unusual"/);
- });
-
- test('inline <img> via mdxJsxTextElement also emits native <img> (not <span class="mdx-inline">)', () => {
- const node: MdxJsxTextElement = {
- type: 'mdxJsxTextElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/inline.png' },
- { type: 'mdxJsxAttribute', name: 'alt', value: 'Inline' },
- ],
- children: [],
- data: { sourceRaw: '<img src="https://x.example/inline.png" alt="Inline" />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<img\b[^>]*src="https:\/\/x\.example\/inline\.png"[^>]*>/);
- expect(out).not.toContain('<span class="mdx-inline">');
- });
-
- test('capitalized JSX (e.g. <Callout>) still emits the <pre class="mdx-component"> source-as-code shape', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'Callout',
- attributes: [{ type: 'mdxJsxAttribute', name: 'type', value: 'note' }],
- children: [],
- data: { sourceRaw: '<Callout type="note">Body</Callout>' },
- };
- const out = html(wrap(node));
- expect(out).toContain('<pre class="mdx-component">');
- expect(out).not.toMatch(/<callout\b/i);
- });
-
- test('non-primitive lowercase tag (<picture>) falls through to <pre> shape (gate is name-keyed, not lowercase-globbed)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'picture',
- attributes: [],
- children: [],
- data: { sourceRaw: '<picture></picture>' },
- };
- const out = html(wrap(node));
- expect(out).toContain('<pre class="mdx-component">');
- expect(out).not.toMatch(/<picture\b/);
- });
-
- test('unsafe srcset on native <img> is stripped by rehypeSanitizeUrls (per-candidate URL-scheme check)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/img.png' },
- { type: 'mdxJsxAttribute', name: 'srcset', value: 'javascript:alert(1) 1x' },
- ],
- children: [],
- data: {
- sourceRaw: '<img src="https://x.example/img.png" srcset="javascript:alert(1) 1x" />',
- },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<img\b/);
- expect(out).not.toContain('javascript:');
- expect(out).not.toMatch(/<img\b[^>]*\bsrcset=/);
- expect(out).toMatch(/<img\b[^>]*src="https:\/\/x\.example\/img\.png"/);
- });
-
- test('safe srcset survives the rehypeSanitizeUrls filter (legitimate retina image set)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/img.png' },
- {
- type: 'mdxJsxAttribute',
- name: 'srcset',
- value: 'https://x.example/[email protected] 2x, https://x.example/[email protected] 3x',
- },
- ],
- children: [],
- data: { sourceRaw: '<img />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(
- /srcset="https:\/\/x\.example\/img@2x\.png 2x, https:\/\/x\.example\/img@3x\.png 3x"/,
- );
- });
-
- test('unsafe poster on native <video> is stripped by rehypeSanitizeUrls', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'video',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/v.mp4' },
- { type: 'mdxJsxAttribute', name: 'poster', value: 'javascript:alert(1)' },
- ],
- children: [],
- data: { sourceRaw: '<video />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<video\b/);
- expect(out).not.toContain('javascript:');
- expect(out).not.toMatch(/poster=/);
- expect(out).toMatch(/src="https:\/\/x\.example\/v\.mp4"/);
- });
-
- test('safe poster survives the rehypeSanitizeUrls filter', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'video',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/v.mp4' },
- { type: 'mdxJsxAttribute', name: 'poster', value: 'https://x.example/poster.jpg' },
- ],
- children: [],
- data: { sourceRaw: '<video />' },
- };
- const out = html(wrap(node));
- expect(out).toContain('poster="https://x.example/poster.jpg"');
- });
-
- test('style with url(javascript:...) is dropped by rehypeSanitizeUrls (defense-in-depth)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/img.png' },
- {
- type: 'mdxJsxAttribute',
- name: 'style',
- value: 'background-image: url(javascript:alert(1))',
- },
- ],
- children: [],
- data: { sourceRaw: '<img />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<img\b/);
- expect(out).not.toContain('javascript:');
- expect(out).not.toMatch(/style=/);
- });
-
- test('style with expression(...) is dropped (legacy IE CSS-expression vector)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/img.png' },
- { type: 'mdxJsxAttribute', name: 'style', value: 'width: expression(alert(1))' },
- ],
- children: [],
- data: { sourceRaw: '<img />' },
- };
- const out = html(wrap(node));
- expect(out).not.toContain('expression(');
- expect(out).not.toMatch(/style=/);
- });
-
- test('safe style survives the rehypeSanitizeUrls filter', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'https://x.example/img.png' },
- { type: 'mdxJsxAttribute', name: 'style', value: 'border: 1px solid #000; color: red' },
- ],
- children: [],
- data: { sourceRaw: '<img />' },
- };
- const out = html(wrap(node));
- expect(out).toContain('style="border: 1px solid #000; color: red"');
- });
-
- test('javascript: src on native <img> is stripped by downstream rehypeSanitizeUrls (composition-boundary lock)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'javascript:alert(1)' },
- { type: 'mdxJsxAttribute', name: 'alt', value: 'evil' },
- ],
- children: [],
- data: { sourceRaw: '<img src="javascript:alert(1)" alt="evil" />' },
- };
- const out = html(wrap(node));
- expect(out).toMatch(/<img\b/);
- expect(out).not.toContain('javascript:');
- expect(out).toMatch(/<img\b[^>]*alt="evil"[^>]*>/);
- expect(out).not.toMatch(/<img\b[^>]*src=/);
- });
-
- test('expression-valued attr (`width={400}`) falls back to <pre> shape (helper bails on dynamic values)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'x' },
- {
- type: 'mdxJsxAttribute',
- name: 'width',
- value: { type: 'mdxJsxAttributeValueExpression', value: '400', data: {} },
- },
- ],
- children: [],
- data: { sourceRaw: '<img src="x" width={400} />' },
- };
- const out = html(wrap(node));
- expect(out).toContain('<pre class="mdx-component">');
- expect(out).not.toMatch(/<img\b[^>]*src="x"/);
- });
-
- test('spread attribute (`{...rest}`) falls back to <pre> shape (helper bails on non-mdxJsxAttribute)', () => {
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: 'img',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: 'x' },
- { type: 'mdxJsxExpressionAttribute', value: '...rest', data: {} },
- ],
- children: [],
- data: { sourceRaw: '<img src="x" {...rest} />' },
- };
- const out = html(wrap(node));
- expect(out).toContain('<pre class="mdx-component">');
- expect(out).not.toMatch(/<img\b[^>]*src="x"/);
- });
-});
-
-describe('mdxJsxTextElement mdast→hast', () => {
- test('renders as <span> carrying both the mdx-inline class and the data-jsx-inline marker', () => {
- const node: MdxJsxTextElement = {
- type: 'mdxJsxTextElement',
- name: null,
- attributes: [],
- children: [],
- data: { sourceRaw: '<Tag/>' },
- };
- const out = html(wrap(node));
- expect(out).toContain('class="mdx-inline"');
- expect(out).toContain('data-jsx-inline=""');
- expect(out).toContain('&#x3C;Tag/>');
- expect(out).not.toContain('<Tag/>');
- });
-});
-
-describe('rawMdxFallback mdast→hast', () => {
- test('renders leading comment + pre/code with both class and data-raw-mdx-fallback markers', () => {
- const node: RawMdxFallbackMdast = {
- type: 'rawMdxFallback',
- value: '<A>\n</B>',
- data: { reason: 'mismatched tag', originalSpan: { start: 0, end: 8 } },
- };
- const out = html(wrap(node));
- expect(out).toContain('<!-- Parse error: mismatched tag -->');
- expect(out).toContain('class="mdx-fallback"');
- expect(out).toContain('data-raw-mdx-fallback=""');
- expect(out).toContain('data-reason="mismatched tag"');
- expect(out).toContain('<code>');
- expect(out).toContain('&#x3C;A>');
- expect(out).toContain('&#x3C;/B>');
- });
-
- test('adversarial raw source never emits live HTML', () => {
- const node: RawMdxFallbackMdast = {
- type: 'rawMdxFallback',
- value: '<script>alert(2)</script>',
- data: { reason: 'xss attempt', originalSpan: { start: 0, end: 0 } },
- };
- const out = html(wrap(node));
- expect(out).not.toContain('<script>');
- expect(out).toContain('&#x3C;script>');
- });
-
- test('missing reason falls back to "unknown"', () => {
- const node = {
- type: 'rawMdxFallback',
- value: '',
- data: { reason: '', originalSpan: { start: 0, end: 0 } },
- } as RawMdxFallbackMdast;
- const out = html(wrap(node));
- expect(out).toContain('<!-- Parse error: unknown -->');
- });
-
- test('reason containing "-->" cannot close the comment prematurely', () => {
- const node: RawMdxFallbackMdast = {
- type: 'rawMdxFallback',
- value: '<script>alert(1)</script>',
- data: {
- reason: 'broken --> escape attempt',
- originalSpan: { start: 0, end: 0 },
- },
- };
- const out = html(wrap(node));
- const commentCloses = out.match(/-->/g) ?? [];
- expect(commentCloses.length).toBe(1);
- expect(out).toContain('\u2014> escape attempt');
- expect(out).not.toContain('<script>');
- expect(out).toContain('&#x3C;script>');
- });
-});
-
-describe('comment + commentBlock mdast→hast (em-dash defense)', () => {
- test('inline `comment` body containing `-->` is rendered with em-dash defense', () => {
- const node = {
- type: 'comment' as const,
- children: [{ type: 'text' as const, value: 'sneaky --> escape attempt' }],
- };
- // biome-ignore lint/suspicious/noExplicitAny: synthetic mdast node for handler smoke test
- const out = html(wrap(node as any));
- const commentCloses = out.match(/-->/g) ?? [];
- expect(commentCloses.length).toBe(1);
- expect(out).toContain('—> escape attempt');
- });
-
- test('block `commentBlock` body containing `-->` is rendered with em-dash defense', () => {
- const node = {
- type: 'commentBlock' as const,
- children: [
- {
- type: 'paragraph' as const,
- children: [{ type: 'text' as const, value: 'block sneaky --> escape' }],
- },
- ],
- };
- // biome-ignore lint/suspicious/noExplicitAny: synthetic mdast node for handler smoke test
- const out = html(wrap(node as any));
- const commentCloses = out.match(/-->/g) ?? [];
- expect(commentCloses.length).toBe(1);
- expect(out).toContain('—> escape');
- });
-});
-
-describe('footnoteReference mdast→hast', () => {
- test('renders as <sup id="fnref-N" data-footnote-ref><a href="#fn-N">[N]</a></sup>', () => {
- const node: FootnoteReference = {
- type: 'footnoteReference',
- identifier: '1',
- label: '1',
- };
- const out = html(wrap(node));
- expect(out).toContain('<sup');
- expect(out).toContain('id="fnref-1"');
- expect(out).toContain('data-footnote-ref');
- expect(out).toContain('data-footnote-id="1"');
- expect(out).toContain('class="footnote-ref"');
- expect(out).toContain('href="#fn-1"');
- expect(out).toContain('class="footnote-ref-link"');
- expect(out).toContain('>[1]</a>');
- });
-
- test('named identifier preserved in href, fnref id, and visible bracket', () => {
- const node: FootnoteReference = {
- type: 'footnoteReference',
- identifier: 'note',
- label: 'note',
- };
- const out = html(wrap(node));
- expect(out).toContain('id="fnref-note"');
- expect(out).toContain('data-footnote-id="note"');
- expect(out).toContain('href="#fn-note"');
- expect(out).toContain('>[note]</a>');
- });
-});
-
-describe('footnoteDefinition mdast→hast', () => {
- test('renders as <aside id="fn-N" data-footnote-def><div class="footnote-body">…</div><a href="#fnref-N">↩</a></aside>', () => {
- const node: FootnoteDefinition = {
- type: 'footnoteDefinition',
- identifier: 'abc',
- label: 'abc',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: 'Body text.' }],
- },
- ],
- };
- const out = html(wrap(node));
- expect(out).toContain('<aside');
- expect(out).toContain('id="fn-abc"');
- expect(out).toContain('data-footnote-def');
- expect(out).toContain('data-footnote-id="abc"');
- expect(out).toContain('class="footnote-def"');
- expect(out).toContain('class="footnote-body"');
- expect(out).toContain('Body text.');
- expect(out).toContain('href="#fnref-abc"');
- expect(out).toContain('class="footnote-backref"');
- expect(out).toContain('↩');
- expect(out).not.toContain('footnote-marker');
- });
-
- test('numeric identifier produces matching anchor target for `<a href="#fn-1">`', () => {
- const node: FootnoteDefinition = {
- type: 'footnoteDefinition',
- identifier: '1',
- label: '1',
- children: [{ type: 'paragraph', children: [{ type: 'text', value: 'x' }] }],
- };
- const out = html(wrap(node));
- expect(out).toContain('id="fn-1"');
- expect(out).toContain('href="#fnref-1"');
- });
-});
-
-describe('FR-20 adversarial fuzz — no unescaped <script> in any emitted HTML', () => {
- const ADVERSARIAL_FRAGMENTS = [
- '<script>alert(1)</script>',
- '<img src=x onerror=alert(1)>',
- '</script><script>x</script>',
- '<style>body{display:none}</style>',
- 'javascript:alert(1)',
- '<iframe src="data:text/html,<script>alert(1)</script>"></iframe>',
- 'null\u0000byte',
- '<?xml version="1.0"?><ns:tag xmlns:ns="foo"/>',
- '&amp;&lt;&gt;&#x22;',
- '"><svg/onload=alert(1)>',
- ];
-
- function randomPayload(seed: number): string {
- let s = seed;
- const rand = () => {
- s = (s * 1664525 + 1013904223) >>> 0;
- return s / 0xffffffff;
- };
- const count = 1 + Math.floor(rand() * 4);
- let out = '';
- for (let i = 0; i < count; i++) {
- out += ADVERSARIAL_FRAGMENTS[Math.floor(rand() * ADVERSARIAL_FRAGMENTS.length)];
- out += ' ';
- }
- return out.trim();
- }
-
- test('mdxJsxFlowElement — 100 random adversarial payloads', () => {
- for (let i = 0; i < 100; i++) {
- const payload = randomPayload(i + 1);
- const node: MdxJsxFlowElement = {
- type: 'mdxJsxFlowElement',
- name: null,
- attributes: [],
- children: [],
- data: { sourceRaw: payload },
- };
- const out = html(wrap(node));
- expect(out).not.toContain('<script>');
- expect(out).not.toContain('<script ');
- expect(out).not.toContain('<iframe');
- expect(out).not.toContain('<style>');
- }
- });
-
- test('mdxJsxTextElement — 100 random adversarial payloads', () => {
- for (let i = 0; i < 100; i++) {
- const payload = randomPayload(i + 1);
- const node: MdxJsxTextElement = {
- type: 'mdxJsxTextElement',
- name: null,
- attributes: [],
- children: [],
- data: { sourceRaw: payload },
- };
- const out = html(wrap(node));
- expect(out).not.toContain('<script>');
- expect(out).not.toContain('<script ');
- expect(out).not.toContain('<iframe');
- }
- });
-
- test('rawMdxFallback — 100 random adversarial payloads', () => {
- for (let i = 0; i < 100; i++) {
- const payload = randomPayload(i + 1);
- const node: RawMdxFallbackMdast = {
- type: 'rawMdxFallback',
- value: payload,
- data: { reason: 'fuzz', originalSpan: { start: 0, end: 0 } },
- };
- const out = html(wrap(node));
- expect(out).not.toContain('<script>');
- expect(out).not.toContain('<script ');
- expect(out).not.toContain('<iframe');
- }
- });
-});
diff --git a/packages/core/src/markdown/mdast-to-html.test.ts b/packages/core/src/markdown/mdast-to-html.test.ts
deleted file mode 100644
index 732140210..000000000
--- a/packages/core/src/markdown/mdast-to-html.test.ts
+++ /dev/null
@@ -1,361 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import { markdownToHtml, mdastToHtml } from './mdast-to-html.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function renderFaithful(md: string): string {
- return mdastToHtml(mdManager.parseToMdast(md));
-}
-
-describe('markdownToHtml — markdown string → HTML', () => {
- test('paragraph', () => {
- expect(markdownToHtml('hello world')).toBe('<p>hello world</p>');
- });
-
- test('heading', () => {
- expect(markdownToHtml('## heading')).toBe('<h2>heading</h2>');
- });
-
- test('all heading levels h1 through h6', () => {
- const html = markdownToHtml('# a\n\n## b\n\n### c\n\n#### d\n\n##### e\n\n###### f');
- expect(html).toContain('<h1>a</h1>');
- expect(html).toContain('<h2>b</h2>');
- expect(html).toContain('<h3>c</h3>');
- expect(html).toContain('<h4>d</h4>');
- expect(html).toContain('<h5>e</h5>');
- expect(html).toContain('<h6>f</h6>');
- });
-
- test('strong and emphasis', () => {
- const html = markdownToHtml('**bold** and *italic*');
- expect(html).toContain('<strong>bold</strong>');
- expect(html).toContain('<em>italic</em>');
- });
-
- test('inline code', () => {
- expect(markdownToHtml('run `npm install`')).toContain('<code>npm install</code>');
- });
-
- test('unordered list', () => {
- const html = markdownToHtml('- one\n- two');
- expect(html).toContain('<ul>');
- expect(html).toContain('<li>one</li>');
- expect(html).toContain('<li>two</li>');
- });
-
- test('ordered list', () => {
- const html = markdownToHtml('1. one\n2. two');
- expect(html).toContain('<ol>');
- });
-
- test('link', () => {
- const html = markdownToHtml('[site](https://example.com)');
- expect(html).toContain('<a href="https://example.com">site</a>');
- });
-
- test('fenced code block with language', () => {
- const html = markdownToHtml('```typescript\nconst x = 1;\n```');
- expect(html).toContain('<pre>');
- expect(html).toContain('<code class="language-typescript">');
- expect(html).toContain('const x = 1;');
- });
-
- test('blockquote', () => {
- const html = markdownToHtml('> quoted');
- expect(html).toContain('<blockquote>');
- expect(html).toContain('<p>quoted</p>');
- });
-
- test('GFM table renders as <table>', () => {
- const md = '| a | b |\n| - | - |\n| 1 | 2 |';
- const html = markdownToHtml(md);
- expect(html).toContain('<table>');
- expect(html).toContain('<th>a</th>');
- expect(html).toContain('<td>1</td>');
- });
-
- test('no OK-private data-* attributes in output', () => {
- const html = markdownToHtml('# title\n\n[link](#x)\n\n**bold**');
- expect(html).not.toContain('data-wiki-link');
- expect(html).not.toContain('data-jsx');
- expect(html).not.toContain('data-raw-mdx-fallback');
- });
-
- test('script HTML in markdown passthrough is dropped (no allowDangerousHtml)', () => {
- const html = markdownToHtml('<script>alert(1)</script>');
- expect(html).not.toContain('<script>');
- });
-});
-
-describe('mdastToHtml — mdast Root → HTML', () => {
- test('paragraph mdast converts to <p>', () => {
- const html = mdastToHtml({
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: 'direct mdast path' }],
- },
- ],
- });
- expect(html).toBe('<p>direct mdast path</p>');
- });
-
- test('cross-view symmetry — same logical content yields same HTML', () => {
- const viaMarkdown = markdownToHtml('## hi');
- const viaMdast = mdastToHtml({
- type: 'root',
- children: [{ type: 'heading', depth: 2, children: [{ type: 'text', value: 'hi' }] }],
- });
- expect(viaMdast).toBe(viaMarkdown);
- });
-});
-
-describe('custom-node regression gate — every promoted mdast type emits semantic HTML', () => {
- describe('(a) markdownToHtml string-entry — remark-plugin-produced types', () => {
- test('wikiLink bare target emits <a class="wiki-link">', () => {
- const html = markdownToHtml('[[Target]]');
- expect(html).toMatch(/<a[^>]*class="wiki-link"[^>]*>Target<\/a>/);
- expect(html).not.toMatch(/\[\[Target\]\]/);
- });
-
- test('wikiLink with alias preserves data-alias and label text', () => {
- const html = markdownToHtml('[[Target|Label]]');
- expect(html).toContain('class="wiki-link"');
- expect(html).toContain('data-target="Target"');
- expect(html).toContain('data-alias="Label"');
- expect(html).toMatch(/>Label<\/a>/);
- expect(html).not.toMatch(/\[\[Target\|Label\]\]/);
- });
- });
-
- describe('(b) mdastToHtml tree-entry — PM→mdast handler-produced types', () => {
- test('mdxJsxFlowElement emits <pre class="mdx-component"> with entity-escaped raw', () => {
- const html = mdastToHtml({
- type: 'root',
- children: [
- {
- type: 'mdxJsxFlowElement',
- name: 'Callout',
- attributes: [],
- children: [],
- data: { sourceRaw: '<Callout type="warning">Heads up</Callout>' },
- // biome-ignore lint/suspicious/noExplicitAny: synthetic mdast mirroring PM→mdast output
- } as any,
- ],
- });
- expect(html).toContain('<pre class="mdx-component">');
- expect(html).toContain('<code>');
- expect(html).toMatch(/&#x3C;Callout/);
- expect(html).not.toMatch(/<Callout/);
- });
-
- test('mdxJsxTextElement emits <span class="mdx-inline" data-jsx-inline=""> with entity-escaped raw', () => {
- const html = mdastToHtml({
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [
- { type: 'text', value: 'before ' },
- {
- type: 'mdxJsxTextElement',
- name: 'Tag',
- attributes: [],
- children: [],
- data: { sourceRaw: '<Tag prop="x"/>' },
- // biome-ignore lint/suspicious/noExplicitAny: synthetic mdast mirroring PM→mdast output
- } as any,
- { type: 'text', value: ' after' },
- ],
- },
- ],
- });
- expect(html).toContain('class="mdx-inline"');
- expect(html).toContain('data-jsx-inline=""');
- expect(html).toMatch(/&#x3C;Tag/);
- expect(html).not.toMatch(/<Tag /);
- });
-
- test('mark (Obsidian-style highlight) emits <mark>text</mark>', () => {
- const html = mdastToHtml({
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [
- {
- type: 'mark',
- children: [{ type: 'text', value: 'hello' }],
- // biome-ignore lint/suspicious/noExplicitAny: synthetic mdast mirroring fromPmMark output
- } as any,
- ],
- },
- ],
- });
- expect(html).toContain('<mark>hello</mark>');
- });
-
- test('rawMdxFallback emits parse-error comment + <pre> with class + data-raw-mdx-fallback markers', () => {
- const html = mdastToHtml({
- type: 'root',
- children: [
- {
- type: 'rawMdxFallback',
- data: { reason: 'Unclosed JSX', originalSpan: [0, 20] },
- value: '<Broken prop="xyz"',
- // biome-ignore lint/suspicious/noExplicitAny: synthetic mdast for handler-direct test
- } as any,
- ],
- });
- expect(html).toContain('<!-- Parse error: Unclosed JSX -->');
- expect(html).toContain('class="mdx-fallback"');
- expect(html).toContain('data-raw-mdx-fallback=""');
- expect(html).toContain('data-reason="Unclosed JSX"');
- expect(html).toContain('<code>');
- expect(html).toMatch(/&#x3C;Broken/);
- expect(html).not.toMatch(/<Broken /);
- });
- });
-});
-
-describe('URL scheme filter — outbound clipboard HTML sanitization', () => {
- test('strips javascript: href from links', () => {
- const html = markdownToHtml('[click](javascript:alert(1))');
- expect(html).not.toContain('javascript:');
- expect(html).toContain('>click<');
- });
-
- test('strips data: href from links', () => {
- const html = markdownToHtml('[boom](data:text/html,<script>alert(1)</script>)');
- expect(html).not.toContain('data:');
- expect(html).toContain('>boom<');
- });
-
- test('strips vbscript: href from links', () => {
- const html = markdownToHtml('[click](vbscript:msgbox)');
- expect(html).not.toContain('vbscript:');
- });
-
- test('strips file: href from links', () => {
- const html = markdownToHtml('[open](file:///etc/passwd)');
- expect(html).not.toContain('file:');
- });
-
- test('preserves https, http, mailto, tel, and relative hrefs', () => {
- expect(markdownToHtml('[a](https://example.com)')).toContain('href="https://example.com"');
- expect(markdownToHtml('[b](http://example.com)')).toContain('href="http://example.com"');
- expect(markdownToHtml('[c](mailto:[email protected])')).toContain('href="mailto:');
- expect(markdownToHtml('[d](tel:+15551234)')).toContain('href="tel:');
- expect(markdownToHtml('[e](/relative/path)')).toContain('href="/relative/path"');
- expect(markdownToHtml('[f](#anchor)')).toContain('href="#anchor"');
- });
-
- test('strips javascript: src from images', () => {
- const html = markdownToHtml('![alt](javascript:alert(1))');
- expect(html).not.toContain('javascript:');
- expect(html).toContain('alt="alt"');
- });
-
- test('case-insensitive: JavaScript:/DATA: variants are stripped', () => {
- const html1 = markdownToHtml('[a](JavaScript:alert(1))');
- const html2 = markdownToHtml('[b](DATA:text/html,x)');
- expect(html1).not.toMatch(/javascript:/i);
- expect(html2).not.toMatch(/data:/i);
- });
-});
-
-describe('faithful render path — compat media descriptors resolve to native elements', () => {
- test('CommonMarkImage emits <p><img></p>, not the mdx-component placeholder', () => {
- expect(renderFaithful('![foo](/url "title")')).toBe(
- '<p><img src="/url" alt="foo" title="title"></p>',
- );
- });
-
- test('empty alt is preserved as alt="" (decorative opt-in), not dropped', () => {
- expect(renderFaithful('![](/url)')).toBe('<p><img src="/url" alt=""></p>');
- });
-
- test('nested image description flattens to plain-text alt', () => {
- expect(renderFaithful('![foo ![bar](/url)](/url2)')).toBe(
- '<p><img src="/url2" alt="foo bar"></p>',
- );
- });
-
- test('inline image inside prose stays a native inline <img>', () => {
- expect(renderFaithful('before ![pic](/i.png) after')).toBe(
- '<p>before <img src="/i.png" alt="pic"> after</p>',
- );
- });
-
- test('lowercase MDX <img> keeps its bare flow shape (no paragraph wrap)', () => {
- expect(renderFaithful('<img src="/x.png" alt="y" />')).toBe('<img src="/x.png" alt="y">');
- });
-
- test('Callout component descriptor keeps the mdx-component placeholder', () => {
- const html = renderFaithful('> [!NOTE]\n> body');
- expect(html).toContain('mdx-component');
- expect(html).not.toContain('<img');
- });
-
- test('event-handler attributes are skipped on the compat path', () => {
- const html = renderFaithful('<CommonMarkImage src="/x.png" alt="y" onerror="alert(1)" />');
- expect(html).toContain('<img');
- expect(html).not.toContain('onerror');
- });
-});
-
-describe('faithful render path — source-preserved url/title attrs decode at render', () => {
- test('empty angle-bracket destination renders href=""', () => {
- expect(renderFaithful('[link](<>)')).toBe('<p><a href="">link</a></p>');
- });
-
- test('empty angle-bracket destination via reference definition renders href=""', () => {
- expect(renderFaithful('[foo]: <>\n\n[foo]')).toBe('<p><a href="">foo</a></p>');
- });
-
- test('entity refs in a link title decode to their characters', () => {
- expect(renderFaithful('[link](/url "title \\"&quot;")')).toBe(
- '<p><a href="/url" title="title &#x22;&#x22;">link</a></p>',
- );
- });
-
- test('entity refs in a destination decode then percent-encode like the spec', () => {
- expect(renderFaithful('[foo](/f&ouml;&ouml; "f&ouml;&ouml;")')).toBe(
- '<p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>',
- );
- });
-
- test('entity-SHAPED-but-invalid references stay literal', () => {
- const html = renderFaithful('[x](/a "t&hl;t")');
- expect(html).toContain('t&#x26;hl;t');
- });
-
- test('entity refs in a fenced-code info string decode in the language- class', () => {
- expect(renderFaithful('```f&ouml;&ouml;\ncode\n```')).toBe(
- '<pre><code class="language-föö">code\n</code></pre>',
- );
- });
-
- test('entity-SHAPED-but-invalid info-string reference stays literal in the class', () => {
- const html = renderFaithful('```f&hl;\ncode\n```');
- expect(html).toContain('language-f&#x26;hl;');
- });
-
- test('body-text entity refs are NOT decoded (storage-fidelity dialect untouched)', () => {
- const html = renderFaithful('&amp; in prose');
- expect(html).toContain('&#x26;amp; in prose');
- });
-
- test('entity-obfuscated javascript: scheme is still stripped', () => {
- const html = renderFaithful('[x](java&#x73;cript:alert(1))');
- expect(html).not.toMatch(/javascript:/i);
- });
-
- test('entity-obfuscated scheme on an image src is still stripped', () => {
- const html = renderFaithful('![x](java&#x73;cript:alert(1))');
- expect(html).not.toMatch(/javascript:/i);
- });
-});
diff --git a/packages/core/src/markdown/mdx-jsx-mdast-promotion.test.ts b/packages/core/src/markdown/mdx-jsx-mdast-promotion.test.ts
deleted file mode 100644
index bce7c733b..000000000
--- a/packages/core/src/markdown/mdx-jsx-mdast-promotion.test.ts
+++ /dev/null
@@ -1,99 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { fromProseMirror } from '@handlewithcare/remark-prosemirror';
-import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';
-import { getSchema } from '@tiptap/core';
-import type { Paragraph, Root } from 'mdast';
-import type { MdxJsxFlowElement, MdxJsxTextElement } from 'mdast-util-mdx';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-type Handlers = Parameters<typeof fromProseMirror>[1];
-type Managerish = {
- pmNodeHandlers: NonNullable<Handlers>['nodeHandlers'];
- pmMarkHandlers: NonNullable<Handlers>['markHandlers'];
-};
-
-function pmToMdast(json: unknown): Root {
- const schema = getSchema(sharedExtensions);
- // biome-ignore lint/suspicious/noExplicitAny: parse returns JSONContent with loose typing
- const pmNode = schema.nodeFromJSON(json as any);
- const internal = mdManager as unknown as Managerish;
- return fromProseMirror(pmNode, {
- schema,
- nodeHandlers: internal.pmNodeHandlers,
- markHandlers: internal.pmMarkHandlers,
- }) as Root;
-}
-
-describe('jsxComponent mdast promotion (US-005 / D7)', () => {
- test('<Callout type="info">content</Callout> round-trips bit-exact', () => {
- const md = '<Callout type="info">content</Callout>\n';
- const pm = mdManager.parse(md);
- const out = mdManager.serialize(pm);
- expect(out).toBe(md);
- });
-
- test('self-closing <Note/> round-trips bit-exact', () => {
- const md = '<Note/>\n';
- const pm = mdManager.parse(md);
- const out = mdManager.serialize(pm);
- expect(out).toBe(md);
- });
-
- test('PM→mdast emits mdxJsxFlowElement (not html)', () => {
- const pm = mdManager.parse('<MyBlock a="1">\n body\n</MyBlock>\n');
- const tree = pmToMdast(pm);
- const jsx = tree.children.find((c) => c.type === 'mdxJsxFlowElement') as
- | MdxJsxFlowElement
- | undefined;
- expect(jsx).toBeDefined();
- if (!jsx) throw new Error('unreachable');
- expect(typeof jsx.data?.sourceRaw).toBe('string');
- expect(jsx.data?.sourceRaw).toContain('<MyBlock');
- });
-
- test('round-trip survives full PM→mdast→md→re-parse→PM', () => {
- const md = '<Block x={y}>\n Hi [[Link]] there.\n</Block>\n';
- const pm1 = mdManager.parse(md);
- const mdOut = mdManager.serialize(pm1);
- const pm2 = mdManager.parse(mdOut);
- expect(pm2).toEqual(pm1);
- });
-});
-
-describe('jsxInline mdast promotion (US-005 / D7)', () => {
- test('inline <Note type="warn">hey</Note> round-trips', () => {
- const md = 'Prose with <Note type="warn">hey</Note> inline.\n';
- const pm = mdManager.parse(md);
- const out = mdManager.serialize(pm);
- expect(out).toBe(md);
- });
-
- test('self-closing inline <br/> round-trips', () => {
- const md = 'Line one<br/>Line two\n';
- const pm = mdManager.parse(md);
- const out = mdManager.serialize(pm);
- expect(out).toBe(md);
- });
-
- test('PM→mdast emits mdxJsxTextElement (not html)', () => {
- const pm = mdManager.parse('prose <Foo/> text\n');
- const tree = pmToMdast(pm);
- const para = tree.children.find((c) => c.type === 'paragraph') as Paragraph;
- expect(para).toBeDefined();
- const jsx = para.children.find((c) => c.type === 'mdxJsxTextElement') as
- | MdxJsxTextElement
- | undefined;
- expect(jsx).toBeDefined();
- if (!jsx) throw new Error('unreachable');
- expect(jsx.data?.sourceRaw).toContain('<Foo');
- });
-
- test('round-trip PM equivalence for inline JSX', () => {
- const md = 'See <Ref id="x"/> and <Em>em</Em> here\n';
- const pm1 = mdManager.parse(md);
- const mdOut = mdManager.serialize(pm1);
- const pm2 = mdManager.parse(mdOut);
- expect(pm2).toEqual(pm1);
- });
-});
diff --git a/packages/core/src/markdown/merged-walker.test.ts b/packages/core/src/markdown/merged-walker.test.ts
deleted file mode 100644
index a2611acd3..000000000
--- a/packages/core/src/markdown/merged-walker.test.ts
+++ /dev/null
@@ -1,200 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { Root as MdastRoot } from 'mdast';
-import { VFile } from 'vfile';
-import { mergedPostParseWalkerPlugin } from './merged-walker.ts';
-
-function makePos(startOffset: number, endOffset: number) {
- return {
- start: { line: 1, column: startOffset + 1, offset: startOffset },
- end: { line: 1, column: endOffset + 1, offset: endOffset },
- };
-}
-
-describe('mergedPostParseWalkerPlugin — Phase B dispatch', () => {
- test('pass 2: promotes <scheme:uri> text to semantic link inside paragraph', () => {
- const src = '<https://example.com>';
- const tree: MdastRoot = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- position: makePos(0, src.length),
- children: [{ type: 'text', value: src, position: makePos(0, src.length) }],
- },
- ],
- };
- mergedPostParseWalkerPlugin()(tree, new VFile(src));
-
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const para = tree.children[0] as any;
- expect(para.type).toBe('paragraph');
- expect(para.children).toHaveLength(1);
- expect(para.children[0].type).toBe('link');
- expect(para.children[0].url).toBe('https://example.com');
- expect(para.children[0].data?.sourceStyle).toBe('autolink');
- });
-
- test('pass 3 retired: a root-position empty yaml node is left untouched', () => {
- const src = '---\n\n---\n';
- const tree: MdastRoot = {
- type: 'root',
- children: [
- // biome-ignore lint/suspicious/noExplicitAny: synthetic yaml node
- { type: 'yaml', value: '', position: makePos(0, 8) } as any,
- ],
- };
- mergedPostParseWalkerPlugin()(tree, new VFile(src));
-
- expect(tree.children.length).toBe(1);
- expect(tree.children[0].type).toBe('yaml');
- });
-
- test('pass 4: attaches sourceDelimiter to emphasis node based on source', () => {
- const src = 'This is *emphasised* text.';
- const tree: MdastRoot = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- position: makePos(0, src.length),
- children: [
- { type: 'text', value: 'This is ', position: makePos(0, 8) },
- {
- type: 'emphasis',
- position: makePos(8, 20),
- children: [{ type: 'text', value: 'emphasised', position: makePos(9, 19) }],
- },
- { type: 'text', value: ' text.', position: makePos(20, 26) },
- ],
- },
- ],
- };
- mergedPostParseWalkerPlugin()(tree, new VFile(src));
-
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const em = (tree.children[0] as any).children[1];
- expect(em.type).toBe('emphasis');
- expect(em.data?.sourceDelimiter).toBe('*');
- });
-
- test('pass 5: replaces unknown type with rawMdxFallbackMdast and SKIPs descent', () => {
- const src = '$$math$$';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'someFutureType',
- position: makePos(0, src.length),
- children: [{ type: 'unknownInner', position: makePos(2, 6) }],
- },
- ],
- } as unknown as MdastRoot;
- mergedPostParseWalkerPlugin()(tree, new VFile(src));
-
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('someFutureType');
- expect(child.value).toBe('$$math$$');
- expect(child.children).toBeUndefined();
- });
-
- test('nested unknown inline: replaces brandNewInlineType inside paragraph, preserves siblings', () => {
- const src = 'hello [[?]] world';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- position: makePos(0, src.length),
- children: [
- { type: 'text', value: 'hello ', position: makePos(0, 6) },
- { type: 'brandNewInlineType', position: makePos(6, 11) },
- { type: 'text', value: ' world', position: makePos(11, 17) },
- ],
- },
- ],
- } as unknown as MdastRoot;
- mergedPostParseWalkerPlugin()(tree, new VFile(src));
-
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const para = tree.children[0] as any;
- expect(para.type).toBe('paragraph');
- expect(para.children[0].type).toBe('text');
- expect(para.children[1].type).toBe('rawMdxFallbackMdast');
- expect(para.children[1].originalType).toBe('brandNewInlineType');
- expect(para.children[1].value).toBe('[[?]]');
- expect(para.children[2].type).toBe('text');
- });
-
- test('known extended types (math, inlineMath, rawMdxFallbackMdast) are not replaced', () => {
- const tree = {
- type: 'root',
- children: [
- { type: 'math', value: 'x^2', position: makePos(0, 3) },
- {
- type: 'paragraph',
- position: makePos(4, 5),
- children: [{ type: 'inlineMath', value: 'y', position: makePos(4, 5) }],
- },
- ],
- } as unknown as MdastRoot;
- mergedPostParseWalkerPlugin()(tree, new VFile('x^2 y'));
-
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- expect((tree.children[0] as any).type).toBe('math');
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- expect((tree.children[1] as any).children[0].type).toBe('inlineMath');
- });
-
- test('pass 2 interleaved with pass 5: unknown sibling stays replaced, autolink in sibling stays promoted', () => {
- const src = 'See <https://a.com> and X and y';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- position: makePos(0, src.length),
- children: [
- { type: 'text', value: 'See <https://a.com> and ', position: makePos(0, 24) },
- { type: 'brandNew', position: makePos(24, 25) }, // unknown
- { type: 'text', value: ' and y', position: makePos(25, 31) },
- ],
- },
- ],
- } as unknown as MdastRoot;
- mergedPostParseWalkerPlugin()(tree, new VFile(src));
-
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const para = tree.children[0] as any;
- const types = para.children.map((c: { type: string }) => c.type);
- expect(types).toContain('link');
- expect(types).toContain('rawMdxFallbackMdast');
- expect(types).toContain('text');
-
- const link = para.children.find((c: { type: string }) => c.type === 'link');
- expect(link.url).toBe('https://a.com');
- expect(link.data?.sourceStyle).toBe('autolink');
-
- const fallback = para.children.find((c: { type: string }) => c.type === 'rawMdxFallbackMdast');
- expect(fallback.originalType).toBe('brandNew');
- });
-
- test('non-root-first yaml is left untouched', () => {
- const tree = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- position: makePos(0, 5),
- children: [{ type: 'text', value: 'hello' }],
- },
- // biome-ignore lint/suspicious/noExplicitAny: synthetic yaml node
- { type: 'yaml', value: '', position: makePos(6, 14) } as any,
- ],
- } as MdastRoot;
- mergedPostParseWalkerPlugin()(tree, new VFile('hello\n---\n---\n'));
-
- expect(tree.children[1].type).toBe('yaml');
- });
-});
diff --git a/packages/core/src/markdown/mermaid-fence.precision.test.ts b/packages/core/src/markdown/mermaid-fence.precision.test.ts
deleted file mode 100644
index 779c42ccf..000000000
--- a/packages/core/src/markdown/mermaid-fence.precision.test.ts
+++ /dev/null
@@ -1,98 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function findInJson(json: JSONContent, predicate: (n: JSONContent) => boolean): JSONContent | null {
- if (predicate(json)) return json;
- for (const child of json.content ?? []) {
- const found = findInJson(child, predicate);
- if (found) return found;
- }
- return null;
-}
-
-const isComponent = (name: string) => (n: JSONContent) =>
- n.type === 'jsxComponent' && n.attrs?.componentName === name;
-
-describe('mermaid fence (` ```mermaid `) → MermaidFence canonical', () => {
- test('` ```mermaid ` fence parses to a MermaidFence jsxComponent', () => {
- const json = mdManager.parse('```mermaid\ngraph TD; A-->B;\n```\n');
- const node = findInJson(json, isComponent('MermaidFence'));
- expect(node).toBeDefined();
- });
-
- test('mermaid fence preserves the chart source on the descriptor props', () => {
- const json = mdManager.parse('```mermaid\ngraph TD\n A-->B\n A-->C\n```\n');
- const node = findInJson(json, isComponent('MermaidFence'));
- expect(node).toBeDefined();
- expect((node?.attrs?.props as Record<string, unknown>)?.chart).toBe(
- 'graph TD\n A-->B\n A-->C',
- );
- });
-
- test('mermaid fence round-trips back to ` ```mermaid `…``` ` (γ pristine)', () => {
- const source = '```mermaid\ngraph TD; A-->B;\n```\n';
- const json = mdManager.parse(source);
- const out = mdManager.serialize(json);
- expect(out).toBe(source);
- });
-
- test('non-mermaid fences (` ```js `) are unchanged — still a code block, NOT MermaidFence', () => {
- const json = mdManager.parse('```js\nconst x = 1;\n```\n');
- const mermaid = findInJson(json, isComponent('MermaidFence'));
- expect(mermaid).toBeNull();
- });
-
- test('plain ` ``` ` fence (no language) is NOT MermaidFence', () => {
- const json = mdManager.parse('```\nplain text\n```\n');
- const mermaid = findInJson(json, isComponent('MermaidFence'));
- expect(mermaid).toBeNull();
- });
-
- test('legacy `<Mermaid chart="…" />` JSX does NOT match MermaidFence — falls through to wildcard', () => {
- const json = mdManager.parse('<Mermaid chart="graph TD; A-->B;" />\n');
- const fence = findInJson(json, isComponent('MermaidFence'));
- expect(fence).toBeNull();
- const wildcard = findInJson(json, isComponent('Mermaid'));
- expect(wildcard).not.toBeNull();
- });
-
- test('dirty-path: edited MermaidFence re-emits as ` ```mermaid ` fence, not JSX', () => {
- const json = mdManager.parse('```mermaid\ngraph TD; A-->B;\n```\n');
- const node = findInJson(json, isComponent('MermaidFence'));
- expect(node).not.toBeNull();
- if (node?.attrs) {
- delete (node.attrs as Record<string, unknown>).sourceRaw;
- (node.attrs.props as Record<string, unknown>).chart = 'flowchart LR; X-->Y;';
- }
- const out = mdManager.serialize(json);
- expect(out).toContain('```mermaid');
- expect(out).toContain('flowchart LR; X-->Y;');
- expect(out).not.toContain('<MermaidFence');
- expect(out).not.toContain('<Mermaid ');
- });
-});
-
-describe('mermaid + other components coexist', () => {
- test('mermaid fence next to a math block — both promote independently', () => {
- const source = '```mermaid\ngraph TD; A-->B;\n```\n\n$$\nE = mc^2\n$$\n';
- const json = mdManager.parse(source);
- expect(findInJson(json, isComponent('MermaidFence'))).toBeDefined();
- expect(findInJson(json, isComponent('DollarMath'))).toBeDefined();
- });
-
- test('two mermaid fences in one document each promote independently', () => {
- const source = '```mermaid\ngraph TD; A-->B;\n```\n\n```mermaid\nflowchart LR; X-->Y;\n```\n';
- const json = mdManager.parse(source);
- let count = 0;
- const walk = (n: JSONContent) => {
- if (isComponent('MermaidFence')(n)) count++;
- for (const child of n.content ?? []) walk(child);
- };
- walk(json);
- expect(count).toBe(2);
- });
-});
diff --git a/packages/core/src/markdown/parse-with-fallback.test.ts b/packages/core/src/markdown/parse-with-fallback.test.ts
deleted file mode 100644
index d62e04379..000000000
--- a/packages/core/src/markdown/parse-with-fallback.test.ts
+++ /dev/null
@@ -1,515 +0,0 @@
-import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { getParseHealth, resetParseHealth } from '../metrics/parse-health.ts';
-import { findFencedRegions } from './fence-regions.ts';
-import { loadPerfFixture } from './fixtures/index.ts';
-import { MarkdownManager } from './index.ts';
-import {
- enumerateFallbackRegions,
- MAX_SPLIT_DEPTH,
- parseRecursive,
- parseWithFallback as parseWithFallbackFn,
- scanTagEvents,
- type TagEvent,
-} from './parse-with-fallback.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-const BENCH_ENABLED = process.env.RUN_BENCH === '1' || process.env.RUN_BENCH === 'true';
-const describeBench = BENCH_ENABLED ? describe : describe.skip;
-
-describe('parseWithFallback (R6)', () => {
- beforeEach(() => resetParseHealth());
- afterEach(() => resetParseHealth());
-
- test('valid markdown parses clean (no fallback)', () => {
- const result = mdManager.parseWithFallback('# Heading\n\nParagraph\n');
- expect(result.content).toBeDefined();
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('heading');
- expect(types).toContain('paragraph');
- expect(getParseHealth().parseFallback.blockLevel).toBe(0);
- expect(getParseHealth().parseFallback.wholeDoc).toBe(0);
- });
-
- test('<Foo>...</Bar> tag mismatch produces rawMdxFallback with surrounding structure', () => {
- const src = '# Heading\n\n<Foo>broken</Bar>\n\n# Another heading\n';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('heading');
- expect(types).toContain('rawMdxFallback');
- const headings = (result.content as { type: string }[]).filter((n) => n.type === 'heading');
- expect(headings.length).toBeGreaterThanOrEqual(1);
- expect(getParseHealth().parseFallback.blockLevel).toBeGreaterThanOrEqual(1);
- });
-
- test('mismatched close tag in middle produces rawMdxFallback', () => {
- const src = '# Title\n\ntext </Bar> more text\n\nSome text after\n';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('rawMdxFallback');
- });
-
- test('position-less error falls through to whole-doc fallback', () => {
- const result = parseWithFallbackFn('some content', {
- parse: () => {
- throw new Error('no position info');
- },
- });
- expect(result.type).toBe('doc');
- expect(getParseHealth().parseFallback.wholeDoc).toBeGreaterThanOrEqual(1);
- });
-
- test('MAX_SPLIT_DEPTH exceeded falls to whole-doc fallback', () => {
- const result = parseWithFallbackFn(
- 'a\n\nb\n\nc\n\nd\n\ne\n\nf\n\ng\n\nh\n\ni\n\nj\n\nk\n\nl\n\nm\n\nn\n\no\n\np\n\nq\n\nr\n\ns\n\nt\n\nu\n\nv\n\nw',
- {
- parse: () => {
- const err = new Error('always fails') as Error & { place: { offset: number } };
- err.place = { offset: 2 };
- throw err;
- },
- },
- );
- expect(result.type).toBe('doc');
- expect(getParseHealth().parseFallback.wholeDoc).toBeGreaterThanOrEqual(1);
- });
-
- test('ref-def hoisting across split: link resolves after fallback', () => {
- const src =
- '[link][ref1]\n\n[ref1]: https://example.com\n\n<Foo>broken</Bar>\n\nAnother [link][ref1]\n';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('rawMdxFallback');
- const serialized = mdManager.serialize(result);
- expect(serialized).toContain('[ref1]: https://example.com');
- expect(serialized).toContain('[link][ref1]');
- });
-
- test('code fence containing <Tag> is not mistaken for JSX', () => {
- const src = '```\nsome code <Tag> inside\n```\n\n<Foo>broken</Bar>\n';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('codeBlock');
- expect(types).toContain('rawMdxFallback');
- });
-
- test('empty input returns empty doc', () => {
- const result = mdManager.parseWithFallback('');
- expect(result.type).toBe('doc');
- });
-
- test('(m2) recovery-failure path: split succeeds but recursive parse throws → whole-doc fallback', () => {
- let callCount = 0;
- const result = parseWithFallbackFn('a\n\nb\n\nc', {
- parse: () => {
- callCount++;
- if (callCount === 1) {
- const err = new Error('first call fails with position') as Error & {
- place: { offset: number };
- };
- err.place = { offset: 4 };
- throw err;
- }
- throw new Error('recovery parse fail');
- },
- });
- expect(result.type).toBe('doc');
- const children = result.content as { type: string }[];
- expect(children.length).toBeGreaterThanOrEqual(1);
- expect(getParseHealth().parseFallback.wholeDoc).toBeGreaterThanOrEqual(1);
- });
-
- test('(m3) enumerateFallbackRegions: surrounding headings preserved when mid-doc fallback fires', () => {
- const src = '# Before\n\nsome text\n\n<Foo>content</Bar>\n\n# After\n\nmore\n';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- const headings = (result.content as { type: string }[]).filter((n) => n.type === 'heading');
- expect(headings.length).toBe(2); // both "Before" and "After" preserved
- expect(types).toContain('rawMdxFallback');
- });
-
- test('(m3) nested broken — innermost paired region captured', () => {
- const src = '# Heading\n\n<Outer><Inner>broken</Bar></Outer>\n\n# Footer\n';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('rawMdxFallback');
- const headings = (result.content as { type: string }[]).filter((n) => n.type === 'heading');
- expect(headings.length).toBe(2);
- });
-
- test('(r23 boundary) depth=MAX_SPLIT_DEPTH permits parse; depth=MAX_SPLIT_DEPTH+1 short-circuits to whole-doc', () => {
- expect(MAX_SPLIT_DEPTH).toBe(20);
-
- let parseCalls = 0;
- const validParse = () => {
- parseCalls++;
- return { type: 'doc' as const, content: [{ type: 'paragraph' }] };
- };
-
- const belowResult = parseRecursive('any content\n', validParse, MAX_SPLIT_DEPTH);
- expect(belowResult.type).toBe('doc');
- expect(parseCalls).toBe(1);
- expect(getParseHealth().parseFallback.wholeDoc).toBe(0);
-
- const aboveResult = parseRecursive('any content\n', validParse, MAX_SPLIT_DEPTH + 1);
- expect(aboveResult.type).toBe('doc');
- const above = aboveResult.content as { type: string; content?: { text?: string }[] }[];
- expect(above.length).toBe(1);
- expect(above[0].type).toBe('paragraph');
- expect(above[0].content?.[0]?.text).toContain('any content');
- expect(parseCalls).toBe(1); // unchanged — short-circuit bypassed parse()
- expect(getParseHealth().parseFallback.wholeDoc).toBe(1);
- });
-
- test('(c1) tryPerBlockFallback single-block early-return: position-less error on one-block doc → whole-doc', () => {
- const singleBlock = 'just one paragraph here no blank lines';
- const result = parseWithFallbackFn(singleBlock, {
- parse: () => {
- throw new Error('always fails, no position');
- },
- });
- expect(result.type).toBe('doc');
- const children = result.content as {
- type: string;
- content?: { type: string; text?: string }[];
- }[];
- expect(children.length).toBe(1);
- expect(children[0].type).toBe('paragraph');
- const text = children[0].content?.[0]?.text ?? '';
- expect(text).toContain('just one paragraph here');
- const types = children.map((c) => c.type);
- expect(types).not.toContain('rawMdxFallback');
- expect(getParseHealth().parseFallback.wholeDoc).toBeGreaterThanOrEqual(1);
- });
-
- describe('never-throws invariant (Observer B hot-path)', () => {
- const pathological = [
- '<Foo>',
- '<Foo><Bar></Foo>',
- '<Foo bar="',
- '<Foo bar={',
- '{ unclosed',
- '{ {{ nested unbalanced }',
- '<div><span>',
- '\u0000\u0001\u0002',
- '',
- '\n',
- '<',
- '{',
- '# H\n\n<Foo>\n\n<Bar attr="xxx',
- ];
-
- for (const src of pathological) {
- const label = JSON.stringify(src).slice(0, 60);
- test(`does not throw on ${label}`, () => {
- expect(() => mdManager.parseWithFallback(src)).not.toThrow();
- });
- }
- });
-});
-
-describeBench('parseWithFallback perf bound vs happy path (R23)', () => {
- const MEASURED_RUNS = 3;
- const WARM_UPS = 2;
- const RATIO_BOUND = 5;
- const TEST_TIMEOUT_MS = 120_000;
-
- function injectBrokenBlocks(valid: string, count: number): string {
- const parts = valid.split(/\n\n+/);
- if (parts.length < count * 2) {
- throw new Error(
- `fixture has ${parts.length} parts, need at least ${count * 2} to inject ${count} broken blocks`,
- );
- }
- const step = Math.floor(parts.length / (count + 1));
- for (let i = 1; i <= count; i++) {
- const idx = i * step;
- parts[idx] = `<Foo>broken ${i}</Bar>`;
- }
- return parts.join('\n\n');
- }
-
- function median(xs: number[]): number {
- const sorted = [...xs].sort((a, b) => a - b);
- const mid = Math.floor(sorted.length / 2);
- return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid];
- }
-
- function measure(fn: () => unknown, warmups: number, runs: number): number[] {
- for (let i = 0; i < warmups; i++) fn();
- const times: number[] = [];
- for (let i = 0; i < runs; i++) {
- Bun.gc(true);
- const t0 = performance.now();
- fn();
- times.push(performance.now() - t0);
- }
- return times;
- }
-
- test.each([1000, 10000] as const)(
- 'fallback path at %i blocks stays within 5× happy-path parse',
- (blockCount) => {
- const valid = loadPerfFixture(blockCount);
- const broken = injectBrokenBlocks(valid, 5);
-
- const happyTimes = measure(() => mdManager.parse(valid), WARM_UPS, MEASURED_RUNS);
- const fallbackTimes = measure(
- () => mdManager.parseWithFallback(broken),
- WARM_UPS,
- MEASURED_RUNS,
- );
-
- const happyMs = median(happyTimes);
- const fallbackMs = median(fallbackTimes);
- const ratio = fallbackMs / happyMs;
-
- console.log(
- `[R23 perf ${blockCount} blocks] happy p50=${happyMs.toFixed(1)}ms fallback p50=${fallbackMs.toFixed(1)}ms ratio=${ratio.toFixed(2)}×`,
- );
-
- expect(ratio).toBeLessThanOrEqual(RATIO_BOUND);
- expect(getParseHealth().parseFallback.blockLevel).toBeGreaterThanOrEqual(1);
- },
- TEST_TIMEOUT_MS,
- );
-});
-
-describe('parse budget counter split', () => {
- beforeEach(() => {
- resetParseHealth();
- });
- afterEach(() => {
- resetParseHealth();
- });
-
- test('budget exhaustion increments wholeDocBudget, never the structural wholeDoc', () => {
- const mgr = new MarkdownManager({ extensions: sharedExtensions });
- const result = parseRecursive('# fine\n\nplain paragraph\n', (s) => mgr.parse(s), 0, {
- startMs: -1_000_000,
- calls: 0,
- });
- expect(result.type).toBe('doc');
- const h = getParseHealth();
- expect(h.parseFallback.wholeDocBudget).toBe(1);
- expect(h.parseFallback.wholeDoc).toBe(0);
- expect(h.parseFallback.blockLevel).toBe(0);
- });
-
- test('structural whole-doc fallback leaves wholeDocBudget untouched', () => {
- const mgr = new MarkdownManager({ extensions: sharedExtensions });
- parseRecursive('# h', (s) => mgr.parse(s), 21);
- const h = getParseHealth();
- expect(h.parseFallback.wholeDoc).toBe(1);
- expect(h.parseFallback.wholeDocBudget).toBe(0);
- });
-});
-
-describe('scanTagEvents (SC series)', () => {
- function scan(src: string): TagEvent[] {
- return scanTagEvents(src, findFencedRegions(src));
- }
-
- test('SC01: paired open/close tags produce OPEN + CLOSE events', () => {
- const events = scan('<Foo bar="baz">text</Foo>');
- expect(events.length).toBe(2);
- expect(events[0].kind).toBe('open');
- expect(events[0].name).toBe('Foo');
- expect(events[0].start).toBe(0);
- expect(events[1].kind).toBe('close');
- expect(events[1].name).toBe('Foo');
- });
-
- test('SC02: unclosed quote (EOL before close) emits no event (v1 safe-coarsening)', () => {
- const events = scan('<Foo bar="');
- expect(events.length).toBe(0);
- });
-
- test('SC03: brace-depth tracking skips > inside {…}', () => {
- const events = scan('<Foo bar={x > 5}>text</Foo>');
- expect(events.length).toBe(2);
- expect(events[0].kind).toBe('open');
- expect(events[0].name).toBe('Foo');
- expect(events[1].kind).toBe('close');
- expect(events[1].name).toBe('Foo');
- });
-
- test('SC04: nested braces with JSX-like content inside expression attr', () => {
- const events = scan('<Foo bar={items.map(x => <span>{x}</span>)}>');
- expect(events.length).toBe(1);
- expect(events[0].kind).toBe('open');
- expect(events[0].name).toBe('Foo');
- });
-
- test('SC05: tag inside fenced code block produces no events', () => {
- const src = '```\n<Foo>\n```';
- const events = scan(src);
- expect(events.length).toBe(0);
- });
-
- test('SC06: comment-like <!-- <Foo> --> does not produce OPEN(Foo)', () => {
- const events = scan('<!-- <Foo> -->');
- for (const ev of events) {
- expect(ev.name).not.toContain('!');
- }
- });
-
- test('SC07: < followed by space is not a tag start', () => {
- const events = scan('< 5');
- expect(events.length).toBe(0);
- });
-
- test('SC08: numeric tag names produce no events', () => {
- const events = scan('<5>');
- expect(events.length).toBe(0);
- const events2 = scan('<123>');
- expect(events2.length).toBe(0);
- });
-
- test('SC09: self-closing variants recognized', () => {
- const variants = ['<Foo/>', '<Foo />', '<Foo />', '<Foo\n/>'];
- for (const src of variants) {
- const events = scan(src);
- expect(events.length).toBe(1);
- expect(events[0].kind).toBe('self-close');
- expect(events[0].name).toBe('Foo');
- }
- });
-
- test('SC10: multi-line tag produces single OPEN event', () => {
- const src = '<Foo\n bar="baz"\n baz="qux"\n>';
- const events = scan(src);
- expect(events.length).toBe(1);
- expect(events[0].kind).toBe('open');
- expect(events[0].name).toBe('Foo');
- expect(events[0].start).toBe(0);
- });
-});
-
-describe('enumerateFallbackRegions + findFallbackRegion (NB series)', () => {
- afterEach(() => resetParseHealth());
-
- test('NB01: broken inner attr inside second Accordion — only second degrades', () => {
- const src =
- '<Accordions>\n<Accordion title="First">ok</Accordion>\n<Accordion title="Second"><Image src="\n</Accordion>\n</Accordions>';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('rawMdxFallback');
- expect(getParseHealth().parseFallback.blockLevel).toBeGreaterThanOrEqual(1);
- expect(getParseHealth().parseFallback.wholeDoc).toBe(0);
- });
-
- test('NB02: tag mismatch inside second Card — fallback fires', () => {
- const src =
- '# Before\n\n<Cards>\n<Card>clean first</Card>\n<Card><Foo>broken</Bar></Card>\n</Cards>\n\n# After';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('rawMdxFallback');
- expect(getParseHealth().parseFallback.wholeDoc).toBe(0);
- });
-
- test('NB03: tag mismatch inside middle Tab — surrounding structure preserved', () => {
- const src =
- '# Before\n\n<Tabs>\n<Tab>a</Tab>\n<Tab><Foo>broken</Bar></Tab>\n<Tab>c</Tab>\n</Tabs>\n\n# After';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('rawMdxFallback');
- expect(getParseHealth().parseFallback.wholeDoc).toBe(0);
- });
-
- test('NB04: tag mismatch deep in nested pairs — fallback preserves outer structure', () => {
- const src =
- '# Before\n\n<Outer>\n<Mid>\n<Inner><Foo>x</Bar></Inner>\n</Mid>\n</Outer>\n\n# After';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('rawMdxFallback');
- expect(getParseHealth().parseFallback.wholeDoc).toBe(0);
- });
-
- test('NB05: error in purely-prose block with no enclosing MDX tags → blank-line bounds', () => {
- const regions = enumerateFallbackRegions('# Hello\n\nsome broken text\n\n# Footer');
- const mdxRegions = regions.filter((r) => r.source === 'pair' || r.source === 'unmatched');
- expect(mdxRegions.length).toBe(0);
- });
-
- test('NB06: two independent broken regions in separate ancestor chains', () => {
- const src = '# Intro\n\n<Foo>broken1</Bar>\n\nClean paragraph\n\n<Baz>broken2</Qux>\n\n# Outro';
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- const fallbacks = types.filter((t) => t === 'rawMdxFallback');
- expect(fallbacks.length).toBeGreaterThanOrEqual(1);
- const headings = (result.content as { type: string }[]).filter((n) => n.type === 'heading');
- expect(headings.length).toBeGreaterThanOrEqual(1);
- expect(getParseHealth().parseFallback.wholeDoc).toBe(0);
- });
-
- test('NB07: broken tag inside fenced code block — no regions emitted for fenced content', () => {
- const src = '```\n<Foo attr="\n```\n\nClean paragraph';
- const regions = enumerateFallbackRegions(src);
- expect(regions.length).toBe(0);
- });
-
- test('NB08: deep nesting stress (8-level) — single-pass with no re-parse', () => {
- const src =
- '<A>\n<B>\n<C>\n<D>\n<E>\n<F>\n<G>\n<H>x<Image src="</H>\n</G>\n</F>\n</E>\n</D>\n</C>\n</B>\n</A>';
- const regions = enumerateFallbackRegions(src);
- const pairs = regions.filter((r) => r.source === 'pair');
- expect(pairs.length).toBe(8);
-
- const _result = mdManager.parseWithFallback(src);
- expect(getParseHealth().parseFallback.wholeDoc).toBe(0);
- });
-
- test('NB09: safe-coarsening via scanTagEvents — unclosed quote suppresses tag event', () => {
- const src = '<Accordions>\n<Accordion broken attr="\n orphan text\n</Accordions>';
- const fences = findFencedRegions(src);
- const events = scanTagEvents(src, fences);
- const accordionOpens = events.filter((e) => e.kind === 'open' && e.name === 'Accordion');
- expect(accordionOpens.length).toBe(0);
- const accordionsOpens = events.filter((e) => e.kind === 'open' && e.name === 'Accordions');
- const accordionsCloses = events.filter((e) => e.kind === 'close' && e.name === 'Accordions');
- expect(accordionsOpens.length).toBe(1);
- expect(accordionsCloses.length).toBe(1);
- });
-
- test("NB10: self-closing tags don't enter stack", () => {
- const src = '<Outer>\n<SelfClose attr="x" />\n<Inner>x<Image src="broken</Inner>\n</Outer>';
- const regions = enumerateFallbackRegions(src);
- const selfCloseRegions = regions.filter((r) => {
- const regionText = src.slice(r.start, r.end);
- return (
- regionText.includes('SelfClose') &&
- r.source === 'pair' &&
- !regionText.includes('Outer') &&
- !regionText.includes('Inner')
- );
- });
- expect(selfCloseRegions.length).toBe(0);
- const innerPairs = regions.filter(
- (r) => r.source === 'pair' && src.slice(r.start, r.end).startsWith('<Inner'),
- );
- expect(innerPairs.length).toBe(1);
- const outerPairs = regions.filter(
- (r) => r.source === 'pair' && src.slice(r.start, r.end).startsWith('<Outer'),
- );
- expect(outerPairs.length).toBe(1);
- });
-
- test('NB11: top-level unmatched-open bounded by blank line', () => {
- const src = '# Intro\n\n<Foo>content</Bar>\n\n# Outro';
- const regions = enumerateFallbackRegions(src);
- const unmatchedFoo = regions.filter(
- (r) => r.source === 'unmatched' && src.slice(r.start, r.start + 4) === '<Foo',
- );
- expect(unmatchedFoo.length).toBe(1);
- const region = unmatchedFoo[0];
- expect(region.end).toBeLessThanOrEqual(src.indexOf('\n\n# Outro'));
-
- const result = mdManager.parseWithFallback(src);
- const types = (result.content as { type: string }[]).map((n) => n.type);
- expect(types).toContain('rawMdxFallback');
- const headings = (result.content as { type: string }[]).filter((n) => n.type === 'heading');
- expect(headings.length).toBeGreaterThanOrEqual(1);
- });
-});
diff --git a/packages/core/src/markdown/parser-drop-closure.test.ts b/packages/core/src/markdown/parser-drop-closure.test.ts
deleted file mode 100644
index 71fe4c9a7..000000000
--- a/packages/core/src/markdown/parser-drop-closure.test.ts
+++ /dev/null
@@ -1,136 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { readFileSync } from 'node:fs';
-import path from 'node:path';
-import { types } from 'micromark-util-symbol';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import {
- checkClosure,
- DROPPED_TOKEN_ADJUDICATIONS,
- extractFromMarkdownHandlerKeys,
- extractPreprocessTransforms,
- PINNED_PREPROCESS_SOURCE_SHA256,
- PREPROCESS_TRANSFORM_ADJUDICATIONS,
-} from './parser-drop-closure.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-const roundTrip = (source: string) => md.serialize(md.parse(source));
-
-function libPath(specifier: string, file: string): string {
- const entry = Bun.resolveSync(specifier, import.meta.dir);
- return path.join(path.dirname(entry), file);
-}
-
-function computeDroppedTokenTypes(): string[] {
- const handlerKeys = new Set(
- extractFromMarkdownHandlerKeys(
- readFileSync(libPath('mdast-util-from-markdown', 'lib/index.js'), 'utf8'),
- ),
- );
- return Object.values(types)
- .filter((tokenType) => !handlerKeys.has(tokenType))
- .sort();
-}
-
-const preprocessSource = readFileSync(libPath('micromark', 'lib/preprocess.js'), 'utf8');
-
-describe('parser-drop closure - dropped token types', () => {
- test('the computed dropped set is fully adjudicated with no stale entries', () => {
- const dropped = computeDroppedTokenTypes();
- expect(dropped.length).toBe(52);
- const result = checkClosure(dropped, DROPPED_TOKEN_ADJUDICATIONS);
- expect(result.unadjudicated).toEqual([]);
- expect(result.stale).toEqual([]);
- });
-
- test('every format-dof-axis adjudication cites axes that exist in the canonical catalog', () => {
- const catalog = JSON.parse(
- readFileSync(
- path.join(import.meta.dir, '../../../md-conformance/md-audit/format-dof-catalog.json'),
- 'utf8',
- ),
- ) as { axes: Array<{ id: string }> };
- const axisIds = new Set(catalog.axes.map((axis) => axis.id));
- for (const [token, adjudication] of Object.entries(DROPPED_TOKEN_ADJUDICATIONS)) {
- if (adjudication.kind !== 'format-dof-axis') continue;
- expect(adjudication.axisIds.length).toBeGreaterThan(0);
- for (const axisId of adjudication.axisIds) {
- expect(axisIds.has(axisId), `${token} cites unknown axis ${axisId}`).toBe(true);
- }
- }
- });
-
- test('every retained-by-capture witness round-trips byte-exactly through the real engine', () => {
- for (const [token, adjudication] of Object.entries(DROPPED_TOKEN_ADJUDICATIONS)) {
- if (adjudication.kind !== 'retained-by-capture') continue;
- const out = roundTrip(adjudication.witness);
- expect(
- out === adjudication.witness || out === `${adjudication.witness}\n`,
- `${token} witness no longer round-trips: ${JSON.stringify(out)}`,
- ).toBe(true);
- }
- });
-
- test('every documented-residual witness pins exact current bytes and is idempotent', () => {
- for (const adjudication of Object.values(DROPPED_TOKEN_ADJUDICATIONS)) {
- if (adjudication.kind !== 'documented-residual') continue;
- for (const witness of adjudication.witnesses) {
- const rt1 = roundTrip(witness.input);
- expect(rt1).toBe(witness.roundTrip);
- expect(roundTrip(rt1)).toBe(rt1);
- }
- }
- });
-});
-
-describe('parser-drop closure - preprocess transforms', () => {
- test('the extracted transform surface is fully adjudicated with no stale entries', () => {
- const extracted = extractPreprocessTransforms(preprocessSource);
- expect(extracted.hasBomHeadCheck).toBe(true);
- const computed = [...extracted.transformChars, 'bom-head-check'];
- const result = checkClosure(computed, PREPROCESS_TRANSFORM_ADJUDICATIONS);
- expect(result.unadjudicated).toEqual([]);
- expect(result.stale).toEqual([]);
- });
-
- test('the resolved preprocess source matches the pinned hash (a bump re-fires adjudication)', () => {
- const hash = new Bun.CryptoHasher('sha256').update(preprocessSource).digest('hex');
- expect(
- hash,
- 'micromark lib/preprocess.js changed. This pin exists to force re-review, not a blind ' +
- 're-pin: diff the new preprocess source, re-review PREPROCESS_TRANSFORM_ADJUDICATIONS ' +
- 'in parser-drop-closure.ts (every transform char + the BOM head-check must stay ' +
- `adjudicated), then set PINNED_PREPROCESS_SOURCE_SHA256 = '${hash}'.`,
- ).toBe(PINNED_PREPROCESS_SOURCE_SHA256);
- });
-
- test('the spec-mandated NUL replacement behaves exactly as pinned', () => {
- for (const adjudication of Object.values(PREPROCESS_TRANSFORM_ADJUDICATIONS)) {
- if (adjudication.kind !== 'spec-mandated-replacement') continue;
- expect(roundTrip(adjudication.witness.input)).toBe(adjudication.witness.roundTrip);
- }
- });
-});
-
-describe('parser-drop closure - non-vacuity (tamper)', () => {
- test('removing one adjudication reports that token as unadjudicated', () => {
- const dropped = computeDroppedTokenTypes();
- const { lineSuffix: _omitted, ...rest } = DROPPED_TOKEN_ADJUDICATIONS;
- const result = checkClosure(dropped, rest);
- expect(result.unadjudicated).toEqual(['lineSuffix']);
- });
-
- test('an adjudication for a token no longer dropped reports as stale', () => {
- const dropped = computeDroppedTokenTypes();
- const result = checkClosure(dropped, {
- ...DROPPED_TOKEN_ADJUDICATIONS,
- paragraph: { kind: 'structural-only', rationale: 'planted stale entry' },
- });
- expect(result.stale).toEqual(['paragraph']);
- });
-
- test('the extractors fail loudly when the upstream source shape changes', () => {
- expect(() => extractFromMarkdownHandlerKeys('nothing here')).toThrow();
- expect(() => extractPreprocessTransforms('nothing here')).toThrow();
- });
-});
diff --git a/packages/core/src/markdown/pdf-embed.precision.test.ts b/packages/core/src/markdown/pdf-embed.precision.test.ts
deleted file mode 100644
index ff7f6ce7c..000000000
--- a/packages/core/src/markdown/pdf-embed.precision.test.ts
+++ /dev/null
@@ -1,109 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { parsePdfAnchor } from '../utils/pdf-anchor.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function findNodes(json: JSONContent, type: string): JSONContent[] {
- const out: JSONContent[] = [];
- const visit = (n: JSONContent) => {
- if (n.type === type) out.push(n);
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-describe('pdf embed — basic round-trip', () => {
- test('plain `![[Document.pdf]]` round-trips byte-stable', () => {
- const src = '![[Document.pdf]]\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`![[Document.pdf#page=3]]` round-trips', () => {
- const src = '![[Document.pdf#page=3]]\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`![[Document.pdf#height=400]]` round-trips', () => {
- const src = '![[Document.pdf#height=400]]\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('`![[Document.pdf#page=3&height=600]]` round-trips', () => {
- const src = '![[Document.pdf#page=3&height=600]]\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-});
-
-describe('pdf embed — parse correctness', () => {
- test('parses to PM jsxComponent atom carrying the WikiEmbedFile descriptor', () => {
- const json = mdManager.parse('![[Document.pdf]]\n');
- const components = findNodes(json, 'jsxComponent');
- expect(components.length).toBe(1);
- expect(components[0].attrs?.componentName).toBe('WikiEmbedFile');
- });
-
- test('props bag carries target / anchor / alias from the wikiLinkEmbed mdast', () => {
- const json = mdManager.parse('![[Document.pdf#page=3|Spec]]\n');
- const components = findNodes(json, 'jsxComponent');
- expect(components.length).toBe(1);
- const props = components[0].attrs?.props as Record<string, unknown>;
- expect(props.target).toBe('Document.pdf');
- expect(props.anchor).toBe('page=3');
- expect(props.alias).toBe('Spec');
- });
-
- test('image extensions route to WikiEmbedImage, not WikiEmbedFile (image-tier branch wins first)', () => {
- const json = mdManager.parse('![[image.png]]\n');
- const components = findNodes(json, 'jsxComponent');
- expect(components.length).toBeGreaterThan(0);
- expect(components[0].attrs?.componentName).toBe('WikiEmbedImage');
- });
-
- test('inline-position `![[doc.pdf]]` mid-prose does NOT promote to WikiEmbedFile', () => {
- const json = mdManager.parse('Inline ![[doc.pdf]] embed.\n');
- const components = findNodes(json, 'jsxComponent');
- expect(components.find((c) => c.attrs?.componentName === 'WikiEmbedFile')).toBeUndefined();
- });
-});
-
-describe('pdf anchor parser', () => {
- test('empty anchor returns null height + empty viewer fragment', () => {
- expect(parsePdfAnchor('')).toEqual({ height: null, viewerFragment: '' });
- expect(parsePdfAnchor(undefined)).toEqual({ height: null, viewerFragment: '' });
- });
-
- test('`page=3` passes through to viewer fragment, height stays null', () => {
- expect(parsePdfAnchor('page=3')).toEqual({ height: null, viewerFragment: 'page=3' });
- });
-
- test('`height=400` extracts height, viewer fragment is empty', () => {
- expect(parsePdfAnchor('height=400')).toEqual({ height: 400, viewerFragment: '' });
- });
-
- test('`page=3&height=600` extracts height + keeps page in viewer fragment', () => {
- expect(parsePdfAnchor('page=3&height=600')).toEqual({
- height: 600,
- viewerFragment: 'page=3',
- });
- });
-
- test('unknown keys pass through to the viewer fragment unchanged', () => {
- expect(parsePdfAnchor('zoom=200&search=foo')).toEqual({
- height: null,
- viewerFragment: 'zoom=200&search=foo',
- });
- });
-
- test('malformed `height=` (non-numeric) is ignored, height stays null', () => {
- expect(parsePdfAnchor('height=tall')).toEqual({ height: null, viewerFragment: '' });
- });
-
- test('negative / zero height is rejected (stays null)', () => {
- expect(parsePdfAnchor('height=-100')).toEqual({ height: null, viewerFragment: '' });
- expect(parsePdfAnchor('height=0')).toEqual({ height: null, viewerFragment: '' });
- });
-});
diff --git a/packages/core/src/markdown/pipeline.doc-boundary.test.ts b/packages/core/src/markdown/pipeline.doc-boundary.test.ts
deleted file mode 100644
index 35ae94e79..000000000
--- a/packages/core/src/markdown/pipeline.doc-boundary.test.ts
+++ /dev/null
@@ -1,138 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import { assertRoundTripIdempotent } from './round-trip-asserts.test-helper.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-
-function rt(source: string): string {
- return md.serialize(md.parse(source));
-}
-
-const expectByteStable = (out: string): void => assertRoundTripIdempotent(rt, out);
-
-describe('indented-code leading-indent capture (tab-expansion)', () => {
- test('a tab-indented code line round-trips byte-exact', () => {
- expect(rt('\tfoo\tbar')).toBe('\tfoo\tbar\n');
- expectByteStable('\tfoo\tbar\n');
- });
-
- test('multi-line tab indents round-trip per line', () => {
- expect(rt('\tfoo\n\tbar')).toBe('\tfoo\n\tbar\n');
- });
-
- test('mixed tab/space indents round-trip per line', () => {
- expect(rt('\tfoo\n bar')).toBe('\tfoo\n bar\n');
- });
-
- test('an interior blank line keeps its own (possibly partial) indent bytes', () => {
- expect(rt('\tfoo\n\n\tbar')).toBe('\tfoo\n\n\tbar\n');
- });
-
- test('canonical 4-space indents are unchanged (no capture noise)', () => {
- expect(rt(' foo')).toBe(' foo\n');
- expect(rt(' foo')).toBe(' foo\n');
- });
-
- test('a WYSIWYG-shaped edit that changes the line count falls back to the canonical indent', () => {
- const json = md.parse('\tfoo\n\tbar') as JSONContent;
- const code = json.content?.[0];
- expect(code?.type).toBe('codeBlock');
- if (code) code.content = [{ type: 'text', text: 'foo\nbar\nbaz' }];
- const out = md.serialize(json);
- expect(out).toBe(' foo\n bar\n baz\n');
- expectByteStable(out);
- });
-});
-
-describe('document-head BOM capture', () => {
- test('a BOM-led document round-trips the BOM', () => {
- expect(rt('\uFEFFhello')).toBe('\uFEFFhello\n');
- expectByteStable('\uFEFFhello\n');
- });
-
- test('the BOM composes with other captured boundary state', () => {
- expect(rt('\uFEFF\n\nhello')).toBe('\uFEFF\n\nhello\n');
- });
-});
-
-describe('BOM offset-rebase latent bug (first-node source-form mis-slice)', () => {
- test('a BOM-led spaced thematic break keeps its exact glyphs (non-doc-start)', () => {
- const out = rt('\uFEFFx\n\n- - -');
- expect(out).toBe('\uFEFFx\n\n- - -\n');
- expectByteStable(out);
- });
-
- test('a BOM-led ATX heading keeps its interior spacing capture', () => {
- expect(rt('\uFEFF## x')).toBe('\uFEFF## x\n');
- });
-
- test('a BOM-led doc-start dash break behaves like its non-BOM twin (glyphs preserved)', () => {
- expect(rt('\uFEFF- - -')).toBe('\uFEFF- - -\n');
- expect(rt('- - -')).toBe('- - -\n');
- expect(md.parseToMdast(rt('- - -')).children[0]?.type).toBe('thematicBreak');
- });
-
- test('non-BOM controls are byte-identical to their pre-capture behavior', () => {
- expect(rt('x\n\n- - -')).toBe('x\n\n- - -\n');
- expect(rt('## x')).toBe('## x\n');
- });
-});
-
-describe('doc-leading blank-line capture', () => {
- test('leading blank lines round-trip', () => {
- expect(rt('\n\nhello')).toBe('\n\nhello\n');
- expect(rt('\nhello')).toBe('\nhello\n');
- expectByteStable('\n\nhello\n');
- });
-});
-
-describe('doc-trailing blank-line capture', () => {
- test('trailing blank lines round-trip byte-exact', () => {
- expect(rt('hello\n\n')).toBe('hello\n\n');
- expect(rt('hello\n\n\n')).toBe('hello\n\n\n');
- });
-
- test('the canonical single final newline is unchanged', () => {
- expect(rt('hello')).toBe('hello\n');
- expect(rt('hello\n')).toBe('hello\n');
- });
-});
-
-describe('inter-block blank-line-count capture', () => {
- test('extra blank lines between blocks round-trip', () => {
- expect(rt('a\n\n\n\nb')).toBe('a\n\n\n\nb\n');
- expect(rt('a\n\n\nb')).toBe('a\n\n\nb\n');
- expectByteStable('a\n\n\n\nb\n');
- });
-
- test('per-boundary counts are independent across three blocks', () => {
- expect(rt('a\n\n\n\nb\n\n\nc')).toBe('a\n\n\n\nb\n\n\nc\n');
- });
-
- test('the canonical single blank line is unchanged', () => {
- expect(rt('a\n\nb')).toBe('a\n\nb\n');
- });
-
- test('a 0-blank adjacency still normalizes to the canonical blank line', () => {
- expect(rt('# H\nP')).toBe('# H\n\nP\n');
- });
-
- test('a WYSIWYG-shaped block deletion drops the stale gap capture', () => {
- const json = md.parse('a\n\n\n\nb\n\n\n\nc') as JSONContent;
- expect(json.content?.length).toBe(3);
- json.content = json.content?.slice(0, 2) ?? [];
- const out = md.serialize(json);
- expect(out).toBe('a\n\nb\n');
- expectByteStable(out);
- });
-});
-
-describe('composed doc-boundary state', () => {
- test('BOM + leading + gaps + trailing all round-trip together', () => {
- const doc = '\uFEFF\n\na\n\n\n\nb\n\n';
- expect(rt(doc)).toBe(doc);
- expectByteStable(doc);
- });
-});
diff --git a/packages/core/src/markdown/pipeline.doc-start.test.ts b/packages/core/src/markdown/pipeline.doc-start.test.ts
deleted file mode 100644
index 9f4edc781..000000000
--- a/packages/core/src/markdown/pipeline.doc-start.test.ts
+++ /dev/null
@@ -1,92 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { stripFrontmatter } from '../extensions/frontmatter.ts';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function rt(source: string): string {
- return mdManager.serialize(mdManager.parse(source));
-}
-
-function topLevelMdastTypes(source: string): string[] {
- return mdManager.parseToMdast(source).children.map((child) => child.type);
-}
-
-describe('content after a doc-start thematic break parses per CommonMark', () => {
- test('a list after a doc-start --- stays a list (no escaped-paragraph collapse)', () => {
- expect(topLevelMdastTypes('---\n\n- a\n- b')).toEqual(['thematicBreak', 'list']);
- expect(rt('---\n\n- a\n- b')).toBe('---\n\n- a\n- b\n');
- });
-
- test('the recovered-list output is byte-stable across a second round-trip', () => {
- const r1 = rt('---\n\n- a\n- b');
- expect(rt(r1)).toBe(r1);
- });
-});
-
-describe('frontmatter-shaped paste is visible content, never silently dropped', () => {
- const fmPaste = '---\nkey: val\n---\n\nbody';
-
- test('every content byte of a pasted frontmatter block stays visible', () => {
- const out = rt(fmPaste);
- expect(out).toContain('key: val');
- expect(out).toContain('body');
- expect(out).toBe('---\n\nkey: val\n---\n\nbody\n');
- });
-
- test('the pasted block parses as thematic break + setext heading + paragraph', () => {
- expect(topLevelMdastTypes(fmPaste)).toEqual(['thematicBreak', 'heading', 'paragraph']);
- const heading = mdManager.parseToMdast(fmPaste).children[1];
- expect(heading.type === 'heading' && heading.depth).toBe(2);
- });
-
- test('the visible-content output is byte-stable across a second round-trip', () => {
- const r1 = rt(fmPaste);
- expect(rt(r1)).toBe(r1);
- });
-
- test('a yaml node never enters the parse tree', () => {
- for (const input of [fmPaste, '---\ntitle: x\n---\n', '---\n---', '---\n\n---']) {
- expect(topLevelMdastTypes(input)).not.toContain('yaml');
- }
- });
-});
-
-describe('doc-start --- is preserved verbatim (the *** override is retired)', () => {
- test('a doc-start --- round-trips as ---', () => {
- expect(rt('---\n\nfoo')).toBe('---\n\nfoo\n');
- expect(rt('---\n')).toBe('---\n');
- });
-
- test('empty-fence shapes keep both --- lines', () => {
- expect(rt('---\n---')).toBe('---\n\n---\n');
- expect(rt('---\n\n---')).toBe('---\n\n---\n');
- });
-
- test('*** authored as *** still round-trips as ***', () => {
- expect(rt('***\n\nfoo')).toBe('***\n\nfoo\n');
- });
-
- test('mid-doc frontmatter-shaped content is untouched (change is doc-start-scoped)', () => {
- expect(rt('x\n\n---\nkey: val\n---')).toBe('x\n\n---\n\nkey: val\n---\n');
- });
-});
-
-describe('the pre-existing intake-strip edge (documented, not changed)', () => {
- test('a doc-start --- HR paired with a later --- line is claimed as frontmatter', () => {
- const doc = '---\n\nintro prose\n\n---\n\nbody';
- expect(stripFrontmatter(doc)).toEqual({
- frontmatter: '---\n\nintro prose\n\n---\n',
- body: '\nbody',
- });
- });
-
- test('a doc-start --- with no later --- line passes through to the parser intact', () => {
- expect(stripFrontmatter('---\n\nbody with no later dashes')).toEqual({
- frontmatter: '',
- body: '---\n\nbody with no later dashes',
- });
- expect(stripFrontmatter('---\n')).toEqual({ frontmatter: '', body: '---\n' });
- });
-});
diff --git a/packages/core/src/markdown/pipeline.test.ts b/packages/core/src/markdown/pipeline.test.ts
deleted file mode 100644
index a691bb6ea..000000000
--- a/packages/core/src/markdown/pipeline.test.ts
+++ /dev/null
@@ -1,185 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import { createSerializeProcessor } from './pipeline.ts';
-import { toMarkdownHandlers } from './to-markdown-handlers.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function buildDirectSerializeProcessor() {
- return createSerializeProcessor({
- schema: undefined as never,
- handlers: {} as never,
- pmNodeHandlers: {},
- pmMarkHandlers: {},
- toMarkdownHandlers,
- });
-}
-
-describe('MarkdownManager scaffold', () => {
- test('parse returns valid JSONContent with heading + paragraph', () => {
- const json = mdManager.parse('# Hello\n\nworld\n');
- expect(json).toBeDefined();
- expect(json.type).toBe('doc');
- expect(json.content).toBeDefined();
- expect(json.content?.length).toBeGreaterThanOrEqual(2);
-
- const heading = json.content?.[0];
- expect(heading.type).toBe('heading');
- expect(heading.attrs?.level).toBe(1);
-
- const para = json.content?.[1];
- expect(para.type).toBe('paragraph');
- });
-
- test('simple heading + paragraph round-trips byte-identically', () => {
- const input = '# Hello\n\nworld\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-
- test('multi-paragraph round-trip', () => {
- const input = 'First paragraph.\n\nSecond paragraph.\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-
- test('emphasis and strong round-trip', () => {
- const input = 'This is *emphasized* and **strong** text.\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-
- test('inline code round-trips', () => {
- const input = 'Use `console.log()` for debug.\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-
- test('code block round-trips', () => {
- const input = '```js\nconsole.log("hello");\n```\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-
- test('blockquote round-trips', () => {
- const input = '> This is a quote.\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-
- test('link round-trips', () => {
- const input = 'Visit [example](https://example.com) for details.\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-
- test('unordered list round-trips', () => {
- const input = '- item one\n- item two\n- item three\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-
- test('horizontal rule round-trips', () => {
- const input = 'Above.\n\n---\n\nBelow.\n';
- const json = mdManager.parse(input);
- const output = mdManager.serialize(json);
- expect(output).toBe(input);
- });
-});
-
-describe('position-aware blank-line Join (FR-12)', () => {
- describe('direct mdast → stringify path (positions intact)', () => {
- test('1 blank line between paragraphs round-trips byte-equal', () => {
- const input = 'P1\n\nP2\n';
- const tree = mdManager.parseToMdast(input);
- const sp = buildDirectSerializeProcessor();
- expect(String(sp.stringify(tree))).toBe(input);
- });
-
- test('2 blank lines preserved (gap=2)', () => {
- const input = 'P1\n\n\nP2\n';
- const tree = mdManager.parseToMdast(input);
- const sp = buildDirectSerializeProcessor();
- expect(String(sp.stringify(tree))).toBe(input);
- });
-
- test('4 blank lines preserved (gap=4)', () => {
- const input = 'P1\n\n\n\n\nP2\n';
- const tree = mdManager.parseToMdast(input);
- const sp = buildDirectSerializeProcessor();
- expect(String(sp.stringify(tree))).toBe(input);
- });
-
- test('mixed flow children (heading, paragraph, list) preserve blank-line gaps', () => {
- const input = '# H\n\n\n\nP1\n\n\n\n- item\n';
- const tree = mdManager.parseToMdast(input);
- const sp = buildDirectSerializeProcessor();
- expect(String(sp.stringify(tree))).toBe(input);
- });
-
- test('synthetic mdast without positions falls through to default 1 blank line', () => {
- const sp = buildDirectSerializeProcessor();
- const tree = {
- type: 'root',
- children: [
- { type: 'paragraph', children: [{ type: 'text', value: 'P1' }] },
- { type: 'paragraph', children: [{ type: 'text', value: 'P2' }] },
- ],
- } as never;
- expect(String(sp.stringify(tree))).toBe('P1\n\nP2\n');
- });
- });
-
- describe('PM round-trip path (doc-boundary snapshot carries the counts)', () => {
- test('1 blank line between paragraphs is the dominant case (byte-identical)', () => {
- const input = 'P1\n\nP2\n';
- const json = mdManager.parse(input);
- expect(mdManager.serialize(json)).toBe(input);
- });
-
- test('2+ blank lines round-trip via the sourceDocBoundary attr', () => {
- const json = mdManager.parse('P1\n\n\nP2\n');
- expect(mdManager.serialize(json)).toBe('P1\n\n\nP2\n');
- });
-
- test('4 blank lines round-trip via the sourceDocBoundary attr', () => {
- const json = mdManager.parse('P1\n\n\n\n\nP2\n');
- expect(mdManager.serialize(json)).toBe('P1\n\n\n\n\nP2\n');
- });
-
- test('a doc without the attr (the CRDT/fragment shape) collapses to the canonical 1 blank line', () => {
- const json = mdManager.parse('P1\n\n\n\n\nP2\n');
- const { sourceDocBoundary: _dropped, ...attrs } = (json.attrs ?? {}) as Record<
- string,
- unknown
- >;
- const stripped = { ...json, attrs };
- expect(mdManager.serialize(stripped)).toBe('P1\n\nP2\n');
- });
- });
-
- describe('parse-without-edit identity', () => {
- test('parse(serialize(parse(md))) === parse(md) for the dominant 1-blank-line case', () => {
- const md = 'P1\n\nP2\n\nP3\n';
- const initial = mdManager.parse(md);
- const round = mdManager.parse(mdManager.serialize(initial));
- expect(round).toEqual(initial);
- });
-
- test('PM identity holds even when source has 2+ blank lines', () => {
- const md = 'P1\n\n\n\nP2\n';
- const initial = mdManager.parse(md);
- const round = mdManager.parse(mdManager.serialize(initial));
- expect(round).toEqual(initial);
- });
- });
-});
diff --git a/packages/core/src/markdown/position-aware-join.test.ts b/packages/core/src/markdown/position-aware-join.test.ts
deleted file mode 100644
index 09e0c90df..000000000
--- a/packages/core/src/markdown/position-aware-join.test.ts
+++ /dev/null
@@ -1,97 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { FlowChildren, FlowParents, State } from 'mdast-util-to-markdown';
-import { positionAwareBlankLineJoin } from './position-aware-join.ts';
-
-const parent = { type: 'root', children: [] } as unknown as FlowParents;
-const state = {} as State;
-
-function paragraph(startLine: number, endLine: number): FlowChildren {
- return {
- type: 'paragraph',
- children: [],
- position: {
- start: { line: startLine, column: 1, offset: 0 },
- end: { line: endLine, column: 1, offset: 0 },
- },
- } as unknown as FlowChildren;
-}
-
-function paragraphNoPosition(): FlowChildren {
- return { type: 'paragraph', children: [] } as unknown as FlowChildren;
-}
-
-describe('positionAwareBlankLineJoin', () => {
- describe('with positions present', () => {
- test('returns 1 for one blank line between siblings (start.line - end.line - 1 = 1)', () => {
- const result = positionAwareBlankLineJoin(paragraph(1, 1), paragraph(3, 3), parent, state);
- expect(result).toBe(1);
- });
-
- test('returns 2 for two blank lines (start.line - end.line - 1 = 2)', () => {
- const result = positionAwareBlankLineJoin(paragraph(1, 1), paragraph(4, 4), parent, state);
- expect(result).toBe(2);
- });
-
- test('returns 4 for four blank lines (start.line - end.line - 1 = 4)', () => {
- const result = positionAwareBlankLineJoin(paragraph(1, 1), paragraph(6, 6), parent, state);
- expect(result).toBe(4);
- });
-
- test('returns 10 for ten blank lines', () => {
- const result = positionAwareBlankLineJoin(paragraph(1, 1), paragraph(12, 12), parent, state);
- expect(result).toBe(10);
- });
-
- test('handles multi-line left node (paragraph spanning lines 1-3)', () => {
- const result = positionAwareBlankLineJoin(paragraph(1, 3), paragraph(5, 5), parent, state);
- expect(result).toBe(1);
- });
- });
-
- describe('returns undefined to fall through to default', () => {
- test('left node missing position', () => {
- const result = positionAwareBlankLineJoin(
- paragraphNoPosition(),
- paragraph(5, 5),
- parent,
- state,
- );
- expect(result).toBeUndefined();
- });
-
- test('right node missing position', () => {
- const result = positionAwareBlankLineJoin(
- paragraph(1, 1),
- paragraphNoPosition(),
- parent,
- state,
- );
- expect(result).toBeUndefined();
- });
-
- test('both nodes missing position', () => {
- const result = positionAwareBlankLineJoin(
- paragraphNoPosition(),
- paragraphNoPosition(),
- parent,
- state,
- );
- expect(result).toBeUndefined();
- });
-
- test('clamps zero-gap (adjacent on same line) to undefined — not 0', () => {
- const result = positionAwareBlankLineJoin(paragraph(1, 1), paragraph(2, 2), parent, state);
- expect(result).toBeUndefined();
- });
-
- test('clamps negative gap (out-of-order positions) to undefined', () => {
- const result = positionAwareBlankLineJoin(paragraph(5, 5), paragraph(2, 2), parent, state);
- expect(result).toBeUndefined();
- });
-
- test('clamps zero-gap from multi-line left node', () => {
- const result = positionAwareBlankLineJoin(paragraph(1, 3), paragraph(4, 4), parent, state);
- expect(result).toBeUndefined();
- });
- });
-});
diff --git a/packages/core/src/markdown/position-slice.test.ts b/packages/core/src/markdown/position-slice.test.ts
deleted file mode 100644
index 7a1f07a2b..000000000
--- a/packages/core/src/markdown/position-slice.test.ts
+++ /dev/null
@@ -1,861 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { Nodes, Root } from 'mdast';
-import remarkGfm from 'remark-gfm';
-import remarkParse from 'remark-parse';
-import { unified } from 'unified';
-import { visit } from 'unist-util-visit';
-import { VFile } from 'vfile';
-import { positionSlicePlugin, splitGfmCellSegments } from './position-slice.ts';
-
-type AnyNode = Nodes & { data?: Record<string, unknown> };
-
-function parseMdast(source: string): Root {
- const processor = unified().use(remarkParse).use(remarkGfm).use(positionSlicePlugin);
- const tree = processor.parse(source);
- processor.runSync(tree, new VFile({ value: source }));
- return tree;
-}
-
-function findNode<T extends AnyNode = AnyNode>(tree: Root, type: string): T {
- let found: AnyNode | null = null;
- visit(tree, type, (node) => {
- found ||= node as AnyNode;
- });
- return found as unknown as T;
-}
-
-function findNodes<T extends AnyNode = AnyNode>(tree: Root, type: string): T[] {
- const nodes: AnyNode[] = [];
- visit(tree, type, (node) => {
- nodes.push(node as AnyNode);
- });
- return nodes as T[];
-}
-
-describe('position-slice: emphasis delimiter recovery', () => {
- test('asterisk emphasis → data.sourceDelimiter = "*"', () => {
- const tree = parseMdast('This is *emphasized* text.\n');
- const em = findNode(tree, 'emphasis');
- expect(em).toBeDefined();
- expect(em.data?.sourceDelimiter).toBe('*');
- });
-
- test('underscore emphasis → data.sourceDelimiter = "_"', () => {
- const tree = parseMdast('This is _emphasized_ text.\n');
- const em = findNode(tree, 'emphasis');
- expect(em).toBeDefined();
- expect(em.data?.sourceDelimiter).toBe('_');
- });
-});
-
-describe('position-slice: strong delimiter recovery', () => {
- test('double-asterisk strong → data.sourceDelimiter = "**"', () => {
- const tree = parseMdast('This is **strong** text.\n');
- const strong = findNode(tree, 'strong');
- expect(strong).toBeDefined();
- expect(strong.data?.sourceDelimiter).toBe('**');
- });
-
- test('double-underscore strong → data.sourceDelimiter = "__"', () => {
- const tree = parseMdast('This is __strong__ text.\n');
- const strong = findNode(tree, 'strong');
- expect(strong).toBeDefined();
- expect(strong.data?.sourceDelimiter).toBe('__');
- });
-});
-
-describe('position-slice: heading style recovery', () => {
- test('ATX heading → data.sourceStyle = "atx"', () => {
- const tree = parseMdast('# Heading\n');
- const heading = findNode(tree, 'heading');
- expect(heading).toBeDefined();
- expect(heading.data?.sourceStyle).toBe('atx');
- });
-
- test('setext heading (=) → data.sourceStyle = "setext"', () => {
- const tree = parseMdast('Heading\n=======\n');
- const heading = findNode(tree, 'heading');
- expect(heading).toBeDefined();
- expect(heading.data?.sourceStyle).toBe('setext');
- });
-
- test('setext heading (-) → data.sourceStyle = "setext"', () => {
- const tree = parseMdast('Heading\n-------\n');
- const heading = findNode(tree, 'heading');
- expect(heading).toBeDefined();
- expect(heading.data?.sourceStyle).toBe('setext');
- });
-});
-
-describe('position-slice: ATX trailing-hashes recovery (FR-15)', () => {
- test('matching closer count is captured', () => {
- const tree = parseMdast('## H ##\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBe(2);
- });
-
- test('asymmetric closer count is captured', () => {
- const tree = parseMdast('## H #####\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBe(5);
- });
-
- test('single trailing hash is captured', () => {
- const tree = parseMdast('### asymmetric trail #\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBe(1);
- });
-
- test('empty content with trailing closer is captured', () => {
- const tree = parseMdast('# ###\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBe(3);
- });
-
- test('no trailing closer leaves attr undefined', () => {
- const tree = parseMdast('## Plain heading\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBeUndefined();
- });
-
- test('hash without preceding space is content (no closer captured)', () => {
- const tree = parseMdast('## H#\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBeUndefined();
- });
-
- test('mid-content # run is not the closer', () => {
- const tree = parseMdast('## My ## heading\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBeUndefined();
- });
-
- test('trailing whitespace after the closer does not corrupt the count', () => {
- const tree = parseMdast('## H ## \n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBe(2);
- });
-
- test('setext heading does not get a sourceTrailingHashes attr', () => {
- const tree = parseMdast('Heading\n=======\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceTrailingHashes).toBeUndefined();
- expect(heading.data?.sourceStyle).toBe('setext');
- });
-});
-
-describe('position-slice: setext underline length recovery (FR-22)', () => {
- test('H1 5-char underline → sourceUnderlineLength = 5', () => {
- const tree = parseMdast('H\n=====\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceStyle).toBe('setext');
- expect(heading.data?.sourceUnderlineLength).toBe(5);
- });
-
- test('H1 1-char underline (CommonMark minimum) → length 1', () => {
- const tree = parseMdast('H\n=\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceStyle).toBe('setext');
- expect(heading.data?.sourceUnderlineLength).toBe(1);
- });
-
- test('H1 11-char underline → length 11', () => {
- const tree = parseMdast('H\n===========\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceUnderlineLength).toBe(11);
- });
-
- test('H2 5-char underline (-) → length 5', () => {
- const tree = parseMdast('H\n-----\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceStyle).toBe('setext');
- expect(heading.data?.sourceUnderlineLength).toBe(5);
- });
-
- test('H2 1-char underline → length 1', () => {
- const tree = parseMdast('H\n-\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceUnderlineLength).toBe(1);
- });
-
- test('underline shorter than content captures actual underline run', () => {
- const tree = parseMdast('this is a long heading\n===\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceUnderlineLength).toBe(3);
- });
-
- test('ATX heading does NOT get sourceUnderlineLength', () => {
- const tree = parseMdast('## ATX H\n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceStyle).toBe('atx');
- expect(heading.data?.sourceUnderlineLength).toBeUndefined();
- });
-
- test('trailing whitespace after underline run does not inflate length', () => {
- const tree = parseMdast('H\n===== \n');
- const heading = findNode(tree, 'heading');
- expect(heading.data?.sourceUnderlineLength).toBe(5);
- });
-});
-
-describe('position-slice: list marker recovery', () => {
- test('dash bullet → data.bulletMarker = "-"', () => {
- const tree = parseMdast('- item\n');
- const list = findNode(tree, 'list');
- expect(list).toBeDefined();
- expect(list.data?.bulletMarker).toBe('-');
- });
-
- test('asterisk bullet → data.bulletMarker = "*"', () => {
- const tree = parseMdast('* item\n');
- const list = findNode(tree, 'list');
- expect(list).toBeDefined();
- expect(list.data?.bulletMarker).toBe('*');
- });
-
- test('plus bullet → data.bulletMarker = "+"', () => {
- const tree = parseMdast('+ item\n');
- const list = findNode(tree, 'list');
- expect(list).toBeDefined();
- expect(list.data?.bulletMarker).toBe('+');
- });
-
- test('ordered list with dot → data.listMarkerDelimiter = "."', () => {
- const tree = parseMdast('1. item\n');
- const list = findNode(tree, 'list');
- expect(list).toBeDefined();
- expect(list.data?.listMarkerDelimiter).toBe('.');
- });
-
- test('ordered list with paren → data.listMarkerDelimiter = ")"', () => {
- const tree = parseMdast('1) item\n');
- const list = findNode(tree, 'list');
- expect(list).toBeDefined();
- expect(list.data?.listMarkerDelimiter).toBe(')');
- });
-});
-
-describe('position-slice: code fence recovery', () => {
- test('backtick fence → data.sourceFenceChar = "`", data.sourceFenceLength = 3', () => {
- const tree = parseMdast('```\ncode\n```\n');
- const code = findNode(tree, 'code');
- expect(code).toBeDefined();
- expect(code.data?.sourceFenceChar).toBe('`');
- expect(code.data?.sourceFenceLength).toBe(3);
- });
-
- test('tilde fence → data.sourceFenceChar = "~", data.sourceFenceLength = 3', () => {
- const tree = parseMdast('~~~\ncode\n~~~\n');
- const code = findNode(tree, 'code');
- expect(code).toBeDefined();
- expect(code.data?.sourceFenceChar).toBe('~');
- expect(code.data?.sourceFenceLength).toBe(3);
- });
-
- test('4-backtick fence → data.sourceFenceLength = 4', () => {
- const tree = parseMdast('````\ncode\n````\n');
- const code = findNode(tree, 'code');
- expect(code).toBeDefined();
- expect(code.data?.sourceFenceChar).toBe('`');
- expect(code.data?.sourceFenceLength).toBe(4);
- });
-});
-
-describe('position-slice: code block sourceStyle recovery (FR-21)', () => {
- test('backtick-fenced code → data.sourceStyle = "fenced"', () => {
- const tree = parseMdast('```\ncode\n```\n');
- const code = findNode(tree, 'code');
- expect(code.data?.sourceStyle).toBe('fenced');
- });
-
- test('tilde-fenced code → data.sourceStyle = "fenced"', () => {
- const tree = parseMdast('~~~\ncode\n~~~\n');
- const code = findNode(tree, 'code');
- expect(code.data?.sourceStyle).toBe('fenced');
- });
-
- test('indented code → data.sourceStyle = "indented"', () => {
- const tree = parseMdast(' code\n line\n');
- const code = findNode(tree, 'code');
- expect(code.data?.sourceStyle).toBe('indented');
- expect(code.data?.sourceFenceChar).toBeUndefined();
- expect(code.data?.sourceFenceLength).toBeUndefined();
- });
-
- test('tab-indented code → data.sourceStyle = "indented"', () => {
- const tree = parseMdast('\tcode\n');
- const code = findNode(tree, 'code');
- expect(code.data?.sourceStyle).toBe('indented');
- });
-
- test('fenced with language preserves fenced sourceStyle', () => {
- const tree = parseMdast('```js\nx\n```\n');
- const code = findNode(tree, 'code');
- expect(code.data?.sourceStyle).toBe('fenced');
- });
-});
-
-describe('position-slice: thematic break recovery', () => {
- test('--- → data.sourceRaw = "---"', () => {
- const tree = parseMdast('---\n');
- const tb = findNode(tree, 'thematicBreak');
- expect(tb).toBeDefined();
- expect(tb.data?.sourceRaw).toBe('---');
- });
-
- test('*** → data.sourceRaw = "***"', () => {
- const tree = parseMdast('***\n');
- const tb = findNode(tree, 'thematicBreak');
- expect(tb).toBeDefined();
- expect(tb.data?.sourceRaw).toBe('***');
- });
-
- test('___ → data.sourceRaw = "___"', () => {
- const tree = parseMdast('___\n');
- const tb = findNode(tree, 'thematicBreak');
- expect(tb).toBeDefined();
- expect(tb.data?.sourceRaw).toBe('___');
- });
-
- test('spaced rule → data.sourceRaw preserves spaces', () => {
- const tree = parseMdast('* * *\n');
- const tb = findNode(tree, 'thematicBreak');
- expect(tb).toBeDefined();
- expect(tb.data?.sourceRaw).toBe('* * *');
- });
-});
-
-describe('position-slice: hard break recovery', () => {
- test('backslash break → data.sourceStyle = "backslash"', () => {
- const tree = parseMdast('line one\\\nline two\n');
- const brk = findNode(tree, 'break');
- expect(brk).toBeDefined();
- expect(brk.data?.sourceStyle).toBe('backslash');
- });
-
- test('two-space break → data.sourceStyle = "spaces"', () => {
- const tree = parseMdast('line one \nline two\n');
- const brk = findNode(tree, 'break');
- expect(brk).toBeDefined();
- expect(brk.data?.sourceStyle).toBe('spaces');
- });
-});
-
-describe('position-slice: sourceRaw text preservation', () => {
- test('literal trailing backslash runs keep data.sourceRaw', () => {
- const triple = '\\'.repeat(3);
- const tree = parseMdast(`text ${triple}\n`);
- const textNodes = findNodes(tree, 'text');
- const trailing = textNodes.find((n) => n.value === `text ${'\\'.repeat(2)}`);
- expect(trailing).toBeDefined();
- expect(trailing?.data?.sourceRaw).toBe(`text ${triple}`);
- });
-
- test('escaped character plus trailing backslash records escapedChars and sourceRaw', () => {
- const trailing = '\\';
- const tree = parseMdast(`\\[text${trailing}\n`);
- const textNodes = findNodes(tree, 'text');
- const node = textNodes.find((n) => n.value === `[text${trailing}`);
- expect(node).toBeDefined();
- expect(node?.data?.escapedChars).toEqual([{ offset: 0, char: '[' }]);
- expect(node?.data?.sourceRaw).toBe(`\\[text${trailing}`);
- });
-});
-
-describe('position-slice: escapeMark tagging (D20)', () => {
- test('backslash-escaped # → data.escapedChars', () => {
- const tree = parseMdast('text \\# more\n');
- const textNodes = findNodes(tree, 'text');
- const escaped = textNodes.find((n) => {
- const e = n.data?.escapedChars as unknown[] | undefined;
- return !!e && e.length > 0;
- });
- expect(escaped).toBeDefined();
- expect(escaped?.data?.escapedChars).toEqual([{ offset: expect.any(Number), char: '#' }]);
- });
-
- test('backslash-escaped * → data.escapedChars', () => {
- const tree = parseMdast('text \\* more\n');
- const textNodes = findNodes(tree, 'text');
- const escaped = textNodes.find((n) => {
- const e = n.data?.escapedChars as unknown[] | undefined;
- return !!e && e.length > 0;
- });
- expect(escaped).toBeDefined();
- const chars = escaped?.data?.escapedChars as Array<{ char: string }>;
- expect(chars[0].char).toBe('*');
- });
-
- test('multiple escaped chars in one text run', () => {
- const tree = parseMdast('\\*literal\\*\n');
- const textNodes = findNodes(tree, 'text');
- const escaped = textNodes.find((n) => {
- const e = n.data?.escapedChars as unknown[] | undefined;
- return !!e && e.length > 0;
- });
- expect(escaped).toBeDefined();
- const chars = escaped?.data?.escapedChars as unknown[];
- expect(chars.length).toBeGreaterThanOrEqual(1);
- });
-
- test('non-ambiguous escape (\\foo) has no escapedChars', () => {
- const tree = parseMdast('text \\q more\n');
- const textNodes = findNodes(tree, 'text');
- const hasEscaped = textNodes.some((n) => {
- const e = n.data?.escapedChars as unknown[] | undefined;
- return !!e && e.length > 0;
- });
- expect(hasEscaped).toBe(false);
- });
-});
-
-describe('position-slice: fallback behavior', () => {
- test('walker does not crash on empty source', () => {
- expect(() => parseMdast('')).not.toThrow();
- });
-
- test('walker does not crash on source with no position data', () => {
- const processor = unified().use(remarkParse).use(positionSlicePlugin);
- const tree = processor.parse('hello\n');
- (tree as { position?: unknown }).position = undefined;
- expect(() => processor.runSync(tree, new VFile({ value: 'hello\n' }))).not.toThrow();
- });
-});
-
-describe('position-slice: GFM table dash-count recovery (FR-16)', () => {
- test(':---: → sourceDashCounts captures 3 dashes per column', () => {
- const tree = parseMdast('| x |\n| :---: |\n| 1234 |\n');
- const table = findNode(tree, 'table');
- expect(table?.data?.sourceDashCounts).toEqual([3]);
- });
-
- test('mixed dash counts captured per-column', () => {
- const tree = parseMdast('| a | b | c |\n| - | --- | ----- |\n| 1 | 2 | 3 |\n');
- const table = findNode(tree, 'table');
- expect(table?.data?.sourceDashCounts).toEqual([1, 3, 5]);
- });
-
- test('per-column align + dash count both captured', () => {
- const tree = parseMdast('| a | b | c |\n| :--- | :----: | -----: |\n| 1 | 2 | 3 |\n');
- const table = findNode(tree, 'table');
- expect(table?.data?.sourceDashCounts).toEqual([3, 4, 5]);
- expect((table as { align?: unknown[] } | null)?.align).toEqual(['left', 'center', 'right']);
- });
-
- test('canonical 1-dash form captured as count 1', () => {
- const tree = parseMdast('| a |\n| - |\n| 1 |\n');
- const table = findNode(tree, 'table');
- expect(table?.data?.sourceDashCounts).toEqual([1]);
- });
-
- test('pipe-less alignment row captured', () => {
- const tree = parseMdast('a | b\n--- | ---\n1 | 2\n');
- const table = findNode(tree, 'table');
- expect(table?.data?.sourceDashCounts).toEqual([3, 3]);
- });
-
- test('whitespace inside cells does not affect dash count', () => {
- const tree = parseMdast('| a |\n| --- |\n| 1 |\n');
- const table = findNode(tree, 'table');
- expect(table?.data?.sourceDashCounts).toEqual([3]);
- });
-
- test('asymmetric padding around colons preserved', () => {
- const tree = parseMdast('| a |\n| :----- |\n| 1 |\n');
- const table = findNode(tree, 'table');
- expect(table?.data?.sourceDashCounts).toEqual([5]);
- });
-});
-
-describe('position-slice: GFM bare-URL autolink detection (FR-17)', () => {
- test('bare https URL tagged with sourceStyle=gfm-autolink', () => {
- const tree = parseMdast('visit https://example.com today\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).toBe('gfm-autolink');
- });
-
- test('bare http URL tagged with sourceStyle=gfm-autolink', () => {
- const tree = parseMdast('see http://x.com here\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).toBe('gfm-autolink');
- });
-
- test('bare email autolink tagged with sourceStyle=gfm-autolink', () => {
- const tree = parseMdast('reach [email protected] please\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).toBe('gfm-autolink');
- });
-
- test('bare www host tagged with sourceStyle=gfm-autolink', () => {
- const tree = parseMdast('use www.example.com today\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).toBe('gfm-autolink');
- });
-
- test('explicit inline link [text](url) NOT tagged as gfm-autolink', () => {
- const tree = parseMdast('[click](https://example.com)\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).not.toBe('gfm-autolink');
- });
-
- test('text==url inline link NOT tagged as gfm-autolink (source slice starts with [)', () => {
- const tree = parseMdast('[https://x.com](https://x.com)\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).not.toBe('gfm-autolink');
- });
-
- test('inline link with title NOT tagged as gfm-autolink', () => {
- const tree = parseMdast('[click](https://x.com "title")\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).not.toBe('gfm-autolink');
- });
-
- test('multiple bare autolinks in one paragraph each tagged', () => {
- const tree = parseMdast('a https://x.com and https://y.com end\n');
- const links = findNodes(tree, 'link');
- expect(links.length).toBe(2);
- expect(links[0]?.data?.sourceStyle).toBe('gfm-autolink');
- expect(links[1]?.data?.sourceStyle).toBe('gfm-autolink');
- });
-
- test('mixed angle-bracket + bare in same paragraph keeps distinct sourceStyles', () => {
- const tree = parseMdast('see <https://x.com> and https://y.com bare\n');
- const links = findNodes(tree, 'link');
- expect(links.length).toBe(2);
- const bareLink = links.find((l) => l.url === 'https://y.com');
- expect(bareLink?.data?.sourceStyle).toBe('gfm-autolink');
- });
-
- test('bare URL inside list item tagged correctly', () => {
- const tree = parseMdast('- https://x.com\n- https://y.com\n');
- const links = findNodes(tree, 'link');
- expect(links.length).toBe(2);
- expect(links[0]?.data?.sourceStyle).toBe('gfm-autolink');
- expect(links[1]?.data?.sourceStyle).toBe('gfm-autolink');
- });
-
- test('bare URL inside emphasis tagged correctly', () => {
- const tree = parseMdast('_em https://x.com em_\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).toBe('gfm-autolink');
- });
-
- test('linkReference (shortcut form) NOT tagged as gfm-autolink', () => {
- const tree = parseMdast('[ref][]\n\n[ref]: https://x.com\n');
- const ref = findNode(tree, 'linkReference');
- expect(ref).toBeDefined();
- expect(ref?.data?.sourceStyle).toBeUndefined();
- });
-});
-
-describe('position-slice: inline-link URL form recovery (FR-19)', () => {
- test('`[text](url)` → no sourceUrlForm', () => {
- const tree = parseMdast('[link](https://x.com)\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceUrlForm).toBeUndefined();
- });
-
- test('`[text](<url>)` → sourceUrlForm = "angle-bracketed"', () => {
- const tree = parseMdast('[link](<https://x.com>)\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceUrlForm).toBe('angle-bracketed');
- });
-
- test('`[text](<url with space>)` → sourceUrlForm = "angle-bracketed"', () => {
- const tree = parseMdast('[link](<http://x.com/foo bar>)\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceUrlForm).toBe('angle-bracketed');
- });
-
- test('`[text](<>)` → sourceUrlForm = "angle-bracketed" (empty URL)', () => {
- const tree = parseMdast('[link](<>)\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceUrlForm).toBe('angle-bracketed');
- });
-
- test('GFM bare URL (no brackets) → no sourceUrlForm, sourceStyle=gfm-autolink', () => {
- const tree = parseMdast('visit https://x.com today\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceStyle).toBe('gfm-autolink');
- expect(link?.data?.sourceUrlForm).toBeUndefined();
- });
-
- test('multiple inline links — each tagged independently', () => {
- const tree = parseMdast('See [a](u1) and [b](<u2>) and [c](u3) here.\n');
- const links = findNodes(tree, 'link');
- expect(links.length).toBe(3);
- expect(links[0]?.data?.sourceUrlForm).toBeUndefined();
- expect(links[1]?.data?.sourceUrlForm).toBe('angle-bracketed');
- expect(links[2]?.data?.sourceUrlForm).toBeUndefined();
- });
-});
-
-describe('position-slice: inline-link title marker recovery (FR-20)', () => {
- test('double-quote title → sourceTitleMarker = "double"', () => {
- const tree = parseMdast('[link](https://x.com "title")\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceTitleMarker).toBe('double');
- });
-
- test('single-quote title → sourceTitleMarker = "single"', () => {
- const tree = parseMdast("[link](https://x.com 'title')\n");
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceTitleMarker).toBe('single');
- });
-
- test('paren title → sourceTitleMarker = "paren"', () => {
- const tree = parseMdast('[link](https://x.com (title))\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceTitleMarker).toBe('paren');
- });
-
- test('link without title → sourceTitleMarker undefined', () => {
- const tree = parseMdast('[link](https://x.com)\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceTitleMarker).toBeUndefined();
- });
-
- test('angle-bracketed URL combined with single-quote title', () => {
- const tree = parseMdast("[link](<https://x.com> 'title')\n");
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceUrlForm).toBe('angle-bracketed');
- expect(link?.data?.sourceTitleMarker).toBe('single');
- });
-
- test('angle-bracketed URL combined with paren title', () => {
- const tree = parseMdast('[link](<https://x.com> (title))\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceUrlForm).toBe('angle-bracketed');
- expect(link?.data?.sourceTitleMarker).toBe('paren');
- });
-
- test('whitespace between title and closing `)` does not break detection', () => {
- const tree = parseMdast('[link](https://x.com "title" )\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceTitleMarker).toBe('double');
- });
-
- test('GFM bare URL (no `[...](...)` wrap) → no sourceTitleMarker', () => {
- const tree = parseMdast('visit https://x.com today\n');
- const link = findNode(tree, 'link');
- expect(link?.data?.sourceTitleMarker).toBeUndefined();
- });
-});
-
-describe('position-slice: blockquote marker spacing recovery (FR-23)', () => {
- test('single-space marker → sourceMarkerSpacings=[1]', () => {
- const tree = parseMdast('> foo\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([1]);
- });
-
- test('no-space marker → sourceMarkerSpacings=[0]', () => {
- const tree = parseMdast('>foo\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([0]);
- });
-
- test('multi-line single-space → array of length 2', () => {
- const tree = parseMdast('> line 1\n> line 2\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([1, 1]);
- });
-
- test('multi-line no-space → array of length 2', () => {
- const tree = parseMdast('>line 1\n>line 2\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([0, 0]);
- });
-
- test('mixed single + none → per-line array', () => {
- const tree = parseMdast('> line 1\n>line 2\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([1, 0]);
- });
-
- test('mixed none + single → per-line array', () => {
- const tree = parseMdast('>line 1\n> line 2\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([0, 1]);
- });
-
- test('blank-line `>` continuation EXCLUDED from capture', () => {
- const tree = parseMdast('> p1\n>\n> p2\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([1, 1]);
- });
-
- test('nested blockquote captures outer marker spacing on outer node', () => {
- const tree = parseMdast('> > nested\n');
- const allBqs = findNodes(tree, 'blockquote');
- expect(allBqs.length).toBe(2);
- expect(allBqs[0]?.data?.sourceMarkerSpacings).toEqual([1]);
- expect(allBqs[1]?.data?.sourceMarkerSpacings).toEqual([1]);
- });
-
- test('nested blockquote `>>` → outer none, inner single', () => {
- const tree = parseMdast('>> nested\n');
- const allBqs = findNodes(tree, 'blockquote');
- expect(allBqs.length).toBe(2);
- expect(allBqs[0]?.data?.sourceMarkerSpacings).toEqual([0]);
- expect(allBqs[1]?.data?.sourceMarkerSpacings).toEqual([1]);
- });
-
- test('tab-after-marker treated as single-spacing', () => {
- const tree = parseMdast('>\tfoo\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([1]);
- });
-
- test('1-3 leading spaces of indent before `>` tolerated (CommonMark §5.1)', () => {
- const tree = parseMdast(' > foo\n');
- const bq = findNode(tree, 'blockquote');
- expect(bq?.data?.sourceMarkerSpacings).toEqual([1]);
- });
-});
-
-describe('position-slice: definition source-form recovery (FR-24)', () => {
- test('single-line `[ref]: url` → sourceLayout=inline, no sourceTitleMarker', () => {
- const tree = parseMdast('[ref]: url\n');
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceLayout).toBe('inline');
- expect(def?.data?.sourceTitleMarker).toBeUndefined();
- });
-
- test('single-line with double-quote title → sourceTitleMarker=double', () => {
- const tree = parseMdast('[ref]: url "title"\n');
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceLayout).toBe('inline');
- expect(def?.data?.sourceTitleMarker).toBe('double');
- });
-
- test('single-line with single-quote title → sourceTitleMarker=single', () => {
- const tree = parseMdast("[ref]: url 'title'\n");
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceTitleMarker).toBe('single');
- });
-
- test('single-line with paren title → sourceTitleMarker=paren', () => {
- const tree = parseMdast('[ref]: url (title)\n');
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceTitleMarker).toBe('paren');
- });
-
- test('multi-line `[ref]:\\n url` → sourceLayout=multiline', () => {
- const tree = parseMdast('[ref]:\n url\n');
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceLayout).toBe('multiline');
- expect(def?.data?.sourceTitleMarker).toBeUndefined();
- });
-
- test('multi-line with double-quote title → sourceLayout=multiline + double marker', () => {
- const tree = parseMdast('[ref]:\n url\n "title"\n');
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceLayout).toBe('multiline');
- expect(def?.data?.sourceTitleMarker).toBe('double');
- });
-
- test('multi-line with single-quote title → sourceLayout=multiline + single marker', () => {
- const tree = parseMdast("[ref]:\n url\n 'title'\n");
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceLayout).toBe('multiline');
- expect(def?.data?.sourceTitleMarker).toBe('single');
- });
-
- test('multi-line with paren title → sourceLayout=multiline + paren marker', () => {
- const tree = parseMdast('[ref]:\n url\n (title)\n');
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceLayout).toBe('multiline');
- expect(def?.data?.sourceTitleMarker).toBe('paren');
- });
-
- test('case-mismatch identifier preserved on label (mdast handles this)', () => {
- const tree = parseMdast('[Ref]: url\n');
- const def = findNode<{ data?: Record<string, unknown>; label?: string; identifier?: string }>(
- tree,
- 'definition',
- );
- expect(def?.label).toBe('Ref');
- expect(def?.identifier).toBe('ref');
- });
-
- test('definition with no title does not set sourceTitleMarker', () => {
- const tree = parseMdast('[ref]: url\n');
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceTitleMarker).toBeUndefined();
- });
-
- test('multi-line definition followed by paragraph captures only the def slice', () => {
- const tree = parseMdast('[ref]:\n url\n "title"\n\nP\n');
- const def = findNode(tree, 'definition');
- expect(def?.data?.sourceLayout).toBe('multiline');
- expect(def?.data?.sourceTitleMarker).toBe('double');
- });
-});
-
-describe('splitGfmCellSegments — GFM cell tokenizer edge cases', () => {
- test('splits canonical pipe-surround row into N+2 segments (leading + trailing empties)', () => {
- expect(splitGfmCellSegments('| a | b | c |')).toEqual(['', ' a ', ' b ', ' c ', '']);
- });
-
- test('splits no-leading-pipe row into N+1 segments (trailing empty only)', () => {
- expect(splitGfmCellSegments('a | b | c')).toEqual(['a ', ' b ', ' c']);
- });
-
- test('escaped pipe `\\|` stays inside the current cell segment', () => {
- expect(splitGfmCellSegments('| a \\| b | c |')).toEqual(['', ' a \\| b ', ' c ', '']);
- });
-
- test('multiple escaped pipes in one cell all survive', () => {
- expect(splitGfmCellSegments('| a \\| b \\| c | d |')).toEqual([
- '',
- ' a \\| b \\| c ',
- ' d ',
- '',
- ]);
- });
-
- test('asymmetric padding preserved per-cell — left-only, right-only, both-sides, none', () => {
- expect(splitGfmCellSegments('|left| right|both | none|')).toEqual([
- '',
- 'left',
- ' right',
- 'both ',
- ' none',
- '',
- ]);
- });
-
- test('no-padding cells (no surrounding whitespace) survive verbatim', () => {
- expect(splitGfmCellSegments('|a|b|c|')).toEqual(['', 'a', 'b', 'c', '']);
- });
-
- test('empty cell (`||`) emits an empty-string segment', () => {
- expect(splitGfmCellSegments('|a||c|')).toEqual(['', 'a', '', 'c', '']);
- });
-
- test('whitespace-only cell preserves the whitespace verbatim', () => {
- expect(splitGfmCellSegments('|a| |c|')).toEqual(['', 'a', ' ', 'c', '']);
- });
-
- test('row with no pipes returns a single-element array containing the input', () => {
- expect(splitGfmCellSegments('plain text')).toEqual(['plain text']);
- });
-
- test('lone backslash at end-of-row is not an escape (no following pipe)', () => {
- expect(splitGfmCellSegments('| a \\| b |')).toEqual(['', ' a \\| b ', '']);
- expect(splitGfmCellSegments('| a \\')).toEqual(['', ' a \\']);
- });
-
- test('backslash followed by NON-pipe is not an escape — passes through then re-tokenizes on next pipe', () => {
- expect(splitGfmCellSegments('| a\\b | c |')).toEqual(['', ' a\\b ', ' c ', '']);
- });
-
- test('double-escape `\\\\|` — the backslash escapes the backslash, leaving `|` as a separator', () => {
- expect(splitGfmCellSegments('| a \\\\| b |')).toEqual(['', ' a \\\\| b ', '']);
- });
-
- test('empty input returns [""] — single empty segment', () => {
- expect(splitGfmCellSegments('')).toEqual(['']);
- });
-});
diff --git a/packages/core/src/markdown/processor-cache.test.ts b/packages/core/src/markdown/processor-cache.test.ts
deleted file mode 100644
index 06b80e43f..000000000
--- a/packages/core/src/markdown/processor-cache.test.ts
+++ /dev/null
@@ -1,116 +0,0 @@
-/**
- * R16 processor-cache behavior (spec 2026-04-16 markdown-pipeline-engineering-health).
- *
- * Proves:
- * (a) MarkdownManager holds exactly one parse processor and one serialize
- * processor across its lifetime — construction builds them, subsequent
- * parse/serialize calls reuse them.
- * (b) The two mutating attachers (`remarkMdxAgnostic`, `remarkWikiLink`)
- * do NOT duplicate entries in `data().micromarkExtensions` even under
- * pathological re-attach (e.g., a test harness re-invokes the attacher
- * on an already-populated `data()` map).
- * (c) Parse output is byte-identical across repeated calls on the same
- * MarkdownManager instance (the cached processor is stateless per call).
- *
- * Traces QA-004 (Processor caching does not duplicate micromark extensions
- * on reuse).
- */
-import { describe, expect, test } from 'bun:test';
-import type { Processor } from 'unified';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import { remarkMdxAgnostic } from './remark-mdx-agnostic.ts';
-import { remarkWikiLink } from './wiki-link-micromark.ts';
-
-interface ManagerInternals {
- parseProcessor: Processor;
- serializeProcessor: Processor;
-}
-const internals = (m: MarkdownManager): ManagerInternals => m as unknown as ManagerInternals;
-
-describe('R16 processor caching', () => {
- test('same processor reference survives 100 parse calls', () => {
- const m = new MarkdownManager({ extensions: sharedExtensions });
- const parseRef = internals(m).parseProcessor;
- for (let i = 0; i < 100; i++) {
- m.parse(`# Run ${i}\n\nParagraph ${i}.\n`);
- }
- expect(internals(m).parseProcessor).toBe(parseRef);
- });
-
- test('same serialize processor reference survives 100 serialize calls', () => {
- const m = new MarkdownManager({ extensions: sharedExtensions });
- const serializeRef = internals(m).serializeProcessor;
- const json = m.parse('# Heading\n\nPara.\n');
- for (let i = 0; i < 100; i++) {
- m.serialize(json);
- }
- expect(internals(m).serializeProcessor).toBe(serializeRef);
- });
-
- test('parse output is byte-identical across 100 calls', () => {
- const m = new MarkdownManager({ extensions: sharedExtensions });
- const src = '# H\n\n**bold** and *em* and `code`.\n\n- a\n- b\n';
- const first = JSON.stringify(m.parse(src));
- for (let i = 0; i < 100; i++) {
- expect(JSON.stringify(m.parse(src))).toBe(first);
- }
- });
-
- test('micromark extensions array does not grow on re-attach — remarkMdxAgnostic', () => {
- const fakeProcessor = {
- _data: {
- micromarkExtensions: [] as unknown[],
- fromMarkdownExtensions: [] as unknown[][],
- toMarkdownExtensions: [] as unknown[],
- },
- data(): ManagerInternals['parseProcessor'] extends Processor
- ? Record<string, unknown>
- : Record<string, unknown> {
- return this._data as unknown as Record<string, unknown>;
- },
- };
- for (let i = 0; i < 10; i++) {
- remarkMdxAgnostic.call(fakeProcessor as unknown as Processor);
- }
- expect(fakeProcessor._data.micromarkExtensions.length).toBe(1);
- expect(fakeProcessor._data.fromMarkdownExtensions.length).toBe(1);
- expect(fakeProcessor._data.toMarkdownExtensions.length).toBe(1);
- });
-
- test('micromark extensions array does not grow on re-attach — remarkWikiLink', () => {
- const fakeProcessor = {
- _data: {
- micromarkExtensions: [] as unknown[],
- fromMarkdownExtensions: [] as unknown[][],
- toMarkdownExtensions: [] as unknown[],
- },
- data(): Record<string, unknown> {
- return this._data as unknown as Record<string, unknown>;
- },
- };
- for (let i = 0; i < 10; i++) {
- remarkWikiLink.call(fakeProcessor as unknown as Processor);
- }
- expect(fakeProcessor._data.micromarkExtensions.length).toBe(1);
- expect(fakeProcessor._data.fromMarkdownExtensions.length).toBe(1);
- expect(fakeProcessor._data.toMarkdownExtensions.length).toBe(1);
- });
-
- test('roundtrip identity on representative fixtures after repeated reuse', () => {
- const m = new MarkdownManager({ extensions: sharedExtensions });
- const cases = [
- '# Heading\n\nText with **bold** and *em*.\n',
- '- item one\n- item two\n',
- '```ts\nconst x = 1;\n```\n',
- '[[WikiLink]] and [inline](http://e.com).\n',
- '| a | b |\n| - | - |\n| 1 | 2 |\n',
- ];
- const firstResults = cases.map((src) => m.serialize(m.parse(src)));
- for (let i = 0; i < 20; i++) {
- for (let c = 0; c < cases.length; c++) {
- expect(m.serialize(m.parse(cases[c]))).toBe(firstResults[c]);
- }
- }
- });
-});
diff --git a/packages/core/src/markdown/promoted-mdast-coverage.test.ts b/packages/core/src/markdown/promoted-mdast-coverage.test.ts
deleted file mode 100644
index 41812d682..000000000
--- a/packages/core/src/markdown/promoted-mdast-coverage.test.ts
+++ /dev/null
@@ -1,288 +0,0 @@
-import { afterAll, describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import type { Parents } from 'mdast';
-import type { State } from 'mdast-util-to-markdown';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { resetParseHealth } from '../metrics/parse-health.ts';
-import { MarkdownManager } from './index.ts';
-import { PROMOTED_MDAST_TYPES, type PromotedMdastType } from './mdast-augmentation.ts';
-import { customNodeHandlers } from './mdast-to-hast-handlers.ts';
-import { tagToMarkdown } from './tag-to-markdown.ts';
-import { toMarkdownHandlers } from './to-markdown-handlers.ts';
-import { wikiLinkToMarkdown } from './wiki-link-micromark.ts';
-
-afterAll(() => {
- resetParseHealth();
-});
-
-// biome-ignore lint/suspicious/noExplicitAny: handler tables accept loose shape at runtime; strict types would require enumerating every mdast type
-type AnyHandlerMap = Record<string, any>;
-
-function toMarkdownHasHandler(type: PromotedMdastType): boolean {
- if ((toMarkdownHandlers as AnyHandlerMap)[type]) return true;
- if (type === 'wikiLink' && wikiLinkToMarkdown.handlers.wikiLink) return true;
- if (type === 'wikiLinkEmbed' && wikiLinkToMarkdown.handlers.wikiLinkEmbed) return true;
- if (type === 'tag' && tagToMarkdown.handlers.tag) return true;
- if (type === 'footnoteReference' || type === 'footnoteDefinition') return true;
- return false;
-}
-
-function toHastHasHandler(type: PromotedMdastType): boolean {
- return (customNodeHandlers as AnyHandlerMap)[type] != null;
-}
-
-type ParseFixture = { md: string; expectedPmType: string } | { md: string; expectedPmMark: string };
-
-const parseFixtures: Record<PromotedMdastType, ParseFixture> = {
- wikiLink: { md: '[[TargetPage]]', expectedPmType: 'wikiLink' },
- wikiLinkEmbed: { md: '![[photo.png]]', expectedPmType: 'jsxComponent' },
- mdxJsxFlowElement: { md: '<MyComponent/>', expectedPmType: 'jsxComponent' },
- mdxJsxTextElement: { md: 'hello <Inline/> world', expectedPmType: 'jsxInline' },
- rawMdxFallback: {
- md: '<Foo>abc</Bar>',
- expectedPmType: 'rawMdxFallback',
- },
- mark: { md: '==hello==', expectedPmMark: 'highlight' },
- tag: { md: 'See #word now.', expectedPmType: 'tag' },
- comment: { md: '%%hello%%', expectedPmMark: 'comment' },
- commentBlock: { md: '%%\nhello\n%%', expectedPmType: 'commentBlock' },
- footnoteReference: {
- md: 'Hi[^1].\n\n[^1]: Body.',
- expectedPmType: 'footnoteReference',
- },
- footnoteDefinition: {
- md: 'Hi[^1].\n\n[^1]: Body.',
- expectedPmType: 'footnoteDefinition',
- },
-};
-
-function findPmNode(json: JSONContent, type: string): boolean {
- if (json.type === type) return true;
- for (const child of json.content ?? []) {
- if (findPmNode(child, type)) return true;
- }
- return false;
-}
-
-function findPmMark(json: JSONContent, markType: string): boolean {
- if ((json.marks ?? []).some((m) => m.type === markType)) return true;
- for (const child of json.content ?? []) {
- if (findPmMark(child, markType)) return true;
- }
- return false;
-}
-
-function parsePathCoverage(type: PromotedMdastType): boolean {
- const mgr = new MarkdownManager({ extensions: sharedExtensions });
- const fixture = parseFixtures[type];
- let json: JSONContent;
- try {
- json = mgr.parseWithFallback(fixture.md);
- } catch {
- return false;
- }
- if ('expectedPmType' in fixture) {
- return findPmNode(json, fixture.expectedPmType);
- }
- return findPmMark(json, fixture.expectedPmMark);
-}
-
-describe('PROMOTED_MDAST_TYPES — three-edge handler parity', () => {
- test('every promoted type has a to-hast handler (static enforcement also via Record type)', () => {
- for (const type of PROMOTED_MDAST_TYPES) {
- expect(toHastHasHandler(type)).toBe(true);
- }
- });
-
- test('every promoted type has a to-markdown handler', () => {
- for (const type of PROMOTED_MDAST_TYPES) {
- expect(toMarkdownHasHandler(type)).toBe(true);
- }
- });
-
- test('every promoted type has a parse-side PM handler (via MarkdownManager)', () => {
- const failures: PromotedMdastType[] = [];
- for (const type of PROMOTED_MDAST_TYPES) {
- if (!parsePathCoverage(type)) failures.push(type);
- }
- expect(failures).toEqual([]);
- });
-
- test('adding a new promoted type without updating customNodeHandlers fails TypeScript', () => {
- const hastKeys = Object.keys(customNodeHandlers).sort();
- const expectedKeys = [...PROMOTED_MDAST_TYPES].sort();
- for (const k of expectedKeys) {
- expect(hastKeys).toContain(k);
- }
- });
-
- test('smoke test: each promoted type produces a non-trivial hast shape', () => {
- const fakeState = {
- patch: () => {},
- applyData: <T>(_node: unknown, result: T) => result,
- all: () => [] as unknown[],
- };
-
- const fixtures: Record<PromotedMdastType, unknown> = {
- wikiLink: {
- type: 'wikiLink',
- value: 'Label',
- data: { target: 'Page', anchor: null, alias: null },
- children: [{ type: 'text', value: 'Label' }],
- },
- wikiLinkEmbed: {
- type: 'wikiLinkEmbed',
- value: 'photo.png',
- data: { target: 'photo.png', anchor: null, alias: null },
- children: [{ type: 'text', value: 'photo.png' }],
- },
- mdxJsxFlowElement: {
- type: 'mdxJsxFlowElement',
- name: 'X',
- attributes: [],
- children: [],
- data: { sourceRaw: '<X/>' },
- },
- mdxJsxTextElement: {
- type: 'mdxJsxTextElement',
- name: 'X',
- attributes: [],
- children: [],
- data: { sourceRaw: '<X/>' },
- },
- rawMdxFallback: {
- type: 'rawMdxFallback',
- value: '<Unclosed',
- data: { reason: 'test', originalSpan: { start: 0, end: 9 } },
- },
- mark: {
- type: 'mark',
- children: [{ type: 'text', value: 'hi' }],
- },
- tag: { type: 'tag', value: 'foo' },
- comment: {
- type: 'comment',
- children: [{ type: 'text', value: 'hi' }],
- data: { sourceForm: 'percent' },
- },
- commentBlock: {
- type: 'commentBlock',
- children: [{ type: 'paragraph', children: [{ type: 'text', value: 'hi' }] }],
- data: { sourceForm: 'percent' },
- },
- footnoteReference: {
- type: 'footnoteReference',
- identifier: '1',
- label: '1',
- },
- footnoteDefinition: {
- type: 'footnoteDefinition',
- identifier: '1',
- label: '1',
- children: [{ type: 'paragraph', children: [{ type: 'text', value: 'body' }] }],
- },
- };
-
- for (const type of PROMOTED_MDAST_TYPES) {
- const handler = (customNodeHandlers as AnyHandlerMap)[type];
- const result = handler(fakeState, fixtures[type] as Parents);
- expect(result).toBeDefined();
- expect(result).not.toBe(null);
- }
- });
-
- test('smoke test: each promoted type produces a non-empty markdown string', () => {
- const minimalState = {
- enter: () => () => {},
- containerPhrasing: () => '',
- containerFlow: () => '',
- createTracker: () => ({
- move: (s: string) => s,
- current: () => ({}),
- }),
- options: {},
- unsafe: [] as Array<{ character: string }>,
- safe: (s: string) => s,
- } as unknown as State;
-
- const fixtures: Record<PromotedMdastType, unknown> = {
- wikiLink: {
- type: 'wikiLink',
- value: 'Page',
- data: { target: 'Page', anchor: null, alias: null },
- children: [{ type: 'text', value: 'Page' }],
- },
- wikiLinkEmbed: {
- type: 'wikiLinkEmbed',
- value: 'photo.png',
- data: { target: 'photo.png', anchor: null, alias: null },
- children: [{ type: 'text', value: 'photo.png' }],
- },
- mdxJsxFlowElement: {
- type: 'mdxJsxFlowElement',
- name: 'X',
- attributes: [],
- children: [],
- data: { sourceRaw: '<X/>' },
- },
- mdxJsxTextElement: {
- type: 'mdxJsxTextElement',
- name: 'X',
- attributes: [],
- children: [],
- data: { sourceRaw: '<X/>' },
- },
- rawMdxFallback: {
- type: 'rawMdxFallback',
- value: '<Unclosed',
- data: { reason: 'test', originalSpan: { start: 0, end: 9 } },
- },
- mark: {
- type: 'mark',
- children: [{ type: 'text', value: 'hi' }],
- },
- tag: { type: 'tag', value: 'foo' },
- comment: {
- type: 'comment',
- children: [{ type: 'text', value: 'hi' }],
- data: { sourceForm: 'percent' },
- },
- commentBlock: {
- type: 'commentBlock',
- children: [{ type: 'paragraph', children: [{ type: 'text', value: 'hi' }] }],
- data: { sourceForm: 'percent' },
- },
- footnoteReference: {
- type: 'footnoteReference',
- identifier: '1',
- label: '1',
- },
- footnoteDefinition: {
- type: 'footnoteDefinition',
- identifier: '1',
- label: '1',
- children: [{ type: 'paragraph', children: [{ type: 'text', value: 'body' }] }],
- },
- };
-
- for (const type of PROMOTED_MDAST_TYPES) {
- if (type === 'footnoteReference' || type === 'footnoteDefinition') continue;
-
- let handler: unknown;
- if (type === 'wikiLink') {
- handler = wikiLinkToMarkdown.handlers.wikiLink;
- } else if (type === 'wikiLinkEmbed') {
- handler = wikiLinkToMarkdown.handlers.wikiLinkEmbed;
- } else if (type === 'tag') {
- handler = tagToMarkdown.handlers.tag;
- } else {
- handler = (toMarkdownHandlers as AnyHandlerMap)[type];
- }
- expect(handler).toBeDefined();
- // biome-ignore lint/suspicious/noExplicitAny: minimal smoke invocation
- const out = (handler as any)(fixtures[type], undefined, minimalState, {});
- expect(typeof out).toBe('string');
- expect((out as string).length).toBeGreaterThan(0);
- }
- });
-});
diff --git a/packages/core/src/markdown/pua-codepoint-uniqueness.test.ts b/packages/core/src/markdown/pua-codepoint-uniqueness.test.ts
deleted file mode 100644
index 099cbc436..000000000
--- a/packages/core/src/markdown/pua-codepoint-uniqueness.test.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { readdirSync, readFileSync, statSync } from 'node:fs';
-import { dirname, join } from 'node:path';
-import { fileURLToPath } from 'node:url';
-
-const __dirname = dirname(fileURLToPath(import.meta.url));
-
-const PUA_LITERAL_RE = /\\u(E[01][0-9A-Fa-f]{2})/g;
-
-function listMarkdownSourceFiles(dir: string): string[] {
- return readdirSync(dir)
- .filter((name) => name.endsWith('.ts'))
- .filter((name) => !name.endsWith('.test.ts'))
- .map((name) => join(dir, name))
- .filter((path) => statSync(path).isFile());
-}
-
-function extractPuaCodepoints(source: string): Set<string> {
- const found = new Set<string>();
- let match: RegExpExecArray | null = PUA_LITERAL_RE.exec(source);
- while (match !== null) {
- if (match[1]) found.add(match[1].toUpperCase());
- match = PUA_LITERAL_RE.exec(source);
- }
- PUA_LITERAL_RE.lastIndex = 0;
- return found;
-}
-
-describe('PUA codepoint uniqueness across markdown guard files', () => {
- test('no codepoint in U+E000–U+E1FF is declared by more than one source file', () => {
- const files = listMarkdownSourceFiles(__dirname);
- expect(files.length).toBeGreaterThan(0);
-
- const ownerByCodepoint = new Map<string, string[]>();
- for (const path of files) {
- const source = readFileSync(path, 'utf8');
- const codepoints = extractPuaCodepoints(source);
- const fileBasename = path.slice(__dirname.length + 1);
- for (const cp of codepoints) {
- const existing = ownerByCodepoint.get(cp) ?? [];
- existing.push(fileBasename);
- ownerByCodepoint.set(cp, existing);
- }
- }
-
- const collisions: Array<{ codepoint: string; files: string[] }> = [];
- for (const [cp, owners] of ownerByCodepoint) {
- if (owners.length > 1) {
- collisions.push({ codepoint: `U+${cp}`, files: owners });
- }
- }
-
- if (collisions.length > 0) {
- const detail = collisions
- .map((c) => ` ${c.codepoint} declared in: ${c.files.join(', ')}`)
- .join('\n');
- throw new Error(
- `PUA codepoint collision detected across guard files:\n${detail}\n\n` +
- `Each guard layer (R23 autolink, FR-2 entity-ref, FR-14 backslash-escape) reserves its own slab of the U+E000–U+E1FF range. A new guard must use a codepoint not declared by any other guard.\n` +
- "Resolution: pick a codepoint not currently owned, update the new guard's JSDoc, and update the cross-references in the existing guards' JSDoc.",
- );
- }
-
- expect(ownerByCodepoint.size).toBeGreaterThan(0);
- });
-});
diff --git a/packages/core/src/markdown/raw-mdx-fallback-mdast-promotion.test.ts b/packages/core/src/markdown/raw-mdx-fallback-mdast-promotion.test.ts
deleted file mode 100644
index b24daa4cf..000000000
--- a/packages/core/src/markdown/raw-mdx-fallback-mdast-promotion.test.ts
+++ /dev/null
@@ -1,99 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { fromProseMirror } from '@handlewithcare/remark-prosemirror';
-import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';
-import type { JSONContent } from '@tiptap/core';
-import { getSchema } from '@tiptap/core';
-import type { Root } from 'mdast';
-import type { RawMdxFallbackMdast } from './mdast-augmentation.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-type Handlers = Parameters<typeof fromProseMirror>[1];
-type Managerish = {
- pmNodeHandlers: NonNullable<Handlers>['nodeHandlers'];
- pmMarkHandlers: NonNullable<Handlers>['markHandlers'];
-};
-
-function pmToMdast(json: JSONContent): Root {
- const schema = getSchema(sharedExtensions);
- const pmNode = schema.nodeFromJSON(json);
- const internal = mdManager as unknown as Managerish;
- return fromProseMirror(pmNode, {
- schema,
- nodeHandlers: internal.pmNodeHandlers,
- markHandlers: internal.pmMarkHandlers,
- }) as Root;
-}
-
-function docWithFallback(rawSource: string, reason: string): JSONContent {
- return {
- type: 'doc',
- content: [
- {
- type: 'paragraph',
- content: [{ type: 'text', text: 'before' }],
- },
- {
- type: 'rawMdxFallback',
- attrs: {
- reason,
- originalSpan: { start: 7, end: 7 + rawSource.length },
- },
- content: rawSource ? [{ type: 'text', text: rawSource }] : [],
- },
- {
- type: 'paragraph',
- content: [{ type: 'text', text: 'after' }],
- },
- ],
- };
-}
-
-describe('rawMdxFallback mdast promotion (US-006 / D7)', () => {
- test('PM→mdast emits first-class rawMdxFallback (not html passthrough)', () => {
- const doc = docWithFallback('<Outer>\n text\n</Inner>', 'Mismatched tag');
- const tree = pmToMdast(doc);
- const fallback = tree.children.find((c) => c.type === 'rawMdxFallback') as
- | RawMdxFallbackMdast
- | undefined;
- expect(fallback).toBeDefined();
- if (!fallback) throw new Error('unreachable');
- expect(fallback.value).toBe('<Outer>\n text\n</Inner>');
- expect(fallback.data.reason).toBe('Mismatched tag');
- expect(fallback.data.originalSpan).toEqual({
- start: 7,
- end: 7 + '<Outer>\n text\n</Inner>'.length,
- });
- });
-
- test('serialize emits rawMdxFallback value verbatim', () => {
- const raw = '<A prop="b">\n</C>';
- const doc = docWithFallback(raw, 'tag mismatch');
- const out = mdManager.serialize(doc);
- expect(out).toContain(raw);
- expect(out).toContain('before');
- expect(out).toContain('after');
- });
-
- test('empty fallback content serializes cleanly', () => {
- const doc = docWithFallback('', 'empty broken block');
- expect(() => mdManager.serialize(doc)).not.toThrow();
- });
-
- test('existing R6 parse-with-fallback path continues to work', async () => {
- const { parseWithFallback } = await import('./parse-with-fallback.ts');
- const md = 'p1\n\n<Outer>\n x\n</Inner>\n\np2\n';
- const doc = parseWithFallback(md, { parse: (s) => mdManager.parse(s) });
- type Visitable = JSONContent & { content?: Visitable[]; type?: string };
- function findType(node: Visitable, type: string): Visitable | undefined {
- if (node.type === type) return node;
- for (const child of node.content ?? []) {
- const hit = findType(child as Visitable, type);
- if (hit) return hit;
- }
- return undefined;
- }
- const fb = findType(doc as Visitable, 'rawMdxFallback');
- expect(fb).toBeDefined();
- });
-});
diff --git a/packages/core/src/markdown/ref-def-hoist.test.ts b/packages/core/src/markdown/ref-def-hoist.test.ts
deleted file mode 100644
index 926c04476..000000000
--- a/packages/core/src/markdown/ref-def-hoist.test.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { hoistRefDefs } from './ref-def-hoist.ts';
-
-describe('hoistRefDefs (R11)', () => {
- test('extracts top-level ref-def', () => {
- const src = '[foo]: https://example.com\n\nSome text\n';
- expect(hoistRefDefs(src)).toBe('[foo]: https://example.com\n\n');
- });
-
- test('extracts ref-def with title (double quotes)', () => {
- const src = '[bar]: https://example.com "A title"\n';
- expect(hoistRefDefs(src)).toBe('[bar]: https://example.com "A title"\n\n');
- });
-
- test('extracts ref-def with title (single quotes)', () => {
- const src = "[baz]: https://example.com 'A title'\n";
- expect(hoistRefDefs(src)).toBe("[baz]: https://example.com 'A title'\n\n");
- });
-
- test('extracts ref-def with title (parens)', () => {
- const src = '[qux]: https://example.com (A title)\n';
- expect(hoistRefDefs(src)).toBe('[qux]: https://example.com (A title)\n\n');
- });
-
- test('extracts multiple ref-defs preserving source order', () => {
- const src = '[a]: url1\n[b]: url2\n\ntext\n';
- expect(hoistRefDefs(src)).toBe('[a]: url1\n[b]: url2\n\n');
- });
-
- test('does NOT extract ref-def inside fenced code block', () => {
- const src = '```\n[foo]: https://example.com\n```\n';
- expect(hoistRefDefs(src)).toBe('');
- });
-
- test('does NOT extract ref-def inside tilde fence', () => {
- const src = '~~~\n[foo]: https://example.com\n~~~\n';
- expect(hoistRefDefs(src)).toBe('');
- });
-
- test('extracts ref-def outside fence but not inside', () => {
- const src = '[outside]: url1\n\n```\n[inside]: url2\n```\n';
- expect(hoistRefDefs(src)).toBe('[outside]: url1\n\n');
- });
-
- test('returns empty string when no ref-defs', () => {
- expect(hoistRefDefs('Just some text\n')).toBe('');
- });
-
- test('returns empty string for empty input', () => {
- expect(hoistRefDefs('')).toBe('');
- });
-
- test('handles ref-def with surrounding blank lines', () => {
- const src = '\n\n[foo]: url\n\n\n';
- expect(hoistRefDefs(src)).toBe('[foo]: url\n\n');
- });
-});
diff --git a/packages/core/src/markdown/roundtrip-boundary-space-overescape.test.ts b/packages/core/src/markdown/roundtrip-boundary-space-overescape.test.ts
deleted file mode 100644
index 55ba420b3..000000000
--- a/packages/core/src/markdown/roundtrip-boundary-space-overescape.test.ts
+++ /dev/null
@@ -1,149 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-
-const NBSP = '\u00A0';
-const SPACE = ' ';
-const TAB = '\t';
-
-const strong = (t: string): JSONContent => ({
- type: 'text',
- marks: [{ type: 'strong', attrs: { sourceDelimiter: '**' } }],
- text: t,
-});
-const text = (t: string): JSONContent => ({ type: 'text', text: t });
-const paragraph = (...inline: JSONContent[]): JSONContent => ({
- type: 'doc',
- content: [{ type: 'paragraph', content: inline }],
-});
-const listItem = (...inline: JSONContent[]): JSONContent => ({
- type: 'doc',
- content: [
- {
- type: 'list',
- attrs: {
- ordered: false,
- start: 1,
- spread: false,
- bulletMarker: '-',
- listMarkerDelimiter: null,
- },
- content: [
- {
- type: 'listItem',
- attrs: { checked: null, spread: false },
- content: [{ type: 'paragraph', content: inline }],
- },
- ],
- },
- ],
-});
-
-const expectNoBoundaryOverEscape = (out: string): void => {
- expect(out).not.toContain('\\&#x20;');
- expect(out).not.toContain('\\&#x9;');
- expect(out).not.toMatch(/\\&#x?[0-9A-Fa-f]/);
-};
-
-describe('boundary whitespace must not over-escape — source NBSP round-trip (path i)', () => {
- const trailing: ReadonlyArray<readonly [string, string]> = [
- ['paragraph', `abc${NBSP}\n`],
- ['emphasis', `*a*${NBSP}\n`],
- ['strong', `**a**${NBSP}\n`],
- ['inline code', `\`a\`${NBSP}\n`],
- ['link', `[a](b)${NBSP}\n`],
- ['strikethrough', `~~a~~${NBSP}\n`],
- ['wiki-link', `[[a]]${NBSP}\n`],
- ['blockquote', `> a${NBSP}\n`],
- ['nested list item', `- x\n - y${NBSP}\n`],
- ];
- for (const [label, src] of trailing) {
- test(`trailing NBSP after ${label}`, () => {
- expectNoBoundaryOverEscape(md.serialize(md.parse(src)));
- });
- }
-
- const leading: ReadonlyArray<readonly [string, string]> = [
- ['paragraph', `${NBSP}abc\n`],
- ['emphasis', `${NBSP}*a*\n`],
- ['strong', `${NBSP}**a**\n`],
- ['inline code', `${NBSP}\`a\`\n`],
- ['link', `${NBSP}[a](b)\n`],
- ['strikethrough', `${NBSP}~~a~~\n`],
- ['wiki-link', `${NBSP}[[a]]\n`],
- ['blockquote', `> ${NBSP}a\n`],
- ];
- for (const [label, src] of leading) {
- test(`leading NBSP before ${label}`, () => {
- expectNoBoundaryOverEscape(md.serialize(md.parse(src)));
- });
- }
-});
-
-describe('boundary whitespace must not over-escape — PM-doc text node (path ii)', () => {
- test('trailing regular space after strong', () => {
- expectNoBoundaryOverEscape(md.serialize(paragraph(strong('work.'), text(SPACE))));
- });
- test('leading regular space before strong', () => {
- expectNoBoundaryOverEscape(md.serialize(paragraph(text(SPACE), strong('bold'))));
- });
- test('trailing regular space after plain text', () => {
- expectNoBoundaryOverEscape(md.serialize(paragraph(text('abc'), text(SPACE))));
- });
- test('trailing regular space in list item', () => {
- expectNoBoundaryOverEscape(md.serialize(listItem(strong('x'), text(SPACE))));
- });
-
- test('trailing tab after strong', () => {
- expectNoBoundaryOverEscape(md.serialize(paragraph(strong('work.'), text(TAB))));
- });
- test('leading tab before strong', () => {
- expectNoBoundaryOverEscape(md.serialize(paragraph(text(TAB), strong('bold'))));
- });
- test('trailing tab in list item', () => {
- expectNoBoundaryOverEscape(md.serialize(listItem(strong('x'), text(TAB))));
- });
-});
-
-describe('guard: user-authored entities round-trip byte-identically (no over-correction)', () => {
- test('bare numeric entity', () => {
- expect(md.serialize(md.parse('a&#x41;b\n'))).toBe('a&#x41;b\n');
- });
- test('bare whitespace numeric entity (hex space)', () => {
- expect(md.serialize(md.parse('a&#x20;b\n'))).toBe('a&#x20;b\n');
- });
- test('bare whitespace numeric entity (decimal space)', () => {
- expect(md.serialize(md.parse('a&#32;b\n'))).toBe('a&#32;b\n');
- });
- test('bare whitespace numeric entity (hex tab)', () => {
- expect(md.serialize(md.parse('a&#x9;b\n'))).toBe('a&#x9;b\n');
- });
- test('bare named entity', () => {
- expect(md.serialize(md.parse('a&amp;b\n'))).toBe('a&amp;b\n');
- });
- test('escaped named entity', () => {
- expect(md.serialize(md.parse('a\\&ouml;b\n'))).toBe('a\\&ouml;b\n');
- });
- test('escaped non-whitespace numeric entity (hex)', () => {
- expect(md.serialize(md.parse('a\\&#x41;b\n'))).toBe('a\\&#x41;b\n');
- });
- test('escaped non-whitespace numeric entity (decimal)', () => {
- expect(md.serialize(md.parse('a\\&#65;b\n'))).toBe('a\\&#65;b\n');
- });
- test('user-authored escaped numeric entity (byte-identical to the corruption)', () => {
- expect(md.serialize(md.parse('Entity \\&#x20; stays.\n'))).toBe('Entity \\&#x20; stays.\n');
- });
-});
-
-describe('guard: heading / table asymmetry — a fix must not start encoding them', () => {
- test('ATX heading with trailing NBSP', () => {
- expect(md.serialize(md.parse(`# h${NBSP}\n`))).not.toContain('\\&#x20;');
- });
- test('table cell with trailing NBSP', () => {
- const tbl = `| a${NBSP} | b |\n| --- | --- |\n| c | d |\n`;
- expect(md.serialize(md.parse(tbl))).not.toContain('\\&#x20;');
- });
-});
diff --git a/packages/core/src/markdown/safe-url.test.ts b/packages/core/src/markdown/safe-url.test.ts
deleted file mode 100644
index 04fe0858f..000000000
--- a/packages/core/src/markdown/safe-url.test.ts
+++ /dev/null
@@ -1,174 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { isRelativeUrl, isSafeUrl, SAFE_URL_SCHEME_RE, SAFE_URL_SCHEMES } from './safe-url.ts';
-
-describe('SAFE_URL_SCHEMES — canonical scheme array', () => {
- test('contains the documented allowlist and nothing else', () => {
- expect([...SAFE_URL_SCHEMES]).toEqual(['https', 'http', 'mailto', 'tel', 'ftp', 'sms']);
- });
-
- test('schemes are stored without a trailing colon', () => {
- for (const scheme of SAFE_URL_SCHEMES) {
- expect(scheme).not.toContain(':');
- }
- });
-});
-
-describe('SAFE_URL_SCHEME_RE — derived regex form', () => {
- test('matches every scheme in the canonical array (with colon)', () => {
- for (const scheme of SAFE_URL_SCHEMES) {
- expect(SAFE_URL_SCHEME_RE.test(`${scheme}:foo`)).toBe(true);
- }
- });
-
- test('matches relative URL prefixes', () => {
- expect(SAFE_URL_SCHEME_RE.test('/abs/path')).toBe(true);
- expect(SAFE_URL_SCHEME_RE.test('#fragment')).toBe(true);
- expect(SAFE_URL_SCHEME_RE.test('?query=1')).toBe(true);
- expect(SAFE_URL_SCHEME_RE.test('./sibling')).toBe(true);
- expect(SAFE_URL_SCHEME_RE.test('../parent')).toBe(true);
- });
-
- test('rejects dangerous schemes', () => {
- expect(SAFE_URL_SCHEME_RE.test('javascript:alert(1)')).toBe(false);
- expect(SAFE_URL_SCHEME_RE.test('vbscript:msgbox(1)')).toBe(false);
- expect(SAFE_URL_SCHEME_RE.test('data:text/html,<script>')).toBe(false);
- expect(SAFE_URL_SCHEME_RE.test('data:image/png;base64,iVBOR')).toBe(false); // intentional — data: in any flavor
- expect(SAFE_URL_SCHEME_RE.test('file:///etc/passwd')).toBe(false);
- expect(SAFE_URL_SCHEME_RE.test('chrome-extension://abc')).toBe(false);
- expect(SAFE_URL_SCHEME_RE.test('moz-extension://abc')).toBe(false);
- });
-
- test('matches case-insensitively (browsers normalize scheme to lowercase per WHATWG URL §3.1)', () => {
- expect(SAFE_URL_SCHEME_RE.test('HTTPS://example.com')).toBe(true);
- expect(SAFE_URL_SCHEME_RE.test('Https://example.com')).toBe(true);
- expect(SAFE_URL_SCHEME_RE.test('JaVaScRiPt:alert(1)')).toBe(false);
- });
-
- test('is head-anchored — does not match URLs embedded in text', () => {
- expect(SAFE_URL_SCHEME_RE.test('Click https://example.com')).toBe(false);
- expect(SAFE_URL_SCHEME_RE.test('See javascript:alert(1)')).toBe(false);
- });
-});
-
-describe('isSafeUrl — public allowlist classifier', () => {
- test('treats empty / whitespace-only input as benign', () => {
- expect(isSafeUrl('')).toBe(true);
- expect(isSafeUrl(' ')).toBe(true);
- expect(isSafeUrl('\t\n')).toBe(true);
- });
-
- test('accepts every scheme in SAFE_URL_SCHEMES', () => {
- expect(isSafeUrl('https://example.com')).toBe(true);
- expect(isSafeUrl('http://example.com')).toBe(true);
- expect(isSafeUrl('mailto:[email protected]')).toBe(true);
- expect(isSafeUrl('tel:+15551234567')).toBe(true);
- expect(isSafeUrl('ftp://files.example.com')).toBe(true);
- expect(isSafeUrl('sms:+15551234567?body=hi')).toBe(true);
- });
-
- test('accepts relative URL forms', () => {
- expect(isSafeUrl('/abs')).toBe(true);
- expect(isSafeUrl('./sibling')).toBe(true);
- expect(isSafeUrl('../parent')).toBe(true);
- expect(isSafeUrl('#anchor')).toBe(true);
- expect(isSafeUrl('?q=1')).toBe(true);
- });
-
- test('rejects dangerous schemes', () => {
- expect(isSafeUrl('javascript:alert(1)')).toBe(false);
- expect(isSafeUrl('vbscript:msgbox(1)')).toBe(false);
- expect(isSafeUrl('data:text/html,<script>')).toBe(false);
- expect(isSafeUrl('file:///etc/passwd')).toBe(false);
- });
-
- test('rejects novel/unknown schemes (fail-closed)', () => {
- expect(isSafeUrl('intent://foo')).toBe(false);
- expect(isSafeUrl('blob:https://x')).toBe(false);
- expect(isSafeUrl('view-source:https://x')).toBe(false);
- expect(isSafeUrl('chrome-extension://abc')).toBe(false);
- expect(isSafeUrl('moz-extension://abc')).toBe(false);
- });
-
- test('trims leading whitespace before classifying (WHATWG URL §4)', () => {
- expect(isSafeUrl(' javascript:alert(1)')).toBe(false);
- expect(isSafeUrl('\tjavascript:alert(1)')).toBe(false);
- expect(isSafeUrl(' https://example.com')).toBe(true);
- });
-
- test('rejects bare relative paths without leading delimiter (mdast scope)', () => {
- expect(isSafeUrl('one.png')).toBe(false);
- expect(isSafeUrl('path/file.jpg')).toBe(false);
- });
-});
-
-describe('isRelativeUrl — relative-URL detection (clipboard walker + sanitize-url shared helper)', () => {
- test('treats empty / whitespace-only input as relative', () => {
- expect(isRelativeUrl('')).toBe(true);
- expect(isRelativeUrl(' ')).toBe(true);
- });
-
- test('returns true when no colon present (bare relative paths)', () => {
- expect(isRelativeUrl('one.png')).toBe(true);
- expect(isRelativeUrl('path/file.jpg')).toBe(true);
- expect(isRelativeUrl('relative-doc')).toBe(true);
- });
-
- test('returns true when path delimiter precedes the colon', () => {
- expect(isRelativeUrl('/path/with:colon')).toBe(true);
- expect(isRelativeUrl('?query=a:b')).toBe(true);
- expect(isRelativeUrl('#section:1')).toBe(true);
- expect(isRelativeUrl('./sib:1')).toBe(true);
- });
-
- test('returns false when colon precedes any path delimiter (looks like a scheme)', () => {
- expect(isRelativeUrl('https://example.com')).toBe(false);
- expect(isRelativeUrl('javascript:alert(1)')).toBe(false);
- expect(isRelativeUrl('mailto:user@host')).toBe(false);
- expect(isRelativeUrl('intent:foo')).toBe(false);
- });
-
- test('combined with isSafeUrl forms the walker / sanitize-url full safety check', () => {
- const cases: Array<[string, boolean]> = [
- ['https://safe.example', true],
- ['mailto:foo@bar', true],
- ['/relative/path.png', true],
- ['one.png', true], // relative — no colon
- ['#anchor', true],
- ['javascript:alert(1)', false],
- ['data:text/html,<script>', false],
- ['vbscript:msgbox(1)', false],
- ['file:///etc/passwd', false],
- ['intent://maps', false], // novel scheme with authority — fail-closed at scheme check, fail-closed at relative check
- ];
- for (const [url, expected] of cases) {
- const result = isSafeUrl(url) || isRelativeUrl(url);
- expect(result, url).toBe(expected);
- }
- });
-});
-
-describe('drift parity invariant — sanitize-url.ts derivation', () => {
- test('SAFE_URL_SCHEMES is the source for both the regex and the Set', () => {
- const setForm = new Set(SAFE_URL_SCHEMES.map((s) => `${s}:`));
- expect(setForm.has('https:')).toBe(true);
- expect(setForm.has('http:')).toBe(true);
- expect(setForm.has('mailto:')).toBe(true);
- expect(setForm.has('tel:')).toBe(true);
- expect(setForm.has('ftp:')).toBe(true);
- expect(setForm.has('sms:')).toBe(true);
- expect(setForm.has('javascript:')).toBe(false);
- expect(setForm.has('data:')).toBe(false);
- });
-
- test('every Set member is accepted by isSafeUrl, every dangerous scheme rejected', () => {
- const setForm = new Set(SAFE_URL_SCHEMES.map((s) => `${s}:`));
- for (const member of setForm) {
- expect(isSafeUrl(`${member}example`)).toBe(true);
- }
- const dangerous = ['javascript:', 'vbscript:', 'data:', 'file:', 'chrome-extension:'];
- for (const scheme of dangerous) {
- expect(setForm.has(scheme)).toBe(false);
- expect(isSafeUrl(`${scheme}payload`)).toBe(false);
- }
- });
-});
diff --git a/packages/core/src/markdown/serialize-helpers.test.ts b/packages/core/src/markdown/serialize-helpers.test.ts
deleted file mode 100644
index 5460a2cc9..000000000
--- a/packages/core/src/markdown/serialize-helpers.test.ts
+++ /dev/null
@@ -1,304 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { Schema } from '@tiptap/pm/model';
-import type { PropDef } from '../registry/types.ts';
-import { emitMdxJsx } from './serialize-helpers.ts';
-
-const schema = new Schema({
- nodes: {
- doc: { content: 'block+' },
- text: { group: 'inline' },
- jsxComponent: {
- group: 'block',
- content: '',
- atom: true,
- attrs: {
- componentName: { default: '' },
- kind: { default: 'element' },
- attributes: { default: [] },
- sourceRaw: { default: '' },
- sourceDirty: { default: true },
- props: { default: {} },
- },
- },
- },
-});
-
-const stubCtx = {
- all: () => [] as never[],
- registry: {
- getOrWildcard: () => {
- throw new Error('not used in these tests');
- },
- },
-} as unknown as Parameters<typeof emitMdxJsx>[2];
-
-function makeNode(props: Record<string, unknown>) {
- return schema.nodes.jsxComponent.create({
- componentName: 'img',
- kind: 'element',
- attributes: [],
- sourceRaw: '',
- sourceDirty: true,
- props,
- });
-}
-
-describe('emitMdxJsx — omitOnDefault behavior', () => {
- test('omits attribute when value strictly matches declared default and flag is set', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- {
- name: 'loading',
- type: 'enum',
- enumValues: ['eager', 'lazy'],
- defaultValue: 'lazy',
- required: false,
- omitOnDefault: true,
- },
- ];
- const node = makeNode({ src: '/x.png', loading: 'lazy' });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- expect(names).not.toContain('loading');
- });
-
- test('keeps attribute when value differs from declared default', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- {
- name: 'loading',
- type: 'enum',
- enumValues: ['eager', 'lazy'],
- defaultValue: 'lazy',
- required: false,
- omitOnDefault: true,
- },
- ];
- const node = makeNode({ src: '/x.png', loading: 'eager' });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('loading');
- });
-
- test('keeps attribute when omitOnDefault is absent even if value matches default', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- {
- name: 'alt',
- type: 'string',
- required: false,
- defaultValue: '',
- },
- ];
- const node = makeNode({ src: '/x.png', alt: '' });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('alt');
- });
-
- test('omits boolean default-true (controls=true on video/audio)', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- {
- name: 'controls',
- type: 'boolean',
- required: false,
- defaultValue: true,
- omitOnDefault: true,
- },
- ];
- const node = makeNode({ src: '/clip.mp4', controls: true });
- const result = emitMdxJsx('video', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- expect(names).not.toContain('controls');
- });
-
- test('keeps boolean false even when default is true', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- {
- name: 'controls',
- type: 'boolean',
- required: false,
- defaultValue: true,
- omitOnDefault: true,
- },
- ];
- const node = makeNode({ src: '/clip.mp4', controls: false });
- const result = emitMdxJsx('video', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('controls');
- });
-
- test('strips matching default from preserved attrs (re-save stability)', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- {
- name: 'loading',
- type: 'enum',
- enumValues: ['eager', 'lazy'],
- defaultValue: 'lazy',
- required: false,
- omitOnDefault: true,
- },
- ];
- const node = schema.nodes.jsxComponent.create({
- componentName: 'img',
- kind: 'element',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: '/x.png' },
- { type: 'mdxJsxAttribute', name: 'loading', value: 'lazy' },
- ],
- sourceRaw: '',
- sourceDirty: true,
- props: { src: '/x.png', loading: 'lazy' },
- });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- expect(names).not.toContain('loading');
- });
-
- test('without props arg, no omission applies (back-compat with non-canonical callers)', () => {
- const node = makeNode({ src: '/x.png', loading: 'lazy' });
- const result = emitMdxJsx('img', node, stubCtx /* no props */);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- expect(names).toContain('loading');
- });
-});
-
-describe('emitMdxJsx — empty-string-omission for optional strings', () => {
- test('omits empty string for optional prop without explicit defaultValue', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- { name: 'srcset', type: 'string', required: false },
- { name: 'sizes', type: 'string', required: false },
- { name: 'title', type: 'string', required: false },
- ];
- const node = makeNode({ src: '/x.png', srcset: '', sizes: '', title: '' });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- expect(names).not.toContain('srcset');
- expect(names).not.toContain('sizes');
- expect(names).not.toContain('title');
- });
-
- test('preserves empty string for prop with explicit defaultValue: "" (alt="" WCAG decorative)', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- { name: 'alt', type: 'string', required: false, defaultValue: '' },
- ];
- const node = makeNode({ src: '/x.png', alt: '' });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('alt');
- });
-
- test('keeps empty string for required string prop (validation-loud failure)', () => {
- const props: PropDef[] = [{ name: 'src', type: 'string', required: true }];
- const node = makeNode({ src: '' });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- });
-
- test('keeps non-empty string regardless of defaultValue', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- { name: 'srcset', type: 'string', required: false },
- ];
- const node = makeNode({ src: '/x.png', srcset: '/[email protected] 2x' });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('srcset');
- });
-
- test('strips empty string from preserved attrs (re-save stability)', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- { name: 'srcset', type: 'string', required: false },
- ];
- const node = schema.nodes.jsxComponent.create({
- componentName: 'img',
- kind: 'element',
- attributes: [
- { type: 'mdxJsxAttribute', name: 'src', value: '/x.png' },
- { type: 'mdxJsxAttribute', name: 'srcset', value: '' },
- ],
- sourceRaw: '',
- sourceDirty: true,
- props: { src: '/x.png', srcset: '' },
- });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- expect(names).not.toContain('srcset');
- });
-
- test('numeric 0 and boolean false are NOT stripped (only empty strings on string PropDefs)', () => {
- const props: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- { name: 'width', type: 'number', required: false },
- { name: 'controls', type: 'boolean', required: false, defaultValue: true },
- ];
- const node = makeNode({ src: '/x.png', width: 0, controls: false });
- const result = emitMdxJsx('img', node, stubCtx, props);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('width');
- expect(names).toContain('controls');
- });
-});
-
-describe('emitMdxJsx — img.align byte-stability invariant', () => {
- const alignPropDefs: PropDef[] = [
- { name: 'src', type: 'string', required: true },
- {
- name: 'align',
- type: 'enum',
- enumValues: ['center', 'left', 'right'],
- defaultValue: 'center',
- required: false,
- omitOnDefault: true,
- },
- ];
-
- test('omits align when value matches default (center)', () => {
- const node = makeNode({ src: '/x.png', align: 'center' });
- const result = emitMdxJsx('img', node, stubCtx, alignPropDefs);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- expect(names).not.toContain('align');
- });
-
- test('omits align when value is unset (treated as default)', () => {
- const node = makeNode({ src: '/x.png' });
- const result = emitMdxJsx('img', node, stubCtx, alignPropDefs);
- const names = result.attributes.map((a) => ('name' in a ? a.name : '<expr>'));
- expect(names).toContain('src');
- expect(names).not.toContain('align');
- });
-
- test('emits align="left" verbatim', () => {
- const node = makeNode({ src: '/x.png', align: 'left' });
- const result = emitMdxJsx('img', node, stubCtx, alignPropDefs);
- const alignAttr = result.attributes.find((a) => 'name' in a && a.name === 'align') as
- | { value?: unknown }
- | undefined;
- expect(alignAttr).toBeDefined();
- expect(alignAttr?.value).toBe('left');
- });
-
- test('emits align="right" verbatim', () => {
- const node = makeNode({ src: '/x.png', align: 'right' });
- const result = emitMdxJsx('img', node, stubCtx, alignPropDefs);
- const alignAttr = result.attributes.find((a) => 'name' in a && a.name === 'align') as
- | { value?: unknown }
- | undefined;
- expect(alignAttr).toBeDefined();
- expect(alignAttr?.value).toBe('right');
- });
-});
diff --git a/packages/core/src/markdown/tag-promotion.test.ts b/packages/core/src/markdown/tag-promotion.test.ts
deleted file mode 100644
index ff7ededf3..000000000
--- a/packages/core/src/markdown/tag-promotion.test.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { INLINE_TAG_VALUE_RE } from './tag-promotion.ts';
-
-describe('INLINE_TAG_VALUE_RE', () => {
- test('accepts letter-leading values', () => {
- expect(INLINE_TAG_VALUE_RE.test('typescript')).toBe(true);
- expect(INLINE_TAG_VALUE_RE.test('proj/team')).toBe(true);
- expect(INLINE_TAG_VALUE_RE.test('a1')).toBe(true);
- });
-
- test('rejects digit-leading values (keeps #2026 as plain text)', () => {
- expect(INLINE_TAG_VALUE_RE.test('2026')).toBe(false);
- expect(INLINE_TAG_VALUE_RE.test('123')).toBe(false);
- expect(INLINE_TAG_VALUE_RE.test('1q-recap')).toBe(false);
- });
-});
diff --git a/packages/core/src/markdown/tag.precision.test.ts b/packages/core/src/markdown/tag.precision.test.ts
deleted file mode 100644
index 9f07321b4..000000000
--- a/packages/core/src/markdown/tag.precision.test.ts
+++ /dev/null
@@ -1,180 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function findNodes(json: JSONContent, type: string): JSONContent[] {
- const out: JSONContent[] = [];
- const visit = (n: JSONContent) => {
- if (n.type === type) out.push(n);
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-function plainTextOf(json: JSONContent): string {
- let out = '';
- const visit = (n: JSONContent) => {
- if (n.type === 'text') out += n.text ?? '';
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-describe('tags — basic round-trip', () => {
- test('simple `#tag` round-trips byte-stable', () => {
- const src = 'A note about #typescript here.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('nested `#parent/child` round-trips', () => {
- const src = 'Issue is #frontend/regression.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('alphanumeric value (`#issue-42-fix`) round-trips', () => {
- const src = 'See #issue-42-fix for details.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-});
-
-describe('tags — parse correctness', () => {
- test('parses as PM `tag` atom with value attr (no `#` prefix)', () => {
- const json = mdManager.parse('Hi #foo there.');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(1);
- expect(tags[0].attrs?.value).toBe('foo');
- });
-
- test('nested value preserved as a single tag with slash', () => {
- const json = mdManager.parse('See #a/b/c.');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(1);
- expect(tags[0].attrs?.value).toBe('a/b/c');
- });
-
- test('digits-only `#123` does NOT promote — stays as plain text', () => {
- const json = mdManager.parse('Number #123 here.');
- expect(findNodes(json, 'tag').length).toBe(0);
- expect(plainTextOf(json)).toContain('#123');
- });
-
- test('`# Heading` parses as heading, NOT tag', () => {
- const json = mdManager.parse('# Hello\n');
- expect(findNodes(json, 'tag').length).toBe(0);
- const headings = findNodes(json, 'heading');
- expect(headings.length).toBe(1);
- });
-});
-
-describe('tags — multiple tags', () => {
- test('two tags in one paragraph round-trip', () => {
- const src = 'See #typescript and #react.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('three tags adjacent with single spaces', () => {
- const json = mdManager.parse('Tags: #a #b #c.');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(3);
- expect(tags.map((t) => t.attrs?.value)).toEqual(['a', 'b', 'c']);
- });
-});
-
-describe('tags — surrounding context', () => {
- test('tag inside a heading round-trips', () => {
- const src = '## Heading with #tag\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('tag inside a list item round-trips', () => {
- const src = '- Item with #tag\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('tag mixed with bold round-trips', () => {
- const src = 'Mark **important** as #priority/high.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-});
-
-describe('tags — boundary edge cases', () => {
- test('tag at start of line (no preceding char) parses', () => {
- const json = mdManager.parse('#first thing\n');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(1);
- expect(tags[0].attrs?.value).toBe('first');
- });
-
- test('tag followed by punctuation — `.` does not become part of value', () => {
- const json = mdManager.parse('Done. #fix.');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(1);
- expect(tags[0].attrs?.value).toBe('fix');
- expect(plainTextOf(json)).toContain('.');
- });
-
- test('`#` after a word character does NOT promote (e.g. `email#frag`)', () => {
- const json = mdManager.parse('Anchor email#frag in url.');
- expect(findNodes(json, 'tag').length).toBe(0);
- expect(plainTextOf(json)).toContain('email#frag');
- });
-});
-
-describe('tags — JSX `<Tag value="…" />` authoring (D-T11 / FR-T13)', () => {
- test('JSX `<Tag value="x" />` parses to a `tag` atom', () => {
- const json = mdManager.parse('Inline <Tag value="frontend" /> mid-prose.\n');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(1);
- expect(tags[0].attrs?.value).toBe('frontend');
- });
-
- test('JSX with hierarchy value — single atom with slash preserved', () => {
- const json = mdManager.parse('See <Tag value="proj/team" /> for context.\n');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(1);
- expect(tags[0].attrs?.value).toBe('proj/team');
- });
-
- test('JSX form round-trips to canonical inline `#value` shape', () => {
- const out = mdManager.serialize(
- mdManager.parse('Inline <Tag value="frontend" /> mid-prose.\n'),
- );
- expect(out).toBe('Inline #frontend mid-prose.\n');
- });
-
- test('JSX with empty value attr produces a placeholder atom (no `#` prefix in serialize)', () => {
- const json = mdManager.parse('A <Tag value="" /> here.\n');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(1);
- expect(tags[0].attrs?.value).toBe('');
- });
-
- test('JSX form coexists with inline `#tag` form in the same paragraph', () => {
- const json = mdManager.parse('Mix #typescript and <Tag value="react" />.\n');
- const tags = findNodes(json, 'tag');
- expect(tags.length).toBe(2);
- expect(tags.map((t) => t.attrs?.value)).toEqual(['typescript', 'react']);
- });
-});
-
-describe('tags — escape and inline-code precision', () => {
- test('HTML entity `\\&#x20;` is NOT double-escaped on round-trip', () => {
- const src = 'Entity \\&#x20; stays intact.\n';
- expect(mdManager.serialize(mdManager.parse(src))).toBe(src);
- });
-
- test('backslash-escaped `\\#word` does NOT promote to a tag', () => {
- const json = mdManager.parse('Escaped \\#notag here.\n');
- expect(findNodes(json, 'tag').length).toBe(0);
- });
-
- test('`#tag` inside inline code backticks does NOT promote', () => {
- const json = mdManager.parse('Use `#config` for settings.');
- expect(findNodes(json, 'tag').length).toBe(0);
- });
-});
diff --git a/packages/core/src/markdown/to-markdown-handlers.attention.test.ts b/packages/core/src/markdown/to-markdown-handlers.attention.test.ts
deleted file mode 100644
index f2cffd43b..000000000
--- a/packages/core/src/markdown/to-markdown-handlers.attention.test.ts
+++ /dev/null
@@ -1,190 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import type { Nodes } from 'mdast';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import { assertRoundTripIdempotent } from './round-trip-asserts.test-helper.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-
-const em = (t: string, sourceDelimiter: '*' | '_' = '*'): JSONContent => ({
- type: 'text',
- marks: [{ type: 'emphasis', attrs: { sourceDelimiter } }],
- text: t,
-});
-const strong = (t: string, sourceDelimiter: '**' | '__' = '**'): JSONContent => ({
- type: 'text',
- marks: [{ type: 'strong', attrs: { sourceDelimiter } }],
- text: t,
-});
-const text = (t: string): JSONContent => ({ type: 'text', text: t });
-const paragraph = (...inline: JSONContent[]): JSONContent => ({
- type: 'doc',
- content: [{ type: 'paragraph', content: inline }],
-});
-
-function allNodeTypes(markdown: string): string[] {
- const out: string[] = [];
- const walk = (node: Nodes): void => {
- out.push(node.type);
- if ('children' in node) {
- for (const child of node.children) {
- walk(child);
- }
- }
- };
- walk(md.parseToMdast(markdown));
- return out;
-}
-
-const expectByteStable = (out: string): void =>
- assertRoundTripIdempotent((s) => md.serialize(md.parse(s)), out);
-
-describe('boundary-whitespace attention encode (WYSIWYG-shaped marks)', () => {
- test('emphasis with trailing space re-parses as emphasis via char-ref', () => {
- const out = md.serialize(paragraph(em('foo ')));
- expect(out).toBe('*foo&#x20;*\n');
- expect(allNodeTypes(out)).toContain('emphasis');
- expectByteStable(out);
- });
-
- test('emphasis with leading space does not degrade to a bullet list', () => {
- const out = md.serialize(paragraph(em(' foo')));
- expect(out).toBe('*&#x20;foo*\n');
- const types = allNodeTypes(out);
- expect(types).toContain('emphasis');
- expect(types).not.toContain('list');
- expectByteStable(out);
- });
-
- test('strong with trailing space re-parses as strong', () => {
- const out = md.serialize(paragraph(strong('foo ')));
- expect(out).toBe('**foo&#x20;**\n');
- expect(allNodeTypes(out)).toContain('strong');
- expectByteStable(out);
- });
-
- test('strong with leading space re-parses as strong', () => {
- const out = md.serialize(paragraph(strong(' foo')));
- expect(out).toBe('**&#x20;foo**\n');
- expect(allNodeTypes(out)).toContain('strong');
- expectByteStable(out);
- });
-
- test('underscore emphasis with trailing space keeps its delimiter form', () => {
- const out = md.serialize(paragraph(em('foo ', '_')));
- expect(out).toBe('_foo&#x20;_\n');
- expect(allNodeTypes(out)).toContain('emphasis');
- expectByteStable(out);
- });
-
- test('underscore strong with trailing space keeps its delimiter form', () => {
- const out = md.serialize(paragraph(strong('foo ', '__')));
- expect(out).toBe('__foo&#x20;__\n');
- expect(allNodeTypes(out)).toContain('strong');
- expectByteStable(out);
- });
-
- test('NBSP inside an emphasis boundary downgrades to an encoded space', () => {
- const out = md.serialize(paragraph(em('a\u00A0')));
- expect(out).toBe('*a&#x20;*\n');
- expect(allNodeTypes(out)).toContain('emphasis');
- expectByteStable(out);
- });
-
- test('whitespace-only emphasis survives', () => {
- const out = md.serialize(paragraph(em(' ')));
- expect(out).toBe('*&#x20;*\n');
- expect(allNodeTypes(out)).toContain('emphasis');
- expectByteStable(out);
- });
-
- test('trailing space emphasis flush against following text survives', () => {
- const out = md.serialize(paragraph(em('foo '), text('bar')));
- expect(out).toBe('*foo&#x20;*bar\n');
- expect(allNodeTypes(out)).toContain('emphasis');
- expectByteStable(out);
- });
-
- test('leading space emphasis flush against preceding text survives', () => {
- const out = md.serialize(paragraph(text('bar'), em(' foo')));
- expect(out).toBe('bar*&#x20;foo*\n');
- expect(allNodeTypes(out)).toContain('emphasis');
- expectByteStable(out);
- });
-
- test('whitespace on both the run boundary and its neighbor needs no outside encode', () => {
- const out = md.serialize(paragraph(text('a '), em(' x')));
- expect(out).toBe('a *&#x20;x*\n');
- expect(allNodeTypes(out)).toContain('emphasis');
- expectByteStable(out);
- });
-
- test('nested strong+emphasis with boundary space survives structurally', () => {
- const out = md.serialize(
- paragraph({
- type: 'text',
- marks: [
- { type: 'strong', attrs: { sourceDelimiter: '**' } },
- { type: 'emphasis', attrs: { sourceDelimiter: '*' } },
- ],
- text: 'x ',
- }),
- );
- expect(out).not.toMatch(/\\&/);
- const types = allNodeTypes(out);
- expect(types).toContain('strong');
- expect(types).toContain('emphasis');
- expectByteStable(out);
- });
-});
-
-describe('guard-irreducible adjacencies stay byte-identical to before', () => {
- test('letter before punctuation-fronted emphasis is left unencoded', () => {
- const out = md.serialize(paragraph(text('x'), em('.y.')));
- expect(out).toBe('x*.y.*\n');
- expect(out).not.toContain('&#');
- });
-
- test('letter after punctuation-tailed emphasis is left unencoded', () => {
- const out = md.serialize(paragraph(em('.y.'), text('z')));
- expect(out).toBe('*.y.*z\n');
- expect(out).not.toContain('&#');
- });
-
- test('intraword underscore emphasis is left unencoded', () => {
- const out = md.serialize(paragraph(text('a'), em('b', '_'), text('c')));
- expect(out).toBe('a_b_c\n');
- expect(out).not.toContain('&#');
- });
-
- test('underscore run with boundary space and letter neighbor is left unencoded', () => {
- const out = md.serialize(paragraph(em('foo ', '_'), text('bar')));
- expect(out).toBe('_foo _bar\n');
- expect(out).not.toContain('&#');
- });
-});
-
-describe('zero regression on already-valid boundaries', () => {
- test('plain emphasis and strong are byte-identical to before', () => {
- expect(md.serialize(paragraph(em('foo')))).toBe('*foo*\n');
- expect(md.serialize(paragraph(strong('foo')))).toBe('**foo**\n');
- expect(md.serialize(paragraph(em('foo', '_')))).toBe('_foo_\n');
- expect(md.serialize(paragraph(strong('foo', '__')))).toBe('__foo__\n');
- });
-
- test('parse-derived attention round-trips byte-identically', () => {
- const sources = [
- 'x *y* z\n',
- '**a** b\n',
- 'a _b_ c\n',
- '*.padded.* x\n',
- '.*foo*\n',
- 'intra*word*em\n',
- '*&#x41;foo*\n',
- ];
- for (const src of sources) {
- expect(md.serialize(md.parse(src))).toBe(src);
- }
- });
-});
diff --git a/packages/core/src/markdown/to-markdown-handlers.charref-attention.test.ts b/packages/core/src/markdown/to-markdown-handlers.charref-attention.test.ts
deleted file mode 100644
index 81a9914de..000000000
--- a/packages/core/src/markdown/to-markdown-handlers.charref-attention.test.ts
+++ /dev/null
@@ -1,83 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-const rt = (s: string): string => md.serialize(md.parse(s));
-
-const expectStable = (src: string, out: string): void => {
- const once = rt(src);
- expect(once).toBe(out);
- expect(rt(once)).toBe(out);
-};
-
-const stripTree = (n: unknown): unknown => {
- if (Array.isArray(n)) return n.map(stripTree);
- if (n && typeof n === 'object') {
- const out: Record<string, unknown> = {};
- for (const [k, v] of Object.entries(n as Record<string, unknown>)) {
- if (k === 'position' || k === 'data') continue;
- out[k] = stripTree(v);
- }
- return out;
- }
- return n;
-};
-
-const expectContentPreserved = (src: string): void => {
- expect(stripTree(md.parseToMdast(rt(src)))).toEqual(stripTree(md.parseToMdast(src)));
-};
-
-describe('char-ref-inert escape churn (the entity is a bystander)', () => {
- test('punct-inner asterisk run after a numeric char ref gains escapes, stays lossless', () => {
- expectStable('a&#x41;*.y.*', 'a&#x41;\\*.y.\\*\n');
- expectContentPreserved('a&#x41;*.y.*');
- });
-
- test('entity bytes survive verbatim through the escape churn', () => {
- expect(rt('a&#x41;*.y.*')).toContain('a&#x41;');
- });
-
- test('the decoded twin escapes identically (the char ref does not alter the escape)', () => {
- expectStable('aA*.y.*', 'aA\\*.y.\\*\n');
- });
-
- test('the no-ref control produces the same escape', () => {
- expectStable('a*.y.*', 'a\\*.y.\\*\n');
- });
-});
-
-describe('char-ref-causal PUA-flanking divergence (the char ref alters escaping)', () => {
- test('underscore run after a numeric char ref escapes both delimiters, stays lossless', () => {
- expectStable('a&#x41;_foo_', 'a&#x41;\\_foo\\_\n');
- expectContentPreserved('a&#x41;_foo_');
- expect(rt('a&#x41;_foo_')).toContain('a&#x41;');
- });
-
- test('the decoded twin diverges: intraword opener stays bare, only the closer escapes', () => {
- expectStable('aA_foo_', 'aA_foo\\_\n');
- });
-
- test('punct-inner asterisk run after a named char ref escapes both delimiters, stays lossless', () => {
- expectStable('a&amp;*.y.*', 'a&amp;\\*.y.\\*\n');
- expectContentPreserved('a&amp;*.y.*');
- expect(rt('a&amp;*.y.*')).toContain('a&amp;');
- });
-
- test('the decoded twin diverges: the bare ampersand form round-trips byte-exact with zero escapes', () => {
- expectStable('a&*.y.*', 'a&*.y.*\n');
- expect(rt('a&*.y.*')).not.toContain('\\');
- });
-
- test('the causal pairs differ in escape count, never in entity bytes or content', () => {
- const refUnderscore = rt('a&#x41;_foo_');
- const decodedUnderscore = rt('aA_foo_');
- expect((refUnderscore.match(/\\/g) ?? []).length).toBe(2);
- expect((decodedUnderscore.match(/\\/g) ?? []).length).toBe(1);
-
- const refAmp = rt('a&amp;*.y.*');
- const decodedAmp = rt('a&*.y.*');
- expect((refAmp.match(/\\/g) ?? []).length).toBe(2);
- expect((decodedAmp.match(/\\/g) ?? []).length).toBe(0);
- });
-});
diff --git a/packages/core/src/markdown/to-markdown-handlers.inline-math.test.ts b/packages/core/src/markdown/to-markdown-handlers.inline-math.test.ts
deleted file mode 100644
index c363ec12b..000000000
--- a/packages/core/src/markdown/to-markdown-handlers.inline-math.test.ts
+++ /dev/null
@@ -1,158 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import { assertByteStable } from './round-trip-asserts.test-helper.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-
-function findInJson(json: JSONContent, predicate: (n: JSONContent) => boolean): JSONContent | null {
- if (predicate(json)) return json;
- for (const child of json.content ?? []) {
- const found = findInJson(child, predicate);
- if (found) return found;
- }
- return null;
-}
-
-function countInJson(json: JSONContent, predicate: (n: JSONContent) => boolean): number {
- let count = predicate(json) ? 1 : 0;
- for (const child of json.content ?? []) {
- count += countInJson(child, predicate);
- }
- return count;
-}
-
-const isMath = (n: JSONContent) => n.type === 'mathInline';
-
-const expectByteStable = (source: string): void =>
- assertByteStable((s) => md.serialize(md.parse(s)), source);
-
-function expectReparsesAsMath(emitted: string, formula: string) {
- const json = md.parse(emitted);
- expect(countInJson(json, isMath)).toBe(1);
- expect(findInJson(json, isMath)?.attrs?.formula).toBe(formula);
-}
-
-describe('single-dollar capture: `$x$` round-trips byte-identical', () => {
- test('`$x$` mid-prose round-trips byte-identical', () => {
- expectByteStable('A formula $x$ in prose.\n');
- });
-
- test('`$x^2$` round-trips byte-identical', () => {
- expectByteStable('Result: $x^2$.\n');
- });
-
- test('the captured delimiter lands on the mathInline atom', () => {
- const json = md.parse('A formula $x$ in prose.\n');
- const node = findInJson(json, isMath);
- expect(node?.attrs?.formula).toBe('x');
- expect(node?.attrs?.sourceDelimiter).toBe('$');
- });
-
- test('`$$x$$` keeps the paired-double form', () => {
- expectByteStable('Result: $$x^2$$.\n');
- const node = findInJson(md.parse('Result: $$x^2$$.\n'), isMath);
- expect(node?.attrs?.sourceDelimiter).toBe('$$');
- });
-
- test('mixed `$a$` and `$$b$$` in one paragraph each keep their form', () => {
- expectByteStable('Both $a$ and $$b$$ here.\n');
- });
-
- test('adjacent single-dollar maths `$y$$x$` round-trip byte-identical', () => {
- const source = '$y$$x$\n';
- const json = md.parse(source);
- expect(countInJson(json, isMath)).toBe(2);
- expect(md.serialize(json)).toBe(source);
- });
-
- test('LaTeX command body `$\\alpha$` round-trips byte-identical', () => {
- expectByteStable('Greek $\\alpha$ letter.\n');
- });
-});
-
-describe('currency precision: the promoter boundary is unchanged', () => {
- test('`Pay $5 to $10 dollars` stays prose, byte-identical', () => {
- const source = 'Pay $5 to $10 dollars.\n';
- expect(countInJson(md.parse(source), isMath)).toBe(0);
- expectByteStable(source);
- });
-
- test('`$PATH and $HOME` stays prose, byte-identical', () => {
- const source = '$PATH and $HOME\n';
- expect(countInJson(md.parse(source), isMath)).toBe(0);
- expectByteStable(source);
- });
-
- test('`$x$5` (digit after close) stays prose, byte-identical', () => {
- const source = 'value $x$5 here\n';
- expect(countInJson(md.parse(source), isMath)).toBe(0);
- expectByteStable(source);
- });
-
- test('`Cost $5$ each` parses as math (documented edge) and round-trips', () => {
- const source = 'Cost $5$ each\n';
- expect(countInJson(md.parse(source), isMath)).toBe(1);
- expectByteStable(source);
- });
-});
-
-describe('demotion boundary: a stale single-$ capture never emits bytes the promoter rejects', () => {
- function parsedCarrier(): JSONContent {
- const json = md.parse('A $x$ b\n');
- expect(findInJson(json, isMath)?.attrs?.sourceDelimiter).toBe('$');
- return json;
- }
-
- test('digit typed right after the atom demotes to `$$…$$`', () => {
- const json = parsedCarrier();
- const para = json.content?.[0];
- const after = para?.content?.[2];
- expect(after?.text).toBe(' b');
- if (after) after.text = '5 b';
- const out = md.serialize(json);
- expect(out).toBe('A $$x$$5 b\n');
- expectReparsesAsMath(out, 'x');
- });
-
- test('formula edited to contain `$` demotes and grows the fence', () => {
- const json = parsedCarrier();
- const node = findInJson(json, isMath);
- if (node?.attrs) node.attrs.formula = 'a$b';
- const out = md.serialize(json);
- expect(out).toBe('A $$a$b$$ b\n');
- expectReparsesAsMath(out, 'a$b');
- });
-
- test('formula edited to start with whitespace demotes', () => {
- const json = parsedCarrier();
- const node = findInJson(json, isMath);
- if (node?.attrs) node.attrs.formula = ' x';
- const out = md.serialize(json);
- expectReparsesAsMath(out, ' x');
- });
-
- test('formula edited to end with a backslash demotes (raw math text keeps it)', () => {
- const json = parsedCarrier();
- const node = findInJson(json, isMath);
- if (node?.attrs) node.attrs.formula = 'x\\';
- const out = md.serialize(json);
- expectReparsesAsMath(out, 'x\\');
- });
-
- test('formula with an escape-active backslash sequence demotes (prose re-parse would eat it)', () => {
- const json = parsedCarrier();
- const node = findInJson(json, isMath);
- if (node?.attrs) node.attrs.formula = 'a\\*b';
- const out = md.serialize(json);
- expectReparsesAsMath(out, 'a\\*b');
- });
-
- test('WYSIWYG-inserted math (no captured delimiter) keeps the `$$…$$` default', () => {
- const json = parsedCarrier();
- const node = findInJson(json, isMath);
- if (node?.attrs) node.attrs.sourceDelimiter = null;
- expect(md.serialize(json)).toBe('A $$x$$ b\n');
- });
-});
diff --git a/packages/core/src/markdown/to-markdown-handlers.overescape.test.ts b/packages/core/src/markdown/to-markdown-handlers.overescape.test.ts
deleted file mode 100644
index 2caf792e8..000000000
--- a/packages/core/src/markdown/to-markdown-handlers.overescape.test.ts
+++ /dev/null
@@ -1,201 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-const rt = (s: string): string => md.serialize(md.parse(s));
-
-const expectByteExact = (src: string): void => {
- const once = rt(src);
- expect(once).toBe(src);
- expect(rt(once)).toBe(src);
-};
-
-const expectStable = (src: string, out: string): void => {
- const once = rt(src);
- expect(once).toBe(out);
- expect(rt(once)).toBe(out);
-};
-
-describe('inert delimiters stay bare (over-escape cleared)', () => {
- test('whitespace-flanked asterisk round-trips byte-exact', () => {
- expectByteExact('a * b\n');
- });
-
- test('whitespace-flanked asterisk run round-trips byte-exact', () => {
- expectByteExact('a ** b\n');
- });
-
- test('intraword underscore round-trips byte-exact', () => {
- expectByteExact('a_b_c\n');
- });
-
- test('under_score round-trips without gaining a backslash', () => {
- expectByteExact('under_score\n');
- });
-
- test('snake_case identifiers in prose round-trip byte-exact', () => {
- expectByteExact('call snake_case_name with under_score args\n');
- });
-
- test('whitespace-flanked tilde round-trips byte-exact', () => {
- expectByteExact('word ~ word\n');
- });
-
- test('whitespace-flanked double tilde round-trips byte-exact', () => {
- expectByteExact('a ~~ b\n');
- });
-
- test('whitespace-flanked asterisk inside an ATX heading round-trips byte-exact', () => {
- expectByteExact('# a * b\n');
- });
-
- test('whitespace-flanked asterisk inside a blockquote round-trips byte-exact', () => {
- expectByteExact('> a * b\n');
- });
-
- test('inert asterisk keeps parsing as plain text after the bare round-trip', () => {
- const out = rt('a * b\n');
- const para = md.parseToMdast(out).children[0];
- expect(para?.type).toBe('paragraph');
- const children = (para as { children: Array<{ type: string; value?: string }> }).children;
- expect(children).toHaveLength(1);
- expect(children[0]?.type).toBe('text');
- expect(children[0]?.value).toBe('a * b');
- });
-
- test('intraword underscore inside an underscore-delimited emphasis stays bare', () => {
- expectByteExact('_x_y_\n');
- const para = md.parseToMdast('_x_y_\n').children[0];
- const em = (para as { children: Array<{ type: string; children?: Array<{ value?: string }> }> })
- .children[0];
- expect(em?.type).toBe('emphasis');
- expect(em?.children?.[0]?.value).toBe('x_y');
- });
-});
-
-describe('line-start = escapes only for the setext-underline shape', () => {
- test('padded highlight-like text does not gain a backslash', () => {
- expectByteExact('== foo ==\n');
- });
-
- test('mid-line double equals stays bare', () => {
- expectByteExact('x == y\n');
- });
-
- test('line-start = followed by prose stays bare', () => {
- expectByteExact('= line of prose\n');
- });
-
- test('non-contiguous equals line stays bare (not a setext-underline shape)', () => {
- expectByteExact('== ==\n');
- });
-
- test('lazy continuation line starting with = followed by prose stays bare', () => {
- expectByteExact('foo\n== bar\n');
- });
-
- test('pure = paragraph stays escaped (setext-underline shape)', () => {
- expectStable('==\n', '\\==\n');
- });
-
- test('escaped pure = line round-trips byte-exact', () => {
- expectByteExact('\\==\n');
- });
-
- test('escaped setext-underline shape after a soft break round-trips byte-exact', () => {
- expectByteExact('foo\n\\==\n');
- });
-
- test('escaped = before prose on a continuation line drops no bytes', () => {
- expectByteExact('foo\n\\= bar\n');
- });
-});
-
-describe('flanking-active delimiters still escape (precision sweep)', () => {
- test('escaped emphasis-able pair round-trips byte-exact', () => {
- expectByteExact('a \\*bc\\* d\n');
- });
-
- test('escaped full emphasis shape round-trips byte-exact', () => {
- expectByteExact('\\*foo\\*\n');
- });
-
- test('escaped left-flanking single asterisk round-trips byte-exact', () => {
- expectByteExact('a \\*b\n');
- });
-
- test('escaped underscore pair round-trips byte-exact', () => {
- expectByteExact('a \\_b\\_ c\n');
- });
-
- test('escaped tilde pairs round-trip byte-exact', () => {
- expectByteExact('a \\~\\~b\\~\\~ c\n');
- });
-
- test('line-start left-flanking asterisk still gains an escape', () => {
- expectStable('*x\n', '\\*x\n');
- });
-
- test('left-flanking asterisk mid-phrasing still gains an escape', () => {
- expectStable('a *b\n', 'a \\*b\n');
- });
-
- test('quote-flanked underscore still escapes (punctuation adjacency can open)', () => {
- expectStable('a "_" b\n', 'a "\\_" b\n');
- });
-
- test('text ending in bare asterisks before emphasis keeps the boundary escaped', () => {
- expectStable('foo***bar*\n', 'foo\\*\\**bar*\n');
- const para = md.parseToMdast('foo\\*\\**bar*\n').children[0];
- const children = (para as { children: Array<{ type: string; value?: string }> }).children;
- expect(children.map((c) => c.type)).toEqual(['text', 'emphasis']);
- expect(children[0]?.value).toBe('foo**');
- });
-
- test('text starting with bare asterisks after emphasis keeps the boundary escaped', () => {
- expectStable('*foo***bar\n', '*foo*\\*\\*bar\n');
- const para = md.parseToMdast('*foo*\\*\\*bar\n').children[0];
- const children = (para as { children: Array<{ type: string; value?: string }> }).children;
- expect(children.map((c) => c.type)).toEqual(['emphasis', 'text']);
- expect(children[1]?.value).toBe('**bar');
- });
-
- test('rule-of-three blocked shapes stay all-literal with every run escaped', () => {
- expectStable('foo**bar*\n', 'foo\\*\\*bar\\*\n');
- expectStable('*foo**bar\n', '\\*foo\\*\\*bar\n');
- });
-});
-
-describe('atBreak guards for * _ ~ are unchanged (list / thematic / fence threats)', () => {
- test('escaped list-marker shape at line start round-trips byte-exact', () => {
- expectByteExact('\\* foo\n');
- });
-
- test('line-start underscore before a space stays escaped (thematic-break guard)', () => {
- expectStable('_ foo\n', '\\_ foo\n');
- });
-
- test('line-start tilde before a space stays escaped (fence guard)', () => {
- expectStable('~ foo\n', '\\~ foo\n');
- });
-});
-
-describe('genuine formation cases are untouched', () => {
- test('emphasis, strong, strikethrough, and highlight round-trip byte-exact', () => {
- expectByteExact('*em*\n');
- expectByteExact('_em_\n');
- expectByteExact('**st**\n');
- expectByteExact('__st__\n');
- expectByteExact('~~del~~\n');
- expectByteExact('==hl==\n');
- });
-
- test('intraword strikethrough still parses and round-trips', () => {
- expectByteExact('a~~b~~c\n');
- });
-
- test('emphasis adjacent to inert delimiters round-trips byte-exact', () => {
- expectByteExact('a * b *em* c * d\n');
- });
-});
diff --git a/packages/core/src/markdown/to-markdown-handlers.residual-axes.test.ts b/packages/core/src/markdown/to-markdown-handlers.residual-axes.test.ts
deleted file mode 100644
index c2465b119..000000000
--- a/packages/core/src/markdown/to-markdown-handlers.residual-axes.test.ts
+++ /dev/null
@@ -1,323 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-
-const md = new MarkdownManager({ extensions: sharedExtensions });
-const rt = (s: string): string => md.serialize(md.parse(s));
-
-const expectByteExact = (src: string): void => {
- const expected = src.endsWith('\n') ? src : `${src}\n`;
- const once = rt(src);
- expect(once).toBe(expected);
- expect(rt(once)).toBe(expected);
-};
-
-describe('gfm-table: outer-pipe presence + dash-row padding', () => {
- test('pipe-less table round-trips byte-exact', () => {
- expectByteExact('col|val\n-|-\nalpha|99');
- });
-
- test('unpadded delimiter row keeps zero padding', () => {
- expectByteExact('|a|b|\n|-|-|\n|c|d|');
- });
-
- test('canonical padded table is unchanged', () => {
- expectByteExact('| a | b |\n| - | - |\n| c | d |');
- });
-
- test('leading-pipe-only style round-trips', () => {
- expectByteExact('|a|b\n|-|-\n|c|d');
- });
-
- test('alignment markers survive with zero padding', () => {
- expectByteExact('|a|b|\n|:-|-:|\n|c|d|');
- });
-
- test('mixed outer-pipe styles fall back to canonical (non-uniform: no capture)', () => {
- const out = rt('|a|b|\n-|-\nc|d');
- expect(out).toBe('|a|b|\n|-|-|\n|c|d|\n');
- expect(rt(out)).toBe(out);
- });
-
- test('stale outer-pipe capture with an emptied boundary cell falls back to piped form', () => {
- const json = md.parse('col|val\n-|-\nalpha|99');
- const firstRow = (
- json as unknown as {
- content: Array<{ type: string; content: Array<{ content: Array<unknown> }> }>;
- }
- ).content.find((n) => n.type === 'table');
- const firstCell = firstRow?.content[0]?.content[0] as { content?: unknown[] } | undefined;
- if (firstCell) firstCell.content = [];
- const out = md.serialize(json);
- expect(out.split('\n')[0]).toBe('||val|');
- expect(rt(out)).toBe(out);
- });
-});
-
-describe('list: marker spacing, ordinal replay, nested indent', () => {
- test('double-space marker spacing round-trips', () => {
- expectByteExact('- item');
- });
-
- test('quad-space marker spacing round-trips', () => {
- expectByteExact('- item');
- });
-
- test('all-ones ordered numbering is not force-incremented', () => {
- expectByteExact('1. a\n1. b');
- });
-
- test('descending-style numbering replays verbatim', () => {
- expectByteExact('3. a\n1. b\n1. c');
- });
-
- test('canonical incrementing numbering is unchanged', () => {
- expectByteExact('1. a\n2. b');
- });
-
- test('non-canonical nested-list indent round-trips', () => {
- expectByteExact('- a\n - b');
- });
-
- test('canonical nested-list indent is unchanged', () => {
- expectByteExact('- a\n - b');
- });
-
- test('three-level canonical nesting is idempotent (relative-indent capture)', () => {
- expectByteExact('- L1\n - L2\n - L3');
- });
-
- test('non-canonical indent on a doubly-nested list round-trips', () => {
- expectByteExact('- L1\n - L2\n - L3');
- });
-
- test('ordered marker spacing composes with ordinal replay', () => {
- expectByteExact('1. a\n1. b');
- });
-
- test('marker spacing of 5+ stays canonical (indented-code boundary)', () => {
- const out = rt('- item');
- expect(rt(out)).toBe(out);
- });
-});
-
-describe('gfm-task: checkbox case', () => {
- test('uppercase [X] round-trips', () => {
- expectByteExact('- [X] done');
- });
-
- test('lowercase [x] round-trips', () => {
- expectByteExact('- [x] done');
- });
-
- test('unchecked task round-trips', () => {
- expectByteExact('- [ ] todo');
- });
-
- test('mixed-case task list round-trips per item', () => {
- expectByteExact('- [X] a\n- [x] b\n- [ ] c');
- });
-});
-
-describe('blockquote: marker-spacing counts', () => {
- test('double-space marker round-trips', () => {
- expectByteExact('> quote');
- });
-
- test('triple-space marker round-trips', () => {
- expectByteExact('> quote');
- });
-
- test('indented code inside a blockquote does not double its indent', () => {
- expectByteExact('> code');
- });
-
- test('no-space marker still round-trips', () => {
- expectByteExact('>quote');
- });
-});
-
-describe('code-fence: closing length, leading indent, info padding', () => {
- test('longer closing fence round-trips', () => {
- expectByteExact('```\ncode\n````');
- });
-
- test('uniformly indented fence round-trips', () => {
- expectByteExact(' ```\n code\n ```');
- });
-
- test('info-string padding round-trips', () => {
- expectByteExact('``` js\ncode\n```');
- });
-
- test('single-space info padding round-trips', () => {
- expectByteExact('``` js\ncode\n```');
- });
-
- test('indent + info padding + closing length compose', () => {
- expectByteExact(' ``` ts\n code\n ````');
- });
-
- test('fence-recompute: synthesized value containing a closing run lengthens the fence', () => {
- const json = JSON.parse(JSON.stringify(md.parse('```\nplaceholder\n```')));
- const codeNode = (
- json as { content: Array<{ type: string; content?: Array<{ text?: string }> }> }
- ).content.find((n) => n.type === 'codeBlock');
- expect(codeNode).toBeDefined();
- if (codeNode?.content?.[0]) codeNode.content[0].text = 'a\n```\nb';
- const out = md.serialize(json as ReturnType<typeof md.parse>);
- expect(out).toBe('````\na\n```\nb\n````\n');
- expect(rt(out)).toBe(out);
- });
-});
-
-describe('atx heading: leading indent + interior space', () => {
- test('leading indent round-trips', () => {
- expectByteExact(' # Heading');
- });
-
- test('interior spacing round-trips', () => {
- expectByteExact('# Heading');
- });
-
- test('indent + interior compose', () => {
- expectByteExact(' # Heading');
- });
-
- test('interior spacing composes with a closing hash run', () => {
- expectByteExact('# Heading ##');
- });
-
- test('heading inside a list item does not capture the container pad', () => {
- expectByteExact('1. item\n\n # heading in item');
- });
-});
-
-describe('gfm-strikethrough: delimiter run', () => {
- test('single-tilde form round-trips', () => {
- expectByteExact('~x~ y');
- });
-
- test('double-tilde form round-trips', () => {
- expectByteExact('~~x~~ y');
- });
-
- test('both forms coexist per node', () => {
- expectByteExact('~a~ and ~~b~~');
- });
-});
-
-describe('wikiLink: authored padding survives the trim', () => {
- test('padded target round-trips', () => {
- expectByteExact('[[ Page ]]');
- });
-
- test('padded target + alias round-trip', () => {
- expectByteExact('[[Page | alias ]]');
- });
-
- test('padded anchor round-trips', () => {
- expectByteExact('[[Page# Anchor ]]');
- });
-
- test('unpadded forms are unchanged', () => {
- expectByteExact('[[Page]]');
- expectByteExact('[[Page|alias]]');
- expectByteExact('[[Page#Anchor|alias]]');
- });
-
- test('stale raw segment is dropped after a target edit', () => {
- const json = JSON.parse(JSON.stringify(md.parse('[[ Page ]]')));
- const para = (json as { content: Array<{ content?: Array<Record<string, unknown>> }> })
- .content[0];
- const wiki = para.content?.find((n) => n.type === 'wikiLink') as
- | { attrs: Record<string, unknown> }
- | undefined;
- expect(wiki).toBeDefined();
- if (wiki) wiki.attrs.target = 'Renamed';
- const out = md.serialize(json as ReturnType<typeof md.parse>);
- expect(out).toBe('[[Renamed]]\n');
- });
-});
-
-describe('inline-code: authored disambiguation pad', () => {
- test('padded inline code round-trips', () => {
- expectByteExact('a ` y ` b');
- });
-
- test('the intra-block multi-axis non-vacuity witness round-trips', () => {
- expectByteExact('one \ntwo with ` y ` pad');
- });
-
- test('unpadded inline code is unchanged', () => {
- expectByteExact('a `y` b');
- });
-
- test('structurally-required padding still applies without capture', () => {
- expectByteExact('a `` `tick `` b');
- });
-});
-
-describe('inline-code: pipe re-escape inside a table cell', () => {
- test('escaped pipe inside inline code in a cell round-trips byte-exact', () => {
- expectByteExact('| `a\\|b` |\n| - |');
- });
-
- test('the emitted bytes re-parse as a table, not a paragraph', () => {
- const out = rt('| `a\\|b` |\n| - |\n');
- expect(md.parseToMdast(out).children[0]?.type).toBe('table');
- });
-
- test('every pipe in a multi-pipe code span re-escapes', () => {
- expectByteExact('| `a\\|b\\|c` |\n| - |');
- });
-
- test('fence disambiguation composes with the cell escape', () => {
- expectByteExact('| ``a`\\|b`` |\n| - |');
- });
-
- test('inline code outside a table never gains the cell escape', () => {
- expectByteExact('`a|b`');
- expectByteExact('`a\\|b`');
- });
-
- test('plain-text cell pipe escape is unchanged', () => {
- expectByteExact('| a\\|b |\n| - |');
- });
-
- test('padded code with a pipe keeps the escape; the pad capture is a separate open residual', () => {
- expect(rt('| ` a\\|b ` |\n| - |\n')).toBe('| `a\\|b` |\n| - |\n');
- });
-});
-
-describe('guard-sentinel literal collision (escape alphabet)', () => {
- test('authored guard sentinels U+E000-E004 round-trip byte-exact', () => {
- expectByteExact('literal \uE000\uE001\uE002\uE003\uE004 chars');
- });
-
- test('sentinel adjacent to guarded syntax round-trips', () => {
- expectByteExact('x\uE000y and <https://example.com>');
- });
-
- test('sentinel inside a fence info string round-trips', () => {
- expectByteExact('```l\uE000ng\ncode\n```');
- });
-
- test('escape alphabet U+E005-E009 is the documented reserved range', () => {
- expect(rt('esc \uE005 char')).toBe('esc \uE000 char\n');
- });
-});
-
-describe('already-byte-identical constructs stay untouched', () => {
- test('%%-comment padding is unchanged', () => {
- expectByteExact('%% padded comment %%');
- });
-
- test('callout case is unchanged', () => {
- expectByteExact('> [!note]\n> body');
- });
-
- test('details-quote form is unchanged', () => {
- expectByteExact('<details>\n\n<summary>t</summary>\n\n</details>');
- });
-});
diff --git a/packages/core/src/markdown/to-markdown-handlers.test.ts b/packages/core/src/markdown/to-markdown-handlers.test.ts
deleted file mode 100644
index aba70e1c8..000000000
--- a/packages/core/src/markdown/to-markdown-handlers.test.ts
+++ /dev/null
@@ -1,1620 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { getSchema } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import { formatLinkUrl } from './to-markdown-handlers.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function roundTrip(md: string): string {
- return mdManager.serialize(mdManager.parse(md));
-}
-
-describe('to-markdown: emphasis delimiter preservation', () => {
- test('underscore emphasis round-trips as _', () => {
- expect(roundTrip('_word_\n')).toBe('_word_\n');
- });
-
- test('asterisk emphasis round-trips as *', () => {
- expect(roundTrip('*word*\n')).toBe('*word*\n');
- });
-});
-
-describe('to-markdown: strong delimiter preservation', () => {
- test('double-underscore strong round-trips as __', () => {
- expect(roundTrip('__word__\n')).toBe('__word__\n');
- });
-
- test('double-asterisk strong round-trips as **', () => {
- expect(roundTrip('**word**\n')).toBe('**word**\n');
- });
-});
-
-describe('to-markdown: code block fence preservation', () => {
- test('backtick fence round-trips', () => {
- expect(roundTrip('```js\ncode\n```\n')).toBe('```js\ncode\n```\n');
- });
-
- test('tilde fence round-trips as ~~~', () => {
- expect(roundTrip('~~~\ncode\n~~~\n')).toBe('~~~\ncode\n~~~\n');
- });
-
- test('4-backtick fence round-trips', () => {
- expect(roundTrip('````\ncode\n````\n')).toBe('````\ncode\n````\n');
- });
-
- test('code block with meta round-trips', () => {
- expect(roundTrip('```js title="foo"\nx\n```\n')).toBe('```js title="foo"\nx\n```\n');
- });
-
- test('code block with meta + multiple lines round-trips', () => {
- expect(roundTrip('```ts {1,3-5}\nline1\nline2\n```\n')).toBe(
- '```ts {1,3-5}\nline1\nline2\n```\n',
- );
- });
-});
-
-describe('to-markdown: code block style fenced vs indented (FR-21)', () => {
- test('indented multi-line round-trips byte-equal', () => {
- expect(roundTrip(' code\n line\n')).toBe(' code\n line\n');
- });
-
- test('indented single-line round-trips byte-equal', () => {
- expect(roundTrip(' just one line\n')).toBe(' just one line\n');
- });
-
- test('indented block followed by paragraph round-trips', () => {
- expect(roundTrip(' code\n\nP after\n')).toBe(' code\n\nP after\n');
- });
-
- test('paragraph then indented round-trips', () => {
- expect(roundTrip('P before\n\n code\n')).toBe('P before\n\n code\n');
- });
-
- test('fenced block stays fenced (regression check)', () => {
- expect(roundTrip('```\ncode\nline\n```\n')).toBe('```\ncode\nline\n```\n');
- });
-
- test('fenced block with language stays fenced (regression check)', () => {
- expect(roundTrip('```js\nconsole.log(1)\n```\n')).toBe('```js\nconsole.log(1)\n```\n');
- });
-
- test('PM JSON carries sourceStyle="indented" attr after parse', () => {
- const json = mdManager.parse(' code\n line\n');
- type DocLike = { content?: { type: string; attrs?: { sourceStyle?: string } }[] };
- const block = (json as DocLike).content?.[0];
- expect(block?.type).toBe('codeBlock');
- expect(block?.attrs?.sourceStyle).toBe('indented');
- });
-
- test('PM JSON carries sourceStyle="fenced" attr for fenced input', () => {
- const json = mdManager.parse('```\ncode\n```\n');
- type DocLike = { content?: { type: string; attrs?: { sourceStyle?: string } }[] };
- const block = (json as DocLike).content?.[0];
- expect(block?.type).toBe('codeBlock');
- expect(block?.attrs?.sourceStyle).toBe('fenced');
- });
-
- test('idempotence: indented round-trip parses identically twice', () => {
- const md = ' code\n line\n';
- const once = roundTrip(md);
- const twice = roundTrip(once);
- expect(once).toBe(md);
- expect(twice).toBe(md);
- });
-
- test('WYSIWYG-authored code block (no source) emits fenced by default', () => {
- const schema = getSchema(sharedExtensions);
- const codeBlock = schema.nodeType('codeBlock');
- const docNode = schema.nodes.doc.create(
- null,
- codeBlock.create({ language: null, fenceDelimiter: '`', fenceLength: 3, meta: null }, [
- schema.text('hello'),
- ]),
- );
- const out = mdManager.serialize(docNode.toJSON());
- expect(out).toBe('```\nhello\n```\n');
- });
-
- test('explicit sourceStyle="indented" on synthesized PM tree emits indented', () => {
- const schema = getSchema(sharedExtensions);
- const codeBlock = schema.nodeType('codeBlock');
- const docNode = schema.nodes.doc.create(
- null,
- codeBlock.create(
- {
- language: null,
- fenceDelimiter: '`',
- fenceLength: 3,
- meta: null,
- sourceStyle: 'indented',
- },
- [schema.text('hello\nworld')],
- ),
- );
- const out = mdManager.serialize(docNode.toJSON());
- expect(out).toBe(' hello\n world\n');
- });
-
- test('indented block does NOT promote inside a list item (root-only scope)', () => {
- const md = '- list item\n\n continuation paragraph (not code)\n';
- const out = roundTrip(md);
- const json = mdManager.parse(out) as { content?: { type: string }[] };
- const hasIndentedCodeBlock = (json.content ?? []).some((n) => n.type === 'codeBlock');
- expect(hasIndentedCodeBlock).toBe(false);
- });
-});
-
-describe('to-markdown: inline code fence preservation (FR-1, FR-2)', () => {
- test('single backtick wrapping round-trips byte-equal', () => {
- expect(roundTrip('`x`\n')).toBe('`x`\n');
- });
-
- test('multi-word inline code round-trips', () => {
- expect(roundTrip('`hello world`\n')).toBe('`hello world`\n');
- });
-
- test('double-backtick fence with simple value round-trips', () => {
- expect(roundTrip('``x``\n')).toBe('``x``\n');
- });
-
- test('content containing single backtick uses double fence + spaces', () => {
- expect(roundTrip('`` `x` ``\n')).toBe('`` `x` ``\n');
- });
-
- test('triple-backtick fence with no internal backticks round-trips', () => {
- expect(roundTrip('```x```\n')).toBe('```x```\n');
- });
-
- test('inline code inside emphasis round-trips', () => {
- expect(roundTrip('*`x`*\n')).toBe('*`x`*\n');
- });
-
- test('inline code inside strong round-trips', () => {
- expect(roundTrip('**`x`**\n')).toBe('**`x`**\n');
- });
-
- test('inline code inside link round-trips', () => {
- expect(roundTrip('[`x`](u)\n')).toBe('[`x`](u)\n');
- });
-});
-
-describe('to-markdown: lone backtick in text NOT escaped (FR-1d)', () => {
- test('single literal backtick mid-paragraph round-trips', () => {
- expect(roundTrip('type a ` here\n')).toBe('type a ` here\n');
- });
-
- test('odd-count backticks (unmatched run) round-trip as literal text', () => {
- expect(roundTrip('a ` b `` c\n')).toBe('a ` b `` c\n');
- });
-
- test('escaped backtick still survives via escapedChars', () => {
- expect(roundTrip('\\`escaped\\`\n')).toBe('\\`escaped\\`\n');
- });
-
- test('lone backtick at line start round-trips', () => {
- expect(roundTrip('` start of line\n')).toBe('` start of line\n');
- });
-});
-
-describe('to-markdown: thematic break preservation', () => {
- test('doc-start --- round-trips as --- (the *** override is retired)', () => {
- expect(roundTrip('---\n')).toBe('---\n');
- });
-
- test('*** round-trips as ***', () => {
- expect(roundTrip('***\n')).toBe('***\n');
- });
-
- test('non-doc-start --- preserves sourceRaw', () => {
- expect(roundTrip('paragraph\n\n---\n\nmore\n')).toBe('paragraph\n\n---\n\nmore\n');
- });
-});
-
-describe('to-markdown: hard break style', () => {
- test('backslash hard break round-trips', () => {
- expect(roundTrip('line\\\nbreak\n')).toBe('line\\\nbreak\n');
- });
-});
-
-describe('to-markdown: heading style', () => {
- test('ATX heading round-trips', () => {
- expect(roundTrip('## Title\n')).toBe('## Title\n');
- });
-});
-
-describe('to-markdown: ATX heading trailing hashes (FR-15)', () => {
- test('no trailing hashes stays bare', () => {
- expect(roundTrip('# H\n')).toBe('# H\n');
- });
-
- test('matching count round-trips byte-equal', () => {
- expect(roundTrip('## H ##\n')).toBe('## H ##\n');
- });
-
- test('asymmetric count preserves the closer length', () => {
- expect(roundTrip('## H #####\n')).toBe('## H #####\n');
- expect(roundTrip('### asymmetric trail #\n')).toBe('### asymmetric trail #\n');
- });
-
- test('all six heading levels with matching closers round-trip', () => {
- expect(roundTrip('# A #\n')).toBe('# A #\n');
- expect(roundTrip('## B ##\n')).toBe('## B ##\n');
- expect(roundTrip('### C ###\n')).toBe('### C ###\n');
- expect(roundTrip('#### D ####\n')).toBe('#### D ####\n');
- expect(roundTrip('##### E #####\n')).toBe('##### E #####\n');
- expect(roundTrip('###### F ######\n')).toBe('###### F ######\n');
- });
-
- test('empty content with trailing closer round-trips', () => {
- expect(roundTrip('# ###\n')).toBe('# ###\n');
- expect(roundTrip('## ###\n')).toBe('## ###\n');
- });
-
- test('hash without space before is content (CommonMark §4.2 — no closer)', () => {
- expect(roundTrip('## H#\n')).toBe('## H#\n');
- expect(roundTrip('## abrupt##\n')).toBe('## abrupt##\n');
- });
-
- test('PM attrs carry sourceTrailingHashes through the round-trip', () => {
- const json = mdManager.parse('### H ####\n');
- const heading = (json.content ?? []).find((n) => n.type === 'heading');
- expect(heading).toBeTruthy();
- expect(heading?.attrs?.headingStyle).toBe('atx');
- expect(heading?.attrs?.sourceTrailingHashes).toBe(4);
- });
-
- test('PM attrs default to null when no trailing closer', () => {
- const json = mdManager.parse('### Plain heading\n');
- const heading = (json.content ?? []).find((n) => n.type === 'heading');
- expect(heading?.attrs?.sourceTrailingHashes).toBeNull();
- });
-
- test('headings authored from WYSIWYG (no source) emit bare ATX', () => {
- const synthesized = {
- type: 'doc',
- content: [
- {
- type: 'heading',
- attrs: { level: 2 },
- content: [{ type: 'text', text: 'WYSIWYG title' }],
- },
- ],
- };
- expect(mdManager.serialize(synthesized)).toBe('## WYSIWYG title\n');
- });
-
- test('explicit sourceTrailingHashes=0 attr emits bare ATX', () => {
- const synthesized = {
- type: 'doc',
- content: [
- {
- type: 'heading',
- attrs: { level: 1, headingStyle: 'atx', sourceTrailingHashes: 0 },
- content: [{ type: 'text', text: 'zero' }],
- },
- ],
- };
- expect(mdManager.serialize(synthesized)).toBe('# zero\n');
- });
-
- test('explicit sourceTrailingHashes attr is honored on synthesized PM tree', () => {
- const synthesized = {
- type: 'doc',
- content: [
- {
- type: 'heading',
- attrs: { level: 3, headingStyle: 'atx', sourceTrailingHashes: 2 },
- content: [{ type: 'text', text: 'manual closer' }],
- },
- ],
- };
- expect(mdManager.serialize(synthesized)).toBe('### manual closer ##\n');
- });
-
- test('setext heading style still wins when sourceStyle=setext', () => {
- const synthesized = {
- type: 'doc',
- content: [
- {
- type: 'heading',
- attrs: { level: 1, headingStyle: 'setext', sourceTrailingHashes: 3 },
- content: [{ type: 'text', text: 'Setext H' }],
- },
- ],
- };
- expect(mdManager.serialize(synthesized)).toBe('Setext H\n========\n');
- });
-});
-
-describe('to-markdown: setext heading underline length (FR-22)', () => {
- test('H1 with 5-char underline round-trips byte-equal', () => {
- expect(roundTrip('H\n=====\n')).toBe('H\n=====\n');
- });
-
- test('H1 with 1-char underline (CommonMark §4.3 minimum) round-trips', () => {
- expect(roundTrip('H\n=\n')).toBe('H\n=\n');
- });
-
- test('H1 with 11-char underline round-trips', () => {
- expect(roundTrip('H\n===========\n')).toBe('H\n===========\n');
- });
-
- test('H2 with 5-char underline round-trips byte-equal', () => {
- expect(roundTrip('H\n-----\n')).toBe('H\n-----\n');
- });
-
- test('H2 with 1-char underline round-trips', () => {
- expect(roundTrip('H\n-\n')).toBe('H\n-\n');
- });
-
- test('H2 with 11-char underline round-trips', () => {
- expect(roundTrip('H\n-----------\n')).toBe('H\n-----------\n');
- });
-
- test('long content with short underline round-trips byte-equal', () => {
- expect(roundTrip('this is a long heading\n===\n')).toBe('this is a long heading\n===\n');
- });
-
- test('content == underline length round-trips (no-change case)', () => {
- expect(roundTrip('short\n=====\n')).toBe('short\n=====\n');
- });
-
- test('PM tree carries sourceUnderlineLength attr', () => {
- const json = mdManager.parse('H\n=====\n') as {
- content: Array<{ type: string; attrs: { sourceUnderlineLength: unknown } }>;
- };
- expect(json.content[0].type).toBe('heading');
- expect(json.content[0].attrs.sourceUnderlineLength).toBe(5);
- });
-
- test('ATX heading does NOT get sourceUnderlineLength attr', () => {
- const json = mdManager.parse('# H\n') as {
- content: Array<{ type: string; attrs: { sourceUnderlineLength: unknown } }>;
- };
- expect(json.content[0].type).toBe('heading');
- expect(json.content[0].attrs.sourceUnderlineLength).toBeNull();
- });
-
- test('synthesized PM tree without sourceUnderlineLength falls back to library default', () => {
- const synthesized = {
- type: 'doc',
- content: [
- {
- type: 'heading',
- attrs: { level: 1, headingStyle: 'setext', sourceUnderlineLength: null },
- content: [{ type: 'text', text: 'WYSIWYG' }],
- },
- ],
- };
- expect(mdManager.serialize(synthesized)).toBe('WYSIWYG\n=======\n');
- });
-
- test('explicit sourceUnderlineLength on synthesized PM tree is honored', () => {
- const synthesized = {
- type: 'doc',
- content: [
- {
- type: 'heading',
- attrs: { level: 2, headingStyle: 'setext', sourceUnderlineLength: 7 },
- content: [{ type: 'text', text: 'Hi' }],
- },
- ],
- };
- expect(mdManager.serialize(synthesized)).toBe('Hi\n-------\n');
- });
-
- test('idempotence: parse → serialize → parse → serialize matches first round', () => {
- const inputs = ['H\n=====\n', 'H\n-\n', 'this is a long heading\n===\n', 'short\n=====\n'];
- for (const input of inputs) {
- const r1 = roundTrip(input);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- }
- });
-
- test('bridge invariant byte-equal WITHOUT tolerance class — setext H1 5-char', async () => {
- const { normalizeBridge } = await import('../bridge/normalize.ts');
- const input = 'H\n=====\n';
- const output = roundTrip(input);
- expect(normalizeBridge(input)).toBe(normalizeBridge(output));
- expect(input).toBe(output);
- });
-
- test('bridge invariant byte-equal WITHOUT tolerance class — setext H2 11-char', async () => {
- const { normalizeBridge } = await import('../bridge/normalize.ts');
- const input = 'H\n-----------\n';
- const output = roundTrip(input);
- expect(normalizeBridge(input)).toBe(normalizeBridge(output));
- expect(input).toBe(output);
- });
-});
-
-describe('to-markdown: list marker preservation', () => {
- test('dash bullet round-trips', () => {
- expect(roundTrip('- item one\n- item two\n')).toBe('- item one\n- item two\n');
- });
-
- test('plus bullet round-trips', () => {
- expect(roundTrip('+ item one\n+ item two\n')).toBe('+ item one\n+ item two\n');
- });
-
- test('asterisk bullet round-trips', () => {
- expect(roundTrip('* item one\n* item two\n')).toBe('* item one\n* item two\n');
- });
-});
-
-describe('to-markdown: text handler (NG5 fidelity)', () => {
- test('literal & in text survives round-trip', () => {
- expect(roundTrip('H&M Store\n')).toBe('H&M Store\n');
- });
-
- test('literal < in text survives round-trip', () => {
- expect(roundTrip('a < b\n')).toBe('a < b\n');
- });
-
- test('literal [ in prose survives round-trip', () => {
- expect(roundTrip('text [ more\n')).toBe('text [ more\n');
- });
-
- test('literal trailing backslash runs stay literal text', () => {
- const triple = '\\'.repeat(3);
- expect(roundTrip('\\\n')).toBe('\\\n');
- expect(roundTrip('text \\\n')).toBe('text \\\n');
- expect(roundTrip(`${triple}\n`)).toBe(`${triple}\n`);
- expect(roundTrip(`text ${triple}\n`)).toBe(`text ${triple}\n`);
- });
-
- test('escaped bracket plus trailing backslash round-trips verbatim', () => {
- const trailing = '\\';
- expect(roundTrip(`\\[text${trailing}\n`)).toBe(`\\[text${trailing}\n`);
- });
-
- test('unfinished link label stays literal text', () => {
- expect(roundTrip('[foo]\n')).toBe('[foo]\n');
- });
-
- test('unfinished wiki-link stays literal text', () => {
- expect(roundTrip('[[Page\n')).toBe('[[Page\n');
- });
-
- test('empty-label inline link stays literal text', () => {
- expect(roundTrip('[]()\n')).toBe('[]()\n');
- expect(roundTrip('[](x)\n')).toBe('[](x)\n');
- });
-
- test('unfinished link destination stays literal text', () => {
- expect(roundTrip('[foo](\n')).toBe('[foo](\n');
- });
-});
-
-describe('to-markdown: link URL preservation', () => {
- test('URL with & survives round-trip', () => {
- const md = '[link](https://example.com?a=1&b=2)\n';
- expect(roundTrip(md)).toBe(md);
- });
-});
-
-describe('to-markdown: sourceLiteral hidden-content injection guard', () => {
- test('mismatched sourceRaw is dropped — serialized output matches visible text', () => {
- const json = {
- type: 'doc',
- content: [
- {
- type: 'paragraph',
- content: [
- {
- type: 'text',
- text: 'Hello',
- marks: [
- {
- type: 'sourceLiteral',
- attrs: { sourceRaw: 'Hello<script>alert(1)</script>' },
- },
- ],
- },
- ],
- },
- ],
- };
- const out = mdManager.serialize(json);
- expect(out).not.toContain('<script>');
- expect(out).not.toContain('alert(1)');
- expect(out.trim()).toBe('Hello');
- });
-
- test('newline injection in sourceRaw is dropped', () => {
- const json = {
- type: 'doc',
- content: [
- {
- type: 'paragraph',
- content: [
- {
- type: 'text',
- text: 'safe',
- marks: [
- {
- type: 'sourceLiteral',
- attrs: { sourceRaw: 'safe\n\n# Hidden heading' },
- },
- ],
- },
- ],
- },
- ],
- };
- const out = mdManager.serialize(json);
- expect(out).not.toContain('# Hidden');
- expect(out).not.toContain('Hidden heading');
- expect(out.trim()).toBe('safe');
- });
-
- test('javascript-URL link payload is dropped', () => {
- const json = {
- type: 'doc',
- content: [
- {
- type: 'paragraph',
- content: [
- {
- type: 'text',
- text: 'click here',
- marks: [
- {
- type: 'sourceLiteral',
- attrs: { sourceRaw: '[click here](javascript:alert(1))' },
- },
- ],
- },
- ],
- },
- ],
- };
- const out = mdManager.serialize(json);
- expect(out).not.toContain('javascript:');
- expect(out).not.toContain('alert(1)');
- });
-
- test('legitimate sourceRaw still round-trips byte-equal', () => {
- expect(roundTrip('[](https://example.com)\n')).toBe('[](https://example.com)\n');
- expect(roundTrip('text \\\\\\\n')).toBe('text \\\\\\\n');
- });
-});
-
-describe('to-markdown: formatLinkUrl unit', () => {
- test('empty URL → empty', () => {
- expect(formatLinkUrl('')).toBe('');
- });
-
- test('plain URL without special chars → verbatim', () => {
- expect(formatLinkUrl('https://example.com/path')).toBe('https://example.com/path');
- });
-
- test('URL with balanced parens → verbatim', () => {
- expect(formatLinkUrl('http://example.com/(paren)')).toBe('http://example.com/(paren)');
- });
-
- test('URL with deeply nested balanced parens → verbatim', () => {
- expect(formatLinkUrl('a((b)(c))d')).toBe('a((b)(c))d');
- });
-
- test('URL with unbalanced opening parens → escape all parens', () => {
- expect(formatLinkUrl('foo(and(bar)')).toBe('foo\\(and\\(bar\\)');
- });
-
- test('URL with unbalanced closing paren → escape all parens', () => {
- expect(formatLinkUrl('a)b')).toBe('a\\)b');
- });
-
- test('URL with literal angle chars + balanced parens → verbatim', () => {
- expect(formatLinkUrl('<url>')).toBe('<url>');
- expect(formatLinkUrl('foo<bar')).toBe('foo<bar');
- expect(formatLinkUrl('foo>bar')).toBe('foo>bar');
- });
-
- test('URL with backslash but balanced parens → verbatim', () => {
- expect(formatLinkUrl('foo\\bar')).toBe('foo\\bar');
- });
-
- test('URL with backslash AND unbalanced parens → escape backslash too', () => {
- expect(formatLinkUrl('foo\\(bar')).toBe('foo\\\\\\(bar');
- });
-});
-
-describe('to-markdown: link handler URL parity (US-010 R6b)', () => {
- test('link with unbalanced escaped parens round-trips byte-identically', () => {
- const md = '[link](foo\\(and\\(bar\\))\n';
- expect(roundTrip(md)).toBe(md);
- expect(roundTrip(roundTrip(md))).toBe(roundTrip(md));
- });
-
- test('link with balanced parens preserves verbatim', () => {
- const md = '[link](http://example.com/(paren))\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('link with literal angle chars in URL value', () => {
- const md = '[link](<url>)\n';
- expect(roundTrip(md)).toBe(md);
- expect(roundTrip(roundTrip(md))).toBe(roundTrip(md));
- });
-
- test('autolink form (sourceStyle=autolink) preserved as <url>', () => {
- const md = '<https://example.com>\n';
- expect(roundTrip(md)).toBe(md);
- });
-});
-
-describe('to-markdown: image handler URL parity (US-010 R6c)', () => {
- test('image with angle-bracket URL form round-trips byte-identically', () => {
- const md = '![foo](<url>)\n';
- expect(roundTrip(md)).toBe(md);
- expect(roundTrip(roundTrip(md))).toBe(roundTrip(md));
- });
-
- test('plain image URL round-trips verbatim', () => {
- const md = '![alt](http://example.com/img.png)\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('image URL with balanced parens preserves verbatim', () => {
- const md = '![alt](http://example.com/(image).png)\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('image with title preserves title quoting', () => {
- const md = '![alt](http://example.com/img.png "title")\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('image with empty alt round-trips', () => {
- const md = '![](http://example.com/img.png)\n';
- expect(roundTrip(md)).toBe(md);
- });
-});
-
-describe('to-markdown: GFM table alignment (FR-13)', () => {
- test('default alignment (no markers) round-trips with - markers', () => {
- expect(roundTrip('| a | b |\n| - | - |\n| 1 | 2 |\n')).toBe(
- '| a | b |\n| - | - |\n| 1 | 2 |\n',
- );
- });
-
- test('left alignment :-- round-trips', () => {
- expect(roundTrip('| a | b |\n| :- | :- |\n| 1 | 2 |\n')).toBe(
- '| a | b |\n| :- | :- |\n| 1 | 2 |\n',
- );
- });
-
- test('center alignment :-: round-trips', () => {
- expect(roundTrip('| a | b |\n| :-: | :-: |\n| 1 | 2 |\n')).toBe(
- '| a | b |\n| :-: | :-: |\n| 1 | 2 |\n',
- );
- });
-
- test('right alignment -: round-trips', () => {
- expect(roundTrip('| a | b |\n| -: | -: |\n| 1 | 2 |\n')).toBe(
- '| a | b |\n| -: | -: |\n| 1 | 2 |\n',
- );
- });
-
- test('mixed alignment per column round-trips', () => {
- expect(roundTrip('| a | b | c |\n| :- | :-: | -: |\n| L | C | R |\n')).toBe(
- '| a | b | c |\n| :- | :-: | -: |\n| L | C | R |\n',
- );
- });
-
- test('alignment survives multiple body rows', () => {
- const md = '| a | b |\n| :-: | -: |\n| 1 | 2 |\n| 3 | 4 |\n';
- expect(roundTrip(md)).toBe(md);
- });
-});
-
-describe('to-markdown: GFM table header semantics (FR-14)', () => {
- test('first-row cells materialize as tableHeader PM nodes', () => {
- const json = mdManager.parse('| a | b |\n| - | - |\n| 1 | 2 |\n');
- const table = json.content?.[0];
- expect(table?.type).toBe('table');
- const firstRow = table?.content?.[0];
- expect(firstRow?.type).toBe('tableRow');
- const firstRowCells = firstRow?.content ?? [];
- expect(firstRowCells.every((c) => c?.type === 'tableHeader')).toBe(true);
- const secondRow = table?.content?.[1];
- const secondRowCells = secondRow?.content ?? [];
- expect(secondRowCells.every((c) => c?.type === 'tableCell')).toBe(true);
- });
-
- test('header semantics preserved across round-trip with alignment', () => {
- const md = '| Header A | Header B |\n| :-: | -: |\n| body 1 | body 2 |\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('single-row header-only table round-trips', () => {
- const md = '| a | b |\n| - | - |\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('first-row cells carry their column align attr', () => {
- const json = mdManager.parse('| a | b | c |\n| :- | :-: | -: |\n| L | C | R |\n');
- const headerCells = json.content?.[0]?.content?.[0]?.content ?? [];
- expect(headerCells[0]?.attrs?.align).toBe('left');
- expect(headerCells[1]?.attrs?.align).toBe('center');
- expect(headerCells[2]?.attrs?.align).toBe('right');
- });
-
- test('body-row cells carry their column align attr', () => {
- const json = mdManager.parse('| a | b | c |\n| :- | :-: | -: |\n| L | C | R |\n');
- const bodyCells = json.content?.[0]?.content?.[1]?.content ?? [];
- expect(bodyCells[0]?.attrs?.align).toBe('left');
- expect(bodyCells[1]?.attrs?.align).toBe('center');
- expect(bodyCells[2]?.attrs?.align).toBe('right');
- });
-});
-
-describe('to-markdown: GFM table column-padding control (FR-16)', () => {
- test('center-aligned :---: with 3 dashes round-trips byte-equal regardless of cell width', () => {
- expect(roundTrip('| x |\n| :---: |\n| 1234 |\n')).toBe('| x |\n| :---: |\n| 1234 |\n');
- });
-
- test('5-dash centered alignment row :-----: round-trips', () => {
- expect(roundTrip('| col |\n| :-----: |\n| body |\n')).toBe('| col |\n| :-----: |\n| body |\n');
- });
-
- test('1-dash canonical-min :-: round-trips', () => {
- expect(roundTrip('| col |\n| :-: |\n| body |\n')).toBe('| col |\n| :-: |\n| body |\n');
- });
-
- test('left-aligned :-- with various dash counts preserves user count', () => {
- expect(roundTrip('| col |\n| :--- |\n| body |\n')).toBe('| col |\n| :--- |\n| body |\n');
- expect(roundTrip('| col |\n| :- |\n| body |\n')).toBe('| col |\n| :- |\n| body |\n');
- });
-
- test('right-aligned --: with various dash counts preserves user count', () => {
- expect(roundTrip('| col |\n| ---: |\n| body |\n')).toBe('| col |\n| ---: |\n| body |\n');
- expect(roundTrip('| col |\n| -: |\n| body |\n')).toBe('| col |\n| -: |\n| body |\n');
- });
-
- test('unaligned --- with various dash counts preserves user count', () => {
- expect(roundTrip('| col |\n| --- |\n| body |\n')).toBe('| col |\n| --- |\n| body |\n');
- expect(roundTrip('| col |\n| ------ |\n| body |\n')).toBe('| col |\n| ------ |\n| body |\n');
- expect(roundTrip('| col |\n| - |\n| body |\n')).toBe('| col |\n| - |\n| body |\n');
- });
-
- test('per-column dash counts can differ across columns', () => {
- const md = '| a | b | c |\n| - | --- | ----- |\n| 1 | 2 | 3 |\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('per-column alignment + per-column dash count both preserved', () => {
- const md = '| a | b | c |\n| :--- | :----: | -----: |\n| 1 | 2 | 3 |\n';
- expect(roundTrip(md)).toBe(md);
- });
-
- test('cell-content alignment within rows preserved (existing behavior)', () => {
- const json = mdManager.parse('| col |\n| :---: |\n| body |\n');
- const table = json.content?.[0];
- expect(table?.type).toBe('table');
- expect(table?.attrs?.sourceDashCounts).toEqual([3]);
- });
-
- test('WYSIWYG-authored table (no source) emits canonical 1-dash markers', () => {
- const json = mdManager.parse('| a |\n| --- |\n| 1 |\n');
- const table = json.content?.[0];
- expect(table?.attrs?.sourceDashCounts).toEqual([3]);
-
- const tableNode = table;
- if (tableNode) {
- tableNode.attrs = { ...tableNode.attrs, sourceDashCounts: null };
- }
- expect(mdManager.serialize(json)).toBe('| a |\n| - |\n| 1 |\n');
- });
-
- test('explicit empty array sourceDashCounts falls through to canonical-min', () => {
- const json = mdManager.parse('| a |\n| --- |\n| 1 |\n');
- const tableNode = json.content?.[0];
- if (tableNode) {
- tableNode.attrs = { ...tableNode.attrs, sourceDashCounts: [] };
- }
- expect(mdManager.serialize(json)).toBe('| a |\n| - |\n| 1 |\n');
- });
-
- test('sourceDashCounts persists through PM JSON round-trip', () => {
- const json = mdManager.parse('| col |\n| :----: |\n| body |\n');
- const table = json.content?.[0];
- expect(table?.attrs?.sourceDashCounts).toEqual([4]);
- });
-});
-
-describe('to-markdown: GFM bare-URL autolink (FR-17)', () => {
- test('bare https URL in paragraph round-trips byte-equal', () => {
- expect(roundTrip('Visit https://example.com today.\n')).toBe(
- 'Visit https://example.com today.\n',
- );
- });
-
- test('bare http URL round-trips byte-equal', () => {
- expect(roundTrip('see http://x.com path\n')).toBe('see http://x.com path\n');
- });
-
- test('bare URL with query string round-trips byte-equal', () => {
- expect(roundTrip('see https://x.com?query=1\n')).toBe('see https://x.com?query=1\n');
- });
-
- test('bare URL with fragment round-trips byte-equal', () => {
- expect(roundTrip('see https://x.com#frag\n')).toBe('see https://x.com#frag\n');
- });
-
- test('bare URL with path round-trips byte-equal', () => {
- expect(roundTrip('see https://x.com/path/to/page\n')).toBe('see https://x.com/path/to/page\n');
- });
-
- test('bare URL standalone (no surrounding text) round-trips byte-equal', () => {
- expect(roundTrip('https://x.com\n')).toBe('https://x.com\n');
- });
-
- test('bare email autolink round-trips byte-equal as mailto-stripped form', () => {
- expect(roundTrip('reach [email protected]\n')).toBe('reach [email protected]\n');
- });
-
- test('bare www host round-trips byte-equal as www-prefix-stripped form', () => {
- expect(roundTrip('use www.example.com today\n')).toBe('use www.example.com today\n');
- });
-
- test('bare URL with trailing punctuation round-trips byte-equal', () => {
- expect(roundTrip('see https://x.com.\n')).toBe('see https://x.com.\n');
- expect(roundTrip('see https://x.com,\n')).toBe('see https://x.com,\n');
- expect(roundTrip('see https://x.com)\n')).toBe('see https://x.com)\n');
- expect(roundTrip('see https://x.com!\n')).toBe('see https://x.com!\n');
- });
-
- test('bare URL inside parens round-trips byte-equal', () => {
- expect(roundTrip('see (https://x.com)\n')).toBe('see (https://x.com)\n');
- });
-
- test('multiple bare URLs in one paragraph round-trip byte-equal', () => {
- expect(roundTrip('a https://x.com and https://y.com here\n')).toBe(
- 'a https://x.com and https://y.com here\n',
- );
- });
-
- test('bare URL inside emphasis round-trips byte-equal', () => {
- expect(roundTrip('_em https://x.com em_\n')).toBe('_em https://x.com em_\n');
- });
-
- test('bare URL inside strong round-trips byte-equal', () => {
- expect(roundTrip('__strong https://x.com strong__\n')).toBe(
- '__strong https://x.com strong__\n',
- );
- });
-
- test('bare URL inside list item round-trips byte-equal', () => {
- expect(roundTrip('- https://x.com\n- https://y.com\n')).toBe(
- '- https://x.com\n- https://y.com\n',
- );
- });
-
- test('bare URL inside blockquote round-trips byte-equal', () => {
- expect(roundTrip('> https://x.com\n')).toBe('> https://x.com\n');
- });
-
- test('bare URL inside heading round-trips byte-equal', () => {
- expect(roundTrip('## https://x.com\n')).toBe('## https://x.com\n');
- });
-
- test('mixed angle-bracket + bare in same paragraph both round-trip byte-equal', () => {
- expect(roundTrip('see <https://x.com> and https://x.com bare\n')).toBe(
- 'see <https://x.com> and https://x.com bare\n',
- );
- });
-
- test('explicit inline link [text](url) where text===url stays inline form', () => {
- expect(roundTrip('[https://x.com](https://x.com)\n')).toBe('[https://x.com](https://x.com)\n');
- });
-
- test('explicit inline link with title stays inline form (defensive vs gfm-autolink)', () => {
- expect(roundTrip('[click](https://x.com "title")\n')).toBe('[click](https://x.com "title")\n');
- });
-
- test('explicit inline link [text](url) where text != url stays inline form', () => {
- expect(roundTrip('[click here](https://example.com)\n')).toBe(
- '[click here](https://example.com)\n',
- );
- });
-
- test('bare URL round-trip is idempotent', () => {
- const input = 'Visit https://example.com today.\n';
- const r1 = roundTrip(input);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- });
-
- test('bare URL paragraph followed by another paragraph round-trips byte-equal', () => {
- expect(roundTrip('https://x.com\n\nP2\n')).toBe('https://x.com\n\nP2\n');
- });
-
- test('PM mark linkStyle is gfm-autolink for bare URLs', () => {
- const json = mdManager.parse('Visit https://example.com today\n');
- function findLinkMark(n: unknown): unknown {
- if (typeof n !== 'object' || n === null) return null;
- const node = n as { marks?: unknown[]; content?: unknown[] };
- if (Array.isArray(node.marks)) {
- for (const m of node.marks) {
- const mark = m as { type?: string };
- if (mark.type === 'link') return m;
- }
- }
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- const found = findLinkMark(child);
- if (found) return found;
- }
- }
- return null;
- }
- const mark = findLinkMark(json) as { attrs?: Record<string, unknown> } | null;
- expect(mark?.attrs?.linkStyle).toBe('gfm-autolink');
- expect(mark?.attrs?.href).toBe('https://example.com');
- });
-
- test('PM mark linkStyle is inline (NOT gfm-autolink) for explicit [text](url) form', () => {
- const json = mdManager.parse('[https://x.com](https://x.com)\n');
- function findLinkMark(n: unknown): unknown {
- if (typeof n !== 'object' || n === null) return null;
- const node = n as { marks?: unknown[]; content?: unknown[] };
- if (Array.isArray(node.marks)) {
- for (const m of node.marks) {
- const mark = m as { type?: string };
- if (mark.type === 'link') return m;
- }
- }
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- const found = findLinkMark(child);
- if (found) return found;
- }
- }
- return null;
- }
- const mark = findLinkMark(json) as { attrs?: Record<string, unknown> } | null;
- expect(mark?.attrs?.linkStyle).toBe('inline');
- });
-
- test('autolink form remains <url> when both forms appear in same doc', () => {
- expect(roundTrip('<https://example.com>\n')).toBe('<https://example.com>\n');
- });
-});
-
-describe('to-markdown: image reference (FR-18)', () => {
- test('full image reference round-trips byte-equal', () => {
- expect(roundTrip('![alt][ref]\n\n[ref]: https://x.com/img.png\n')).toBe(
- '![alt][ref]\n\n[ref]: https://x.com/img.png\n',
- );
- });
-
- test('collapsed image reference round-trips byte-equal', () => {
- expect(roundTrip('![ref][]\n\n[ref]: https://x.com/img.png\n')).toBe(
- '![ref][]\n\n[ref]: https://x.com/img.png\n',
- );
- });
-
- test('shortcut image reference round-trips byte-equal', () => {
- expect(roundTrip('![ref]\n\n[ref]: https://x.com/img.png\n')).toBe(
- '![ref]\n\n[ref]: https://x.com/img.png\n',
- );
- });
-
- test('full image reference with title preserves title bytes', () => {
- expect(roundTrip('![alt][ref]\n\n[ref]: https://x.com/img.png "title"\n')).toBe(
- '![alt][ref]\n\n[ref]: https://x.com/img.png "title"\n',
- );
- });
-
- test('inline image control case is unaffected (jsxComponent path)', () => {
- expect(roundTrip('![alt](https://x.com/img.png)\n')).toBe('![alt](https://x.com/img.png)\n');
- });
-
- test('image reference inside a paragraph keeps surrounding text', () => {
- expect(roundTrip('Look at ![alt][ref] inline.\n\n[ref]: https://x.com/img.png\n')).toBe(
- 'Look at ![alt][ref] inline.\n\n[ref]: https://x.com/img.png\n',
- );
- });
-
- test('multiple image references sharing one definition round-trip', () => {
- expect(roundTrip('![alt1][ref] ![alt2][ref]\n\n[ref]: https://x.com/img.png\n')).toBe(
- '![alt1][ref] ![alt2][ref]\n\n[ref]: https://x.com/img.png\n',
- );
- });
-
- test('case-mixed identifier label preserved (e.g., [Ref])', () => {
- expect(roundTrip('![alt][Ref]\n\n[Ref]: https://x.com/img.png\n')).toBe(
- '![alt][Ref]\n\n[Ref]: https://x.com/img.png\n',
- );
- });
-
- test('image reference inside emphasis round-trips', () => {
- expect(roundTrip('*![alt][ref]*\n\n[ref]: https://x.com/img.png\n')).toBe(
- '*![alt][ref]*\n\n[ref]: https://x.com/img.png\n',
- );
- });
-
- test('round-trip is idempotent for image references', () => {
- const input = '![alt][ref]\n\n[ref]: https://x.com/img.png\n';
- const r1 = roundTrip(input);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- });
-
- test('PM atom carries label + identifier + referenceType attrs', () => {
- const json = mdManager.parse('![alt][Ref]\n\n[Ref]: https://x.com/img.png\n');
- function findImageRef(n: unknown): { attrs?: Record<string, unknown> } | null {
- if (!n || typeof n !== 'object') return null;
- const node = n as { type?: string; content?: unknown[]; attrs?: Record<string, unknown> };
- if (node.type === 'imageReference') return node;
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- const found = findImageRef(child);
- if (found) return found;
- }
- }
- return null;
- }
- const ref = findImageRef(json);
- expect(ref?.attrs?.label).toBe('Ref');
- expect(ref?.attrs?.identifier).toBe('ref');
- expect(ref?.attrs?.alt).toBe('alt');
- expect(ref?.attrs?.referenceType).toBe('full');
- });
-
- test('plain inline image stays as inline image (image node, not imageReference)', () => {
- const json = mdManager.parse('![alt](https://x.com/img.png)\n');
- function hasImageReference(n: unknown): boolean {
- if (!n || typeof n !== 'object') return false;
- const node = n as { type?: string; content?: unknown[] };
- if (node.type === 'imageReference') return true;
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- if (hasImageReference(child)) return true;
- }
- }
- return false;
- }
- expect(hasImageReference(json)).toBe(false);
- });
-
- test('image reference with empty alt and shortcut form round-trips', () => {
- expect(roundTrip('![image label]\n\n[image label]: /img.png\n')).toBe(
- '![image label]\n\n[image label]: /img.png\n',
- );
- });
-});
-
-describe('to-markdown: link URL angle-bracket form (FR-19)', () => {
- test('inline link bare URL round-trips bare', () => {
- expect(roundTrip('[link](https://example.com)\n')).toBe('[link](https://example.com)\n');
- });
-
- test('inline link with angle-bracketed URL round-trips angle-bracketed', () => {
- expect(roundTrip('[link](<https://example.com>)\n')).toBe('[link](<https://example.com>)\n');
- });
-
- test('autolink standalone <url> stays angle-bracketed (linkStyle=autolink path)', () => {
- expect(roundTrip('<https://example.com>\n')).toBe('<https://example.com>\n');
- });
-
- test('GFM bare URL standalone stays bare (linkStyle=gfm-autolink path)', () => {
- expect(roundTrip('visit https://example.com today\n')).toBe(
- 'visit https://example.com today\n',
- );
- });
-
- test('angle-bracketed URL with internal space (impossible bare) round-trips', () => {
- expect(roundTrip('[link](<http://example.com/foo bar>)\n')).toBe(
- '[link](<http://example.com/foo bar>)\n',
- );
- });
-
- test('angle-bracketed empty URL `<>` round-trips', () => {
- expect(roundTrip('[link](<>)\n')).toBe('[link](<>)\n');
- });
-
- test('multiple links with mixed URL forms round-trip in same paragraph', () => {
- const input = 'See [a](https://x.com) and [b](<https://y.com>) inline.\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('PM mark carries sourceUrlForm=angle-bracketed for [text](<url>)', () => {
- const json = mdManager.parse('[link](<https://x.com>)\n');
- function findLinkMark(n: unknown): { attrs?: Record<string, unknown> } | null {
- if (!n || typeof n !== 'object') return null;
- const node = n as {
- type?: string;
- content?: unknown[];
- marks?: Array<{ type?: string; attrs?: Record<string, unknown> }>;
- };
- const found = node.marks?.find?.((m) => m.type === 'link');
- if (found) return found;
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- const m = findLinkMark(child);
- if (m) return m;
- }
- }
- return null;
- }
- const mark = findLinkMark(json);
- expect(mark?.attrs?.sourceUrlForm).toBe('angle-bracketed');
- });
-
- test('PM mark sourceUrlForm defaults null for `[text](url)` form', () => {
- const json = mdManager.parse('[link](https://x.com)\n');
- function findLinkMark(n: unknown): { attrs?: Record<string, unknown> } | null {
- if (!n || typeof n !== 'object') return null;
- const node = n as {
- type?: string;
- content?: unknown[];
- marks?: Array<{ type?: string; attrs?: Record<string, unknown> }>;
- };
- const found = node.marks?.find?.((m) => m.type === 'link');
- if (found) return found;
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- const m = findLinkMark(child);
- if (m) return m;
- }
- }
- return null;
- }
- const mark = findLinkMark(json);
- expect(mark?.attrs?.sourceUrlForm).toBe(null);
- });
-
- test('round-trip is idempotent for angle-bracketed inline links', () => {
- const input = '[link](<https://x.com>)\n';
- const r1 = roundTrip(input);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- });
-});
-
-describe('to-markdown: link title quote-style (FR-20)', () => {
- test('double-quote title round-trips with double quotes', () => {
- expect(roundTrip('[link](https://x.com "title")\n')).toBe('[link](https://x.com "title")\n');
- });
-
- test('single-quote title round-trips with single quotes', () => {
- expect(roundTrip("[link](https://x.com 'title')\n")).toBe("[link](https://x.com 'title')\n");
- });
-
- test('paren title round-trips with parens', () => {
- expect(roundTrip('[link](https://x.com (title))\n')).toBe('[link](https://x.com (title))\n');
- });
-
- test("single-quote title with internal `\\'` escape round-trips", () => {
- expect(roundTrip("[link](https://x.com 'it\\'s')\n")).toBe("[link](https://x.com 'it\\'s')\n");
- });
-
- test('double-quote title with internal `\\"` escape round-trips', () => {
- expect(roundTrip('[link](https://x.com "say \\"hi\\"")\n')).toBe(
- '[link](https://x.com "say \\"hi\\"")\n',
- );
- });
-
- test('paren title with internal `\\(` `\\)` escapes round-trips', () => {
- expect(roundTrip('[link](https://x.com (a\\(b\\)c))\n')).toBe(
- '[link](https://x.com (a\\(b\\)c))\n',
- );
- });
-
- test('single-quote title containing literal double-quote stays untouched', () => {
- expect(roundTrip(`[link](https://x.com 'a "quoted" t')\n`)).toBe(
- `[link](https://x.com 'a "quoted" t')\n`,
- );
- });
-
- test('double-quote title containing literal single-quote stays untouched', () => {
- expect(roundTrip(`[link](https://x.com "it's fine")\n`)).toBe(
- `[link](https://x.com "it's fine")\n`,
- );
- });
-
- test('paren title containing literal single + double quotes stays untouched', () => {
- expect(roundTrip(`[link](https://x.com (it's "fine"))\n`)).toBe(
- `[link](https://x.com (it's "fine"))\n`,
- );
- });
-
- test('angle-bracketed URL combined with single-quote title round-trips', () => {
- expect(roundTrip("[link](<https://x.com> 'title')\n")).toBe(
- "[link](<https://x.com> 'title')\n",
- );
- });
-
- test('angle-bracketed URL combined with paren title round-trips', () => {
- expect(roundTrip('[link](<https://x.com> (title))\n')).toBe(
- '[link](<https://x.com> (title))\n',
- );
- });
-
- test("PM mark carries sourceTitleMarker=single for `'title'` form", () => {
- const json = mdManager.parse("[link](https://x.com 'a')\n");
- function findLinkMark(n: unknown): { attrs?: Record<string, unknown> } | null {
- if (!n || typeof n !== 'object') return null;
- const node = n as {
- type?: string;
- content?: unknown[];
- marks?: Array<{ type?: string; attrs?: Record<string, unknown> }>;
- };
- const found = node.marks?.find?.((m) => m.type === 'link');
- if (found) return found;
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- const m = findLinkMark(child);
- if (m) return m;
- }
- }
- return null;
- }
- const mark = findLinkMark(json);
- expect(mark?.attrs?.sourceTitleMarker).toBe('single');
- expect(mark?.attrs?.title).toBe('a');
- });
-
- test('PM mark carries sourceTitleMarker=paren for `(title)` form', () => {
- const json = mdManager.parse('[link](https://x.com (a))\n');
- function findLinkMark(n: unknown): { attrs?: Record<string, unknown> } | null {
- if (!n || typeof n !== 'object') return null;
- const node = n as {
- type?: string;
- content?: unknown[];
- marks?: Array<{ type?: string; attrs?: Record<string, unknown> }>;
- };
- const found = node.marks?.find?.((m) => m.type === 'link');
- if (found) return found;
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- const m = findLinkMark(child);
- if (m) return m;
- }
- }
- return null;
- }
- const mark = findLinkMark(json);
- expect(mark?.attrs?.sourceTitleMarker).toBe('paren');
- });
-
- test('PM mark sourceTitleMarker null for link with no title', () => {
- const json = mdManager.parse('[link](https://x.com)\n');
- function findLinkMark(n: unknown): { attrs?: Record<string, unknown> } | null {
- if (!n || typeof n !== 'object') return null;
- const node = n as {
- type?: string;
- content?: unknown[];
- marks?: Array<{ type?: string; attrs?: Record<string, unknown> }>;
- };
- const found = node.marks?.find?.((m) => m.type === 'link');
- if (found) return found;
- if (Array.isArray(node.content)) {
- for (const child of node.content) {
- const m = findLinkMark(child);
- if (m) return m;
- }
- }
- return null;
- }
- const mark = findLinkMark(json);
- expect(mark?.attrs?.sourceTitleMarker).toBe(null);
- });
-
- test('WYSIWYG-authored link (no title attr) emits bare without title', () => {
- expect(roundTrip('[link](https://x.com)\n')).toBe('[link](https://x.com)\n');
- });
-
- test('round-trip is idempotent for paren-style title links', () => {
- const input = '[link](https://x.com (paren))\n';
- const r1 = roundTrip(input);
- const r2 = roundTrip(r1);
- expect(r2).toBe(r1);
- });
-
- test('mixed: paragraph with all four title forms round-trips byte-equal', () => {
- const input = 'Visit [a](u1 \'one\') and [b](u2 "two") and [c](u3 (three)) and [d](u4) here.\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('title forms preserved when wrapped inside emphasis / list / blockquote', () => {
- expect(roundTrip("*[link](u 'a')*\n")).toBe("*[link](u 'a')*\n");
- expect(roundTrip('- [link](u (b))\n')).toBe('- [link](u (b))\n');
- expect(roundTrip('> [link](u "c")\n')).toBe('> [link](u "c")\n');
- });
-});
-
-describe('to-markdown: blockquote marker spacing (FR-23)', () => {
- test('default `> foo` (single-space) round-trips byte-equal', () => {
- expect(roundTrip('> foo\n')).toBe('> foo\n');
- });
-
- test('`>foo` (no-space) round-trips byte-equal', () => {
- expect(roundTrip('>foo\n')).toBe('>foo\n');
- });
-
- test('multi-line `> ` (single-space) preserved per line', () => {
- expect(roundTrip('> line 1\n> line 2\n')).toBe('> line 1\n> line 2\n');
- });
-
- test('multi-line `>` (no-space) preserved per line', () => {
- expect(roundTrip('>line 1\n>line 2\n')).toBe('>line 1\n>line 2\n');
- });
-
- test('mixed `> line\n>line` preserved per line (single then none)', () => {
- expect(roundTrip('> line 1\n>line 2\n')).toBe('> line 1\n>line 2\n');
- });
-
- test('mixed `>line\n> line` preserved per line (none then single)', () => {
- expect(roundTrip('>line 1\n> line 2\n')).toBe('>line 1\n> line 2\n');
- });
-
- test('multi-paragraph blockquote with blank-line `>` continuation preserves per-line spacing', () => {
- const input = '> paragraph 1\n>\n> paragraph 2\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('multi-paragraph all-no-space blockquote preserves per-line spacing', () => {
- const input = '>paragraph 1\n>\n>paragraph 2\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('mixed across paragraphs (single first, none second) preserves per-paragraph spacing', () => {
- const input = '> paragraph 1\n>\n>paragraph 2\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('nested blockquote `> > nested` preserves spacing on both levels', () => {
- expect(roundTrip('> > nested\n')).toBe('> > nested\n');
- });
-
- test('nested blockquote `>> nested` preserves no-space outer + single inner', () => {
- expect(roundTrip('>> nested\n')).toBe('>> nested\n');
- });
-
- test('nested blockquote `>>nested` preserves no-space on both levels', () => {
- expect(roundTrip('>>nested\n')).toBe('>>nested\n');
- });
-
- test('blockquote inside paragraph context preserves no-space form', () => {
- expect(roundTrip('before\n\n>blockquote\n\nafter\n')).toBe('before\n\n>blockquote\n\nafter\n');
- });
-
- test('idempotence: parse → serialize → parse → serialize preserves bytes', () => {
- const inputs = [
- '> foo\n',
- '>foo\n',
- '> line 1\n>line 2\n',
- '> paragraph 1\n>\n>paragraph 2\n',
- '>> nested\n',
- ];
- for (const input of inputs) {
- const out1 = roundTrip(input);
- const out2 = roundTrip(out1);
- expect(out2).toBe(out1);
- }
- });
-
- test('PM blockquote node carries sourceMarkerSpacings attr from source', () => {
- const schema = getSchema(sharedExtensions);
- const json = mdManager.parse('> a\n>b\n');
- const doc = schema.nodeFromJSON(json);
- const blockquote = doc.firstChild;
- expect(blockquote?.type.name).toBe('blockquote');
- expect(blockquote?.attrs.sourceMarkerSpacings).toEqual([1, 0]);
- });
-
- test('PM blockquote attr is null for WYSIWYG-authored blockquote (no source)', () => {
- const schema = getSchema(sharedExtensions);
- const blockquote = schema.nodes.blockquote.create(
- null,
- schema.nodes.paragraph.create(null, schema.text('hello')),
- );
- const doc = schema.nodes.doc.create(null, blockquote);
- const json = doc.toJSON();
- const out = mdManager.serialize(json);
- expect(out).toBe('> hello\n');
- });
-
- test('explicit sourceMarkerSpacings on synthesized blockquote honored on serialize', () => {
- const schema = getSchema(sharedExtensions);
- const blockquote = schema.nodes.blockquote.create(
- { sourceMarkerSpacings: ['none'] },
- schema.nodes.paragraph.create(null, schema.text('hello')),
- );
- const doc = schema.nodes.doc.create(null, blockquote);
- const json = doc.toJSON();
- const out = mdManager.serialize(json);
- expect(out).toBe('>hello\n');
- });
-
- test('out-of-range index defaults to single-space (WYSIWYG-grown blockquote)', () => {
- const schema = getSchema(sharedExtensions);
- const blockquote = schema.nodes.blockquote.create({ sourceMarkerSpacings: ['none'] }, [
- schema.nodes.paragraph.create(null, schema.text('original')),
- schema.nodes.paragraph.create(null, schema.text('grown')),
- ]);
- const doc = schema.nodes.doc.create(null, blockquote);
- const json = doc.toJSON();
- const out = mdManager.serialize(json);
- expect(out).toBe('>original\n>\n> grown\n');
- });
-});
-
-describe('to-markdown: definition source-form preservation (FR-24)', () => {
- test('single-line `[ref]: url` round-trips byte-equal', () => {
- expect(roundTrip('[ref]: url\n')).toBe('[ref]: url\n');
- });
-
- test('single-line with double-quote title round-trips byte-equal', () => {
- expect(roundTrip('[ref]: url "title"\n')).toBe('[ref]: url "title"\n');
- });
-
- test('single-line with single-quote title round-trips byte-equal', () => {
- expect(roundTrip("[ref]: url 'title'\n")).toBe("[ref]: url 'title'\n");
- });
-
- test('single-line with paren title round-trips byte-equal', () => {
- expect(roundTrip('[ref]: url (title)\n')).toBe('[ref]: url (title)\n');
- });
-
- test('multi-line url-only round-trips byte-equal', () => {
- expect(roundTrip('[ref]:\n url\n')).toBe('[ref]:\n url\n');
- });
-
- test('multi-line full (double-quote) round-trips byte-equal', () => {
- const input = '[ref]:\n url\n "title"\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('multi-line full (single-quote) round-trips byte-equal', () => {
- const input = "[ref]:\n url\n 'title'\n";
- expect(roundTrip(input)).toBe(input);
- });
-
- test('multi-line full (paren) round-trips byte-equal', () => {
- const input = '[ref]:\n url\n (title)\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('case-mismatch identifier `[Ref]` preserves label case', () => {
- expect(roundTrip('[Ref]: url\n')).toBe('[Ref]: url\n');
- });
-
- test('angle-bracketed url preserved on single-line via R23 PUA', () => {
- expect(roundTrip('[ref]: <url>\n')).toBe('[ref]: <url>\n');
- });
-
- test('angle-bracketed url preserved on multi-line', () => {
- expect(roundTrip('[ref]:\n <url>\n')).toBe('[ref]:\n <url>\n');
- });
-
- test('angle-bracketed url with title on multi-line', () => {
- const input = '[ref]:\n <url>\n "title"\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('paren title with internal escaped paren preserved', () => {
- const input = '[ref]: url (with \\(escaped\\))\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('single-quote title with internal escaped apostrophe preserved', () => {
- const input = "[ref]: url 'with \\'escape\\''\n";
- expect(roundTrip(input)).toBe(input);
- });
-
- test('definition + reference usage (full form) round-trips byte-equal', () => {
- const input = '[link][ref]\n\n[ref]: url\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('definition + collapsed reference round-trips byte-equal', () => {
- const input = '[ref][]\n\n[ref]: url\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('definition + shortcut reference round-trips byte-equal', () => {
- const input = '[ref]\n\n[ref]: url\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('definition + image reference round-trips byte-equal', () => {
- const input = '![alt][ref]\n\n[ref]: url\n';
- expect(roundTrip(input)).toBe(input);
- });
-
- test('definition + multi-line def with single-quote title via reference', () => {
- const input = "[link][ref]\n\n[ref]:\n url\n 'title'\n";
- expect(roundTrip(input)).toBe(input);
- });
-
- test('idempotence: parse → serialize → parse → serialize preserves bytes', () => {
- const inputs = [
- '[ref]: url\n',
- '[ref]: url "title"\n',
- "[ref]: url 'title'\n",
- '[ref]: url (title)\n',
- '[ref]:\n url\n "title"\n',
- "[ref]:\n url\n 'title'\n",
- '[ref]:\n url\n (title)\n',
- '[ref]: <url>\n',
- '[Ref]: url\n',
- ];
- for (const input of inputs) {
- const out1 = roundTrip(input);
- const out2 = roundTrip(out1);
- expect(out1).toBe(input);
- expect(out2).toBe(out1);
- }
- });
-
- test('PM linkRefDef carries sourceLayout=multiline from multi-line source', () => {
- const schema = getSchema(sharedExtensions);
- const json = mdManager.parse('[ref]:\n url\n "title"\n');
- const doc = schema.nodeFromJSON(json);
- const def = doc.firstChild;
- expect(def?.type.name).toBe('linkRefDef');
- expect(def?.attrs.sourceLayout).toBe('multiline');
- expect(def?.attrs.sourceTitleMarker).toBe('double');
- });
-
- test('PM linkRefDef carries sourceLayout=inline from single-line source', () => {
- const schema = getSchema(sharedExtensions);
- const json = mdManager.parse('[ref]: url\n');
- const doc = schema.nodeFromJSON(json);
- const def = doc.firstChild;
- expect(def?.type.name).toBe('linkRefDef');
- expect(def?.attrs.sourceLayout).toBe('inline');
- expect(def?.attrs.sourceTitleMarker).toBe(null);
- });
-
- test('PM linkRefDef sourceTitleMarker=single from single-quote source', () => {
- const schema = getSchema(sharedExtensions);
- const json = mdManager.parse("[ref]: url 'title'\n");
- const doc = schema.nodeFromJSON(json);
- const def = doc.firstChild;
- expect(def?.attrs.sourceTitleMarker).toBe('single');
- });
-
- test('PM linkRefDef sourceTitleMarker=paren from paren source', () => {
- const schema = getSchema(sharedExtensions);
- const json = mdManager.parse('[ref]: url (title)\n');
- const doc = schema.nodeFromJSON(json);
- const def = doc.firstChild;
- expect(def?.attrs.sourceTitleMarker).toBe('paren');
- });
-
- test('WYSIWYG-authored linkRefDef (no source attrs) defaults to canonical form', () => {
- const schema = getSchema(sharedExtensions);
- const def = schema.nodes.linkRefDef.create({
- label: 'ref',
- href: 'url',
- title: 'title',
- });
- const doc = schema.nodes.doc.create(null, def);
- const json = doc.toJSON();
- const out = mdManager.serialize(json);
- expect(out).toBe('[ref]: url "title"\n');
- });
-
- test('explicit sourceLayout=multiline on synthesized PM tree honored on serialize', () => {
- const schema = getSchema(sharedExtensions);
- const def = schema.nodes.linkRefDef.create({
- label: 'ref',
- href: 'url',
- title: 'title',
- sourceLayout: 'multiline',
- sourceTitleMarker: 'single',
- });
- const doc = schema.nodes.doc.create(null, def);
- const json = doc.toJSON();
- const out = mdManager.serialize(json);
- expect(out).toBe("[ref]:\n url\n 'title'\n");
- });
-});
diff --git a/packages/core/src/markdown/unknown-mdast-guard.test.ts b/packages/core/src/markdown/unknown-mdast-guard.test.ts
deleted file mode 100644
index 9f209a8cb..000000000
--- a/packages/core/src/markdown/unknown-mdast-guard.test.ts
+++ /dev/null
@@ -1,373 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { Root as MdastRoot } from 'mdast';
-import { VFile } from 'vfile';
-import { unknownMdastGuardPlugin } from './unknown-mdast-guard.ts';
-
-describe('unknownMdastGuardPlugin (R8 wildcard)', () => {
- test('leaves known mdast types unchanged', () => {
- const tree: MdastRoot = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- children: [{ type: 'text', value: 'hello' }],
- position: makePos(0, 5),
- },
- ],
- };
- const file = new VFile('hello');
- unknownMdastGuardPlugin()(tree, file);
- expect(tree.children[0]?.type).toBe('paragraph');
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- expect((tree.children[0] as any).children[0].type).toBe('text');
- });
-
- test('replaces unknown top-level type with rawMdxFallbackMdast', () => {
- const src = '$$math$$';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'someFutureType',
- position: makePos(0, src.length),
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('someFutureType');
- expect(child.value).toBe('$$math$$');
- expect(child.position.start.offset).toBe(0);
- expect(child.position.end.offset).toBe(src.length);
- });
-
- test('replaces unknown nested inline type (inside paragraph)', () => {
- const src = 'hello [[?]] world';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'paragraph',
- position: makePos(0, src.length),
- children: [
- { type: 'text', value: 'hello ', position: makePos(0, 6) },
- { type: 'brandNewInlineType', position: makePos(6, 11) },
- { type: 'text', value: ' world', position: makePos(11, 17) },
- ],
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const para = tree.children[0] as any;
- expect(para.type).toBe('paragraph');
- expect(para.children[0].type).toBe('text');
- expect(para.children[1].type).toBe('rawMdxFallbackMdast');
- expect(para.children[1].originalType).toBe('brandNewInlineType');
- expect(para.children[1].value).toBe('[[?]]');
- expect(para.children[2].type).toBe('text');
- });
-
- test('unknown type with no position degrades without fabricating type-name content', () => {
- const tree = {
- type: 'root',
- children: [{ type: 'typeWithoutPosition' }],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(''));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('typeWithoutPosition');
- expect(child.value).not.toBe('typeWithoutPosition');
- expect(child.value).toBe('');
- expect(child.unresolvedPosition).toBe(true);
- });
-
- test('does not recurse into a node it just replaced', () => {
- const src = '<<outer>> <<inner>>';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'unknownOuter',
- position: makePos(0, src.length),
- children: [{ type: 'unknownInner', position: makePos(10, 19) }],
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const outer = tree.children[0] as any;
- expect(outer.type).toBe('rawMdxFallbackMdast');
- expect(outer.originalType).toBe('unknownOuter');
- expect(outer.children).toBeUndefined();
- expect(outer.value).toBe(src);
- });
-
- test('recognizes known extended types (math, inlineMath, rawMdxFallbackMdast)', () => {
- const tree = {
- type: 'root',
- children: [
- { type: 'math', value: 'x^2', position: makePos(0, 3) },
- {
- type: 'paragraph',
- children: [{ type: 'inlineMath', value: 'y', position: makePos(4, 5) }],
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile('x^2 y'));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- expect((tree.children[0] as any).type).toBe('math');
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- expect((tree.children[1] as any).children[0].type).toBe('inlineMath');
- });
-
- test('end-to-end: synthetic unknown-type mdast does NOT throw whole-doc — block-level fallback', async () => {
- const { MarkdownManager } = await import('./index.ts');
- const { sharedExtensions } = await import('../extensions/shared.ts');
- const mgr = new MarkdownManager({ extensions: sharedExtensions });
-
- const md = '# Heading\n\nparagraph\n\n## Section\n';
- const result = mgr.parseWithFallback(md);
- expect(result.content?.length).toBeGreaterThan(1);
- expect(result.content?.[0]?.type).toBe('heading');
- });
-});
-
-describe('rawMdxFallbackMdast content preservation', () => {
- test('positioned node without offsets: fallback carries the source bytes, not the type name', () => {
- const tableBlock = '| Species | Count |\n| --- | --- |\n| Coho | 2 |';
- const src = `# Title\n\n${tableBlock}\n`;
- const tree = {
- type: 'root',
- children: [
- {
- type: 'futureTableVariant',
- position: {
- start: { line: 3, column: 1 },
- end: { line: 5, column: 13 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('futureTableVariant');
- expect(child.value).not.toBe('futureTableVariant');
- expect(child.value).toBe(tableBlock);
- expect(child.unresolvedPosition).toBe(false);
- });
-
- test('zero-width span with offsets: fallback content is the (empty) slice, not the type name', () => {
- const src = 'hello world';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'phantomMarker',
- position: {
- start: { line: 1, column: 6, offset: 5 },
- end: { line: 1, column: 6, offset: 5 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.value).toBe('');
- expect(child.unresolvedPosition).toBe(false);
- });
-
- test('offsets beyond the source bounds: fallback must not fabricate type-name content', () => {
- const src = 'short doc\n';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'staleSpanType',
- position: {
- start: { line: 99, column: 1, offset: 5000 },
- end: { line: 99, column: 20, offset: 5019 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.value).not.toBe('staleSpanType');
- expect(child.value).toBe('');
- expect(child.unresolvedPosition).toBe(true);
- });
-
- test('line/column-only position with line beyond EOF: degrades to empty, not the type name', () => {
- const src = 'first line\nsecond line\n';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'lineBeyondEofType',
- position: {
- start: { line: 7, column: 1 },
- end: { line: 7, column: 5 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('lineBeyondEofType');
- expect(child.value).not.toBe('lineBeyondEofType');
- expect(child.value).toBe('');
- expect(child.unresolvedPosition).toBe(true);
- });
-
- test('line/column-only position with column past the line end: degrades to empty, not the type name', () => {
- const src = 'alpha\nbeta\n';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'overlongColumnType',
- position: {
- start: { line: 1, column: 1 },
- end: { line: 1, column: 99 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('overlongColumnType');
- expect(child.value).not.toBe('overlongColumnType');
- expect(child.value).toBe('');
- expect(child.unresolvedPosition).toBe(true);
- });
-
- test('inverted span (end before start) with in-bounds offsets: degrades to empty, not the type name', () => {
- const src = 'hello world';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'invertedSpanType',
- position: {
- start: { line: 1, column: 9, offset: 8 },
- end: { line: 1, column: 4, offset: 3 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('invertedSpanType');
- expect(child.value).not.toBe('invertedSpanType');
- expect(child.value).toBe('');
- expect(child.unresolvedPosition).toBe(true);
- });
-
- test('mixed points (offset-bearing start, line/column-only end): fallback slices the exact bytes', () => {
- const directive = '::directive::';
- const src = `intro\n${directive}\noutro\n`;
- const tree = {
- type: 'root',
- children: [
- {
- type: 'mixedPointDirective',
- position: {
- start: { offset: 6 },
- end: { line: 2, column: 14 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('mixedPointDirective');
- expect(child.value).not.toBe('mixedPointDirective');
- expect(child.value).toBe(directive);
- expect(child.unresolvedPosition).toBe(false);
- });
-
- test('composed mixed-point failure (resolvable offset start, line beyond EOF end): degrades to empty', () => {
- const src = 'intro\nbody text\n';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'partiallyResolvableType',
- position: {
- start: { offset: 6 },
- end: { line: 99, column: 1 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const child = tree.children[0] as any;
- expect(child.type).toBe('rawMdxFallbackMdast');
- expect(child.originalType).toBe('partiallyResolvableType');
- expect(child.value).not.toBe('partiallyResolvableType');
- expect(child.value).toBe('');
- expect(child.unresolvedPosition).toBe(true);
- });
-
- test('line or column below 1: degrades to empty, not the type name', () => {
- const src = 'content line\n';
- const tree = {
- type: 'root',
- children: [
- {
- type: 'zeroLineType',
- position: {
- start: { line: 0, column: 1 },
- end: { line: 1, column: 8 },
- },
- },
- {
- type: 'zeroColumnType',
- position: {
- start: { line: 1, column: 0 },
- end: { line: 1, column: 8 },
- },
- },
- ],
- } as unknown as MdastRoot;
- unknownMdastGuardPlugin()(tree, new VFile(src));
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const zeroLine = tree.children[0] as any;
- expect(zeroLine.type).toBe('rawMdxFallbackMdast');
- expect(zeroLine.value).not.toBe('zeroLineType');
- expect(zeroLine.value).toBe('');
- expect(zeroLine.unresolvedPosition).toBe(true);
- // biome-ignore lint/suspicious/noExplicitAny: test inspects mdast shape
- const zeroColumn = tree.children[1] as any;
- expect(zeroColumn.type).toBe('rawMdxFallbackMdast');
- expect(zeroColumn.value).not.toBe('zeroColumnType');
- expect(zeroColumn.value).toBe('');
- expect(zeroColumn.unresolvedPosition).toBe(true);
- });
-});
-
-function makePos(startOffset: number, endOffset: number) {
- return {
- start: { line: 1, column: startOffset + 1, offset: startOffset },
- end: { line: 1, column: endOffset + 1, offset: endOffset },
- };
-}
diff --git a/packages/core/src/markdown/whitespace-char-ref.test.ts b/packages/core/src/markdown/whitespace-char-ref.test.ts
deleted file mode 100644
index ba4c3cda0..000000000
--- a/packages/core/src/markdown/whitespace-char-ref.test.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import {
- decodeInlineWhitespaceNumericCharRef,
- decodeInlineWhitespaceNumericCharRefRun,
- isInlineWhitespaceNumericCharRef,
-} from './whitespace-char-ref.ts';
-
-describe('isInlineWhitespaceNumericCharRef', () => {
- test('true for space + tab numeric refs (hex + decimal, X case)', () => {
- for (const ref of ['&#x20;', '&#X20;', '&#32;', '&#x9;', '&#X9;', '&#9;', '&#x09;']) {
- expect(isInlineWhitespaceNumericCharRef(ref)).toBe(true);
- }
- });
-
- test('false for vertical whitespace (newline / CR / VT / FF)', () => {
- for (const ref of ['&#xA;', '&#10;', '&#xD;', '&#13;', '&#xB;', '&#xC;']) {
- expect(isInlineWhitespaceNumericCharRef(ref)).toBe(false);
- }
- });
-
- test('false for non-whitespace numeric refs', () => {
- for (const ref of ['&#x41;', '&#65;', '&#x26;', '&#x2003;' /* em space */]) {
- expect(isInlineWhitespaceNumericCharRef(ref)).toBe(false);
- }
- });
-
- test('false for named refs and malformed input', () => {
- for (const ref of ['&amp;', '&nbsp;', '&#x20', '#x20;', '&#;', '&#xZZ;', '&#x20;x', '', ' ']) {
- expect(isInlineWhitespaceNumericCharRef(ref)).toBe(false);
- }
- });
-});
-
-describe('decodeInlineWhitespaceNumericCharRef', () => {
- test('decodes space + tab forms to the real character', () => {
- expect(decodeInlineWhitespaceNumericCharRef('&#x20;')).toBe(' ');
- expect(decodeInlineWhitespaceNumericCharRef('&#32;')).toBe(' ');
- expect(decodeInlineWhitespaceNumericCharRef('&#x9;')).toBe('\t');
- expect(decodeInlineWhitespaceNumericCharRef('&#9;')).toBe('\t');
- });
-
- test('returns null for everything that is not an inline-whitespace numeric ref', () => {
- for (const ref of ['&#xA;', '&#x41;', '&amp;', '&#x20', 'plain', '']) {
- expect(decodeInlineWhitespaceNumericCharRef(ref)).toBeNull();
- }
- });
-});
-
-describe('decodeInlineWhitespaceNumericCharRefRun', () => {
- test('decodes a single ref (run of one)', () => {
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;')).toBe(' ');
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x9;')).toBe('\t');
- });
-
- test('decodes a run of back-to-back whitespace refs', () => {
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;&#x20;')).toBe(' ');
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x9;&#x9;')).toBe('\t\t');
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;&#x9;&#x20;')).toBe(' \t ');
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;&#32;')).toBe(' '); // hex + decimal
- });
-
- test('returns null when ANY member is not inline whitespace', () => {
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;&#xA;')).toBeNull(); // newline member
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;&#x41;')).toBeNull(); // letter member
- });
-
- test('returns null for any gap, embedded, or trailing non-ref bytes', () => {
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20; &#x20;')).toBeNull(); // literal space gap
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;x&#x20;')).toBeNull();
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;<script>')).toBeNull();
- expect(decodeInlineWhitespaceNumericCharRefRun('x&#x20;')).toBeNull();
- expect(decodeInlineWhitespaceNumericCharRefRun('&#x20;extra')).toBeNull();
- expect(decodeInlineWhitespaceNumericCharRefRun('')).toBeNull();
- expect(decodeInlineWhitespaceNumericCharRefRun(' ')).toBeNull();
- });
-});
diff --git a/packages/core/src/markdown/wiki-embed-opaque.test.ts b/packages/core/src/markdown/wiki-embed-opaque.test.ts
deleted file mode 100644
index cbca58d56..000000000
--- a/packages/core/src/markdown/wiki-embed-opaque.test.ts
+++ /dev/null
@@ -1,178 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import type { JSONContent } from '@tiptap/core';
-import { sharedExtensions } from '../extensions/shared.ts';
-import { MarkdownManager } from './index.ts';
-import { assertByteStable } from './round-trip-asserts.test-helper.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function roundTrip(md: string): string {
- return mdManager.serialize(mdManager.parse(md));
-}
-
-const expectByteStable = (md: string): void => assertByteStable(roundTrip, md);
-
-function findNodes(json: JSONContent, type: string): JSONContent[] {
- const out: JSONContent[] = [];
- const visit = (n: JSONContent) => {
- if (n.type === type) out.push(n);
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- return out;
-}
-
-describe('opaque-extension wiki-embed — byte-exact PM round-trip', () => {
- test('block-context opaque embed round-trips byte-identical', () => {
- expectByteStable('![[notes.foo]]\n');
- });
-
- test('inline mid-prose opaque embed round-trips byte-identical', () => {
- expectByteStable('see ![[notes.foo]] here\n');
- });
-
- test('extension-less embed round-trips byte-identical', () => {
- expectByteStable('![[plainname]]\n');
- });
-
- test('serveable-but-not-inline-renderable extension round-trips byte-identical', () => {
- expectByteStable('![[diagram.avi]]\n');
- });
-
- test('opaque embed with alias round-trips byte-identical', () => {
- expectByteStable('![[notes.foo|My Alias]]\n');
- });
-
- test('opaque embed with anchor round-trips byte-identical', () => {
- expectByteStable('![[notes.foo#section]]\n');
- });
-
- test('opaque embed with anchor and alias round-trips byte-identical', () => {
- expectByteStable('![[notes.foo#section|Custom]]\n');
- });
-
- test('opaque embed coexisting with wikiLink and allowlisted embed round-trips', () => {
- expectByteStable('See [[Index]], ![[diagram.png]] and ![[archive.xyz]] together.\n');
- });
-});
-
-describe('opaque-extension wiki-embed — WYSIWYG label edit is honored, never reverted', () => {
- function editEmbedLabel(json: JSONContent, newText: string): JSONContent {
- let edited = 0;
- const visit = (n: JSONContent) => {
- if (
- n.type === 'text' &&
- n.marks?.some((m) => m.type === 'link' && m.attrs?.sourceForm === 'wikiembed')
- ) {
- n.text = newText;
- edited++;
- }
- for (const child of n.content ?? []) visit(child);
- };
- visit(json);
- expect(edited).toBe(1);
- return json;
- }
-
- test('editing an aliased embed label replaces the alias', () => {
- const json = editEmbedLabel(mdManager.parse('![[notes.foo|My Label]]\n'), 'Edited Label');
- expect(mdManager.serialize(json)).toBe('![[notes.foo|Edited Label]]\n');
- });
-
- test('editing an alias-less embed label adds an alias (target preserved)', () => {
- const json = editEmbedLabel(mdManager.parse('![[notes.foo]]\n'), 'My New Text');
- expect(mdManager.serialize(json)).toBe('![[notes.foo|My New Text]]\n');
- });
-
- test('editing an anchored embed label adds an alias (target#anchor preserved)', () => {
- const json = editEmbedLabel(mdManager.parse('![[notes.foo#section]]\n'), 'Renamed');
- expect(mdManager.serialize(json)).toBe('![[notes.foo#section|Renamed]]\n');
- });
-
- test('the edited form is byte-stable on subsequent round-trips', () => {
- const json = editEmbedLabel(mdManager.parse('![[notes.foo|My Label]]\n'), 'Edited Label');
- const once = mdManager.serialize(json);
- expect(roundTrip(once)).toBe(once);
- });
-
- test('an unedited label still round-trips byte-identical through the attrs path', () => {
- const json = mdManager.parse('![[notes.foo|My Label]]\n');
- expect(mdManager.serialize(json)).toBe('![[notes.foo|My Label]]\n');
- });
-
- test('a whitespace-only label edit does not produce an empty alias', () => {
- const json = editEmbedLabel(mdManager.parse('![[notes.foo|My Label]]\n'), ' ');
- expect(mdManager.serialize(json)).toBe('![[notes.foo|My Label]]\n');
- });
-
- test('a label edit containing `]]` keeps the attrs label — the closing delimiter is unrepresentable in the alias slot', () => {
- const json = editEmbedLabel(mdManager.parse('![[notes.foo|My Label]]\n'), 'My ]] Label');
- const once = mdManager.serialize(json);
- expect(once).toBe('![[notes.foo|My Label]]\n');
- expect(roundTrip(once)).toBe(once);
- });
-});
-
-describe('opaque-extension wiki-embed — PM shape contract', () => {
- test('opaque embed lands on text+link-mark tagged wikiembed, never a PM wikiLinkEmbed node', () => {
- const json = mdManager.parse('![[notes.foo]]\n');
- expect(findNodes(json, 'wikiLinkEmbed')).toHaveLength(0);
- const texts = findNodes(json, 'text');
- expect(texts).toHaveLength(1);
- const linkMark = texts[0]?.marks?.find((mk) => mk.type === 'link');
- expect(linkMark).toBeDefined();
- expect(linkMark?.attrs?.sourceForm).toBe('wikiembed');
- expect(linkMark?.attrs?.target).toBe('notes.foo');
- expect(linkMark?.attrs?.anchor).toBeNull();
- expect(linkMark?.attrs?.alias).toBeNull();
- });
-
- test('alias and anchor are carried on the mark, label shows the alias', () => {
- const json = mdManager.parse('![[notes.foo#sec|My Alias]]\n');
- const texts = findNodes(json, 'text');
- expect(texts[0]?.text).toBe('My Alias');
- const linkMark = texts[0]?.marks?.find((mk) => mk.type === 'link');
- expect(linkMark?.attrs?.target).toBe('notes.foo');
- expect(linkMark?.attrs?.anchor).toBe('sec');
- expect(linkMark?.attrs?.alias).toBe('My Alias');
- });
-
- test('resolver-remapped href does not perturb the byte round-trip', () => {
- const json = mdManager.parse('![[notes.foo]]\n', {
- resolveEmbed: (target) => (target === 'notes.foo' ? 'attachments/notes.foo' : null),
- sourcePath: 'docs/meeting.md',
- });
- const texts = findNodes(json, 'text');
- const linkMark = texts[0]?.marks?.find((mk) => mk.type === 'link');
- expect(linkMark?.attrs?.href).toBe('/attachments/notes.foo');
- expect(linkMark?.attrs?.target).toBe('notes.foo');
- expect(mdManager.serialize(json)).toBe('![[notes.foo]]\n');
- });
-});
-
-describe('opaque-extension widening — no regression on neighbors', () => {
- test('plain markdown link keeps a null sourceForm and round-trips unchanged', () => {
- const json = mdManager.parse('[label](dest.foo)\n');
- const texts = findNodes(json, 'text');
- const linkMark = texts[0]?.marks?.find((mk) => mk.type === 'link');
- expect(linkMark).toBeDefined();
- expect(linkMark?.attrs?.sourceForm).toBeNull();
- expect(mdManager.serialize(json)).toBe('[label](dest.foo)\n');
- });
-
- test('allowlisted block-context embed still promotes to jsxComponent and round-trips', () => {
- const json = mdManager.parse('![[photo.png]]\n');
- const node = json.content?.[0];
- expect(node?.type).toBe('jsxComponent');
- expect(node?.attrs?.componentName).toBe('WikiEmbedImage');
- expect(mdManager.serialize(json)).toBe('![[photo.png]]\n');
- });
-
- test('allowlisted inline embed keeps the wikiembed chip and round-trips', () => {
- expectByteStable('text ![[photo.png]] more text\n');
- });
-
- test('wikiLink sibling is untouched', () => {
- expectByteStable('[[Page]]\n');
- });
-});
diff --git a/packages/core/src/markdown/wiki-link-mdast-promotion.test.ts b/packages/core/src/markdown/wiki-link-mdast-promotion.test.ts
deleted file mode 100644
index 071d0a7f2..000000000
--- a/packages/core/src/markdown/wiki-link-mdast-promotion.test.ts
+++ /dev/null
@@ -1,98 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import { fromProseMirror } from '@handlewithcare/remark-prosemirror';
-import { MarkdownManager, sharedExtensions } from '@inkeep/open-knowledge-core';
-import type { JSONContent } from '@tiptap/core';
-import { getSchema } from '@tiptap/core';
-import type { Paragraph, Root } from 'mdast';
-import type { WikiLinkMdast } from './mdast-augmentation.ts';
-
-const mdManager = new MarkdownManager({ extensions: sharedExtensions });
-
-function findWikiLinks(json: JSONContent): JSONContent[] {
- const found: JSONContent[] = [];
- function walk(node: JSONContent) {
- if (node.type === 'wikiLink') found.push(node);
- if (node.content) for (const child of node.content) walk(child);
- }
- walk(json);
- return found;
-}
-
-describe('wikiLink mdast promotion (US-004 / D7)', () => {
- test('simple [[Page]] round-trips bit-exact', () => {
- const md = 'See [[Page]] here\n';
- const pm = mdManager.parse(md);
- const out = mdManager.serialize(pm);
- expect(out).toBe(md);
- });
-
- test('[[Page#anchor]] round-trips bit-exact', () => {
- const md = 'See [[Page#section]] here\n';
- const out = mdManager.serialize(mdManager.parse(md));
- expect(out).toBe(md);
- });
-
- test('[[Page|Alias]] round-trips bit-exact', () => {
- const md = 'See [[Page|Alias]] here\n';
- const out = mdManager.serialize(mdManager.parse(md));
- expect(out).toBe(md);
- });
-
- test('[[Page#anchor|Alias]] round-trips bit-exact', () => {
- const md = 'See [[Page#section|Alias]] here\n';
- const out = mdManager.serialize(mdManager.parse(md));
- expect(out).toBe(md);
- });
-
- test('PM→mdast emits first-class wikiLink node (not html passthrough)', () => {
- const pm = mdManager.parse('See [[Page|Alias]] here');
- const schema = getSchema(sharedExtensions);
- const pmNode = schema.nodeFromJSON(pm);
-
- type Handlers = Parameters<typeof fromProseMirror>[1];
- type Managerish = {
- pmNodeHandlers: NonNullable<Handlers>['nodeHandlers'];
- pmMarkHandlers: NonNullable<Handlers>['markHandlers'];
- };
- const internal = mdManager as unknown as Managerish;
- const tree = fromProseMirror(pmNode, {
- schema,
- nodeHandlers: internal.pmNodeHandlers,
- markHandlers: internal.pmMarkHandlers,
- }) as Root;
-
- const para = tree.children.find((c) => c.type === 'paragraph') as Paragraph;
- expect(para).toBeDefined();
- const wiki = para.children.find((c) => c.type === 'wikiLink') as unknown as
- | WikiLinkMdast
- | undefined;
- expect(wiki).toBeDefined();
- if (!wiki) throw new Error('unreachable');
- expect(wiki.data.target).toBe('Page');
- expect(wiki.data.alias).toBe('Alias');
- expect(wiki.data.anchor).toBeNull();
- expect(wiki.children).toHaveLength(1);
- expect(wiki.children[0]?.value).toBe('Alias');
- });
-
- test('PM doc survives a full markdown round-trip with wikiLink nodes preserved', () => {
- const md = 'Link to [[Page#heading|Alias]] in prose\n';
- const pm1 = mdManager.parse(md);
- const mdOut = mdManager.serialize(pm1);
- const pm2 = mdManager.parse(mdOut);
- expect(pm2).toEqual(pm1);
- expect(mdOut).toBe(md);
- const wikis = findWikiLinks(pm1);
- expect(wikis).toHaveLength(1);
- expect(wikis[0]?.attrs?.target).toBe('Page');
- expect(wikis[0]?.attrs?.alias).toBe('Alias');
- expect(wikis[0]?.attrs?.anchor).toBe('heading');
- });
-
- test('multiple wikiLinks in one paragraph round-trip', () => {
- const md = 'See [[A]] and [[B#c|d]] together\n';
- const pm = mdManager.parse(md);
- const out = mdManager.serialize(pm);
- expect(out).toBe(md);
- });
-});
diff --git a/packages/core/src/markdown/wiki-link-micromark.test.ts b/packages/core/src/markdown/wiki-link-micromark.test.ts
deleted file mode 100644
index c376876ae..000000000
--- a/packages/core/src/markdown/wiki-link-micromark.test.ts
+++ /dev/null
@@ -1,289 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import fc from 'fast-check';
-import type { Nodes, Root } from 'mdast';
-import { fromMarkdown } from 'mdast-util-from-markdown';
-import { toMarkdown } from 'mdast-util-to-markdown';
-import { visit } from 'unist-util-visit';
-import type { WikiLinkEmbedMdast, WikiLinkMdast } from './mdast-augmentation.ts';
-import { wikiLinkFromMarkdown, wikiLinkSyntax, wikiLinkToMarkdown } from './wiki-link-micromark.ts';
-
-function parseMdast(md: string): Root {
- return fromMarkdown(md, {
- extensions: [wikiLinkSyntax()],
- mdastExtensions: [wikiLinkFromMarkdown],
- });
-}
-
-function serializeMdast(tree: Root): string {
- return toMarkdown(tree, { extensions: [wikiLinkToMarkdown] }).replace(/\n+$/, '');
-}
-
-function findWikiLinks(tree: Root): WikiLinkMdast[] {
- const links: WikiLinkMdast[] = [];
- visit(tree, (node: Nodes) => {
- if (node.type === 'wikiLink') links.push(node as unknown as WikiLinkMdast);
- });
- return links;
-}
-
-function findWikiLinkEmbeds(tree: Root): WikiLinkEmbedMdast[] {
- const embeds: WikiLinkEmbedMdast[] = [];
- visit(tree, (node: Nodes) => {
- if (node.type === 'wikiLinkEmbed') {
- embeds.push(node as unknown as WikiLinkEmbedMdast);
- }
- });
- return embeds;
-}
-
-describe('wiki-link: 4 functional shapes', () => {
- test('[[Page]] — bare target', () => {
- const tree = parseMdast('[[Page]]');
- const links = findWikiLinks(tree);
- expect(links).toHaveLength(1);
- expect(links[0].data.target).toBe('Page');
- expect(links[0].data.anchor).toBeNull();
- expect(links[0].data.alias).toBeNull();
- expect(serializeMdast(tree)).toBe('[[Page]]');
- });
-
- test('[[Page|Alias]] — with alias', () => {
- const tree = parseMdast('[[Page|Alias]]');
- const links = findWikiLinks(tree);
- expect(links).toHaveLength(1);
- expect(links[0].data.target).toBe('Page');
- expect(links[0].data.alias).toBe('Alias');
- expect(serializeMdast(tree)).toBe('[[Page|Alias]]');
- });
-
- test('[[Page#Heading]] — with anchor', () => {
- const tree = parseMdast('[[Page#Heading]]');
- const links = findWikiLinks(tree);
- expect(links).toHaveLength(1);
- expect(links[0].data.target).toBe('Page');
- expect(links[0].data.anchor).toBe('Heading');
- expect(serializeMdast(tree)).toBe('[[Page#Heading]]');
- });
-
- test('[[Page#Heading|Alias]] — full form', () => {
- const tree = parseMdast('[[Page#Heading|Alias]]');
- const links = findWikiLinks(tree);
- expect(links).toHaveLength(1);
- expect(links[0].data.target).toBe('Page');
- expect(links[0].data.anchor).toBe('Heading');
- expect(links[0].data.alias).toBe('Alias');
- expect(serializeMdast(tree)).toBe('[[Page#Heading|Alias]]');
- });
-});
-
-describe('wiki-link: edge cases', () => {
- test('spaces in target', () => {
- const links = findWikiLinks(parseMdast('[[Page Name With Spaces]]'));
- expect(links).toHaveLength(1);
- expect(links[0].data.target).toBe('Page Name With Spaces');
- });
-
- test('adjacent text', () => {
- const links = findWikiLinks(parseMdast('[[Page]]-adjacent'));
- expect(links).toHaveLength(1);
- });
-
- test('text before and after', () => {
- const links = findWikiLinks(parseMdast('before [[Page]] after'));
- expect(links).toHaveLength(1);
- });
-
- test('two wiki-links', () => {
- const links = findWikiLinks(parseMdast('[[Page]] [[Another]]'));
- expect(links).toHaveLength(2);
- });
-
- test('empty target [[ ]] — not a wiki-link', () => {
- const links = findWikiLinks(parseMdast('[[]]'));
- expect(links).toHaveLength(0);
- });
-
- test('unicode target', () => {
- const links = findWikiLinks(parseMdast('[[Página]]'));
- expect(links).toHaveLength(1);
- expect(links[0].data.target).toBe('Página');
- });
-
- test('double hash in anchor', () => {
- const links = findWikiLinks(parseMdast('[[Page#H#H]]'));
- expect(links).toHaveLength(1);
- expect(links[0].data.anchor).toBe('H#H');
- });
-});
-
-describe('wiki-link: integration with other markdown', () => {
- test('inside heading', () => {
- const links = findWikiLinks(parseMdast('# See [[Page]] for details'));
- expect(links).toHaveLength(1);
- });
-
- test('inside list item', () => {
- const links = findWikiLinks(parseMdast('- See [[Page]]'));
- expect(links).toHaveLength(1);
- });
-
- test('inside emphasis', () => {
- const links = findWikiLinks(parseMdast('*See [[Page]]*'));
- expect(links).toHaveLength(1);
- });
-
- test('inside strong', () => {
- const links = findWikiLinks(parseMdast('**See [[Page]]**'));
- expect(links).toHaveLength(1);
- });
-
- test('alongside inline link', () => {
- const links = findWikiLinks(parseMdast('[[Page]] and [inline](link)'));
- expect(links).toHaveLength(1);
- });
-});
-
-describe('wiki-embed: 4 functional shapes (FR-3a)', () => {
- test('![[photo.png]] — bare embed', () => {
- const tree = parseMdast('![[photo.png]]');
- const embeds = findWikiLinkEmbeds(tree);
- expect(embeds).toHaveLength(1);
- expect(embeds[0].data.target).toBe('photo.png');
- expect(embeds[0].data.anchor).toBeNull();
- expect(embeds[0].data.alias).toBeNull();
- expect(serializeMdast(tree)).toBe('![[photo.png]]');
- });
-
- test('![[file.pdf|alt text]] — with alias', () => {
- const tree = parseMdast('![[file.pdf|alt text]]');
- const embeds = findWikiLinkEmbeds(tree);
- expect(embeds).toHaveLength(1);
- expect(embeds[0].data.target).toBe('file.pdf');
- expect(embeds[0].data.alias).toBe('alt text');
- expect(serializeMdast(tree)).toBe('![[file.pdf|alt text]]');
- });
-
- test('![[file.pdf#page=3]] — with anchor (page fragment)', () => {
- const tree = parseMdast('![[file.pdf#page=3]]');
- const embeds = findWikiLinkEmbeds(tree);
- expect(embeds).toHaveLength(1);
- expect(embeds[0].data.target).toBe('file.pdf');
- expect(embeds[0].data.anchor).toBe('page=3');
- expect(serializeMdast(tree)).toBe('![[file.pdf#page=3]]');
- });
-
- test('![[file.pdf#page=3|Page 3]] — full form', () => {
- const tree = parseMdast('![[file.pdf#page=3|Page 3]]');
- const embeds = findWikiLinkEmbeds(tree);
- expect(embeds).toHaveLength(1);
- expect(embeds[0].data.target).toBe('file.pdf');
- expect(embeds[0].data.anchor).toBe('page=3');
- expect(embeds[0].data.alias).toBe('Page 3');
- expect(serializeMdast(tree)).toBe('![[file.pdf#page=3|Page 3]]');
- });
-});
-
-describe('wiki-embed: interaction with wikiLink (additive, not mutual)', () => {
- test('[[Page]] without ! still parses as wikiLink, not wikiLinkEmbed', () => {
- const tree = parseMdast('[[Page]]');
- expect(findWikiLinkEmbeds(tree)).toHaveLength(0);
- expect(findWikiLinks(tree)).toHaveLength(1);
- });
-
- test('mixed document: one link + one embed', () => {
- const tree = parseMdast('See [[Index]] and ![[diagram.png]]');
- expect(findWikiLinks(tree)).toHaveLength(1);
- expect(findWikiLinkEmbeds(tree)).toHaveLength(1);
- expect(serializeMdast(tree)).toBe('See [[Index]] and ![[diagram.png]]');
- });
-
- test('backslash-escape of ! defeats embed tokenization — renders as wikiLink', () => {
- const tree = parseMdast('\\![[photo.png]]');
- expect(findWikiLinkEmbeds(tree)).toHaveLength(0);
- const links = findWikiLinks(tree);
- expect(links).toHaveLength(1);
- expect(links[0].data.target).toBe('photo.png');
- });
-
- test('bang before non-embed text does not eat following brackets', () => {
- const tree = parseMdast('!not-an-embed');
- expect(findWikiLinkEmbeds(tree)).toHaveLength(0);
- });
-});
-
-describe('wiki-embed: mid-line context + edge cases', () => {
- test('adjacent text is fine', () => {
- const tree = parseMdast('before ![[photo.png]] after');
- expect(findWikiLinkEmbeds(tree)).toHaveLength(1);
- });
-
- test('two embeds on one line', () => {
- const tree = parseMdast('![[a.png]] and ![[b.mp4]]');
- const embeds = findWikiLinkEmbeds(tree);
- expect(embeds).toHaveLength(2);
- expect(embeds[0].data.target).toBe('a.png');
- expect(embeds[1].data.target).toBe('b.mp4');
- });
-
- test('mid-word bang before [[…]] does tokenize as embed', () => {
- const tree = parseMdast('a![[x.png]]');
- expect(findWikiLinkEmbeds(tree)).toHaveLength(1);
- });
-
- test('empty target ![[]] is NOT a wiki-embed', () => {
- const tree = parseMdast('![[]]');
- expect(findWikiLinkEmbeds(tree)).toHaveLength(0);
- });
-
- test('unicode target', () => {
- const tree = parseMdast('![[会議メモ.pdf]]');
- const embeds = findWikiLinkEmbeds(tree);
- expect(embeds).toHaveLength(1);
- expect(embeds[0].data.target).toBe('会議メモ.pdf');
- expect(serializeMdast(tree)).toBe('![[会議メモ.pdf]]');
- });
-
- test('target containing dots and dashes survives round-trip', () => {
- const tree = parseMdast('![[screenshot-2026-04-21_at-14.32.png]]');
- expect(serializeMdast(tree)).toBe('![[screenshot-2026-04-21_at-14.32.png]]');
- });
-});
-
-describe('wiki-embed: invariants I1 and I4 (mdast-util level)', () => {
- const extensionPool = ['png', 'jpg', 'pdf', 'mp4', 'mp3', 'wav', 'ogg', 'webm', 'm4a'];
-
- test('I1 — parse then serialize is byte-identical for canonical embed shapes', () => {
- fc.assert(
- fc.property(
- fc.stringMatching(/^[a-z][a-z0-9_-]{0,12}$/).filter((s) => s.length > 0),
- fc.constantFrom(...extensionPool),
- fc.option(fc.stringMatching(/^[a-zA-Z0-9_=-]{1,12}$/), { nil: null }),
- fc.option(fc.stringMatching(/^[a-zA-Z0-9_-]{1,12}$/), { nil: null }),
- (stem, ext, anchor, alias) => {
- let source = `![[${stem}.${ext}`;
- if (anchor) source += `#${anchor}`;
- if (alias) source += `|${alias}`;
- source += ']]';
- const roundTripped = serializeMdast(parseMdast(source));
- expect(roundTripped).toBe(source);
- },
- ),
- { numRuns: Number(process.env.STRESS_FIDELITY) === 1 ? 10_000 : 500 },
- );
- });
-
- test('I4 — serialize(parse(X)) is idempotent across two compositions', () => {
- fc.assert(
- fc.property(
- fc.stringMatching(/^[a-z][a-z0-9_-]{0,10}$/).filter((s) => s.length > 0),
- fc.constantFrom(...extensionPool),
- (stem, ext) => {
- const first = serializeMdast(parseMdast(`![[${stem}.${ext}]]`));
- const second = serializeMdast(parseMdast(first));
- expect(second).toBe(first);
- },
- ),
- { numRuns: Number(process.env.STRESS_FIDELITY) === 1 ? 10_000 : 500 },
- );
- });
-});
diff --git a/packages/core/src/markdown/wrap-as-inline-code.test.ts b/packages/core/src/markdown/wrap-as-inline-code.test.ts
deleted file mode 100644
index 1425c4894..000000000
--- a/packages/core/src/markdown/wrap-as-inline-code.test.ts
+++ /dev/null
@@ -1,140 +0,0 @@
-import { describe, expect, test } from 'bun:test';
-import * as fc from 'fast-check';
-import type { Nodes as MdastNodes } from 'mdast';
-import { wrapAsInlineCode } from './index.ts';
-
-const PBT_TIMEOUT_MS = process.env.STRESS_FIDELITY === '1' ? 90_000 : 30_000;
-const PBT_NUM_RUNS = process.env.STRESS_FIDELITY === '1' ? 1_000 : 100;
-
-const text = (value: string) => ({ type: 'text' as const, value });
-const link = (url: string, children: MdastNodes[]) => ({
- type: 'link' as const,
- url,
- title: null,
- children,
-});
-const strong = (children: MdastNodes[]) => ({ type: 'strong' as const, children });
-const emphasis = (children: MdastNodes[]) => ({ type: 'emphasis' as const, children });
-const del = (children: MdastNodes[]) => ({ type: 'delete' as const, children });
-
-describe('wrapAsInlineCode — children-shape coverage', () => {
- test('empty children array → inlineCode with empty value', () => {
- const out = wrapAsInlineCode([]);
- expect(out).toEqual({ type: 'inlineCode', value: '' } as MdastNodes);
- });
-
- test('text-only children → concatenated inlineCode value', () => {
- const out = wrapAsInlineCode([text('foo'), text('bar')]);
- expect(out).toEqual({ type: 'inlineCode', value: 'foobar' } as MdastNodes);
- });
-
- test('single-child wrapper with link → link preserved, inner replaced with inlineCode', () => {
- const input = [link('https://example.com', [text('abc123')])];
- const out = wrapAsInlineCode(input as MdastNodes[]);
- expect(out).toEqual({
- type: 'link',
- url: 'https://example.com',
- title: null,
- children: [{ type: 'inlineCode', value: 'abc123' }],
- } as MdastNodes);
- });
-
- test('single-child wrapper with strong → strong preserved, inner replaced with inlineCode', () => {
- const input = [strong([text('bold-code')])];
- const out = wrapAsInlineCode(input as MdastNodes[]);
- expect(out).toEqual({
- type: 'strong',
- children: [{ type: 'inlineCode', value: 'bold-code' }],
- } as MdastNodes);
- });
-
- test('single-child wrapper with emphasis → emphasis preserved, inner replaced with inlineCode', () => {
- const input = [emphasis([text('em-code')])];
- const out = wrapAsInlineCode(input as MdastNodes[]);
- expect(out).toEqual({
- type: 'emphasis',
- children: [{ type: 'inlineCode', value: 'em-code' }],
- } as MdastNodes);
- });
-
- test('single-child wrapper with delete → delete preserved, inner replaced with inlineCode', () => {
- const input = [del([text('struck-code')])];
- const out = wrapAsInlineCode(input as MdastNodes[]);
- expect(out).toEqual({
- type: 'delete',
- children: [{ type: 'inlineCode', value: 'struck-code' }],
- } as MdastNodes);
- });
-
- test('heterogeneous multi-child → recursive text extraction flat inlineCode', () => {
- const input = [text('a'), link('https://x', [text('b')]), strong([text('c')])];
- const out = wrapAsInlineCode(input as MdastNodes[]);
- expect(out).toEqual({ type: 'inlineCode', value: 'abc' } as MdastNodes);
- });
-
- test('nested wrapper preserves both levels (link containing strong)', () => {
- const input = [link('https://x', [strong([text('deep')])])];
- const out = wrapAsInlineCode(input as MdastNodes[]);
- expect(out).toEqual({
- type: 'link',
- url: 'https://x',
- title: null,
- children: [
- {
- type: 'strong',
- children: [{ type: 'inlineCode', value: 'deep' }],
- },
- ],
- } as MdastNodes);
- });
-});
-
-describe('wrapAsInlineCode — properties: shape + content preservation', () => {
- const ALLOWED = new Set(['inlineCode', 'link', 'strong', 'emphasis', 'delete']);
-
- function collectText(nodeOrArray: MdastNodes | MdastNodes[]): string {
- const arr = Array.isArray(nodeOrArray) ? nodeOrArray : [nodeOrArray];
- let out = '';
- for (const n of arr) {
- if (n.type === 'text') {
- out += n.value;
- } else if (n.type === 'inlineCode') {
- out += n.value;
- } else if ('children' in n && Array.isArray(n.children)) {
- out += collectText(n.children as MdastNodes[]);
- }
- }
- return out;
- }
-
- test(
- 'random inline children → output.type ∈ allowed set AND input text is preserved',
- () => {
- fc.assert(
- fc.property(
- fc.array(
- fc.oneof(
- fc.string({ maxLength: 8 }).map(text),
- fc
- .record({ url: fc.webUrl(), inner: fc.string({ maxLength: 8 }) })
- .map(({ url, inner }) => link(url, [text(inner)])),
- fc.string({ maxLength: 8 }).map((s) => strong([text(s)])),
- fc.string({ maxLength: 8 }).map((s) => emphasis([text(s)])),
- fc.string({ maxLength: 8 }).map((s) => del([text(s)])),
- ),
- { maxLength: 4 },
- ),
- (children) => {
- const out = wrapAsInlineCode(children as MdastNodes[]);
- expect(ALLOWED.has(out.type)).toBe(true);
- const inputText = collectText(children as MdastNodes[]);
- const outputText = collectText(out);
- expect(outputText).toBe(inputText);
- },
- ),
- { numRuns: PBT_NUM_RUNS, seed: 42 },
- );
- },
- PBT_TIMEOUT_MS,
- );
-});