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

environmentGrouping.test.ts

453 lines15.5 KB

1import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
2import { scopeProjectRef } from "@t3tools/client-runtime";
3import { describe, expect, it } from "vitest";
4​
5import {
6 selectProjectsAcrossEnvironments,
7 selectSidebarThreadsAcrossEnvironments,
8 selectSidebarThreadsForProjectRef,
9 selectSidebarThreadsForProjectRefs,
10 type AppState,
11 type EnvironmentState,
12} from "./store";
13import { deriveLogicalProjectKey } from "./logicalProject";
14import type { Project, SidebarThreadSummary } from "./types";
15import { DEFAULT_INTERACTION_MODE } from "./types";
16​
17// ── Fixture Identifiers ──────────────────────────────────────────────
18​
19const primaryEnvId = EnvironmentId.makeUnsafe("env-primary");
20const remoteEnvId = EnvironmentId.makeUnsafe("env-remote");
21​
22const sharedProjectPrimaryId = ProjectId.makeUnsafe("shared-proj-primary");
23const sharedProjectRemoteId = ProjectId.makeUnsafe("shared-proj-remote");
24const localOnlyProjectId = ProjectId.makeUnsafe("local-only-proj");
25const remoteOnlyProjectId = ProjectId.makeUnsafe("remote-only-proj");
26​
27const threadP1 = ThreadId.makeUnsafe("thread-shared-primary-1");
28const threadP2 = ThreadId.makeUnsafe("thread-shared-primary-2");
29const threadR1 = ThreadId.makeUnsafe("thread-shared-remote-1");
30const threadL1 = ThreadId.makeUnsafe("thread-local-only-1");
31const threadRO1 = ThreadId.makeUnsafe("thread-remote-only-1");
32​
33const SHARED_REPO_CANONICAL_KEY = "github.com/example/shared-repo";
34​
35// ── Factory Helpers ──────────────────────────────────────────────────
36​
37function makeProject(
38 overrides: Partial<Project> & Pick<Project, "id" | "environmentId" | "name">,
39): Project {
40 return {
41 cwd: `/tmp/${overrides.name}`,
42 defaultModelSelection: { provider: "codex" as const, model: "gpt-5-codex" },
43 createdAt: "2026-01-01T00:00:00.000Z",
44 updatedAt: "2026-01-01T00:00:00.000Z",
45 scripts: [],
46 ...overrides,
47 };
49​
50function makeSidebarThreadSummary(
51 overrides: Partial<SidebarThreadSummary> &
52 Pick<SidebarThreadSummary, "id" | "environmentId" | "projectId" | "title">,
53): SidebarThreadSummary {
54 return {
55 interactionMode: DEFAULT_INTERACTION_MODE,
56 session: null,
57 createdAt: "2026-01-01T00:00:00.000Z",
58 archivedAt: null,
59 updatedAt: "2026-01-01T00:00:00.000Z",
60 latestTurn: null,
61 branch: null,
62 worktreePath: null,
63 latestUserMessageAt: null,
64 hasPendingApprovals: false,
65 hasPendingUserInput: false,
66 hasActionableProposedPlan: false,
67 ...overrides,
68 };
70​
71function makeEmptyEnvironmentState(): EnvironmentState {
72 return {
73 projectIds: [],
74 projectById: {},
75 threadIds: [],
76 threadIdsByProjectId: {},
77 threadShellById: {},
78 threadSessionById: {},
79 threadTurnStateById: {},
80 messageIdsByThreadId: {},
81 messageByThreadId: {},
82 activityIdsByThreadId: {},
83 activityByThreadId: {},
84 proposedPlanIdsByThreadId: {},
85 proposedPlanByThreadId: {},
86 turnDiffIdsByThreadId: {},
87 turnDiffSummaryByThreadId: {},
88 sidebarThreadSummaryById: {},
89 bootstrapComplete: true,
90 };
92​
93// ── Fixture: Two environments, shared + local-only + remote-only projects ──
94​
95function makeFixtureState(): AppState {
96 // Shared project: same repo in both envs
97 const sharedProjectPrimary = makeProject({
98 id: sharedProjectPrimaryId,
99 environmentId: primaryEnvId,
100 name: "shared-repo",
101 repositoryIdentity: {
102 canonicalKey: SHARED_REPO_CANONICAL_KEY,
103 locator: {
104 source: "git-remote",
105 remoteName: "origin",
106 remoteUrl: "https://github.com/example/shared-repo.git",
107 },
108 },
109 });
110 const sharedProjectRemote = makeProject({
111 id: sharedProjectRemoteId,
112 environmentId: remoteEnvId,
113 name: "shared-repo",
114 repositoryIdentity: {
115 canonicalKey: SHARED_REPO_CANONICAL_KEY,
116 locator: {
117 source: "git-remote",
118 remoteName: "origin",
119 remoteUrl: "https://github.com/example/shared-repo.git",
120 },
121 },
122 });
123 // Local-only project
124 const localOnlyProject = makeProject({
125 id: localOnlyProjectId,
126 environmentId: primaryEnvId,
127 name: "local-only",
128 });
129 // Remote-only project
130 const remoteOnlyProject = makeProject({
131 id: remoteOnlyProjectId,
132 environmentId: remoteEnvId,
133 name: "remote-only",
134 });
135​
136 // Threads
137 const summaryP1 = makeSidebarThreadSummary({
138 id: threadP1,
139 environmentId: primaryEnvId,
140 projectId: sharedProjectPrimaryId,
141 title: "Shared primary thread 1",
142 });
143 const summaryP2 = makeSidebarThreadSummary({
144 id: threadP2,
145 environmentId: primaryEnvId,
146 projectId: sharedProjectPrimaryId,
147 title: "Shared primary thread 2",
148 });
149 const summaryR1 = makeSidebarThreadSummary({
150 id: threadR1,
151 environmentId: remoteEnvId,
152 projectId: sharedProjectRemoteId,
153 title: "Shared remote thread 1",
154 });
155 const summaryL1 = makeSidebarThreadSummary({
156 id: threadL1,
157 environmentId: primaryEnvId,
158 projectId: localOnlyProjectId,
159 title: "Local only thread 1",
160 });
161 const summaryRO1 = makeSidebarThreadSummary({
162 id: threadRO1,
163 environmentId: remoteEnvId,
164 projectId: remoteOnlyProjectId,
165 title: "Remote only thread 1",
166 });
167​
168 const primaryEnvState: EnvironmentState = {
169 ...makeEmptyEnvironmentState(),
170 projectIds: [sharedProjectPrimaryId, localOnlyProjectId],
171 projectById: {
172 [sharedProjectPrimaryId]: sharedProjectPrimary,
173 [localOnlyProjectId]: localOnlyProject,
174 },
175 threadIds: [threadP1, threadP2, threadL1],
176 threadIdsByProjectId: {
177 [sharedProjectPrimaryId]: [threadP1, threadP2],
178 [localOnlyProjectId]: [threadL1],
179 },
180 sidebarThreadSummaryById: {
181 [threadP1]: summaryP1,
182 [threadP2]: summaryP2,
183 [threadL1]: summaryL1,
184 },
185 };
186​
187 const remoteEnvState: EnvironmentState = {
188 ...makeEmptyEnvironmentState(),
189 projectIds: [sharedProjectRemoteId, remoteOnlyProjectId],
190 projectById: {
191 [sharedProjectRemoteId]: sharedProjectRemote,
192 [remoteOnlyProjectId]: remoteOnlyProject,
193 },
194 threadIds: [threadR1, threadRO1],
195 threadIdsByProjectId: {
196 [sharedProjectRemoteId]: [threadR1],
197 [remoteOnlyProjectId]: [threadRO1],
198 },
199 sidebarThreadSummaryById: {
200 [threadR1]: summaryR1,
201 [threadRO1]: summaryRO1,
202 },
203 };
204​
205 return {
206 activeEnvironmentId: primaryEnvId,
207 environmentStateById: {
208 [primaryEnvId]: primaryEnvState,
209 [remoteEnvId]: remoteEnvState,
210 },
211 };
213​
214// ── Tests ────────────────────────────────────────────────────────────
215​
216describe("environment grouping", () => {
217 describe("deriveLogicalProjectKey", () => {
218 it("uses repositoryIdentity.canonicalKey when present", () => {
219 const project = makeProject({
220 id: sharedProjectPrimaryId,
221 environmentId: primaryEnvId,
222 name: "shared-repo",
223 repositoryIdentity: {
224 canonicalKey: SHARED_REPO_CANONICAL_KEY,
225 locator: {
226 source: "git-remote",
227 remoteName: "origin",
228 remoteUrl: "https://github.com/example/shared-repo.git",
229 },
230 },
231 });
232 expect(deriveLogicalProjectKey(project)).toBe(SHARED_REPO_CANONICAL_KEY);
233 });
234​
235 it("falls back to scoped project key when no repositoryIdentity", () => {
236 const project = makeProject({
237 id: localOnlyProjectId,
238 environmentId: primaryEnvId,
239 name: "local-only",
240 });
241 const key = deriveLogicalProjectKey(project);
242 expect(key).toContain(primaryEnvId);
243 expect(key).toContain(localOnlyProjectId);
244 });
245​
246 it("groups projects from different environments that share the same canonical key", () => {
247 const primary = makeProject({
248 id: sharedProjectPrimaryId,
249 environmentId: primaryEnvId,
250 name: "shared-repo",
251 repositoryIdentity: {
252 canonicalKey: SHARED_REPO_CANONICAL_KEY,
253 locator: {
254 source: "git-remote",
255 remoteName: "origin",
256 remoteUrl: "https://github.com/example/shared-repo.git",
257 },
258 },
259 });
260 const remote = makeProject({
261 id: sharedProjectRemoteId,
262 environmentId: remoteEnvId,
263 name: "shared-repo",
264 repositoryIdentity: {
265 canonicalKey: SHARED_REPO_CANONICAL_KEY,
266 locator: {
267 source: "git-remote",
268 remoteName: "origin",
269 remoteUrl: "https://github.com/example/shared-repo.git",
270 },
271 },
272 });
273 expect(deriveLogicalProjectKey(primary)).toBe(deriveLogicalProjectKey(remote));
274 });
275​
276 it("does NOT group projects without shared canonical key", () => {
277 const local = makeProject({
278 id: localOnlyProjectId,
279 environmentId: primaryEnvId,
280 name: "local-only",
281 });
282 const remote = makeProject({
283 id: remoteOnlyProjectId,
284 environmentId: remoteEnvId,
285 name: "remote-only",
286 });
287 expect(deriveLogicalProjectKey(local)).not.toBe(deriveLogicalProjectKey(remote));
288 });
289 });
290​
291 describe("selectProjectsAcrossEnvironments", () => {
292 it("returns all projects from all environments", () => {
293 const state = makeFixtureState();
294 const projects = selectProjectsAcrossEnvironments(state);
295 expect(projects).toHaveLength(4);
296 const names = projects.map((p) => p.name).toSorted();
297 expect(names).toEqual(["local-only", "remote-only", "shared-repo", "shared-repo"]);
298 });
299 });
300​
301 describe("selectSidebarThreadsAcrossEnvironments", () => {
302 it("returns all sidebar thread summaries from all environments", () => {
303 const state = makeFixtureState();
304 const threads = selectSidebarThreadsAcrossEnvironments(state);
305 expect(threads).toHaveLength(5);
306 const ids = new Set(threads.map((t) => t.id));
307 expect(ids).toContain(threadP1);
308 expect(ids).toContain(threadP2);
309 expect(ids).toContain(threadR1);
310 expect(ids).toContain(threadL1);
311 expect(ids).toContain(threadRO1);
312 });
313 });
314​
315 describe("selectSidebarThreadsForProjectRef", () => {
316 it("returns threads for a single project ref", () => {
317 const state = makeFixtureState();
318 const ref = scopeProjectRef(primaryEnvId, sharedProjectPrimaryId);
319 const threads = selectSidebarThreadsForProjectRef(state, ref);
320 expect(threads).toHaveLength(2);
321 expect(threads.map((t) => t.id)).toEqual([threadP1, threadP2]);
322 });
323​
324 it("returns empty array for null ref", () => {
325 const state = makeFixtureState();
326 expect(selectSidebarThreadsForProjectRef(state, null)).toEqual([]);
327 });
328​
329 it("returns empty array for nonexistent environment", () => {
330 const state = makeFixtureState();
331 const ref = scopeProjectRef(EnvironmentId.makeUnsafe("nonexistent"), sharedProjectPrimaryId);
332 expect(selectSidebarThreadsForProjectRef(state, ref)).toEqual([]);
333 });
334 });
335​
336 describe("selectSidebarThreadsForProjectRefs", () => {
337 it("returns empty for empty refs", () => {
338 const state = makeFixtureState();
339 expect(selectSidebarThreadsForProjectRefs(state, [])).toEqual([]);
340 });
341​
342 it("returns threads for a single ref", () => {
343 const state = makeFixtureState();
344 const refs = [scopeProjectRef(primaryEnvId, sharedProjectPrimaryId)];
345 const threads = selectSidebarThreadsForProjectRefs(state, refs);
346 expect(threads).toHaveLength(2);
347 expect(threads.map((t) => t.id)).toEqual([threadP1, threadP2]);
348 });
349​
350 it("returns combined threads from multiple refs across environments", () => {
351 const state = makeFixtureState();
352 const refs = [
353 scopeProjectRef(primaryEnvId, sharedProjectPrimaryId),
354 scopeProjectRef(remoteEnvId, sharedProjectRemoteId),
355 ];
356 const threads = selectSidebarThreadsForProjectRefs(state, refs);
357 expect(threads).toHaveLength(3);
358 const ids = new Set(threads.map((t) => t.id));
359 expect(ids).toContain(threadP1);
360 expect(ids).toContain(threadP2);
361 expect(ids).toContain(threadR1);
362 });
363​
364 it("returns threads from remote-only project", () => {
365 const state = makeFixtureState();
366 const refs = [scopeProjectRef(remoteEnvId, remoteOnlyProjectId)];
367 const threads = selectSidebarThreadsForProjectRefs(state, refs);
368 expect(threads).toHaveLength(1);
369 expect(threads[0]?.id).toBe(threadRO1);
370 });
371​
372 it("returns threads from local-only project", () => {
373 const state = makeFixtureState();
374 const refs = [scopeProjectRef(primaryEnvId, localOnlyProjectId)];
375 const threads = selectSidebarThreadsForProjectRefs(state, refs);
376 expect(threads).toHaveLength(1);
377 expect(threads[0]?.id).toBe(threadL1);
378 });
379​
380 it("handles refs with nonexistent environment gracefully", () => {
381 const state = makeFixtureState();
382 const refs = [
383 scopeProjectRef(primaryEnvId, sharedProjectPrimaryId),
384 scopeProjectRef(EnvironmentId.makeUnsafe("nonexistent"), ProjectId.makeUnsafe("nope")),
385 ];
386 const threads = selectSidebarThreadsForProjectRefs(state, refs);
387 // Only returns threads from the valid ref
388 expect(threads).toHaveLength(2);
389 expect(threads.map((t) => t.id)).toEqual([threadP1, threadP2]);
390 });
391 });
392​
393 describe("logical project grouping for sidebar", () => {
394 it("computes correct logical key for grouped projects and aggregates threads", () => {
395 const state = makeFixtureState();
396 const allProjects = selectProjectsAcrossEnvironments(state);
397​
398 // Group by logical key
399 const groups = new Map<string, Project[]>();
400 for (const project of allProjects) {
401 const key = deriveLogicalProjectKey(project);
402 const existing = groups.get(key) ?? [];
403 existing.push(project);
404 groups.set(key, existing);
405 }
406​
407 // Shared project should be grouped
408 const sharedGroup = groups.get(SHARED_REPO_CANONICAL_KEY);
409 expect(sharedGroup).toBeDefined();
410 expect(sharedGroup).toHaveLength(2);
411 expect(sharedGroup!.map((p) => p.environmentId).toSorted()).toEqual(
412 [primaryEnvId, remoteEnvId].toSorted(),
413 );
414​
415 // Build member refs for the grouped project and fetch threads
416 const memberRefs = sharedGroup!.map((p) => scopeProjectRef(p.environmentId, p.id));
417 const threads = selectSidebarThreadsForProjectRefs(state, memberRefs);
418 expect(threads).toHaveLength(3);
419 const threadIds = threads.map((t) => t.id);
420 expect(threadIds).toContain(threadP1);
421 expect(threadIds).toContain(threadP2);
422 expect(threadIds).toContain(threadR1);
423 });
424​
425 it("local-only and remote-only projects remain ungrouped", () => {
426 const state = makeFixtureState();
427 const allProjects = selectProjectsAcrossEnvironments(state);
428​
429 const groups = new Map<string, Project[]>();
430 for (const project of allProjects) {
431 const key = deriveLogicalProjectKey(project);
432 const existing = groups.get(key) ?? [];
433 existing.push(project);
434 groups.set(key, existing);
435 }
436​
437 // Should have 3 groups total: shared, local-only, remote-only
438 expect(groups.size).toBe(3);
439​
440 // Local-only group
441 const localKey = deriveLogicalProjectKey(
442 allProjects.find((p) => p.id === localOnlyProjectId)!,
443 );
444 expect(groups.get(localKey)).toHaveLength(1);
445​
446 // Remote-only group
447 const remoteKey = deriveLogicalProjectKey(
448 allProjects.find((p) => p.id === remoteOnlyProjectId)!,
449 );
450 expect(groups.get(remoteKey)).toHaveLength(1);
451 });
452 });
453});