Lutra/ui/tests/editor/lint.test.ts
danny 13c76d3046 M3.3: engine wiring for Phpactor and diagnostics
- One Phpactor per open project on its own session thread, started when
  the toolchain is ready; ordered document sync from buffer.edit and the
  pump; lspStatus, diagnosticsChanged, diagnostics.list, lsp.status,
  lsp.restart; app.quit stops the server synchronously.
- Phpactor 2026.06.25.0 on Windows: keep-alive $/lutra/tick (its event
  loop only runs on stdin input), in-process diagnostics and a polling
  watcher through initializationOptions (amphp subprocesses fail), 500 ms
  shutdown timeout.
- Editor lint extension (underlines, gutter dots, hover; tokens only) and
  a Problems tab in the bottom panel; problems.open Mod+Alt+P.
- Tests: engine tests against the fake server (incl. sync proptest), an
  ignored real-Phpactor test, a TCP test, Vitest for lint and Problems.
- Docs: architecture, gaps G6, parity (Inspections 4% est.), ui-parity,
  roadmap, ROADMAP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 18:58:32 +02:00

173 lines
5.5 KiB
TypeScript

/**
* Tests of the editor's diagnostics (M3.3, `lint.ts`): conversion to
* CodeMirror positions (CRLF, clamping, order, source and code), the view
* plugin applying a path's diagnostics when a view shows the state and on
* every publication, clearing, and colours from tokens only.
*/
import { diagnosticCount, forEachDiagnostic, type Diagnostic } from "@codemirror/lint";
import { EditorState, Text } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
clearDiagnostics,
diagnosticsOf,
lintExtensions,
publishDiagnostics,
toCodeMirror,
type DiagnosticItem,
} from "../../src/components/editor/lint";
/** Views created by a test, destroyed after it. */
let views: EditorView[] = [];
/**
* A diagnostic on one line.
*
* @param line - 0-based line.
* @param from - start column.
* @param to - end column.
* @param extra - fields to override.
*/
function item(line: number, from: number, to: number, extra: Partial<DiagnosticItem> = {}): DiagnosticItem {
return {
range: { start: { line, column: from }, end: { line, column: to } },
severity: "error",
message: `problem at ${String(line)}:${String(from)}`,
...extra,
};
}
/** Lets the plugin's microtask run. */
async function settle(): Promise<void> {
await Promise.resolve();
await Promise.resolve();
}
/**
* A view showing `doc` as `path`.
*
* @param path - project-relative path.
* @param doc - the text.
*/
function viewOf(path: string, doc: string): EditorView {
const view = new EditorView({
state: EditorState.create({ doc, extensions: lintExtensions(path) }),
parent: document.body,
});
views.push(view);
return view;
}
/**
* The diagnostics in a state.
*
* @param state - the editor state.
*/
function shown(state: EditorState): Diagnostic[] {
const out: Diagnostic[] = [];
forEachDiagnostic(state, (d, from, to) => out.push({ ...d, from, to }));
return out;
}
afterEach(() => {
for (const view of views) {
view.destroy();
}
views = [];
clearDiagnostics();
});
describe("toCodeMirror", () => {
it("converts lines and UTF-16 columns, CRLF-safe and clamped", () => {
const doc = Text.of(["<?php", "echo 🦦 $x;", "end"]);
const out = toCodeMirror(doc, [
item(1, 8, 10, { severity: "warning", source: "phpactor", code: "W1" }),
item(0, 0, 3),
item(9, 0, 99, { severity: "hint" }),
item(2, 2, 1, { severity: "info" }),
]);
expect(out.map((d) => [d.from, d.to, d.severity])).toEqual([
[0, 3, "error"],
[14, 16, "warning"],
[18, 21, "hint"],
[20, 20, "info"],
]);
expect(out[1]?.source).toBe("phpactor W1");
expect(out[0]?.source).toBeUndefined();
});
});
describe("lintExtensions", () => {
it("shows a file's diagnostics and follows every publication", async () => {
publishDiagnostics("a.php", [item(1, 0, 4)]);
const view = viewOf("a.php", "<?php\necho 1;\n");
await settle();
expect(shown(view.state).map((d) => [d.from, d.to, d.message])).toEqual([[6, 10, "problem at 1:0"]]);
expect(view.dom.querySelector(".cm-lintRange-error")).not.toBeNull();
expect(view.dom.querySelector(".cm-gutter-lint .cm-lint-marker-error")).not.toBeNull();
publishDiagnostics("a.php", [item(0, 0, 5, { severity: "warning" }), item(1, 5, 6)]);
expect(diagnosticCount(view.state)).toBe(2);
expect(view.dom.querySelector(".cm-lintRange-warning")).not.toBeNull();
publishDiagnostics("other.php", [item(0, 0, 1)]);
expect(diagnosticCount(view.state)).toBe(2);
publishDiagnostics("a.php", []);
expect(diagnosticCount(view.state)).toBe(0);
expect(diagnosticsOf("a.php")).toEqual([]);
});
it("brings a background state up to date when it is shown again", async () => {
const view = viewOf("a.php", "<?php\n");
await settle();
const background = view.state;
view.setState(EditorState.create({ doc: "x", extensions: lintExtensions("b.php") }));
publishDiagnostics("a.php", [item(0, 0, 5)]);
expect(diagnosticCount(view.state)).toBe(0);
view.setState(background);
await settle();
expect(diagnosticCount(view.state)).toBe(1);
});
it("maps diagnostics through edits until the next publication", async () => {
publishDiagnostics("a.php", [item(0, 6, 10)]);
const view = viewOf("a.php", "<?php echo;");
await settle();
view.dispatch({ changes: { from: 0, insert: "\n\n" } });
expect(shown(view.state).map((d) => [d.from, d.to])).toEqual([[8, 12]]);
});
it("clears every file on clearDiagnostics", async () => {
publishDiagnostics("a.php", [item(0, 0, 1)]);
const view = viewOf("a.php", "<?php");
await settle();
expect(diagnosticCount(view.state)).toBe(1);
clearDiagnostics();
expect(diagnosticCount(view.state)).toBe(0);
});
it("stops listening when the view is destroyed", async () => {
const view = viewOf("a.php", "<?php");
await settle();
view.destroy();
views = [];
expect(() => {
publishDiagnostics("a.php", [item(0, 0, 1)]);
}).not.toThrow();
});
it("adds nothing without a path", () => {
expect(lintExtensions("")).toEqual([]);
});
it("takes its colours from tokens only", () => {
const source = readFileSync(join(import.meta.dirname, "..", "..", "src", "components", "editor", "lint.ts"), "utf8");
expect(source).not.toMatch(/#[0-9a-fA-F]{3,8}\b|rgba?\(|hsla?\(/);
for (const colour of source.matchAll(/var\((--[a-z0-9-]+)\)/g)) {
expect(colour[1]).toMatch(/^--lt-/);
}
});
});