The best way to control Claude Code, Codex and more from any computer or phone

main
ts 241 lines 8.72 KB
Raw
1 import { describe, expect, it } from "vite-plus/test";
2
3 import {
4 agentWorkingDirectories,
5 findAgentDirectoryMatch,
6 pickProjectLinkMatch,
7 pickWorkspaceBasenameMatch,
8 pickWorkspaceLinkMatch,
9 workspaceLinkLookupQuery,
10 } from "@t3tools/client-runtime/workspace-basename-lookup";
11
12 describe("pickWorkspaceBasenameMatch", () => {
13 const entries = [
14 { path: "apps/web/src/components/ChatView.test.tsx", kind: "file" as const },
15 { path: "apps/web/src/components/ChatView.tsx", kind: "file" as const },
16 ];
17
18 it("takes the first exact filename match, not the closest fuzzy one", () => {
19 expect(pickWorkspaceBasenameMatch("ChatView.tsx", entries)).toBe(
20 "apps/web/src/components/ChatView.tsx",
21 );
22 });
23
24 // regression: #6297 a bare filename resolves to a file, never a same-named directory
25 it("ignores directories", () => {
26 expect(
27 pickWorkspaceBasenameMatch("components", [
28 { path: "apps/web/src/components", kind: "directory" },
29 { path: "apps/web/src/components/components", kind: "file" },
30 ]),
31 ).toBe("apps/web/src/components/components");
32 });
33
34 it("prefers the exactly-cased file over a case-only twin", () => {
35 expect(
36 pickWorkspaceBasenameMatch("foo.ts", [
37 { path: "src/Foo.ts", kind: "file" },
38 { path: "src/foo.ts", kind: "file" },
39 ]),
40 ).toBe("src/foo.ts");
41 });
42
43 it("falls back to case-insensitive when only the casing differs", () => {
44 expect(pickWorkspaceBasenameMatch("chatview.tsx", entries)).toBe(
45 "apps/web/src/components/ChatView.tsx",
46 );
47 });
48
49 it("returns null when the case-insensitive fallback is ambiguous", () => {
50 expect(
51 pickWorkspaceBasenameMatch("FOO.ts", [
52 { path: "src/Foo.ts", kind: "file" },
53 { path: "src/foo.ts", kind: "file" },
54 ]),
55 ).toBeNull();
56 });
57 });
58
59 describe("chat file links an agent shortened", () => {
60 const cwd = "/home/user/acme";
61 const entries = [
62 { path: "packages/server/src/cloud/http.ts", kind: "file" as const },
63 { path: "packages/server/src/cloud", kind: "directory" as const },
64 { path: "packages/web/src/api/http.ts", kind: "file" as const },
65 { path: "packages/server/src/index.ts", kind: "file" as const },
66 { path: "packages/web/src/index.ts", kind: "file" as const },
67 ];
68
69 it("searches for package-relative paths and repeated prefixes, not exact ones", () => {
70 expect(workspaceLinkLookupQuery("cloud/http.ts", cwd)).toBe("cloud/http.ts");
71 expect(workspaceLinkLookupQuery("acme/packages/web/src/index.ts", cwd)).toBe(
72 "packages/web/src/index.ts",
73 );
74 expect(workspaceLinkLookupQuery("./cloud/http.ts", cwd)).toBeNull();
75 expect(workspaceLinkLookupQuery("~/acme/src/config.ts", cwd)).toBeNull();
76 expect(workspaceLinkLookupQuery("packages/server/", cwd)).toBeNull();
77 });
78
79 it("opens the one file a package-relative path is the suffix of", () => {
80 expect(pickWorkspaceLinkMatch("cloud/http.ts", cwd, entries)).toBe(
81 "packages/server/src/cloud/http.ts",
82 );
83 expect(pickWorkspaceLinkMatch("acme/packages/web/src/index.ts", cwd, entries)).toBe(
84 "packages/web/src/index.ts",
85 );
86 });
87
88 it("opens nothing when the suffix names several files or splits a segment", () => {
89 expect(pickWorkspaceLinkMatch("src/index.ts", cwd, entries)).toBeNull();
90 expect(pickWorkspaceLinkMatch("ud/http.ts", cwd, entries)).toBeNull();
91 });
92 });
93
94 describe("chat file links into another project", () => {
95 const appB = {
96 root: "/home/user/app-b",
97 entries: [{ path: "src/api/rules/categories.ts", kind: "file" as const }],
98 };
99
100 // regression: #10553 a path the agent cited inside another registered project
101 it("resolves to the absolute path in the one project that has the file", () => {
102 expect(
103 pickProjectLinkMatch("rules/categories.ts", [
104 { root: "/home/user/app-c", entries: [] },
105 appB,
106 ]),
107 ).toBe("/home/user/app-b/src/api/rules/categories.ts");
108 });
109
110 it.each([
111 {
112 outer: "/home/user/reports",
113 inner: "/home/user/reports/docs",
114 found: "/home/user/reports/docs/handoff.txt",
115 },
116 { outer: "C:\\Reports", inner: "C:/Reports/docs/", found: "C:\\Reports\\docs\\handoff.txt" },
117 ])("resolves one file that a project nested in $outer also lists", ({ outer, inner, found }) => {
118 expect(
119 pickProjectLinkMatch("handoff.txt", [
120 { root: outer, entries: [{ path: "docs/handoff.txt", kind: "file" }] },
121 { root: inner, entries: [{ path: "handoff.txt", kind: "file" }] },
122 ]),
123 ).toBe(found);
124 });
125
126 it("opens nothing when several projects have the file", () => {
127 expect(
128 pickProjectLinkMatch("rules/categories.ts", [appB, { ...appB, root: "/home/user/app-c" }]),
129 ).toBeNull();
130 });
131
132 it("opens nothing when overlapping projects also list a second suffix match", () => {
133 expect(
134 pickProjectLinkMatch("docs/handoff.txt", [
135 {
136 root: "/home/user/reports",
137 entries: [
138 { path: "quarter/docs/handoff.txt", kind: "file" },
139 { path: "archive/docs/handoff.txt", kind: "file" },
140 ],
141 },
142 {
143 root: "/home/user/reports/quarter",
144 entries: [{ path: "docs/handoff.txt", kind: "file" }],
145 },
146 {
147 root: "/home/user/reports/quarter/docs",
148 entries: [{ path: "handoff.txt", kind: "file" }],
149 },
150 ]),
151 ).toBeNull();
152 });
153
154 it.each([
155 { cited: "handoff.txt", other: "archive/handoff.txt" },
156 { cited: "HANDOFF.txt", other: "archive/Handoff.txt" },
157 ])("opens nothing when overlapping projects also list $other", ({ cited, other }) => {
158 expect(
159 pickProjectLinkMatch("handoff.txt", [
160 {
161 root: "/home/user/reports",
162 entries: [
163 { path: `team/docs/${cited}`, kind: "file" },
164 { path: other, kind: "file" },
165 ],
166 },
167 { root: "/home/user/reports/team", entries: [{ path: `docs/${cited}`, kind: "file" }] },
168 { root: "/home/user/reports/team/docs", entries: [{ path: cited, kind: "file" }] },
169 ]),
170 ).toBeNull();
171 });
172 });
173
174 // regression: #35337 a path the agent cited inside a checkout no project is rooted at
175 describe("agentWorkingDirectories", () => {
176 const directories = (...commands: ReadonlyArray<string>) =>
177 agentWorkingDirectories(
178 [...commands, "the reply", "cd /work/later"].map((input, index) => ({
179 sourceThreadId: "thread",
180 sourceItemId: `item-${index}`,
181 item: { type: "command_execution", input },
182 })),
183 { sourceThreadId: "thread", sourceItemId: `item-${commands.length}` },
184 );
185
186 it("lists the folders commands above the reply changed into, the latest first", () => {
187 expect(
188 directories(
189 "cd /work/repo-c && cd /work/repo-b && git diff",
190 "git status",
191 `/bin/zsh -lc "cd '/work/my repo'; ls"`,
192 "cd -P /work/repo-a # not this; cd /work/commented",
193 "cd /work/repo-c\npwd",
194 ),
195 ).toEqual(["/work/repo-c", "/work/repo-a", "/work/my repo", "/work/repo-b"]);
196 });
197
198 it("skips operands only the shell can resolve and words that are not the command", () => {
199 expect(
200 directories(
201 "cd docs && cd ../other && cd ~/repo && cd $ROOT/x && cd /work/*/src",
202 "abcd /work/a; echo cd /work/b; git -C /work/c status; cd /work/d",
203 `echo "cd /work/g"; echo 'x; cd /work/h' && grep -c "cd /work/i" notes`,
204 "echo bash -c 'cd /work/j'; cd -",
205 "cat <<EOF > notes\ncd /work/k\nEOF",
206 "cd /work/e\\ f && cd `pwd`/x",
207 ),
208 ).toEqual(["/work/d"]);
209 });
210 });
211
212 // regression: #35337 a bare file name the agent cited from a subfolder of an unregistered checkout
213 describe("findAgentDirectoryMatch", () => {
214 const search = (found: Record<string, ReadonlyArray<string>>) => (root: string) =>
215 Promise.resolve({
216 root,
217 entries: (found[root] ?? []).map((path) => ({ path, kind: "file" as const })),
218 });
219
220 it("opens the one file of that name in the latest folder the agent worked in", async () => {
221 expect(
222 await findAgentDirectoryMatch(
223 "config.test.yaml",
224 ["/work/repo-b/server", "/work/repo-b", "/work/repo-c"],
225 search({
226 "/work/repo-b/server": ["config.test.yaml"],
227 "/work/repo-b": ["server/config.test.yaml"],
228 "/work/repo-c": ["config.test.yaml"],
229 }),
230 ),
231 ).toBe("/work/repo-b/server/config.test.yaml");
232 });
233
234 it("passes over a folder without the file, and opens nothing when none has it", async () => {
235 const found = search({ "/work/repo-c": ["etc/config.test.yaml"] });
236 expect(
237 await findAgentDirectoryMatch("config.test.yaml", ["/work/repo-b", "/work/repo-c"], found),
238 ).toBe("/work/repo-c/etc/config.test.yaml");
239 expect(await findAgentDirectoryMatch("config.test.yaml", ["/work/repo-b"], found)).toBeNull();
240 });
241 });