Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

test(editor): regression-cover the mismatched-transaction list crash

Sergey Kozyrenko4d1e1161eed2
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-history.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-history.test.ts
new file mode 100644
index 00000000..7ba5b8eb
--- /dev/null
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-history.test.ts
@@ -0,0 +1,71 @@
+import { Editor, Extension } from '@tiptap/core';
+import { Plugin, PluginKey } from '@tiptap/pm/state';
+import { beforeAll, expect, it } from 'vitest';
+
+import { resetUndoHistory } from './markdown-editor';
+import { createMarkdownExtensions } from './markdown-editor-extensions';
+
+beforeAll(() => {
+ document.elementFromPoint = () => null;
+ const r = { bottom: 0, height: 0, left: 0, right: 0, toJSON: () => ({}), top: 0, width: 0, x: 0, y: 0 };
+ Range.prototype.getBoundingClientRect = () => r as DOMRect;
+ Range.prototype.getClientRects = () =>
+ ({ item: () => null, length: 0, [Symbol.iterator]: [][Symbol.iterator] }) as unknown as DOMRectList;
+});
+
+// Reproduces the production-only crash class: `view.updateState` inside resetUndoHistory can synchronously
+// advance `view.state` (a plugin view dispatching during reconfigure), so a poke transaction built from the
+// pre-updateState `newState` has a `before` doc that no longer matches — ProseMirror throws "Applying a
+// mismatched transaction". This synthetic plugin forces that advance on demand; jsdom's real editor never
+// hits the browser timing that triggers it, so the fault has to be simulated to be regression-tested.
+let armed = false;
+
+const AdvanceStateOnUpdate = Extension.create({
+ addProseMirrorPlugins() {
+ return [
+ new Plugin({
+ key: new PluginKey('advanceStateOnUpdate'),
+ // Dispatch from the plugin-view FACTORY, not `update`: swapping the history plugin changes the
+ // plugin set, so `view.updateState` recreates plugin views (factory re-runs) rather than
+ // calling `update`. Dispatching here fires mid-reconfigure, advancing view.state exactly as the
+ // real prod crash does.
+ view: (view) => {
+ if (armed) {
+ armed = false;
+
+ let pos = 1;
+
+ view.state.doc.descendants((node, nodePos) => {
+ if (node.isText) {
+ pos = nodePos;
+
+ return false;
+ }
+
+ return true;
+ });
+
+ view.dispatch(view.state.tr.insertText('Z', pos));
+ }
+
+ return {};
+ },
+ }),
+ ];
+ },
+ name: 'advanceStateOnUpdate',
+});
+
+it('resetUndoHistory survives view.state advancing during updateState', () => {
+ const editor = new Editor({
+ content: '- first item\n- second item',
+ contentType: 'markdown',
+ extensions: [...createMarkdownExtensions(), AdvanceStateOnUpdate],
+ });
+
+ armed = true;
+
+ expect(() => resetUndoHistory(editor)).not.toThrow();
+
+ editor.destroy();
+});
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor.tsx b/frontend/src/components/shared/markdown-editor/markdown-editor.tsx
index 595588cf..76c26692 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor.tsx
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor.tsx
@@ -51,7 +51,7 @@ export interface MarkdownEditorProps {
// Clears the undo/redo stack by swapping the history plugin for a fresh instance. MUST NOT run on user
// edits (value changes carry user input too) — that would wipe the user's own Ctrl+Z history.
-const resetUndoHistory = (editor: Editor): void => {
+export const resetUndoHistory = (editor: Editor): void => {
const { state, view } = editor;
// A fresh history() shares prosemirror-history's module-level singleton PluginKey, so match the
// existing plugin by key identity rather than sniffing the stringified key name.