| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | fileRoutePathSegments, |
| 5 | isSvgImagePreviewFile, |
| 6 | resolveWorkspaceRelativeFilePath, |
| 7 | fileHeaderSubtitle, |
| 8 | } from "./filePath"; |
| 9 | |
| 10 | describe("fileRoutePathSegments", () => { |
| 11 | it("round-trips workspace-relative and host paths through the route", () => { |
| 12 | expect(fileRoutePathSegments("src/main.ts")).toEqual(["src", "main.ts"]); |
| 13 | expect(fileRoutePathSegments("/tmp/t3-cleanup/report.md").join("/")).toBe( |
| 14 | "/tmp/t3-cleanup/report.md", |
| 15 | ); |
| 16 | }); |
| 17 | }); |
| 18 | |
| 19 | describe("resolveWorkspaceRelativeFilePath", () => { |
| 20 | it("keeps normalized workspace-relative paths", () => { |
| 21 | expect(resolveWorkspaceRelativeFilePath("/repo", "./src/../src/main.ts")).toBe("src/main.ts"); |
| 22 | }); |
| 23 | |
| 24 | it("converts absolute paths inside the workspace", () => { |
| 25 | expect( |
| 26 | resolveWorkspaceRelativeFilePath("/Users/julius/repo", "/Users/julius/repo/src/main.ts"), |
| 27 | ).toBe("src/main.ts"); |
| 28 | expect(resolveWorkspaceRelativeFilePath("C:\\repo", "c:\\repo\\src\\main.ts")).toBe( |
| 29 | "src/main.ts", |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | it("rejects paths outside the workspace", () => { |
| 34 | expect(resolveWorkspaceRelativeFilePath("/repo", "/other/main.ts")).toBeNull(); |
| 35 | expect(resolveWorkspaceRelativeFilePath("/repo", "../other/main.ts")).toBeNull(); |
| 36 | expect(resolveWorkspaceRelativeFilePath("/repo", "/repo/../outside.txt")).toBeNull(); |
| 37 | expect(resolveWorkspaceRelativeFilePath(null, "/repo/main.ts")).toBeNull(); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe("file preview types", () => { |
| 42 | it("identifies SVG images that need web rendering", () => { |
| 43 | expect(isSvgImagePreviewFile("assets/diagram.svg#icon")).toBe(true); |
| 44 | expect(isSvgImagePreviewFile("assets/photo.png")).toBe(false); |
| 45 | }); |
| 46 | }); |
| 47 | |
| 48 | describe("fileHeaderSubtitle", () => { |
| 49 | it("places a workspace file under its project", () => { |
| 50 | expect( |
| 51 | fileHeaderSubtitle("t3code", "apps/mobile/src/features/threads/fileChipMenu.test.ts"), |
| 52 | ).toBe("t3code · apps/mobile/src/features/threads"); |
| 53 | }); |
| 54 | |
| 55 | it("shows only the directory for a host file outside the workspace", () => { |
| 56 | // It is not under the project, so naming the project there would be a lie. |
| 57 | expect(fileHeaderSubtitle("t3code", "/tmp/report.md")).toBe("/tmp"); |
| 58 | }); |
| 59 | |
| 60 | it("shows only the project for a file at the workspace root", () => { |
| 61 | expect(fileHeaderSubtitle("t3code", "README.md")).toBe("t3code"); |
| 62 | }); |
| 63 | }); |