1import { expect, it, vi } from "vite-plus/test";
2import { detectLinks } from "../src/links.ts";
3
4it.each([
5 String.raw`C:\repo\src\file.ts`,
6 "C:/repo/src/file.ts",
7 String.raw`\\server\share\file.ts`,
8 "~/src/file.ts",
9 "/repo/src/file.ts",
10 "../src/file.ts",
11 "src/file.ts",
12 String.raw`src\file.ts`,
13])("detects the full host path %s with its source position", (path) => {
14 const validate = vi.fn(() => true);
15 const text = `error (${path}:12:3):`;
16 expect(detectLinks(text, { validate })).toEqual([
17 {
18 kind: "path",
19 start: 7,
20 end: 7 + path.length + 5,
21 text: `${path}:12:3`,
22 target: path,
23 line: 12,
24 column: 3,
25 },
26 ]);
27 expect(validate).toHaveBeenCalledExactlyOnceWith(path);
28});
29
30it("validates the complete target and never falls back to an allowed basename", () => {
31 const validate = vi.fn((target: string) => target === "file.ts");
32 expect(detectLinks(String.raw`C:\repo\src\file.ts:12:3 ~/src/file.ts:7`, { validate })).toEqual(
33 [],
34 );
35 expect(validate.mock.calls.map(([target]) => target)).toEqual([
36 String.raw`C:\repo\src\file.ts`,
37 "~/src/file.ts",
38 ]);
39});
40
41it("keeps URL matches distinct from filesystem paths", () => {
42 const links = detectLinks("https://example.test/src/file.ts:12 ~/src/file.ts:3", {
43 validate: () => true,
44 });
45 expect(links.map(({ kind, target, line }) => ({ kind, target, line }))).toEqual([
46 { kind: "url", target: "https://example.test/src/file.ts:12", line: null },
47 { kind: "path", target: "~/src/file.ts", line: 3 },
48 ]);
49});