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

projectImport.logic.test.ts

351 lines10.8 KB

1import { EnvironmentId, ProjectId, type AgentSessionProjectCandidate } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3​
4import {
5 groupOnboardingProjects,
6 partitionOnboardingProjects,
7 onboardingProjectKey,
8 resolveOnboardingLandingProject,
9 resolveOnboardingProjectId,
10} from "./projectImport.logic";
11​
12const now = Date.parse("2026-08-22T12:00:00.000Z");
13​
14function candidate(
15 path: string,
16 overrides: Partial<AgentSessionProjectCandidate> = {},
17): AgentSessionProjectCandidate {
18 return {
19 title: path.split("/").at(-1) ?? path,
20 path,
21 sources: ["codex"],
22 threadCount: 3,
23 lastActiveAt: "2026-08-20T12:00:00.000Z",
24 alreadyImported: false,
25 git: { remoteKey: null, repository: null },
26 ...overrides,
27 };
29​
30const github = (repository: string) => ({
31 remoteKey: `github.com/${repository.toLowerCase()}`,
32 repository,
33});
34​
35describe("partitionOnboardingProjects", () => {
36 it("keeps existing projects available for thread history import", () => {
37 const imported = candidate("/projects/current", { alreadyImported: true });
38 const available = candidate("/projects/other");
39​
40 expect(partitionOnboardingProjects([imported, available], now)).toEqual({
41 available: [imported, available],
42 recent: [imported, available],
43 });
44 });
45​
46 it("keeps projects older than 30 days out of the default selection", () => {
47 const recent = candidate("/projects/recent");
48 const older = candidate("/projects/older", {
49 lastActiveAt: "2026-07-01T12:00:00.000Z",
50 });
51​
52 expect(partitionOnboardingProjects([recent, older], now)).toEqual({
53 available: [recent, older],
54 recent: [recent],
55 });
56 });
57​
58 it("keeps future activity out of the default selection", () => {
59 const recent = candidate("/projects/recent");
60 const future = candidate("/projects/future", {
61 lastActiveAt: "2026-08-23T12:00:00.000Z",
62 });
63​
64 expect(partitionOnboardingProjects([recent, future], now)).toEqual({
65 available: [recent, future],
66 recent: [recent],
67 });
68 });
69​
70 it("keeps non-git folders and thin histories out of the default selection", () => {
71 const repo = candidate("/projects/repo");
72 const folder = candidate("/projects/folder", { git: null });
73 const thin = candidate("/projects/thin", { threadCount: 2 });
74​
75 expect(partitionOnboardingProjects([repo, folder, thin], now).recent).toEqual([repo]);
76 });
77​
78 it("selects candidates from servers that do not report git identity", () => {
79 const { git: _git, ...legacy } = candidate("/projects/legacy");
80​
81 expect(partitionOnboardingProjects([legacy], now).recent).toEqual([legacy]);
82 });
83});
84​
85describe("groupOnboardingProjects", () => {
86 it("groups clones by origin, keeps local repos separate, and folds non-git folders away", () => {
87 const main = candidate("/code/t3code", {
88 git: github("pingdotgg/t3code"),
89 threadCount: 79,
90 lastActiveAt: "2026-08-21T12:00:00.000Z",
91 });
92 const clone = candidate("/code/clones/t3code-2", {
93 git: github("pingdotgg/t3code"),
94 threadCount: 13,
95 lastActiveAt: "2026-08-10T12:00:00.000Z",
96 });
97 const older = candidate("/code/fleet", {
98 git: github("t3dotgg/fleet"),
99 threadCount: 295,
100 lastActiveAt: "2026-08-22T00:00:00.000Z",
101 });
102 const local = candidate("/code/scratch-repo", { title: "scratch-repo" });
103 const folder = candidate("/tmp/notes", { git: null });
104​
105 const grouped = groupOnboardingProjects([main, clone, older, local, folder]);
106​
107 expect(grouped.other).toEqual([folder]);
108 expect(
109 grouped.repositories.map((group) => ({
110 label: group.label,
111 paths: group.candidates.map((item) => item.path),
112 threadCount: group.threadCount,
113 lastActiveAt: group.lastActiveAt,
114 })),
115 ).toEqual([
116 {
117 label: "t3dotgg/fleet",
118 paths: ["/code/fleet"],
119 threadCount: 295,
120 lastActiveAt: "2026-08-22T00:00:00.000Z",
121 },
122 {
123 label: "pingdotgg/t3code",
124 paths: ["/code/t3code", "/code/clones/t3code-2"],
125 threadCount: 92,
126 lastActiveAt: "2026-08-21T12:00:00.000Z",
127 },
128 {
129 label: "scratch-repo",
130 paths: ["/code/scratch-repo"],
131 threadCount: 3,
132 lastActiveAt: "2026-08-20T12:00:00.000Z",
133 },
134 ]);
135 });
136​
137 it("lists candidates without git identity as standalone repositories", () => {
138 const { git: _git, ...legacy } = candidate("/code/legacy", { title: "legacy" });
139​
140 const grouped = groupOnboardingProjects([legacy]);
141​
142 expect(grouped.other).toEqual([]);
143 expect(grouped.repositories.map((group) => [group.label, group.repository])).toEqual([
144 ["legacy", null],
145 ]);
146 });
147});
148​
149describe("resolveOnboardingProjectId", () => {
150 const localEnvironmentId = EnvironmentId.make("local");
151 const remoteEnvironmentId = EnvironmentId.make("remote");
152 const localProjectId = ProjectId.make("local-project");
153​
154 it("uses the scanned project ID before the project reaches the client", () => {
155 expect(
156 resolveOnboardingProjectId(
157 [],
158 localEnvironmentId,
159 candidate("/projects/repo", { projectId: localProjectId }),
160 ),
161 ).toBe(localProjectId);
162 });
163​
164 it("uses the scanned project ID when the client still has an older project at that root", () => {
165 expect(
166 resolveOnboardingProjectId(
167 [
168 {
169 id: ProjectId.make("stale-project"),
170 environmentId: localEnvironmentId,
171 workspaceRoot: "/projects/repo",
172 },
173 ],
174 localEnvironmentId,
175 candidate("/projects/repo", { projectId: localProjectId }),
176 ),
177 ).toBe(localProjectId);
178 });
179​
180 it("returns null to create a project when neither the scan nor the client has a project ID", () => {
181 expect(
182 resolveOnboardingProjectId([], localEnvironmentId, candidate("/projects/new")),
183 ).toBeNull();
184 });
185​
186 it("finds an existing project by normalized root in the target environment", () => {
187 expect(
188 resolveOnboardingProjectId(
189 [
190 {
191 id: ProjectId.make("remote-project"),
192 environmentId: remoteEnvironmentId,
193 workspaceRoot: "C:\\Work\\Repo",
194 },
195 {
196 id: localProjectId,
197 environmentId: localEnvironmentId,
198 workspaceRoot: "C:\\Work\\Repo\\",
199 },
200 ],
201 localEnvironmentId,
202 candidate("c:/work/repo"),
203 ),
204 ).toBe(localProjectId);
205 });
206​
207 it("does not reuse a project from another environment", () => {
208 expect(
209 resolveOnboardingProjectId(
210 [
211 {
212 id: ProjectId.make("remote-project"),
213 environmentId: remoteEnvironmentId,
214 workspaceRoot: "/projects/repo",
215 },
216 ],
217 localEnvironmentId,
218 candidate("/projects/repo"),
219 ),
220 ).toBeNull();
221 });
222​
223 it("finds an alias after the scanner returns its persisted project root", () => {
224 expect(
225 resolveOnboardingProjectId(
226 [
227 {
228 id: localProjectId,
229 environmentId: localEnvironmentId,
230 workspaceRoot: "/real/projects/repo",
231 },
232 ],
233 localEnvironmentId,
234 candidate("/real/projects/repo"),
235 ),
236 ).toBe(localProjectId);
237 });
238​
239 it("finds the current root owner when the scan has no project ID", () => {
240 const recreatedProjectId = ProjectId.make("recreated-project");
241 expect(
242 resolveOnboardingProjectId(
243 [
244 {
245 id: localProjectId,
246 environmentId: localEnvironmentId,
247 workspaceRoot: "/projects/other",
248 },
249 {
250 id: recreatedProjectId,
251 environmentId: localEnvironmentId,
252 workspaceRoot: "/projects/repo",
253 },
254 ],
255 localEnvironmentId,
256 candidate("/projects/repo"),
257 ),
258 ).toBe(recreatedProjectId);
259 });
260​
261 it("does not reuse a moved project when the scan has no project ID", () => {
262 expect(
263 resolveOnboardingProjectId(
264 [
265 {
266 id: localProjectId,
267 environmentId: localEnvironmentId,
268 workspaceRoot: "/projects/moved",
269 },
270 ],
271 localEnvironmentId,
272 candidate("/projects/repo"),
273 ),
274 ).toBeNull();
275 });
276});
277​
278describe("resolveOnboardingLandingProject", () => {
279 it("skips a failed first project for a later project with imported history", () => {
280 expect(
281 resolveOnboardingLandingProject(
282 ["/projects/failed", "/projects/imported"],
283 new Map([["/projects/imported", "imported"]]),
284 new Map([["/projects/imported", "imported"]]),
285 ),
286 ).toBe("imported");
287 });
288​
289 it("prefers a partial first import that added history", () => {
290 expect(
291 resolveOnboardingLandingProject(
292 ["/projects/partial", "/projects/complete"],
293 new Map([["/projects/partial", "partial"]]),
294 new Map([["/projects/complete", "complete"]]),
295 ),
296 ).toBe("partial");
297 });
298​
299 it("uses a completed zero-history project when no import added history", () => {
300 expect(
301 resolveOnboardingLandingProject(
302 ["/projects/empty", "/projects/failed"],
303 new Map(),
304 new Map([["/projects/empty", "empty"]]),
305 ),
306 ).toBe("empty");
307 });
308​
309 it("keeps an earlier successful import available on retry", () => {
310 expect(
311 resolveOnboardingLandingProject(
312 ["/projects/imported", "/projects/retry"],
313 new Map([["/projects/imported", "imported"]]),
314 new Map([["/projects/imported", "imported"]]),
315 ),
316 ).toBe("imported");
317 });
318​
319 it("ignores cached successes outside the current retry selection", () => {
320 expect(
321 resolveOnboardingLandingProject(
322 ["/projects/current"],
323 new Map([["/projects/previous", "previous"]]),
324 new Map([
325 ["/projects/previous", "previous"],
326 ["/projects/current", "current"],
327 ]),
328 ),
329 ).toBe("current");
330 });
331});
332​
333describe("projects on multiple computers", () => {
334 it("keeps identical paths independent when selecting a landing project after partial imports", () => {
335 const first = onboardingProjectKey(EnvironmentId.make("first"), "/code/app");
336 const second = onboardingProjectKey(EnvironmentId.make("second"), "/code/app");
337 const completed = new Map([[first, "first-project"]]);
338 expect(completed.has(second)).toBe(false);
339 expect(resolveOnboardingLandingProject([second], completed, completed)).toBeUndefined();
340 completed.set(second, "second-project");
341 expect(resolveOnboardingLandingProject([second, first], completed, completed)).toBe(
342 "second-project",
343 );
344 });
345​
346 it("preserves computer identity when choosing recent projects", () => {
347 const first = { ...candidate("/code/app"), environmentId: "first" };
348 const second = { ...candidate("/code/app"), environmentId: "second" };
349 expect(partitionOnboardingProjects([first, second], now).recent).toEqual([first, second]);
350 });
351});