summaryrefslogtreecommitdiffhomepage
path: root/packages/core/tests/lsp/diagnostic.test.ts
blob: 93ffde9ee9ceebf3c7de155bdcfdd25ec160cd19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, expect, it } from "vitest";
import type { Diagnostic } from "vscode-languageserver-types";
import { pretty, report } from "../../src/lsp/diagnostic.js";

function diag(partial: Partial<Diagnostic> & { message: string }): Diagnostic {
	return {
		range: { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } },
		severity: 1,
		...partial,
	};
}

describe("lsp/diagnostic", () => {
	describe("pretty", () => {
		it("renders 1-based line/col with severity label", () => {
			const out = pretty(
				diag({
					message: "Expected number",
					range: { start: { line: 4, character: 2 }, end: { line: 4, character: 8 } },
				}),
			);
			expect(out).toBe("ERROR [5:3] Expected number");
		});

		it("maps severities to labels", () => {
			expect(pretty(diag({ message: "w", severity: 2 }))).toMatch(/^WARN /);
			expect(pretty(diag({ message: "i", severity: 3 }))).toMatch(/^INFO /);
			expect(pretty(diag({ message: "h", severity: 4 }))).toMatch(/^HINT /);
		});

		it("defaults missing severity to ERROR", () => {
			expect(pretty(diag({ message: "x", severity: undefined }))).toMatch(/^ERROR /);
		});
	});

	describe("report", () => {
		it("returns empty string when there are no errors", () => {
			expect(report("a.luau", [])).toBe("");
			// Warnings only → still empty (errors-only).
			expect(report("a.luau", [diag({ message: "w", severity: 2 })])).toBe("");
		});

		it("wraps errors in a <diagnostics file> block", () => {
			const out = report("src/a.luau", [diag({ message: "boom" })]);
			expect(out).toContain('<diagnostics file="src/a.luau">');
			expect(out).toContain("ERROR [1:1] boom");
			expect(out).toContain("</diagnostics>");
		});

		it("filters out non-error severities", () => {
			const out = report("a.luau", [
				diag({ message: "err" }),
				diag({ message: "warn", severity: 2 }),
			]);
			expect(out).toContain("err");
			expect(out).not.toContain("warn");
		});

		it("caps at 20 and notes the remainder", () => {
			const issues = Array.from({ length: 25 }, (_, i) => diag({ message: `e${i}` }));
			const out = report("a.luau", issues);
			expect(out).toContain("... and 5 more");
			expect(out).toContain("e0");
			expect(out).not.toContain("e24");
		});
	});
});