Open History to see the last commit
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 };
28}
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
79describe("groupOnboardingProjects", () => {
80 it("groups clones by origin, keeps local repos separate, and folds non-git folders away", () => {
81 const main = candidate("/code/t3code", {
82 git: github("pingdotgg/t3code"),
83 threadCount: 79,
84 lastActiveAt: "2026-08-21T12:00:00.000Z",
85 });
86 const clone = candidate("/code/clones/t3code-2", {
87 git: github("pingdotgg/t3code"),
88 threadCount: 13,
89 lastActiveAt: "2026-08-10T12:00:00.000Z",
90 });
91 const older = candidate("/code/fleet", {
92 git: github("t3dotgg/fleet"),
93 threadCount: 295,
94 lastActiveAt: "2026-08-22T00:00:00.000Z",
95 });
96 const local = candidate("/code/scratch-repo", { title: "scratch-repo" });
97 const folder = candidate("/tmp/notes", { git: null });
98
99 const grouped = groupOnboardingProjects([main, clone, older, local, folder]);
100
101 expect(grouped.other).toEqual([folder]);
102 expect(
103 grouped.repositories.map((group) => ({
104 label: group.label,
105 paths: group.candidates.map((item) => item.path),
106 threadCount: group.threadCount,
107 lastActiveAt: group.lastActiveAt,
108 })),
109 ).toEqual([
110 {
111 label: "t3dotgg/fleet",
112 paths: ["/code/fleet"],
113 threadCount: 295,
114 lastActiveAt: "2026-08-22T00:00:00.000Z",
115 },
116 {
117 label: "pingdotgg/t3code",
118 paths: ["/code/t3code", "/code/clones/t3code-2"],
119 threadCount: 92,
120 lastActiveAt: "2026-08-21T12:00:00.000Z",
121 },
122 {
123 label: "scratch-repo",
124 paths: ["/code/scratch-repo"],
125 threadCount: 3,
126 lastActiveAt: "2026-08-20T12:00:00.000Z",
127 },
128 ]);
129 });
130});
131
132describe("resolveOnboardingProjectId", () => {
133 const localEnvironmentId = EnvironmentId.make("local");
134 const remoteEnvironmentId = EnvironmentId.make("remote");
135 const localProjectId = ProjectId.make("local-project");
136
137 it("uses the scanned project ID before the project reaches the client", () => {
138 expect(
139 resolveOnboardingProjectId(
140 [],
141 localEnvironmentId,
142 candidate("/projects/repo", { projectId: localProjectId }),
143 ),
144 ).toBe(localProjectId);
145 });
146
147 it("uses the scanned project ID when the client still has an older project at that root", () => {
148 expect(
149 resolveOnboardingProjectId(
150 [
151 {
152 id: ProjectId.make("stale-project"),
153 environmentId: localEnvironmentId,
154 workspaceRoot: "/projects/repo",
155 },
156 ],
157 localEnvironmentId,
158 candidate("/projects/repo", { projectId: localProjectId }),
159 ),
160 ).toBe(localProjectId);
161 });
162
163 it("returns null to create a project when neither the scan nor the client has a project ID", () => {
164 expect(
165 resolveOnboardingProjectId([], localEnvironmentId, candidate("/projects/new")),
166 ).toBeNull();
167 });
168
169 it("finds an existing project by normalized root in the target environment", () => {
170 expect(
171 resolveOnboardingProjectId(
172 [
173 {
174 id: ProjectId.make("remote-project"),
175 environmentId: remoteEnvironmentId,
176 workspaceRoot: "C:\\Work\\Repo",
177 },
178 {
179 id: localProjectId,
180 environmentId: localEnvironmentId,
181 workspaceRoot: "C:\\Work\\Repo\\",
182 },
183 ],
184 localEnvironmentId,
185 candidate("c:/work/repo"),
186 ),
187 ).toBe(localProjectId);
188 });
189
190 it("does not reuse a project from another environment", () => {
191 expect(
192 resolveOnboardingProjectId(
193 [
194 {
195 id: ProjectId.make("remote-project"),
196 environmentId: remoteEnvironmentId,
197 workspaceRoot: "/projects/repo",
198 },
199 ],
200 localEnvironmentId,
201 candidate("/projects/repo"),
202 ),
203 ).toBeNull();
204 });
205
206 it("finds an alias after the scanner returns its persisted project root", () => {
207 expect(
208 resolveOnboardingProjectId(
209 [
210 {
211 id: localProjectId,
212 environmentId: localEnvironmentId,
213 workspaceRoot: "/real/projects/repo",
214 },
215 ],
216 localEnvironmentId,
217 candidate("/real/projects/repo"),
218 ),
219 ).toBe(localProjectId);
220 });
221
222 it("finds the current root owner when the scan has no project ID", () => {
223 const recreatedProjectId = ProjectId.make("recreated-project");
224 expect(
225 resolveOnboardingProjectId(
226 [
227 {
228 id: localProjectId,
229 environmentId: localEnvironmentId,
230 workspaceRoot: "/projects/other",
231 },
232 {
233 id: recreatedProjectId,
234 environmentId: localEnvironmentId,
235 workspaceRoot: "/projects/repo",
236 },
237 ],
238 localEnvironmentId,
239 candidate("/projects/repo"),
240 ),
241 ).toBe(recreatedProjectId);
242 });
243
244 it("does not reuse a moved project when the scan has no project ID", () => {
245 expect(
246 resolveOnboardingProjectId(
247 [
248 {
249 id: localProjectId,
250 environmentId: localEnvironmentId,
251 workspaceRoot: "/projects/moved",
252 },
253 ],
254 localEnvironmentId,
255 candidate("/projects/repo"),
256 ),
257 ).toBeNull();
258 });
259});
260
261describe("resolveOnboardingLandingProject", () => {
262 it("skips a failed first project for a later project with imported history", () => {
263 expect(
264 resolveOnboardingLandingProject(
265 ["/projects/failed", "/projects/imported"],
266 new Map([["/projects/imported", "imported"]]),
267 new Map([["/projects/imported", "imported"]]),
268 ),
269 ).toBe("imported");
270 });
271
272 it("prefers a partial first import that added history", () => {
273 expect(
274 resolveOnboardingLandingProject(
275 ["/projects/partial", "/projects/complete"],
276 new Map([["/projects/partial", "partial"]]),
277 new Map([["/projects/complete", "complete"]]),
278 ),
279 ).toBe("partial");
280 });
281
282 it("uses a completed zero-history project when no import added history", () => {
283 expect(
284 resolveOnboardingLandingProject(
285 ["/projects/empty", "/projects/failed"],
286 new Map(),
287 new Map([["/projects/empty", "empty"]]),
288 ),
289 ).toBe("empty");
290 });
291
292 it("keeps an earlier successful import available on retry", () => {
293 expect(
294 resolveOnboardingLandingProject(
295 ["/projects/imported", "/projects/retry"],
296 new Map([["/projects/imported", "imported"]]),
297 new Map([["/projects/imported", "imported"]]),
298 ),
299 ).toBe("imported");
300 });
301
302 it("ignores cached successes outside the current retry selection", () => {
303 expect(
304 resolveOnboardingLandingProject(
305 ["/projects/current"],
306 new Map([["/projects/previous", "previous"]]),
307 new Map([
308 ["/projects/previous", "previous"],
309 ["/projects/current", "current"],
310 ]),
311 ),
312 ).toBe("current");
313 });
314});
315
316describe("projects on multiple computers", () => {
317 it("keeps identical paths independent when selecting a landing project after partial imports", () => {
318 const first = onboardingProjectKey(EnvironmentId.make("first"), "/code/app");
319 const second = onboardingProjectKey(EnvironmentId.make("second"), "/code/app");
320 const completed = new Map([[first, "first-project"]]);
321 expect(completed.has(second)).toBe(false);
322 expect(resolveOnboardingLandingProject([second], completed, completed)).toBeUndefined();
323 completed.set(second, "second-project");
324 expect(resolveOnboardingLandingProject([second, first], completed, completed)).toBe(
325 "second-project",
326 );
327 });
328
329 it("preserves computer identity when choosing recent projects", () => {
330 const first = { ...candidate("/code/app"), environmentId: "first" };
331 const second = { ...candidate("/code/app"), environmentId: "second" };
332 expect(partitionOnboardingProjects([first, second], now).recent).toEqual([first, second]);
333 });
334});