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

workspaceBasenameLookup.test.ts

256 lines9.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 agentWorkingDirectories,
5 findAgentDirectoryMatch,
6 pickProjectLinkMatch,
7 pickWorkspaceBasenameMatch,
8 pickWorkspaceLinkMatch,
9 workspaceLinkLookupQuery,
10} from "@t3tools/client-runtime/workspace-basename-lookup";
11​
12describe("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​
59describe("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​
94describe("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 a project left ambiguous by case also has the name", () => {
133 expect(
134 pickProjectLinkMatch("handoff.txt", [
135 { root: "/reports", entries: [{ path: "notes/HANDOFF.txt", kind: "file" }] },
136 {
137 root: "/support",
138 entries: [
139 { path: "current/Handoff.txt", kind: "file" },
140 { path: "archive/HANDOFF.txt", kind: "file" },
141 ],
142 },
143 ]),
144 ).toBeNull();
145 });
146​
147 it("opens nothing when overlapping projects also list a second suffix match", () => {
148 expect(
149 pickProjectLinkMatch("docs/handoff.txt", [
150 {
151 root: "/home/user/reports",
152 entries: [
153 { path: "quarter/docs/handoff.txt", kind: "file" },
154 { path: "archive/docs/handoff.txt", kind: "file" },
155 ],
156 },
157 {
158 root: "/home/user/reports/quarter",
159 entries: [{ path: "docs/handoff.txt", kind: "file" }],
160 },
161 {
162 root: "/home/user/reports/quarter/docs",
163 entries: [{ path: "handoff.txt", kind: "file" }],
164 },
165 ]),
166 ).toBeNull();
167 });
168​
169 it.each([
170 { cited: "handoff.txt", other: "archive/handoff.txt" },
171 { cited: "HANDOFF.txt", other: "archive/Handoff.txt" },
172 ])("opens nothing when overlapping projects also list $other", ({ cited, other }) => {
173 expect(
174 pickProjectLinkMatch("handoff.txt", [
175 {
176 root: "/home/user/reports",
177 entries: [
178 { path: `team/docs/${cited}`, kind: "file" },
179 { path: other, kind: "file" },
180 ],
181 },
182 { root: "/home/user/reports/team", entries: [{ path: `docs/${cited}`, kind: "file" }] },
183 { root: "/home/user/reports/team/docs", entries: [{ path: cited, kind: "file" }] },
184 ]),
185 ).toBeNull();
186 });
187});
188​
189// regression: #35337 a path the agent cited inside a checkout no project is rooted at
190describe("agentWorkingDirectories", () => {
191 const directories = (...commands: ReadonlyArray<string>) =>
192 agentWorkingDirectories(
193 [...commands, "the reply", "cd /work/later"].map((input, index) => ({
194 sourceThreadId: "thread",
195 sourceItemId: `item-${index}`,
196 item: { type: "command_execution", input },
197 })),
198 { sourceThreadId: "thread", sourceItemId: `item-${commands.length}` },
199 );
200​
201 it("lists the folders commands above the reply changed into, the latest first", () => {
202 expect(
203 directories(
204 "cd /work/repo-c && cd /work/repo-b && git diff",
205 "git status",
206 `/bin/zsh -lc "cd '/work/my repo'; ls"`,
207 "cd -P /work/repo-a # not this; cd /work/commented",
208 "cd /work/repo-c\npwd",
209 ),
210 ).toEqual(["/work/repo-c", "/work/repo-a", "/work/my repo", "/work/repo-b"]);
211 });
212​
213 it("skips operands only the shell can resolve and words that are not the command", () => {
214 expect(
215 directories(
216 "cd docs && cd ../other && cd ~/repo && cd $ROOT/x && cd /work/*/src",
217 "abcd /work/a; echo cd /work/b; git -C /work/c status; cd /work/d",
218 `echo "cd /work/g"; echo 'x; cd /work/h' && grep -c "cd /work/i" notes`,
219 "echo bash -c 'cd /work/j'; cd -",
220 "cat <<EOF > notes\ncd /work/k\nEOF",
221 "cd /work/e\\ f && cd `pwd`/x",
222 ),
223 ).toEqual(["/work/d"]);
224 });
225});
226​
227// regression: #35337 a bare file name the agent cited from a subfolder of an unregistered checkout
228describe("findAgentDirectoryMatch", () => {
229 const search = (found: Record<string, ReadonlyArray<string>>) => (root: string) =>
230 Promise.resolve({
231 root,
232 entries: (found[root] ?? []).map((path) => ({ path, kind: "file" as const })),
233 });
234​
235 it("opens the one file of that name in the latest folder the agent worked in", async () => {
236 expect(
237 await findAgentDirectoryMatch(
238 "config.test.yaml",
239 ["/work/repo-b/server", "/work/repo-b", "/work/repo-c"],
240 search({
241 "/work/repo-b/server": ["config.test.yaml"],
242 "/work/repo-b": ["server/config.test.yaml"],
243 "/work/repo-c": ["config.test.yaml"],
244 }),
245 ),
246 ).toBe("/work/repo-b/server/config.test.yaml");
247 });
248​
249 it("passes over a folder without the file, and opens nothing when none has it", async () => {
250 const found = search({ "/work/repo-c": ["etc/config.test.yaml"] });
251 expect(
252 await findAgentDirectoryMatch("config.test.yaml", ["/work/repo-b", "/work/repo-c"], found),
253 ).toBe("/work/repo-c/etc/config.test.yaml");
254 expect(await findAgentDirectoryMatch("config.test.yaml", ["/work/repo-b"], found)).toBeNull();
255 });
256});