Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fileChipMenu.test.ts

103 lines3.6 KB

1import { ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 fileChipClipboardPath,
6 fileChipMenu,
7 fileChipShareSource,
8 resolveFileChipTarget,
9} from "./fileChipMenu";
10​
11it("copies native paths from a relative file chip in a Windows workspace", () => {
12 const target = resolveFileChipTarget("reports/Device Users.csv", "C:/repo");
13 if (!target) throw new Error("File-chip target unavailable");
14 expect(fileChipClipboardPath(target, "copy-full-path")).toBe(
15 String.raw`C:\repo\reports\Device Users.csv`,
16 );
17 expect(fileChipClipboardPath(target, "copy-relative-path")).toBe(
18 String.raw`reports\Device Users.csv`,
19 );
20});
21​
22describe("resolveFileChipTarget", () => {
23 it("resolves a workspace-relative link to both paths", () => {
24 expect(resolveFileChipTarget("src/app.ts:12", "/repo")).toEqual({
25 fullPath: "/repo/src/app.ts",
26 relativePath: "src/app.ts",
27 });
28 });
29​
30 it("keeps only the full path for a host file outside the workspace", () => {
31 expect(resolveFileChipTarget("/tmp/report.md", "/repo")).toEqual({
32 fullPath: "/tmp/report.md",
33 });
34 });
35​
36 it("keeps only the relative path when the workspace root is unknown", () => {
37 expect(resolveFileChipTarget("src/app.ts", null)).toEqual({ relativePath: "src/app.ts" });
38 });
39​
40 it("ignores links that are not files or cannot be opened", () => {
41 expect(resolveFileChipTarget("https://example.com/app.ts", "/repo")).toBeNull();
42 expect(resolveFileChipTarget("~/report.md", "/repo")).toBeNull();
43 expect(resolveFileChipTarget("../other/file.ts", "/repo")).toBeNull();
44 });
45});
46​
47describe("fileChipMenu", () => {
48 it("offers only the copies the target can satisfy", () => {
49 expect(fileChipMenu({ fullPath: "/tmp/report.md" })).toEqual({
50 title: "/tmp/report.md",
51 actions: [
52 { id: "copy-full-path", title: "Copy full path" },
53 { id: "open-file", title: "Open in file viewer" },
54 ],
55 });
56 expect(fileChipMenu({ relativePath: "src/app.ts" }).actions.map(({ id }) => id)).toEqual([
57 "copy-relative-path",
58 "open-file",
59 ]);
60 });
61});
62​
63describe("file chip downloads", () => {
64 const threadId = ThreadId.make("thread-1");
65​
66 it.each([
67 [
68 "/tmp/maria-counter/maria-counter-final.mp4",
69 "/tmp/maria-counter/maria-counter-final.mp4",
70 "video/mp4",
71 ],
72 ["/tmp/take%2520%23one.mp4:12", "/tmp/take%20#one.mp4", "video/mp4"],
73 ["/tmp/report.pdf", "/tmp/report.pdf", "application/pdf"],
74 ["screens/image.PNG", "/repo/screens/image.PNG", "image/png"],
75 ])("offers a host download for %s", (href, path, mimeType) => {
76 const target = resolveFileChipTarget(href, "/repo")!;
77 expect(fileChipMenu(target).actions).toContainEqual({
78 id: "save",
79 title: "Save or share",
80 });
81 expect(fileChipShareSource(target, threadId)).toEqual({
82 name: path.split("/").at(-1),
83 mimeType,
84 resource: { _tag: "media-file", threadId, path },
85 });
86 });
87​
88 it("retains the thread context for a relative file without a known workspace root", () => {
89 expect(
90 fileChipShareSource(resolveFileChipTarget("clips/demo.mp4", null)!, threadId),
91 ).toMatchObject({
92 resource: { _tag: "media-file", threadId, path: "clips/demo.mp4" },
93 });
94 });
95​
96 it("does not offer downloads the host asset endpoint cannot serve", () => {
97 for (const href of ["src/app.ts", "/tmp/archive.zip", "/tmp/clip.mp4.txt"]) {
98 const target = resolveFileChipTarget(href, "/repo")!;
99 expect(fileChipShareSource(target, threadId)).toBeNull();
100 expect(fileChipMenu(target).actions.some(({ id }) => id === "save")).toBe(false);
101 }
102 });
103});