Skip to content

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

test(editor): generative content-integrity round-trip + correct the code-drop comment

Sergey Kozyrenkoc0ac3d6f907f
diff --git a/frontend/src/components/shared/markdown-editor/editor-content-integrity.test.ts b/frontend/src/components/shared/markdown-editor/editor-content-integrity.test.ts
new file mode 100644
index 00000000..18ba0dae
--- /dev/null
+++ b/frontend/src/components/shared/markdown-editor/editor-content-integrity.test.ts
@@ -0,0 +1,107 @@
+import { Editor } from '@tiptap/core';
+import { beforeAll, describe, expect, it } from 'vitest';
+
+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;
+});
+
+const roundTrip = (md: string): string => {
+ const editor = new Editor({ content: md, contentType: 'markdown', extensions: createMarkdownExtensions() });
+ const out = editor.getMarkdown();
+ editor.destroy();
+
+ return out;
+};
+
+const mulberry32 = (seed: number) => () => {
+ seed = (seed + 0x6d2b79f5) | 0;
+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
+
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
+};
+
+// Content atoms that MUST survive load→serialize verbatim: identifiers/dunders, Go-template variables,
+// xml-like tags, regex/path backslashes, HTML entities, C++. Whitespace/formatting may reflow; these
+// bytes may not disappear or mutate.
+const ATOMS = [
+ '__init__',
+ '__call__',
+ 'os.execute()',
+ 'ngx_http_lua_module',
+ 'snake_case_name',
+ '{{.TargetURL}}',
+ '{{.Lang}}',
+ '{{- if .Enabled}}',
+ '<container_environment>',
+ '<input>',
+ '<br>',
+ 'match \\d+ then \\w*',
+ 'C:\\Users\\bob\\a.txt',
+ '&lt;script&gt;',
+ 'AT&T',
+ 'x &amp; y',
+ 'C++ then C++',
+ '~10% left',
+];
+
+const WORDS = ['firewall', 'payload', 'exploit', 'nmap', 'recon', 'shell', 'token', 'vector'];
+
+// Block contexts that keep inline content literal: paragraph, heading, bullet item, ordered item,
+// blockquote, inline code. (ordered>bullet>code nesting is intentionally NOT generated — it hits the
+// pinned @tiptap/markdown code-drop bug covered separately.)
+const wrap = (context: number, text: string): string => {
+ switch (context) {
+ case 1:
+ return `# ${text}`;
+ case 2:
+ return `- ${text}`;
+ case 3:
+ return `1. ${text}`;
+ case 4:
+ return `> ${text}`;
+ case 5:
+ return `\`${text}\``;
+ default:
+ return text;
+ }
+};
+
+describe('generative content-integrity — atoms survive load↔serialize across random combinations', () => {
+ it('300 seeded random docs converge and drop no atom', () => {
+ const rng = mulberry32(0xc0ffee);
+ const pick = <T>(arr: T[]): T => arr[Math.floor(rng() * arr.length)] as T;
+
+ for (let iteration = 0; iteration < 300; iteration++) {
+ const blockCount = 1 + Math.floor(rng() * 5);
+ const placed: string[] = [];
+ const blocks: string[] = [];
+
+ for (let block = 0; block < blockCount; block++) {
+ const atom = pick(ATOMS);
+
+ placed.push(atom);
+ const inline = rng() < 0.5 ? `${pick(WORDS)} ${atom} ${pick(WORDS)}` : `${atom} ${pick(WORDS)}`;
+ blocks.push(wrap(Math.floor(rng() * 6), inline));
+ }
+
+ const doc = blocks.join('\n\n');
+ const out = roundTrip(doc);
+ const out2 = roundTrip(out);
+
+ expect(out2, `did not converge (seed 0xc0ffee, iteration ${iteration}):\n${doc}`).toBe(out);
+
+ for (const atom of placed) {
+ expect(out.includes(atom), `atom "${atom}" lost (iteration ${iteration}):\n${doc}\n--> ${out}`).toBe(
+ true,
+ );
+ }
+ }
+ });
+});
diff --git a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
index e52f7f87..ec4a5e0a 100644
--- a/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
+++ b/frontend/src/components/shared/markdown-editor/markdown-editor-extensions.test.ts
@@ -191,11 +191,13 @@ describe('nesting & sequencing — content preserved and converges (≤2 saves)'
});
});
-// KNOWN BUG (marked parser, not our customizations — vanilla @tiptap/markdown repros it). A code block
-// nested in a bullet sublist that is itself inside an ordered list is SILENTLY DROPPED. markdown-it kept
-// it. 0 corpus docs hit it. This test pins the CURRENT (buggy) behavior so we notice if an upstream
-// @tiptap/markdown release fixes it (then flip it to a passing round-trip + drop this note).
-describe('KNOWN UPSTREAM BUG — ordered > bullet > code drops the code block', () => {
+// KNOWN @tiptap/markdown BUG (not marked, not our customizations). A code block nested in a bullet sublist
+// inside an ordered list is SILENTLY DROPPED. Root cause verified: marked's lexer KEEPS the code token
+// (marked.lexer retains it, marked.parse emits <pre><code>); the loss is downstream in @tiptap/markdown's
+// list-item token→ProseMirror reconstruction, so a marked-side hook can't recover it and there is no clean
+// fix short of patching @tiptap. 0 corpus docs hit it. This test pins the CURRENT (buggy) behavior so we
+// notice if an upstream @tiptap/markdown release fixes it (then flip it to a passing round-trip + drop this).
+describe('KNOWN @tiptap/markdown BUG — ordered > bullet > code drops the code block', () => {
it('still loses the deeply-nested code (remove this test once @tiptap/markdown fixes it)', () => {
const out = roundTrip('1. lvl1\n - lvl2\n\n ```\n deepcode\n ```');