Open History to see the last commit
1import { EnvironmentId, ProjectId, ProviderInstanceId, 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 {
14 deriveLogicalProjectKey,
15 deriveLogicalProjectKeyFromSettings,
16 derivePhysicalProjectKey,
17 resolveProjectGroupingMode,
18} from "./logicalProject";
19import type { Project, SidebarThreadSummary } from "./types";
20import { DEFAULT_INTERACTION_MODE } from "./types";
21
22// ── Fixture Identifiers ──────────────────────────────────────────────
23
24const primaryEnvId = EnvironmentId.make("env-primary");
25const remoteEnvId = EnvironmentId.make("env-remote");
26
27const sharedProjectPrimaryId = ProjectId.make("shared-proj-primary");
28const sharedProjectRemoteId = ProjectId.make("shared-proj-remote");
29const localOnlyProjectId = ProjectId.make("local-only-proj");
30const remoteOnlyProjectId = ProjectId.make("remote-only-proj");
31
32const threadP1 = ThreadId.make("thread-shared-primary-1");
33const threadP2 = ThreadId.make("thread-shared-primary-2");
34const threadR1 = ThreadId.make("thread-shared-remote-1");
35const threadL1 = ThreadId.make("thread-local-only-1");
36const threadRO1 = ThreadId.make("thread-remote-only-1");
37
38const SHARED_REPO_CANONICAL_KEY = "github.com/example/shared-repo";
39const DEFAULT_GROUPING_SETTINGS = {
40 sidebarProjectGroupingMode: "repository" as const,
41 sidebarProjectGroupingOverrides: {},
42};
43
44// ── Factory Helpers ──────────────────────────────────────────────────
45
46function makeProject(
47 overrides: Partial<Project> & Pick<Project, "id" | "environmentId" | "name">,
48): Project {
49 return {
50 cwd: `/tmp/${overrides.name}`,
51 defaultModelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5-codex" },
52 createdAt: "2026-01-01T00:00:00.000Z",
53 updatedAt: "2026-01-01T00:00:00.000Z",
54 scripts: [],
55 ...overrides,
56 };
57}
58
59function makeSidebarThreadSummary(
60 overrides: Partial<SidebarThreadSummary> &
61 Pick<SidebarThreadSummary, "id" | "environmentId" | "projectId" | "title">,
62): SidebarThreadSummary {
63 return {
64 interactionMode: DEFAULT_INTERACTION_MODE,
65 session: null,
66 createdAt: "2026-01-01T00:00:00.000Z",
67 archivedAt: null,
68 updatedAt: "2026-01-01T00:00:00.000Z",
69 latestTurn: null,
70 branch: null,
71 worktreePath: null,
72 latestUserMessageAt: null,
73 hasPendingApprovals: false,
74 hasPendingUserInput: false,
75 hasActionableProposedPlan: false,
76 ...overrides,
77 };
78}
79
80function makeEmptyEnvironmentState(): EnvironmentState {
81 return {
82 projectIds: [],
83 projectById: {},
84 threadIds: [],
85 threadIdsByProjectId: {},
86 threadShellById: {},
87 threadSessionById: {},
88 threadTurnStateById: {},
89 messageIdsByThreadId: {},
90 messageByThreadId: {},
91 activityIdsByThreadId: {},
92 activityByThreadId: {},
93 proposedPlanIdsByThreadId: {},
94 proposedPlanByThreadId: {},
95 turnDiffIdsByThreadId: {},
96 turnDiffSummaryByThreadId: {},
97 sidebarThreadSummaryById: {},
98 bootstrapComplete: true,
99 };
100}
101
102// ── Fixture: Two environments, shared + local-only + remote-only projects ──
103
104function makeFixtureState(): AppState {
105 // Shared project: same repo in both envs
106 const sharedProjectPrimary = makeProject({
107 id: sharedProjectPrimaryId,
108 environmentId: primaryEnvId,
109 name: "shared-repo",
110 repositoryIdentity: {
111 canonicalKey: SHARED_REPO_CANONICAL_KEY,
112 locator: {
113 source: "git-remote",
114 remoteName: "origin",
115 remoteUrl: "https://github.com/example/shared-repo.git",
116 },
117 },
118 });
119 const sharedProjectRemote = makeProject({
120 id: sharedProjectRemoteId,
121 environmentId: remoteEnvId,
122 name: "shared-repo",
123 repositoryIdentity: {
124 canonicalKey: SHARED_REPO_CANONICAL_KEY,
125 locator: {
126 source: "git-remote",
127 remoteName: "origin",
128 remoteUrl: "https://github.com/example/shared-repo.git",
129 },
130 },
131 });
132 // Local-only project
133 const localOnlyProject = makeProject({
134 id: localOnlyProjectId,
135 environmentId: primaryEnvId,
136 name: "local-only",
137 });
138 // Remote-only project
139 const remoteOnlyProject = makeProject({
140 id: remoteOnlyProjectId,
141 environmentId: remoteEnvId,
142 name: "remote-only",
143 });
144
145 // Threads
146 const summaryP1 = makeSidebarThreadSummary({
147 id: threadP1,
148 environmentId: primaryEnvId,
149 projectId: sharedProjectPrimaryId,
150 title: "Shared primary thread 1",
151 });
152 const summaryP2 = makeSidebarThreadSummary({
153 id: threadP2,
154 environmentId: primaryEnvId,
155 projectId: sharedProjectPrimaryId,
156 title: "Shared primary thread 2",
157 });
158 const summaryR1 = makeSidebarThreadSummary({
159 id: threadR1,
160 environmentId: remoteEnvId,
161 projectId: sharedProjectRemoteId,
162 title: "Shared remote thread 1",
163 });
164 const summaryL1 = makeSidebarThreadSummary({
165 id: threadL1,
166 environmentId: primaryEnvId,
167 projectId: localOnlyProjectId,
168 title: "Local only thread 1",
169 });
170 const summaryRO1 = makeSidebarThreadSummary({
171 id: threadRO1,
172 environmentId: remoteEnvId,
173 projectId: remoteOnlyProjectId,
174 title: "Remote only thread 1",
175 });
176
177 const primaryEnvState: EnvironmentState = {
178 ...makeEmptyEnvironmentState(),
179 projectIds: [sharedProjectPrimaryId, localOnlyProjectId],
180 projectById: {
181 [sharedProjectPrimaryId]: sharedProjectPrimary,
182 [localOnlyProjectId]: localOnlyProject,
183 },
184 threadIds: [threadP1, threadP2, threadL1],
185 threadIdsByProjectId: {
186 [sharedProjectPrimaryId]: [threadP1, threadP2],
187 [localOnlyProjectId]: [threadL1],
188 },
189 sidebarThreadSummaryById: {
190 [threadP1]: summaryP1,
191 [threadP2]: summaryP2,
192 [threadL1]: summaryL1,
193 },
194 };
195
196 const remoteEnvState: EnvironmentState = {
197 ...makeEmptyEnvironmentState(),
198 projectIds: [sharedProjectRemoteId, remoteOnlyProjectId],
199 projectById: {
200 [sharedProjectRemoteId]: sharedProjectRemote,
201 [remoteOnlyProjectId]: remoteOnlyProject,
202 },
203 threadIds: [threadR1, threadRO1],
204 threadIdsByProjectId: {
205 [sharedProjectRemoteId]: [threadR1],
206 [remoteOnlyProjectId]: [threadRO1],
207 },
208 sidebarThreadSummaryById: {
209 [threadR1]: summaryR1,
210 [threadRO1]: summaryRO1,
211 },
212 };
213
214 return {
215 activeEnvironmentId: primaryEnvId,
216 environmentStateById: {
217 [primaryEnvId]: primaryEnvState,
218 [remoteEnvId]: remoteEnvState,
219 },
220 };
221}
222
223// ── Tests ────────────────────────────────────────────────────────────
224
225describe("environment grouping", () => {
226 describe("deriveLogicalProjectKey", () => {
227 it("uses repositoryIdentity.canonicalKey when present", () => {
228 const project = makeProject({
229 id: sharedProjectPrimaryId,
230 environmentId: primaryEnvId,
231 name: "shared-repo",
232 repositoryIdentity: {
233 canonicalKey: SHARED_REPO_CANONICAL_KEY,
234 locator: {
235 source: "git-remote",
236 remoteName: "origin",
237 remoteUrl: "https://github.com/example/shared-repo.git",
238 },
239 },
240 });
241 expect(deriveLogicalProjectKey(project)).toBe(SHARED_REPO_CANONICAL_KEY);
242 });
243
244 it("falls back to scoped project key when no repositoryIdentity", () => {
245 const project = makeProject({
246 id: localOnlyProjectId,
247 environmentId: primaryEnvId,
248 name: "local-only",
249 });
250 expect(deriveLogicalProjectKey(project)).toBe(derivePhysicalProjectKey(project));
251 });
252
253 it("groups projects from different environments that share the same canonical key", () => {
254 const primary = makeProject({
255 id: sharedProjectPrimaryId,
256 environmentId: primaryEnvId,
257 name: "shared-repo",
258 repositoryIdentity: {
259 canonicalKey: SHARED_REPO_CANONICAL_KEY,
260 locator: {
261 source: "git-remote",
262 remoteName: "origin",
263 remoteUrl: "https://github.com/example/shared-repo.git",
264 },
265 },
266 });
267 const remote = makeProject({
268 id: sharedProjectRemoteId,
269 environmentId: remoteEnvId,
270 name: "shared-repo",
271 repositoryIdentity: {
272 canonicalKey: SHARED_REPO_CANONICAL_KEY,
273 locator: {
274 source: "git-remote",
275 remoteName: "origin",
276 remoteUrl: "https://github.com/example/shared-repo.git",
277 },
278 },
279 });
280 expect(deriveLogicalProjectKey(primary)).toBe(deriveLogicalProjectKey(remote));
281 });
282
283 it("groups repo root and nested projects from the same repository by default", () => {
284 const rootProject = makeProject({
285 id: sharedProjectPrimaryId,
286 environmentId: primaryEnvId,
287 name: "shared-repo",
288 cwd: "/workspace/repo",
289 repositoryIdentity: {
290 canonicalKey: SHARED_REPO_CANONICAL_KEY,
291 rootPath: "/workspace/repo",
292 locator: {
293 source: "git-remote",
294 remoteName: "origin",
295 remoteUrl: "https://github.com/example/shared-repo.git",
296 },
297 },
298 });
299 const nestedProject = makeProject({
300 id: localOnlyProjectId,
301 environmentId: primaryEnvId,
302 name: "web",
303 cwd: "/workspace/repo/apps/web",
304 repositoryIdentity: {
305 canonicalKey: SHARED_REPO_CANONICAL_KEY,
306 rootPath: "/workspace/repo",
307 locator: {
308 source: "git-remote",
309 remoteName: "origin",
310 remoteUrl: "https://github.com/example/shared-repo.git",
311 },
312 },
313 });
314
315 expect(deriveLogicalProjectKey(rootProject)).toBe(SHARED_REPO_CANONICAL_KEY);
316 expect(deriveLogicalProjectKey(nestedProject)).toBe(SHARED_REPO_CANONICAL_KEY);
317 });
318
319 it("uses repository path grouping when requested", () => {
320 const rootProject = makeProject({
321 id: sharedProjectPrimaryId,
322 environmentId: primaryEnvId,
323 name: "shared-repo",
324 cwd: "/workspace/repo",
325 repositoryIdentity: {
326 canonicalKey: SHARED_REPO_CANONICAL_KEY,
327 rootPath: "/workspace/repo",
328 locator: {
329 source: "git-remote",
330 remoteName: "origin",
331 remoteUrl: "https://github.com/example/shared-repo.git",
332 },
333 },
334 });
335 const nestedProject = makeProject({
336 id: localOnlyProjectId,
337 environmentId: primaryEnvId,
338 name: "web",
339 cwd: "/workspace/repo/apps/web",
340 repositoryIdentity: {
341 canonicalKey: SHARED_REPO_CANONICAL_KEY,
342 rootPath: "/workspace/repo",
343 locator: {
344 source: "git-remote",
345 remoteName: "origin",
346 remoteUrl: "https://github.com/example/shared-repo.git",
347 },
348 },
349 });
350
351 expect(
352 deriveLogicalProjectKey(rootProject, {
353 groupingMode: "repository_path",
354 }),
355 ).toBe(SHARED_REPO_CANONICAL_KEY);
356 expect(
357 deriveLogicalProjectKey(nestedProject, {
358 groupingMode: "repository_path",
359 }),
360 ).toBe(`${SHARED_REPO_CANONICAL_KEY}::apps/web`);
361 });
362
363 it("groups matching nested project paths across environments when repo roots differ", () => {
364 const primary = makeProject({
365 id: sharedProjectPrimaryId,
366 environmentId: primaryEnvId,
367 name: "web",
368 cwd: "/workspace/repo/apps/web",
369 repositoryIdentity: {
370 canonicalKey: SHARED_REPO_CANONICAL_KEY,
371 rootPath: "/workspace/repo",
372 locator: {
373 source: "git-remote",
374 remoteName: "origin",
375 remoteUrl: "https://github.com/example/shared-repo.git",
376 },
377 },
378 });
379 const remote = makeProject({
380 id: sharedProjectRemoteId,
381 environmentId: remoteEnvId,
382 name: "web",
383 cwd: "/srv/checkout/apps/web",
384 repositoryIdentity: {
385 canonicalKey: SHARED_REPO_CANONICAL_KEY,
386 rootPath: "/srv/checkout",
387 locator: {
388 source: "git-remote",
389 remoteName: "origin",
390 remoteUrl: "https://github.com/example/shared-repo.git",
391 },
392 },
393 });
394
395 expect(
396 deriveLogicalProjectKey(primary, {
397 groupingMode: "repository_path",
398 }),
399 ).toBe(`${SHARED_REPO_CANONICAL_KEY}::apps/web`);
400 expect(
401 deriveLogicalProjectKey(primary, {
402 groupingMode: "repository_path",
403 }),
404 ).toBe(
405 deriveLogicalProjectKey(remote, {
406 groupingMode: "repository_path",
407 }),
408 );
409 });
410
411 it("does NOT group projects without shared canonical key", () => {
412 const local = makeProject({
413 id: localOnlyProjectId,
414 environmentId: primaryEnvId,
415 name: "local-only",
416 });
417 const remote = makeProject({
418 id: remoteOnlyProjectId,
419 environmentId: remoteEnvId,
420 name: "remote-only",
421 });
422 expect(deriveLogicalProjectKey(local)).not.toBe(deriveLogicalProjectKey(remote));
423 });
424
425 it("uses per-project overrides from settings", () => {
426 const project = makeProject({
427 id: sharedProjectPrimaryId,
428 environmentId: primaryEnvId,
429 name: "shared-repo",
430 repositoryIdentity: {
431 canonicalKey: SHARED_REPO_CANONICAL_KEY,
432 locator: {
433 source: "git-remote",
434 remoteName: "origin",
435 remoteUrl: "https://github.com/example/shared-repo.git",
436 },
437 },
438 });
439
440 expect(resolveProjectGroupingMode(project, DEFAULT_GROUPING_SETTINGS)).toBe("repository");
441 expect(
442 deriveLogicalProjectKeyFromSettings(project, {
443 ...DEFAULT_GROUPING_SETTINGS,
444 sidebarProjectGroupingOverrides: {
445 [derivePhysicalProjectKey(project)]: "separate",
446 },
447 }),
448 ).toBe(derivePhysicalProjectKey(project));
449 });
450 });
451
452 describe("selectProjectsAcrossEnvironments", () => {
453 it("returns all projects from all environments", () => {
454 const state = makeFixtureState();
455 const projects = selectProjectsAcrossEnvironments(state);
456 expect(projects).toHaveLength(4);
457 const names = projects.map((p) => p.name).toSorted();
458 expect(names).toEqual(["local-only", "remote-only", "shared-repo", "shared-repo"]);
459 });
460 });
461
462 describe("selectSidebarThreadsAcrossEnvironments", () => {
463 it("returns all sidebar thread summaries from all environments", () => {
464 const state = makeFixtureState();
465 const threads = selectSidebarThreadsAcrossEnvironments(state);
466 expect(threads).toHaveLength(5);
467 const ids = new Set(threads.map((t) => t.id));
468 expect(ids).toContain(threadP1);
469 expect(ids).toContain(threadP2);
470 expect(ids).toContain(threadR1);
471 expect(ids).toContain(threadL1);
472 expect(ids).toContain(threadRO1);
473 });
474 });
475
476 describe("selectSidebarThreadsForProjectRef", () => {
477 it("returns threads for a single project ref", () => {
478 const state = makeFixtureState();
479 const ref = scopeProjectRef(primaryEnvId, sharedProjectPrimaryId);
480 const threads = selectSidebarThreadsForProjectRef(state, ref);
481 expect(threads).toHaveLength(2);
482 expect(threads.map((t) => t.id)).toEqual([threadP1, threadP2]);
483 });
484
485 it("returns empty array for null ref", () => {
486 const state = makeFixtureState();
487 expect(selectSidebarThreadsForProjectRef(state, null)).toEqual([]);
488 });
489
490 it("returns empty array for nonexistent environment", () => {
491 const state = makeFixtureState();
492 const ref = scopeProjectRef(EnvironmentId.make("nonexistent"), sharedProjectPrimaryId);
493 expect(selectSidebarThreadsForProjectRef(state, ref)).toEqual([]);
494 });
495 });
496
497 describe("selectSidebarThreadsForProjectRefs", () => {
498 it("returns empty for empty refs", () => {
499 const state = makeFixtureState();
500 expect(selectSidebarThreadsForProjectRefs(state, [])).toEqual([]);
501 });
502
503 it("returns threads for a single ref", () => {
504 const state = makeFixtureState();
505 const refs = [scopeProjectRef(primaryEnvId, sharedProjectPrimaryId)];
506 const threads = selectSidebarThreadsForProjectRefs(state, refs);
507 expect(threads).toHaveLength(2);
508 expect(threads.map((t) => t.id)).toEqual([threadP1, threadP2]);
509 });
510
511 it("returns combined threads from multiple refs across environments", () => {
512 const state = makeFixtureState();
513 const refs = [
514 scopeProjectRef(primaryEnvId, sharedProjectPrimaryId),
515 scopeProjectRef(remoteEnvId, sharedProjectRemoteId),
516 ];
517 const threads = selectSidebarThreadsForProjectRefs(state, refs);
518 expect(threads).toHaveLength(3);
519 const ids = new Set(threads.map((t) => t.id));
520 expect(ids).toContain(threadP1);
521 expect(ids).toContain(threadP2);
522 expect(ids).toContain(threadR1);
523 });
524
525 it("returns threads from remote-only project", () => {
526 const state = makeFixtureState();
527 const refs = [scopeProjectRef(remoteEnvId, remoteOnlyProjectId)];
528 const threads = selectSidebarThreadsForProjectRefs(state, refs);
529 expect(threads).toHaveLength(1);
530 expect(threads[0]?.id).toBe(threadRO1);
531 });
532
533 it("returns threads from local-only project", () => {
534 const state = makeFixtureState();
535 const refs = [scopeProjectRef(primaryEnvId, localOnlyProjectId)];
536 const threads = selectSidebarThreadsForProjectRefs(state, refs);
537 expect(threads).toHaveLength(1);
538 expect(threads[0]?.id).toBe(threadL1);
539 });
540
541 it("handles refs with nonexistent environment gracefully", () => {
542 const state = makeFixtureState();
543 const refs = [
544 scopeProjectRef(primaryEnvId, sharedProjectPrimaryId),
545 scopeProjectRef(EnvironmentId.make("nonexistent"), ProjectId.make("nope")),
546 ];
547 const threads = selectSidebarThreadsForProjectRefs(state, refs);
548 // Only returns threads from the valid ref
549 expect(threads).toHaveLength(2);
550 expect(threads.map((t) => t.id)).toEqual([threadP1, threadP2]);
551 });
552 });
553
554 describe("logical project grouping for sidebar", () => {
555 it("computes correct logical key for grouped projects and aggregates threads", () => {
556 const state = makeFixtureState();
557 const allProjects = selectProjectsAcrossEnvironments(state);
558
559 // Group by logical key
560 const groups = new Map<string, Project[]>();
561 for (const project of allProjects) {
562 const key = deriveLogicalProjectKey(project);
563 const existing = groups.get(key) ?? [];
564 existing.push(project);
565 groups.set(key, existing);
566 }
567
568 // Shared project should be grouped
569 const sharedGroup = groups.get(SHARED_REPO_CANONICAL_KEY);
570 expect(sharedGroup).toBeDefined();
571 expect(sharedGroup).toHaveLength(2);
572 expect(sharedGroup!.map((p) => p.environmentId).toSorted()).toEqual(
573 [primaryEnvId, remoteEnvId].toSorted(),
574 );
575
576 // Build member refs for the grouped project and fetch threads
577 const memberRefs = sharedGroup!.map((p) => scopeProjectRef(p.environmentId, p.id));
578 const threads = selectSidebarThreadsForProjectRefs(state, memberRefs);
579 expect(threads).toHaveLength(3);
580 const threadIds = threads.map((t) => t.id);
581 expect(threadIds).toContain(threadP1);
582 expect(threadIds).toContain(threadP2);
583 expect(threadIds).toContain(threadR1);
584 });
585
586 it("local-only and remote-only projects remain ungrouped", () => {
587 const state = makeFixtureState();
588 const allProjects = selectProjectsAcrossEnvironments(state);
589
590 const groups = new Map<string, Project[]>();
591 for (const project of allProjects) {
592 const key = deriveLogicalProjectKey(project);
593 const existing = groups.get(key) ?? [];
594 existing.push(project);
595 groups.set(key, existing);
596 }
597
598 // Should have 3 groups total: shared, local-only, remote-only
599 expect(groups.size).toBe(3);
600
601 // Local-only group
602 const localKey = deriveLogicalProjectKey(
603 allProjects.find((p) => p.id === localOnlyProjectId)!,
604 );
605 expect(groups.get(localKey)).toHaveLength(1);
606
607 // Remote-only group
608 const remoteKey = deriveLogicalProjectKey(
609 allProjects.find((p) => p.id === remoteOnlyProjectId)!,
610 );
611 expect(groups.get(remoteKey)).toHaveLength(1);
612 });
613 });
614});