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 non-git folders and thin histories out of the default selection", () => {
59 const repo = candidate("/projects/repo");
60 const folder = candidate("/projects/folder", { git: null });
61 const thin = candidate("/projects/thin", { threadCount: 2 });
62
63 expect(partitionOnboardingProjects([repo, folder, thin], now).recent).toEqual([repo]);
64 });
65
66 it("selects candidates from servers that do not report git identity", () => {
67 const { git: _git, ...legacy } = candidate("/projects/legacy");
68
69 expect(partitionOnboardingProjects([legacy], now).recent).toEqual([legacy]);
70 });
71});
72
73describe("groupOnboardingProjects", () => {
74 it("groups clones by origin, keeps local repos separate, and folds non-git folders away", () => {
75 const main = candidate("/code/t3code", {
76 git: github("pingdotgg/t3code"),
77 threadCount: 79,
78 lastActiveAt: "2026-08-21T12:00:00.000Z",
79 });
80 const clone = candidate("/code/clones/t3code-2", {
81 git: github("pingdotgg/t3code"),
82 threadCount: 13,
83 lastActiveAt: "2026-08-10T12:00:00.000Z",
84 });
85 const older = candidate("/code/fleet", {
86 git: github("t3dotgg/fleet"),
87 threadCount: 295,
88 lastActiveAt: "2026-08-22T00:00:00.000Z",
89 });
90 const local = candidate("/code/scratch-repo", { title: "scratch-repo" });
91 const folder = candidate("/tmp/notes", { git: null });
92
93 const grouped = groupOnboardingProjects([main, clone, older, local, folder]);
94
95 expect(grouped.other).toEqual([folder]);
96 expect(
97 grouped.repositories.map((group) => ({
98 label: group.label,
99 paths: group.candidates.map((item) => item.path),
100 threadCount: group.threadCount,
101 lastActiveAt: group.lastActiveAt,
102 })),
103 ).toEqual([
104 {
105 label: "t3dotgg/fleet",
106 paths: ["/code/fleet"],
107 threadCount: 295,
108 lastActiveAt: "2026-08-22T00:00:00.000Z",
109 },
110 {
111 label: "pingdotgg/t3code",
112 paths: ["/code/t3code", "/code/clones/t3code-2"],
113 threadCount: 92,
114 lastActiveAt: "2026-08-21T12:00:00.000Z",
115 },
116 {
117 label: "scratch-repo",
118 paths: ["/code/scratch-repo"],
119 threadCount: 3,
120 lastActiveAt: "2026-08-20T12:00:00.000Z",
121 },
122 ]);
123 });
124
125 // regression: #10547 project import broke on servers that predate git identity
126 it("lists candidates without git identity as standalone repositories", () => {
127 const { git: _git, ...legacy } = candidate("/code/legacy", { title: "legacy" });
128
129 const grouped = groupOnboardingProjects([legacy]);
130
131 expect(grouped.other).toEqual([]);
132 expect(grouped.repositories.map((group) => [group.label, group.repository])).toEqual([
133 ["legacy", null],
134 ]);
135 });
136});
137
138describe("resolveOnboardingProjectId", () => {
139 const localEnvironmentId = EnvironmentId.make("local");
140 const remoteEnvironmentId = EnvironmentId.make("remote");
141 const localProjectId = ProjectId.make("local-project");
142
143 it("uses the scanned project ID when the client still has an older project at that root", () => {
144 expect(
145 resolveOnboardingProjectId(
146 [
147 {
148 id: ProjectId.make("stale-project"),
149 environmentId: localEnvironmentId,
150 workspaceRoot: "/projects/repo",
151 },
152 ],
153 localEnvironmentId,
154 candidate("/projects/repo", { projectId: localProjectId }),
155 ),
156 ).toBe(localProjectId);
157 });
158
159 it("finds an existing project by normalized root in the target environment", () => {
160 expect(
161 resolveOnboardingProjectId(
162 [
163 {
164 id: ProjectId.make("remote-project"),
165 environmentId: remoteEnvironmentId,
166 workspaceRoot: "C:\\Work\\Repo",
167 },
168 {
169 id: localProjectId,
170 environmentId: localEnvironmentId,
171 workspaceRoot: "C:\\Work\\Repo\\",
172 },
173 ],
174 localEnvironmentId,
175 candidate("c:/work/repo"),
176 ),
177 ).toBe(localProjectId);
178 });
179
180 it("does not reuse a project from another environment", () => {
181 expect(
182 resolveOnboardingProjectId(
183 [
184 {
185 id: ProjectId.make("remote-project"),
186 environmentId: remoteEnvironmentId,
187 workspaceRoot: "/projects/repo",
188 },
189 ],
190 localEnvironmentId,
191 candidate("/projects/repo"),
192 ),
193 ).toBeNull();
194 });
195});
196
197describe("resolveOnboardingLandingProject", () => {
198 it("prefers a partial first import that added history", () => {
199 expect(
200 resolveOnboardingLandingProject(
201 ["/projects/partial", "/projects/complete"],
202 new Map([["/projects/partial", "partial"]]),
203 new Map([["/projects/complete", "complete"]]),
204 ),
205 ).toBe("partial");
206 });
207
208 it("uses a completed zero-history project when no import added history", () => {
209 expect(
210 resolveOnboardingLandingProject(
211 ["/projects/empty", "/projects/failed"],
212 new Map(),
213 new Map([["/projects/empty", "empty"]]),
214 ),
215 ).toBe("empty");
216 });
217
218 it("ignores cached successes outside the current retry selection", () => {
219 expect(
220 resolveOnboardingLandingProject(
221 ["/projects/current"],
222 new Map([["/projects/previous", "previous"]]),
223 new Map([
224 ["/projects/previous", "previous"],
225 ["/projects/current", "current"],
226 ]),
227 ),
228 ).toBe("current");
229 });
230});
231
232describe("projects on multiple computers", () => {
233 it("keeps identical paths independent when selecting a landing project after partial imports", () => {
234 const first = onboardingProjectKey(EnvironmentId.make("first"), "/code/app");
235 const second = onboardingProjectKey(EnvironmentId.make("second"), "/code/app");
236 const completed = new Map([[first, "first-project"]]);
237 expect(completed.has(second)).toBe(false);
238 expect(resolveOnboardingLandingProject([second], completed, completed)).toBeUndefined();
239 completed.set(second, "second-project");
240 expect(resolveOnboardingLandingProject([second, first], completed, completed)).toBe(
241 "second-project",
242 );
243 });
244
245 // regression: #10465 multi-computer onboarding mixed up which computer a project came from
246 it("preserves computer identity when choosing recent projects", () => {
247 const first = { ...candidate("/code/app"), environmentId: "first" };
248 const second = { ...candidate("/code/app"), environmentId: "second" };
249 expect(partitionOnboardingProjects([first, second], now).recent).toEqual([first, second]);
250 });
251});