@@ -424,6 +424,92 @@ describe("real product index", () => {
424424 } )
425425 } )
426426
427+ it ( "keeps the prototype count consistent when a branch is retired during the read" , async ( ) => {
428+ const repositoryId = randomUUID ( )
429+ const prototypeId = randomUUID ( )
430+ const retiredAt = new Date ( "2026-11-10T00:00:00Z" )
431+ await prisma . repository . create ( {
432+ data : { id : repositoryId , nameWithOwner : "team/design" , defaultBranch : "main" } ,
433+ } )
434+ await prisma . prototype . create ( {
435+ data : {
436+ id : prototypeId ,
437+ repositoryId,
438+ branchName : "prototype/welcome" ,
439+ name : "Welcome" ,
440+ registeredAt : new Date ( ) ,
441+ } ,
442+ } )
443+ const writer = new pg . Client ( { connectionString : getDatabaseUrl ( ) } )
444+ onTestFinished ( async ( ) => {
445+ await writer . end ( )
446+ } )
447+ await writer . connect ( )
448+ const query : typeof pg . Client . prototype . query = Reflect . get ( pg . Client . prototype , "query" )
449+ let retirement : Promise < void > | undefined
450+ function timeQueries ( this : pg . Client , config : unknown , values ?: unknown , callback ?: unknown ) {
451+ const sql =
452+ typeof config === "object" && config !== null && "text" in config ? config . text : undefined
453+ if ( typeof sql !== "string" || ! / \b s e l e c t \b / i. test ( sql ) || ! / \b p r o t o t y p e s \b / i. test ( sql ) ) {
454+ return Reflect . apply ( query , this , [ config , values , callback ] ) as
455+ Promise < pg . QueryResult > | undefined
456+ }
457+
458+ const done = (
459+ typeof callback === "function"
460+ ? callback
461+ : typeof values === "function"
462+ ? values
463+ : undefined
464+ ) as ( ( error : Error | null , result ?: pg . QueryResult ) => void ) | undefined
465+ function read ( this : pg . Client ) {
466+ return Reflect . apply ( query , this , [
467+ config ,
468+ typeof values === "function" ? undefined : values ,
469+ ] ) as Promise < pg . QueryResult >
470+ }
471+ let result : Promise < pg . QueryResult >
472+ if ( ! retirement ) {
473+ const firstRead = read . call ( this )
474+ retirement = firstRead . then ( async ( ) => {
475+ await writer . query ( "UPDATE prototypes SET branch_deleted_at = $1 WHERE id = $2" , [
476+ retiredAt ,
477+ prototypeId ,
478+ ] )
479+ } )
480+ result = retirement . then ( ( ) => firstRead )
481+ } else {
482+ result = retirement . then ( ( ) => read . call ( this ) )
483+ }
484+ if ( ! done ) return result
485+ void result . then (
486+ ( rows ) => done ( null , rows ) ,
487+ ( error : unknown ) => done ( error instanceof Error ? error : new Error ( String ( error ) ) ) ,
488+ )
489+ }
490+ // Keep the database reads real while the branch changes.
491+ const timing = vi
492+ . spyOn ( pg . Client . prototype , "query" )
493+ . mockImplementation (
494+ timeQueries as ( ...args : Parameters < typeof pg . Client . prototype . query > ) => void ,
495+ )
496+ onTestFinished ( ( ) => timing . mockRestore ( ) )
497+
498+ const index = await createProductRouteDependencies ( ) . findRepositoryIndex ( repositoryId )
499+ timing . mockRestore ( )
500+
501+ expect ( index ) . toMatchObject ( {
502+ prototypes : [ { id : prototypeId } ] ,
503+ pagination : { totalCount : 1 } ,
504+ } )
505+ expect (
506+ await prisma . prototype . findUnique ( {
507+ where : { id : prototypeId } ,
508+ select : { branchDeletedAt : true } ,
509+ } ) ,
510+ ) . toEqual ( { branchDeletedAt : retiredAt } )
511+ } , 8_000 )
512+
427513 it ( "does not return an index when a repository changes during the read" , async ( ) => {
428514 const removedId = randomUUID ( )
429515 const changedId = randomUUID ( )
0 commit comments