@@ -7,6 +7,10 @@ import {
77 derivePhysicalProjectKey ,
88 resolveProjectGroupingMode ,
99} from "./logicalProject" ;
10+ import {
11+ buildPhysicalToLogicalProjectKeyMap ,
12+ buildSidebarProjectSnapshots ,
13+ } from "./sidebarProjectGrouping" ;
1014import type { Project } from "./types" ;
1115
1216const primaryEnvironmentId = EnvironmentId . make ( "env-primary" ) ;
@@ -119,4 +123,121 @@ describe("environment grouping", () => {
119123 } ) ,
120124 ) . toBe ( "separate" ) ;
121125 } ) ;
126+
127+ it ( "dedupes stale project rows with the same environment and workspace path" , ( ) => {
128+ const duplicate = makeProject ( {
129+ id : ProjectId . make ( "project-duplicate" ) ,
130+ workspaceRoot : "/tmp/shared-repo/" ,
131+ repositoryIdentity,
132+ updatedAt : "2026-01-01T00:00:00.000Z" ,
133+ } ) ;
134+ const primary = makeProject ( {
135+ id : ProjectId . make ( "project-primary" ) ,
136+ repositoryIdentity,
137+ updatedAt : "2026-01-02T00:00:00.000Z" ,
138+ } ) ;
139+ const remote = makeProject ( {
140+ id : ProjectId . make ( "project-remote" ) ,
141+ environmentId : remoteEnvironmentId ,
142+ workspaceRoot : "/tmp/shared-repo" ,
143+ repositoryIdentity,
144+ } ) ;
145+
146+ const snapshots = buildSidebarProjectSnapshots ( {
147+ projects : [ primary , duplicate , remote ] ,
148+ settings : defaultGroupingSettings ,
149+ primaryEnvironmentId,
150+ resolveEnvironmentLabel : ( environmentId ) =>
151+ environmentId === remoteEnvironmentId ? "remote" : "primary" ,
152+ } ) ;
153+
154+ expect ( snapshots ) . toHaveLength ( 1 ) ;
155+ expect ( snapshots [ 0 ] ?. groupedProjectCount ) . toBe ( 2 ) ;
156+ expect ( snapshots [ 0 ] ?. memberProjects . map ( ( project ) => project . id ) ) . toEqual ( [
157+ primary . id ,
158+ remote . id ,
159+ ] ) ;
160+ } ) ;
161+
162+ it ( "prefers the fresher project row when duplicate stale rows are ordered first" , ( ) => {
163+ const staleDuplicate = makeProject ( {
164+ id : ProjectId . make ( "project-stale" ) ,
165+ workspaceRoot : "/tmp/shared-repo/" ,
166+ repositoryIdentity,
167+ updatedAt : "2026-01-01T00:00:00.000Z" ,
168+ } ) ;
169+ const canonical = makeProject ( {
170+ id : ProjectId . make ( "project-canonical" ) ,
171+ workspaceRoot : "/tmp/shared-repo" ,
172+ repositoryIdentity,
173+ updatedAt : "2026-01-02T00:00:00.000Z" ,
174+ } ) ;
175+
176+ const snapshots = buildSidebarProjectSnapshots ( {
177+ projects : [ staleDuplicate , canonical ] ,
178+ settings : defaultGroupingSettings ,
179+ primaryEnvironmentId,
180+ resolveEnvironmentLabel : ( ) => "primary" ,
181+ } ) ;
182+
183+ expect ( snapshots ) . toHaveLength ( 1 ) ;
184+ expect ( snapshots [ 0 ] ?. memberProjects . map ( ( project ) => project . id ) ) . toEqual ( [ canonical . id ] ) ;
185+ expect ( snapshots [ 0 ] ?. id ) . toBe ( canonical . id ) ;
186+ } ) ;
187+
188+ it ( "dedupes stale project rows before logical grouping" , ( ) => {
189+ const staleWithoutRepositoryIdentity = makeProject ( {
190+ id : ProjectId . make ( "project-stale" ) ,
191+ repositoryIdentity : null ,
192+ updatedAt : "2026-01-01T00:00:00.000Z" ,
193+ } ) ;
194+ const canonical = makeProject ( {
195+ id : ProjectId . make ( "project-canonical" ) ,
196+ repositoryIdentity,
197+ updatedAt : "2026-01-02T00:00:00.000Z" ,
198+ } ) ;
199+ const remote = makeProject ( {
200+ id : ProjectId . make ( "project-remote" ) ,
201+ environmentId : remoteEnvironmentId ,
202+ repositoryIdentity,
203+ } ) ;
204+
205+ const snapshots = buildSidebarProjectSnapshots ( {
206+ projects : [ staleWithoutRepositoryIdentity , canonical , remote ] ,
207+ settings : defaultGroupingSettings ,
208+ primaryEnvironmentId,
209+ resolveEnvironmentLabel : ( environmentId ) =>
210+ environmentId === remoteEnvironmentId ? "remote" : "primary" ,
211+ } ) ;
212+
213+ expect ( snapshots ) . toHaveLength ( 1 ) ;
214+ expect ( snapshots [ 0 ] ?. projectKey ) . toBe ( repositoryIdentity . canonicalKey ) ;
215+ expect ( snapshots [ 0 ] ?. memberProjects . map ( ( project ) => project . id ) ) . toEqual ( [
216+ canonical . id ,
217+ remote . id ,
218+ ] ) ;
219+ } ) ;
220+
221+ it ( "routes duplicate physical project keys to the winning logical group" , ( ) => {
222+ const staleWithoutRepositoryIdentity = makeProject ( {
223+ id : ProjectId . make ( "project-stale" ) ,
224+ repositoryIdentity : null ,
225+ updatedAt : "2026-01-01T00:00:00.000Z" ,
226+ } ) ;
227+ const canonical = makeProject ( {
228+ id : ProjectId . make ( "project-canonical" ) ,
229+ repositoryIdentity,
230+ updatedAt : "2026-01-02T00:00:00.000Z" ,
231+ } ) ;
232+
233+ const physicalToLogicalKey = buildPhysicalToLogicalProjectKeyMap ( {
234+ projects : [ staleWithoutRepositoryIdentity , canonical ] ,
235+ settings : defaultGroupingSettings ,
236+ primaryEnvironmentId,
237+ } ) ;
238+
239+ expect ( physicalToLogicalKey . get ( derivePhysicalProjectKey ( staleWithoutRepositoryIdentity ) ) ) . toBe (
240+ repositoryIdentity . canonicalKey ,
241+ ) ;
242+ } ) ;
122243} ) ;
0 commit comments