The first CodeMirror view and the first render of the editor area, the palette and the settings dialog in a jsdom worker cost 0.3-0.6 s once. The first test of EditorArea, language and modals paid it inside its own 5 s budget and timed out under load. A beforeAll warm-up now pays it once; no timeout changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
173 lines
6.4 KiB
TypeScript
173 lines
6.4 KiB
TypeScript
/**
|
|
* Tests of the editor's language support (`language.ts`): the language
|
|
* picked per file extension, and the highlight classes (`lt-syntax-*`) on
|
|
* the spans CodeMirror draws for PHP, embedded HTML and the other
|
|
* languages, each coloured by its `--lt-syntax-*` token.
|
|
*/
|
|
import { forceParsing, syntaxTree } from "@codemirror/language";
|
|
import { EditorState } from "@codemirror/state";
|
|
import { EditorView } from "@codemirror/view";
|
|
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
import { editorExtensions, lutraEditorTheme } from "../../src/components/editor/extensions";
|
|
import { languageOf, SYNTAX_TOKENS } from "../../src/components/editor/language";
|
|
|
|
/** Views to destroy after each test. */
|
|
let views: EditorView[] = [];
|
|
|
|
/** jsdom has no layout; give text ranges empty rectangles. */
|
|
function stubRangeGeometry(): void {
|
|
const empty = (): DOMRectList => Object.assign([] as DOMRect[], { item: () => null });
|
|
Range.prototype.getClientRects = empty;
|
|
Range.prototype.getBoundingClientRect = () => new DOMRect(0, 0, 0, 0);
|
|
}
|
|
|
|
/**
|
|
* Shows a document in a view with the extensions of a file path, parsed
|
|
* completely before it returns. The parse is forced on the view itself:
|
|
* left alone, the view parses in time-budgeted slices, so under load the
|
|
* nested HTML layer of a PHP file could still be missing when a test reads
|
|
* the spans. Plain text has nothing to parse.
|
|
*
|
|
* @param path - the file path that picks the language.
|
|
* @param doc - the text.
|
|
* @throws Error if the parse does not finish within a minute.
|
|
*/
|
|
function show(path: string, doc: string): EditorView {
|
|
const view = new EditorView({ state: EditorState.create({ doc, extensions: editorExtensions(path) }), parent: document.body });
|
|
views.push(view);
|
|
if (languageOf(path) !== null && (!forceParsing(view, doc.length, 60_000) || syntaxTree(view.state).length < doc.length)) {
|
|
throw new Error(`parse of ${path} did not finish`);
|
|
}
|
|
return view;
|
|
}
|
|
|
|
/**
|
|
* The text of every span with a highlight class, by token.
|
|
*
|
|
* @param view - the view.
|
|
*/
|
|
function highlighted(view: EditorView): Record<string, string[]> {
|
|
const out: Record<string, string[]> = {};
|
|
for (const token of SYNTAX_TOKENS) {
|
|
const spans = [...view.contentDOM.querySelectorAll(`.lt-syntax-${token}`)];
|
|
if (spans.length > 0) {
|
|
out[token] = spans.map((span) => span.textContent);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/** Destroys every view a test showed. */
|
|
function destroyViews(): void {
|
|
for (const view of views) {
|
|
view.destroy();
|
|
}
|
|
views = [];
|
|
}
|
|
|
|
/**
|
|
* Pays the one-time cost of the first highlighted PHP view outside any
|
|
* test. The first view in a jsdom worker injects CodeMirror's base theme
|
|
* and builds the PHP, HTML, CSS and JavaScript parsers cold (about 0.5 s
|
|
* on an idle machine, against 5-30 ms for later views). Without this, the
|
|
* first highlighting test paid that cost inside its own 5 s budget and
|
|
* timed out under load.
|
|
*/
|
|
function warmUpHighlighting(): void {
|
|
stubRangeGeometry();
|
|
show("a.php", '<div class="a"><?php\nfunction f(int $a): float { return $a * 1.5; }\n?></div>\n<script>let x = 1;</script>\n');
|
|
destroyViews();
|
|
}
|
|
|
|
beforeAll(warmUpHighlighting);
|
|
|
|
beforeEach(() => {
|
|
stubRangeGeometry();
|
|
});
|
|
|
|
afterEach(destroyViews);
|
|
|
|
describe("languageOf", () => {
|
|
it.each([
|
|
["src/Controller/Home.php", "php"],
|
|
["templates/view.PHTML", "php"],
|
|
["legacy\\lib.inc", "php"],
|
|
["public/index.html", "html"],
|
|
["assets/app.css", "css"],
|
|
["assets/app.js", "javascript"],
|
|
["assets/app.tsx", "typescript"],
|
|
["composer.json", "json"],
|
|
["config/services.yaml", "yaml"],
|
|
[".github/ci.yml", "yaml"],
|
|
["README.md", "markdown"],
|
|
])("%s is %s", (path, id) => {
|
|
expect(languageOf(path)).toBe(id);
|
|
});
|
|
|
|
it.each(["Makefile", ".env", "notes.txt", "archive.tar.gz", "dir.php/file", ".php", ""])(
|
|
"%s is plain text",
|
|
(path) => {
|
|
expect(languageOf(path)).toBeNull();
|
|
},
|
|
);
|
|
});
|
|
|
|
describe("highlighting", () => {
|
|
it("marks PHP tokens with lt-syntax classes", () => {
|
|
const view = show(
|
|
"a.php",
|
|
[
|
|
"<?php",
|
|
"// a note",
|
|
"namespace App;",
|
|
"final class Shop extends Base {",
|
|
" const LIMIT = 10;",
|
|
" public function total(int $count): float {",
|
|
' return $count * 4.5 + strlen("otter") + self::LIMIT;',
|
|
" }",
|
|
"}",
|
|
"$ok = true;",
|
|
].join("\n"),
|
|
);
|
|
const marks = highlighted(view);
|
|
expect(marks.comment).toContain("// a note");
|
|
expect(marks.keyword).toEqual(expect.arrayContaining(["namespace", "final", "class", "extends", "public", "function", "return"]));
|
|
expect(marks.type).toEqual(expect.arrayContaining(["Shop", "int", "float"]));
|
|
expect(marks.variable).toEqual(expect.arrayContaining(["$count", "$ok"]));
|
|
expect(marks.number).toEqual(expect.arrayContaining(["10", "4.5"]));
|
|
expect(marks.string).toContain('"otter"');
|
|
expect(marks.function).toEqual(expect.arrayContaining(["total", "strlen"]));
|
|
expect(marks.constant).toContain("true");
|
|
expect(marks.operator).toEqual(expect.arrayContaining(["*", "+", "="]));
|
|
});
|
|
|
|
it("marks the HTML around PHP blocks", () => {
|
|
const marks = highlighted(show("view.php", '<div class="box"><?= $name ?></div>\n'));
|
|
expect(marks.tag).toEqual(expect.arrayContaining(["div"]));
|
|
expect(marks.attribute).toContain("class");
|
|
expect(marks.variable).toContain("$name");
|
|
});
|
|
|
|
it.each([
|
|
["composer.json", '{"name": "lutra/demo", "n": 1}', "string", '"lutra/demo"'],
|
|
["ci.yml", "on: push # build\n", "comment", "# build"],
|
|
["app.ts", "const x: number = 1;\n", "keyword", "const"],
|
|
["app.css", "a { color: red; }\n", "tag", "a"],
|
|
["README.md", "# Lutra\n", "keyword", " Lutra"],
|
|
])("highlights %s", (path, doc, token, text) => {
|
|
expect(highlighted(show(path, doc))[token]).toContain(text);
|
|
});
|
|
|
|
it("leaves plain text unmarked", () => {
|
|
expect(highlighted(show("notes.txt", "class Foo { $x = 1; }"))).toEqual({});
|
|
});
|
|
|
|
it("colours every highlight class with its theme token", () => {
|
|
const view = new EditorView({ state: EditorState.create({ extensions: lutraEditorTheme }), parent: document.body });
|
|
views.push(view);
|
|
const css = [...document.querySelectorAll("style")].map((style) => style.textContent).join("\n");
|
|
for (const token of SYNTAX_TOKENS) {
|
|
expect(css).toMatch(new RegExp(`\\.lt-syntax-${token}\\s*\\{\\s*color:\\s*var\\(--lt-syntax-${token}\\)`));
|
|
}
|
|
});
|
|
});
|