Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 claimWorkspaceBasenameLookup,
5 pickWorkspaceBasenameMatch,
6 pickWorkspaceLinkMatch,
7 workspaceLinkLookupQuery,
8} from "@t3tools/client-runtime/workspace-basename-lookup";
9
10describe("pickWorkspaceBasenameMatch", () => {
11 const entries = [
12 { path: "apps/web/src/components/ChatView.test.tsx", kind: "file" as const },
13 { path: "apps/web/src/components/ChatView.tsx", kind: "file" as const },
14 ];
15
16 it("takes the first exact filename match, not the closest fuzzy one", () => {
17 expect(pickWorkspaceBasenameMatch("ChatView.tsx", entries)).toBe(
18 "apps/web/src/components/ChatView.tsx",
19 );
20 });
21
22 // regression: #6297 a bare filename resolves to a file, never a same-named directory
23 it("ignores directories", () => {
24 expect(
25 pickWorkspaceBasenameMatch("components", [
26 { path: "apps/web/src/components", kind: "directory" },
27 { path: "apps/web/src/components/components", kind: "file" },
28 ]),
29 ).toBe("apps/web/src/components/components");
30 });
31
32 it("prefers the exactly-cased file over a case-only twin", () => {
33 expect(
34 pickWorkspaceBasenameMatch("foo.ts", [
35 { path: "src/Foo.ts", kind: "file" },
36 { path: "src/foo.ts", kind: "file" },
37 ]),
38 ).toBe("src/foo.ts");
39 });
40
41 it("falls back to case-insensitive when only the casing differs", () => {
42 expect(pickWorkspaceBasenameMatch("chatview.tsx", entries)).toBe(
43 "apps/web/src/components/ChatView.tsx",
44 );
45 });
46
47 it("returns null when the case-insensitive fallback is ambiguous", () => {
48 expect(
49 pickWorkspaceBasenameMatch("FOO.ts", [
50 { path: "src/Foo.ts", kind: "file" },
51 { path: "src/foo.ts", kind: "file" },
52 ]),
53 ).toBeNull();
54 });
55});
56
57describe("chat file links an agent shortened", () => {
58 const cwd = "/home/user/acme";
59 const entries = [
60 { path: "packages/server/src/cloud/http.ts", kind: "file" as const },
61 { path: "packages/server/src/cloud", kind: "directory" as const },
62 { path: "packages/web/src/api/http.ts", kind: "file" as const },
63 { path: "packages/server/src/index.ts", kind: "file" as const },
64 { path: "packages/web/src/index.ts", kind: "file" as const },
65 ];
66
67 it("searches for package-relative paths and repeated prefixes, not exact ones", () => {
68 expect(workspaceLinkLookupQuery("cloud/http.ts", cwd)).toBe("cloud/http.ts");
69 expect(workspaceLinkLookupQuery("acme/packages/web/src/index.ts", cwd)).toBe(
70 "packages/web/src/index.ts",
71 );
72 expect(workspaceLinkLookupQuery("./cloud/http.ts", cwd)).toBeNull();
73 expect(workspaceLinkLookupQuery("~/acme/src/config.ts", cwd)).toBeNull();
74 expect(workspaceLinkLookupQuery("packages/server/", cwd)).toBeNull();
75 });
76
77 it("opens the one file a package-relative path is the suffix of", () => {
78 expect(pickWorkspaceLinkMatch("cloud/http.ts", cwd, entries)).toBe(
79 "packages/server/src/cloud/http.ts",
80 );
81 expect(pickWorkspaceLinkMatch("acme/packages/web/src/index.ts", cwd, entries)).toBe(
82 "packages/web/src/index.ts",
83 );
84 });
85
86 it("opens nothing when the suffix names several files or splits a segment", () => {
87 expect(pickWorkspaceLinkMatch("src/index.ts", cwd, entries)).toBeNull();
88 expect(pickWorkspaceLinkMatch("ud/http.ts", cwd, entries)).toBeNull();
89 });
90});
91
92describe("claimWorkspaceBasenameLookup", () => {
93 it("keeps only the newest claim, whatever order the lookups settle in", () => {
94 const first = claimWorkspaceBasenameLookup();
95 const second = claimWorkspaceBasenameLookup();
96
97 expect(second()).toBe(true);
98 expect(first()).toBe(false);
99 });
100});