Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Codex: migrate missing raw source tests (#1776)

Dimitri POSTOLOVf91b33a0c53f
diff --git a/packages/app/src/editor/clipboard/wysiwyg-stop-rule.test.ts b/packages/app/src/editor/clipboard/wysiwyg-stop-rule.test.ts
index 7fec7e1ba..98d9887c2 100644
--- a/packages/app/src/editor/clipboard/wysiwyg-stop-rule.test.ts
+++ b/packages/app/src/editor/clipboard/wysiwyg-stop-rule.test.ts
@@ -1,31 +1,40 @@
import { describe, expect, test } from 'bun:test';
-import { readFileSync } from 'node:fs';
-import { dirname, join } from 'node:path';
-import { fileURLToPath } from 'node:url';
+import { buildPatternDConstructorOptions } from '../TiptapEditor';
+import { buildSeededPatternDProvider, fakeClipboard } from '../walk-currency-test-harness';
-const __dirname = dirname(fileURLToPath(import.meta.url));
-const TIPTAP_EDITOR_PATH = join(__dirname, '..', 'TiptapEditor.tsx');
+type WysiwygEditorProps = NonNullable<
+ ReturnType<typeof buildPatternDConstructorOptions>['editorProps']
+> & {
+ handleDOMEvents?: Record<string, unknown>;
+};
-describe('WYSIWYG STOP rule — no handleDOMEvents.copy/cut/dragstart (precedent #19(b))', () => {
- const source = readFileSync(TIPTAP_EDITOR_PATH, 'utf-8');
+function buildWysiwygEditorProps(): WysiwygEditorProps {
+ const { provider, cleanup } = buildSeededPatternDProvider('wysiwyg-stop-rule');
+ try {
+ return buildPatternDConstructorOptions({
+ provider,
+ clipboard: fakeClipboard,
+ ctorStart: 0,
+ }).editorProps as WysiwygEditorProps;
+ } finally {
+ cleanup();
+ }
+}
- test('TiptapEditor.tsx does NOT register handleDOMEvents.copy', () => {
- expect(source).not.toMatch(/handleDOMEvents\s*:\s*\{[^}]*\bcopy\s*:/);
- expect(source).not.toMatch(/handleDOMEvents\.copy\s*=/);
- });
+describe('WYSIWYG STOP rule — ProseMirror clipboard hooks', () => {
+ test('wires the ProseMirror clipboard serializer hooks', () => {
+ const props = buildWysiwygEditorProps();
- test('TiptapEditor.tsx does NOT register handleDOMEvents.cut', () => {
- expect(source).not.toMatch(/handleDOMEvents\s*:\s*\{[^}]*\bcut\s*:/);
- expect(source).not.toMatch(/handleDOMEvents\.cut\s*=/);
+ expect(typeof props.clipboardTextSerializer).toBe('function');
+ expect(props.clipboardSerializer).toBe(fakeClipboard.html.serializer);
});
- test('TiptapEditor.tsx does NOT register handleDOMEvents.dragstart', () => {
- expect(source).not.toMatch(/handleDOMEvents\s*:\s*\{[^}]*\bdragstart\s*:/);
- expect(source).not.toMatch(/handleDOMEvents\.dragstart\s*=/);
- });
+ test('does not wire DOM-level copy/cut/dragstart handlers on editorProps', () => {
+ const props = buildWysiwygEditorProps();
+ const handleDOMEvents = props.handleDOMEvents ?? {};
- test('TiptapEditor.tsx DOES wire clipboardTextSerializer (PM-hook path)', () => {
- expect(source).toContain('clipboardTextSerializer');
- expect(source).toContain('clipboardSerializer');
+ expect(handleDOMEvents).not.toHaveProperty('copy');
+ expect(handleDOMEvents).not.toHaveProperty('cut');
+ expect(handleDOMEvents).not.toHaveProperty('dragstart');
});
});
diff --git a/packages/app/src/editor/extensions/JsxComponentView.test.ts b/packages/app/src/editor/extensions/JsxComponentView.test.ts
index a1d4e11a3..ce9f99689 100644
--- a/packages/app/src/editor/extensions/JsxComponentView.test.ts
+++ b/packages/app/src/editor/extensions/JsxComponentView.test.ts
@@ -2,7 +2,7 @@ import { describe, expect, test } from 'bun:test';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
-import { extractPrimitiveProps, stableHash } from './JsxComponentView.tsx';
+import { extractPrimitiveProps, getElementJsxAttrs, stableHash } from './JsxComponentView.tsx';
/** Test helper: build a `ReadonlySet<string>` of reactnode-typed prop names.
* (In production the descriptor registry pre-computes this once at build
@@ -122,6 +122,32 @@ describe('extractPrimitiveProps', () => {
});
});
+describe('getElementJsxAttrs', () => {
+ test('returns attrs for element-kind jsxComponent nodes', () => {
+ const attrs = {
+ kind: 'element',
+ componentName: 'Callout',
+ props: { title: 'Heads up' },
+ };
+
+ expect(getElementJsxAttrs(attrs)).toBe(attrs);
+ });
+
+ test('returns null for expression-kind jsxComponent nodes', () => {
+ expect(
+ getElementJsxAttrs({
+ kind: 'expression',
+ sourceRaw: '{/* comment */}',
+ props: { title: 'should not be writable' },
+ }),
+ ).toBeNull();
+ });
+
+ test('returns null when kind is absent', () => {
+ expect(getElementJsxAttrs({ props: {} })).toBeNull();
+ });
+});
+
describe('stableHash', () => {
test('key-order independence — primary load-bearing invariant', () => {
expect(stableHash({ a: 1, b: 2 })).toBe(stableHash({ b: 2, a: 1 }));
@@ -151,33 +177,64 @@ describe('stableHash', () => {
});
const VIEW_FILE = join(dirname(fileURLToPath(import.meta.url)), 'JsxComponentView.tsx');
-const KIND_GUARD_RE = /attrs\.kind\s*(?:!==|===)\s*['"]element['"]/;
-const EXEMPTION_PHRASE = 'setNodeMarkup-no-kind-guard';
-
-describe('JsxComponentView kind-discriminator drift-guard (Pass 1 Minor 1)', () => {
- const GUARD_WINDOW = 40;
-
- test(`every setNodeMarkup call has a 'kind === element' guard within ${GUARD_WINDOW} lines above`, () => {
- const src = readFileSync(VIEW_FILE, 'utf8');
- const lines = src.split('\n');
- const offenders: Array<{ line: number; snippet: string }> = [];
- for (let i = 0; i < lines.length; i++) {
- const line = lines[i] ?? '';
- if (line.trim().startsWith('//')) continue;
- if (line.trim().startsWith('*')) continue;
- if (!line.includes('setNodeMarkup(')) continue;
- const window = lines.slice(Math.max(0, i - GUARD_WINDOW), i).join('\n');
- const hasGuard = KIND_GUARD_RE.test(window);
- const hasExemption = window.includes(EXEMPTION_PHRASE);
- if (!hasGuard && !hasExemption) {
- offenders.push({ line: i + 1, snippet: line.trim().slice(0, 100) });
- }
+const SET_NODE_MARKUP_GUARD_WINDOW = 40;
+
+function findSetNodeMarkupSitesWithoutElementAttrsHelper(
+ source: string,
+): Array<{ line: number; snippet: string }> {
+ const lines = source.split('\n');
+ const offenders: Array<{ line: number; snippet: string }> = [];
+ for (let i = 0; i < lines.length; i += 1) {
+ const line = lines[i] ?? '';
+ const trimmed = line.trim();
+ if (trimmed.startsWith('//') || trimmed.startsWith('*')) continue;
+ if (!line.includes('setNodeMarkup(')) continue;
+ const priorWindow = lines.slice(Math.max(0, i - SET_NODE_MARKUP_GUARD_WINDOW), i).join('\n');
+ if (!priorWindow.includes('getElementJsxAttrs(')) {
+ offenders.push({ line: i + 1, snippet: trimmed.slice(0, 100) });
}
+ }
+ return offenders;
+}
+
+describe('JsxComponentView setNodeMarkup write-boundary coverage', () => {
+ test('finder flags a setNodeMarkup call without nearby getElementJsxAttrs', () => {
+ const offenders = findSetNodeMarkupSitesWithoutElementAttrsHelper(`
+ function update() {
+ editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, null, { ...curNode.attrs }));
+ }
+ `);
+
+ expect(offenders).toEqual([
+ {
+ line: 3,
+ snippet:
+ 'editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, null, { ...curNode.attrs }));',
+ },
+ ]);
+ });
+
+ test('finder accepts a setNodeMarkup call guarded by getElementJsxAttrs', () => {
+ const offenders = findSetNodeMarkupSitesWithoutElementAttrsHelper(`
+ function update() {
+ const elementAttrs = getElementJsxAttrs(curNode.attrs);
+ if (!elementAttrs) return;
+ editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, null, { ...elementAttrs }));
+ }
+ `);
+
+ expect(offenders).toEqual([]);
+ });
+
+ test('every setNodeMarkup call site uses getElementJsxAttrs nearby', () => {
+ const source = readFileSync(VIEW_FILE, 'utf8');
+ const offenders = findSetNodeMarkupSitesWithoutElementAttrsHelper(source);
+
expect(
offenders,
- `setNodeMarkup call site(s) without a 'kind === element' guard within ${GUARD_WINDOW} lines: ` +
- `${JSON.stringify(offenders)}. Either add the guard or document the exemption ` +
- `with a comment containing the literal phrase '${EXEMPTION_PHRASE}'.`,
+ `setNodeMarkup call site(s) without a nearby getElementJsxAttrs guard: ${JSON.stringify(
+ offenders,
+ )}`,
).toEqual([]);
});
});
diff --git a/packages/app/src/editor/extensions/JsxComponentView.tsx b/packages/app/src/editor/extensions/JsxComponentView.tsx
index 9e693d9e3..ed6fd8a8b 100644
--- a/packages/app/src/editor/extensions/JsxComponentView.tsx
+++ b/packages/app/src/editor/extensions/JsxComponentView.tsx
@@ -174,6 +174,15 @@ export function extractPrimitiveProps(
return sanitizeComponentProps(result);
}
+interface ElementJsxAttrs extends Record<string, unknown> {
+ kind: 'element';
+ props: Record<string, unknown>;
+}
+
+export function getElementJsxAttrs(attrs: Record<string, unknown>): ElementJsxAttrs | null {
+ return attrs.kind === 'element' ? (attrs as ElementJsxAttrs) : null;
+}
+
const MAX_AUTO_CONVERT_RETRIES = 3;
export function JsxComponentView({ node, editor, extension, getPos, selected }: NodeViewProps) {
@@ -644,20 +653,21 @@ export function JsxComponentView({ node, editor, extension, getPos, selected }:
if (!ALIGNABLE_DESCRIPTOR_NAMES.has(curDescriptorName)) {
return;
}
- if (curNode.attrs.kind !== 'element') return;
+ const elementAttrs = getElementJsxAttrs(curNode.attrs);
+ if (!elementAttrs) return;
const isCommonMark = curDescriptorName === 'CommonMarkImage';
const nextProps = {
- ...(curNode.attrs.props as Record<string, unknown>),
+ ...elementAttrs.props,
align: value,
};
const nextAttrs = isCommonMark
? {
- ...curNode.attrs,
+ ...elementAttrs,
componentName: 'img',
props: nextProps,
sourceDirty: true,
}
- : { ...curNode.attrs, props: nextProps, sourceDirty: true };
+ : { ...elementAttrs, props: nextProps, sourceDirty: true };
runWithAlignAnimation(wrapperEl, () => {
editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, null, nextAttrs));
markUserTyping();
@@ -1024,15 +1034,16 @@ export function JsxComponentView({ node, editor, extension, getPos, selected }:
if (typeof livePos !== 'number') return;
const curNode = editor.state.doc.nodeAt(livePos);
if (!curNode) return;
- if (curNode.attrs.kind !== 'element') return;
+ const elementAttrs = getElementJsxAttrs(curNode.attrs);
+ if (!elementAttrs) return;
try {
- const currentNodeProps = (curNode.attrs.props as Record<string, unknown>) ?? {};
+ const currentNodeProps = elementAttrs.props;
const nextProps = {
...currentNodeProps,
[editableSource.propName]: value,
};
const nextAttrs = {
- ...curNode.attrs,
+ ...elementAttrs,
props: nextProps,
sourceDirty: true,
};
@@ -1077,8 +1088,9 @@ export function JsxComponentView({ node, editor, extension, getPos, selected }:
if (typeof p !== 'number') return;
const curNode = editor.state.doc.nodeAt(p);
if (!curNode) return;
- if (curNode.attrs.kind !== 'element') return;
- const currentNodeProps = (curNode.attrs.props as Record<string, unknown>) ?? {};
+ const elementAttrs = getElementJsxAttrs(curNode.attrs);
+ if (!elementAttrs) return;
+ const currentNodeProps = elementAttrs.props;
const nextProps: Record<string, unknown> = { ...currentNodeProps };
const currentAttributes = Array.isArray(curNode.attrs.attributes)
? (curNode.attrs.attributes as unknown[])
@@ -1100,7 +1112,7 @@ export function JsxComponentView({ node, editor, extension, getPos, selected }:
}
editor.view.dispatch(
editor.state.tr.setNodeMarkup(p, null, {
- ...curNode.attrs,
+ ...elementAttrs,
attributes: nextAttributes,
props: nextProps,
sourceDirty: true,