Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 fileRoutePathSegments,
5 needsWorkspaceFileContents,
6 resolveFileLinkPath,
7 resolveWorkspaceRelativeFilePath,
8} from "./filePath";
9
10describe("resolveFileLinkPath", () => {
11 it.each([
12 ["/home/maria/project", "~/.agents/AGENTS.md", "/home/maria/.agents/AGENTS.md"],
13 ["/Users/maria/project", "~/notes.md", "/Users/maria/notes.md"],
14 ["C:\\Users\\maria\\project", "~\\.agents\\AGENTS.md", "C:\\Users\\maria\\.agents\\AGENTS.md"],
15 ])("expands a home link from %s", (cwd, path, expected) => {
16 expect(resolveFileLinkPath(cwd, path)).toBe(expected);
17 expect(fileRoutePathSegments(expected).join("/")).toBe(expected);
18 });
19
20 it("does not turn an unresolved home into a workspace path", () => {
21 expect(resolveFileLinkPath("/repo", "~/notes.md")).toBeNull();
22 expect(resolveFileLinkPath(null, "~/notes.md")).toBeNull();
23 expect(resolveFileLinkPath("/repo", "../notes.md")).toBeNull();
24 });
25});
26
27describe("resolveWorkspaceRelativeFilePath", () => {
28 it("converts absolute paths inside the workspace", () => {
29 expect(
30 resolveWorkspaceRelativeFilePath("/Users/julius/repo", "/Users/julius/repo/src/main.ts"),
31 ).toBe("src/main.ts");
32 expect(resolveWorkspaceRelativeFilePath("C:\\repo", "c:\\repo\\src\\main.ts")).toBe(
33 "src/main.ts",
34 );
35 });
36
37 it("recognizes home links inside the workspace", () => {
38 expect(resolveWorkspaceRelativeFilePath("/home/maria/project", "~/project/src/main.ts")).toBe(
39 "src/main.ts",
40 );
41 expect(
42 resolveWorkspaceRelativeFilePath("/home/maria/project", "~/.agents/AGENTS.md"),
43 ).toBeNull();
44 });
45
46 it("rejects paths outside the workspace", () => {
47 expect(resolveWorkspaceRelativeFilePath("/repo", "/other/main.ts")).toBeNull();
48 expect(resolveWorkspaceRelativeFilePath("/repo", "../other/main.ts")).toBeNull();
49 expect(resolveWorkspaceRelativeFilePath("/repo", "/repo/../outside.txt")).toBeNull();
50 expect(resolveWorkspaceRelativeFilePath(null, "/repo/main.ts")).toBeNull();
51 });
52});
53
54describe("needsWorkspaceFileContents", () => {
55 it("reads a text file while its metadata is still pending, but not a directory", () => {
56 const input = { relativePath: "package.json", activeMode: "source" } as const;
57 expect(needsWorkspaceFileContents({ ...input, metadataKind: undefined })).toBe(true);
58 expect(needsWorkspaceFileContents({ ...input, metadataKind: "directory" })).toBe(false);
59 });
60});