Lutra/ui/tests/editor/assist/markdown.test.ts
danny a212a44490 M3.4: completion, hover and signature help from Phpactor
Engine (crates/engine/src/cmd/lsp_assist.rs), on M3.5's pending outcomes:
- lsp.completion {id, position, triggerCharacter?}: items with label,
  kind, detail, Markdown documentation, insertText, textEdit,
  additionalTextEdits, sortText; sorted, capped at 500 (cut lists are
  incomplete), snippets turned into plain text.
- lsp.hover {id, position}: Markdown contents and range.
- lsp.signatureHelp {id, position}: signatures with UTF-16 parameter spans.
- The request is queued after every didChange; the answer is converted
  in the reply callback, off the engine lock. Positions are checked
  against the buffer. Server strings and lists are capped.
- lutra-lsp-fake answers completion and signature help.

Editor (ui/src/components/editor/assist/):
- Completion source with Lutra's triggers (identifiers, $, ->, ?->, ::,
  namespace \; not in strings or comments unless asked), the server's
  ranking as boost, text edits and imports applied in UTF-16 (CRLF too),
  Lucide kind icons.
- Hover documentation (350 ms) and editor.quickDocumentation (Mod+I).
- Signature help on ( and , following the cursor; editor.parameterInfo.
- editor.complete.
- Lutra's own Markdown renderer without HTML (XSS-tested), tokens only.
- Requests wait for the buffer sync to be idle and use runWaiting.
- New dependency @codemirror/autocomplete (MIT, already transitive).

Tests: 6 engine tests against the fake server, 9 unit tests, an ignored
real-Phpactor latency test, 29 Vitest tests; palette and menu tests list
the new UI commands.

Measured with the real Phpactor (Windows 11, debug build, 25 ms tick):
- Completion 61-109 ms while typing (median 76 ms; ~107 ms in the app).
- Hover and signature help ~16 ms median.
- A useful list 2.3 s after opening a file in a 20-class project, ~65 s
  for 2,000 classes (indexing).
- Phpactor 56-71 MB working set, ~6% of one core while typing.

Live-checked in the app on port 7934. Docs: Code completion 0% -> 5%
(est.), feature depth ~4.1% -> ~4.6%, breadth 26/155, architecture, G6,
ui-parity, language-parity, roadmap, README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:38:07 +02:00

97 lines
4.2 KiB
TypeScript

/**
* Tests of the safe Markdown renderer (M3.4, `assist/markdown.ts`): the
* supported blocks and inlines, and that nothing in a server answer turns
* into HTML, script, a link target or an image source (XSS).
*/
import { describe, expect, it } from "vitest";
import { MAX_MARKDOWN, renderMarkdown } from "../../../src/components/editor/assist/markdown";
/** Tag names of every element below `root`. */
function tags(root: Element): string[] {
return [...root.querySelectorAll("*")].map((e) => e.tagName.toLowerCase());
}
describe("renderMarkdown", () => {
it("renders code fences, headings, rules, lists and paragraphs", () => {
const root = renderMarkdown(
[
"```php",
"class A { }",
"```",
"",
"---",
"",
"# Title",
"First line",
"second line ",
"third",
"",
"- one",
"- two",
"1. first",
"> quoted",
].join("\n"),
);
expect(root.className).toBe("lt-markdown");
const code = root.querySelector("pre > code");
expect(code?.textContent).toBe("class A { }");
expect((code as HTMLElement | null)?.dataset.language).toBe("php");
expect(root.querySelector("hr")).not.toBeNull();
expect(root.querySelector(".lt-markdown-heading strong")?.textContent).toBe("Title");
const paragraph = [...root.querySelectorAll("p")].find((p) => p.textContent.startsWith("First"));
expect(paragraph?.textContent).toBe("First line second linethird");
expect(paragraph?.querySelector("br")).not.toBeNull();
expect([...root.querySelectorAll("ul > li")].map((li) => li.textContent)).toEqual(["one", "two"]);
expect(root.querySelector("ol > li")?.textContent).toBe("first");
expect(root.querySelector("blockquote")?.textContent).toBe("quoted");
});
it("renders inline code, strong, emphasis, links and escapes", () => {
const root = renderMarkdown("Use `a*b*c` with **bold**, *em*, _em2_, snake_case_name, [docs](https://php.net) and \\*not em\\*.");
const p = root.querySelector("p");
expect(p?.querySelector("code")?.textContent).toBe("a*b*c");
expect(p?.querySelector("strong")?.textContent).toBe("bold");
expect([...(p?.querySelectorAll("em") ?? [])].map((e) => e.textContent)).toEqual(["em", "em2"]);
expect(p?.querySelector(".lt-markdown-link")?.textContent).toBe("docs");
expect(p?.textContent).toContain("snake_case_name");
expect(p?.textContent).toContain("*not em*.");
});
it("never creates HTML, links, images or script from the source (XSS)", () => {
const attacks = [
"<img src=x onerror=alert(1)>",
"<script>alert(1)</script>",
"[click](javascript:alert(1))",
"![x](https://evil.example/x.png)",
"<a href=\"javascript:alert(1)\">a</a>",
"<svg onload=alert(1)>",
"```\n</code><script>alert(1)</script>\n```",
"**<iframe src=//evil>**",
"<https://evil.example>",
"&lt;b&gt; &#60;script&#62;",
];
for (const attack of attacks) {
const root = renderMarkdown(attack);
const allowed = ["p", "pre", "code", "strong", "em", "ul", "ol", "li", "hr", "blockquote", "span", "br"];
expect(tags(root).every((tag) => allowed.includes(tag)), attack).toBe(true);
for (const element of root.querySelectorAll("*")) {
const names = [...element.attributes].map((a) => a.name);
expect(names.every((n) => n === "class" || n === "data-language"), attack).toBe(true);
}
}
const img = renderMarkdown("<img src=x onerror=alert(1)>");
expect(img.textContent).toBe("<img src=x onerror=alert(1)>");
expect(renderMarkdown("&lt;b&gt;").textContent).toBe("&lt;b&gt;");
const fenced = renderMarkdown("```\"onmouseover=alert(1)\n```");
expect(fenced.querySelector<HTMLElement>("code")?.dataset.language).toBeUndefined();
});
it("caps the source and survives deep nesting", () => {
const long = renderMarkdown("a".repeat(MAX_MARKDOWN + 100));
expect(long.textContent.length).toBe(MAX_MARKDOWN);
const nested = renderMarkdown("*".repeat(200) + "x" + "*".repeat(200));
expect(nested.textContent).toContain("x");
const brackets = renderMarkdown("[".repeat(500) + "x");
expect(brackets.textContent).toContain("x");
});
});