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

workspaceBasenameLookup.test.ts

195 lines7.1 KB

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