Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 fileRoutePathSegments,
5 isAudioPreviewFile,
6 isMarkdownPreviewFile,
7 needsWorkspaceFileContents,
8 resolveFileLinkPath,
9 resolveMarkdownFileLinkPath,
10 resolveWorkspaceRelativeFilePath,
11} from "./filePath";
12
13describe("resolveFileLinkPath", () => {
14 it.each([
15 ["/home/maria/project", "~/.agents/AGENTS.md", "/home/maria/.agents/AGENTS.md"],
16 ["/Users/maria/project", "~/notes.md", "/Users/maria/notes.md"],
17 ["C:\\Users\\maria\\project", "~\\.agents\\AGENTS.md", "C:\\Users\\maria\\.agents\\AGENTS.md"],
18 ])("expands a home link from %s", (cwd, path, expected) => {
19 expect(resolveFileLinkPath(cwd, path)).toBe(expected);
20 expect(fileRoutePathSegments(expected).join("/")).toBe(expected);
21 });
22
23 it("does not turn an unresolved home into a workspace path", () => {
24 expect(resolveFileLinkPath("/repo", "~/notes.md")).toBeNull();
25 expect(resolveFileLinkPath(null, "~/notes.md")).toBeNull();
26 expect(resolveFileLinkPath("/repo", "../notes.md")).toBeNull();
27 });
28});
29
30describe("resolveMarkdownFileLinkPath", () => {
31 it("resolves home links from home and other links from the Markdown folder", () => {
32 const resolve = (path: string) =>
33 resolveMarkdownFileLinkPath("/home/u/repo", "/home/u/repo/docs", path);
34 expect(resolve("~/notes.md")).toBe("/home/u/notes.md");
35 expect(resolve("other.md")).toBe("/home/u/repo/docs/other.md");
36 });
37});
38
39describe("resolveWorkspaceRelativeFilePath", () => {
40 it("converts absolute paths inside the workspace", () => {
41 expect(
42 resolveWorkspaceRelativeFilePath("/Users/julius/repo", "/Users/julius/repo/src/main.ts"),
43 ).toBe("src/main.ts");
44 expect(resolveWorkspaceRelativeFilePath("C:\\repo", "c:\\repo\\src\\main.ts")).toBe(
45 "src/main.ts",
46 );
47 });
48
49 it("recognizes home links inside the workspace", () => {
50 expect(resolveWorkspaceRelativeFilePath("/home/maria/project", "~/project/src/main.ts")).toBe(
51 "src/main.ts",
52 );
53 expect(
54 resolveWorkspaceRelativeFilePath("/home/maria/project", "~/.agents/AGENTS.md"),
55 ).toBeNull();
56 });
57
58 it("rejects paths outside the workspace", () => {
59 expect(resolveWorkspaceRelativeFilePath("/repo", "/other/main.ts")).toBeNull();
60 expect(resolveWorkspaceRelativeFilePath("/repo", "../other/main.ts")).toBeNull();
61 expect(resolveWorkspaceRelativeFilePath("/repo", "/repo/../outside.txt")).toBeNull();
62 expect(resolveWorkspaceRelativeFilePath(null, "/repo/main.ts")).toBeNull();
63 });
64});
65
66describe("needsWorkspaceFileContents", () => {
67 it("reads a text file while its metadata is still pending, but not a directory", () => {
68 const input = { relativePath: "package.json", activeMode: "source" } as const;
69 expect(needsWorkspaceFileContents({ ...input, metadataKind: undefined })).toBe(true);
70 expect(needsWorkspaceFileContents({ ...input, metadataKind: "directory" })).toBe(false);
71 });
72});
73
74describe("workspace preview classifiers", () => {
75 it("recognize preview extensions and treat # and ? as literal filename characters", () => {
76 for (const path of ["notes/clip#1.wav", "notes/clip?1.wav", "notes#old/clip.wav"]) {
77 expect(isAudioPreviewFile(path)).toBe(true);
78 }
79 for (const path of [
80 "docs/draft#1.md",
81 "docs/draft?1.md",
82 "docs#old/draft.md",
83 "CHANGELOG.markdown",
84 "notes.MDOWN",
85 "todo.mkd",
86 ]) {
87 expect(isMarkdownPreviewFile(path)).toBe(true);
88 }
89 });
90});