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

projectImport.logic.test.ts

273 lines8.7 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 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 it("finds the current root owner when the scan has no project ID", () => {
197 const recreatedProjectId = ProjectId.make("recreated-project");
198 expect(
199 resolveOnboardingProjectId(
200 [
201 {
202 id: localProjectId,
203 environmentId: localEnvironmentId,
204 workspaceRoot: "/projects/other",
205 },
206 {
207 id: recreatedProjectId,
208 environmentId: localEnvironmentId,
209 workspaceRoot: "/projects/repo",
210 },
211 ],
212 localEnvironmentId,
213 candidate("/projects/repo"),
214 ),
215 ).toBe(recreatedProjectId);
216 });
217});
218​
219describe("resolveOnboardingLandingProject", () => {
220 it("prefers a partial first import that added history", () => {
221 expect(
222 resolveOnboardingLandingProject(
223 ["/projects/partial", "/projects/complete"],
224 new Map([["/projects/partial", "partial"]]),
225 new Map([["/projects/complete", "complete"]]),
226 ),
227 ).toBe("partial");
228 });
229​
230 it("uses a completed zero-history project when no import added history", () => {
231 expect(
232 resolveOnboardingLandingProject(
233 ["/projects/empty", "/projects/failed"],
234 new Map(),
235 new Map([["/projects/empty", "empty"]]),
236 ),
237 ).toBe("empty");
238 });
239​
240 it("ignores cached successes outside the current retry selection", () => {
241 expect(
242 resolveOnboardingLandingProject(
243 ["/projects/current"],
244 new Map([["/projects/previous", "previous"]]),
245 new Map([
246 ["/projects/previous", "previous"],
247 ["/projects/current", "current"],
248 ]),
249 ),
250 ).toBe("current");
251 });
252});
253​
254describe("projects on multiple computers", () => {
255 it("keeps identical paths independent when selecting a landing project after partial imports", () => {
256 const first = onboardingProjectKey(EnvironmentId.make("first"), "/code/app");
257 const second = onboardingProjectKey(EnvironmentId.make("second"), "/code/app");
258 const completed = new Map([[first, "first-project"]]);
259 expect(completed.has(second)).toBe(false);
260 expect(resolveOnboardingLandingProject([second], completed, completed)).toBeUndefined();
261 completed.set(second, "second-project");
262 expect(resolveOnboardingLandingProject([second, first], completed, completed)).toBe(
263 "second-project",
264 );
265 });
266​
267 // regression: #10465 multi-computer onboarding mixed up which computer a project came from
268 it("preserves computer identity when choosing recent projects", () => {
269 const first = { ...candidate("/code/app"), environmentId: "first" };
270 const second = { ...candidate("/code/app"), environmentId: "second" };
271 expect(partitionOnboardingProjects([first, second], now).recent).toEqual([first, second]);
272 });
273});