@@ -62,7 +62,12 @@ async function seedControlPlane(prisma: PrismaClient) {
6262async function seedWorker (
6363 prisma : PrismaClient ,
6464 ctx : { projectId : string ; environmentId : string } ,
65- opts ?: { promote ?: boolean }
65+ opts ?: {
66+ promote ?: boolean ;
67+ createdAt ?: Date ;
68+ deploymentType ?: "MANAGED" | "UNMANAGED" | "V1" ;
69+ deploymentCreatedAt ?: Date ;
70+ }
6671) {
6772 const n = seedCounter ++ ;
6873 const worker = await prisma . backgroundWorker . create ( {
@@ -74,6 +79,7 @@ async function seedWorker(
7479 version : `2024.1.${ n } ` ,
7580 metadata : { } ,
7681 engine : "V2" ,
82+ ...( opts ?. createdAt ? { createdAt : opts . createdAt } : { } ) ,
7783 } ,
7884 } ) ;
7985 const task = await prisma . backgroundWorkerTask . create ( {
@@ -104,11 +110,12 @@ async function seedWorker(
104110 contentHash : `hash_${ n } ` ,
105111 version : worker . version ,
106112 shortCode : `dep_${ n } ` ,
107- type : "MANAGED" ,
113+ type : opts ?. deploymentType ?? "MANAGED" ,
108114 status : "DEPLOYED" ,
109115 projectId : ctx . projectId ,
110116 environmentId : ctx . environmentId ,
111117 workerId : worker . id ,
118+ ...( opts ?. deploymentCreatedAt ? { createdAt : opts . deploymentCreatedAt } : { } ) ,
112119 } ,
113120 } ) ;
114121 await prisma . workerDeploymentPromotion . create ( {
@@ -748,3 +755,103 @@ heteroPostgresTest(
748755 expect ( reads ( ) ) . toBe ( readsAfterFirst * 2 ) ;
749756 }
750757) ;
758+
759+ heteroPostgresTest (
760+ "resolveWorkerVersion (DEVELOPMENT) resolves the newest worker by createdAt, not by id" ,
761+ async ( { prisma14 } ) => {
762+ const { environment, project } = await seedControlPlane ( prisma14 ) ;
763+ const ctx = { projectId : project . id , environmentId : environment . id } ;
764+
765+ const newest = await seedWorker ( prisma14 , ctx , {
766+ createdAt : new Date ( "2026-07-31T12:00:00.000Z" ) ,
767+ } ) ;
768+ const oldest = await seedWorker ( prisma14 , ctx , {
769+ createdAt : new Date ( "2026-07-30T12:00:00.000Z" ) ,
770+ } ) ;
771+
772+ expect ( oldest . worker . id > newest . worker . id ) . toBe ( true ) ;
773+ expect ( oldest . worker . createdAt < newest . worker . createdAt ) . toBe ( true ) ;
774+
775+ const resolver = new ControlPlaneResolver ( {
776+ controlPlaneReplica : prisma14 ,
777+ controlPlanePrimary : prisma14 ,
778+ cache : new ControlPlaneCache ( ) ,
779+ splitEnabled : ( ) => true ,
780+ } ) ;
781+
782+ const resolved = await resolver . resolveWorkerVersion ( {
783+ environmentId : environment . id ,
784+ type : "DEVELOPMENT" ,
785+ } ) ;
786+
787+ expect ( resolved ) . not . toBeNull ( ) ;
788+ expect ( resolved ! . worker . id ) . toBe ( newest . worker . id ) ;
789+ }
790+ ) ;
791+
792+ heteroPostgresTest (
793+ "resolveWorkerVersion latest-MANAGED fallback resolves by createdAt, not by id" ,
794+ async ( { prisma14 } ) => {
795+ const { environment, project } = await seedControlPlane ( prisma14 ) ;
796+ const ctx = { projectId : project . id , environmentId : environment . id } ;
797+
798+ await seedWorker ( prisma14 , ctx , { promote : true , deploymentType : "V1" } ) ;
799+
800+ const newest = await seedWorker ( prisma14 , ctx , {
801+ promote : false ,
802+ deploymentCreatedAt : new Date ( "2026-07-31T12:00:00.000Z" ) ,
803+ } ) ;
804+ const oldest = await seedWorker ( prisma14 , ctx , {
805+ promote : false ,
806+ deploymentCreatedAt : new Date ( "2026-07-30T12:00:00.000Z" ) ,
807+ } ) ;
808+
809+ const newestDeployment = await prisma14 . workerDeployment . create ( {
810+ data : {
811+ friendlyId : `deployment_newest_${ environment . id } ` ,
812+ contentHash : "hash_newest" ,
813+ version : newest . worker . version ,
814+ shortCode : "dep_newest" ,
815+ type : "MANAGED" ,
816+ status : "DEPLOYED" ,
817+ projectId : project . id ,
818+ environmentId : environment . id ,
819+ workerId : newest . worker . id ,
820+ createdAt : new Date ( "2026-07-31T12:00:00.000Z" ) ,
821+ } ,
822+ } ) ;
823+ const oldestDeployment = await prisma14 . workerDeployment . create ( {
824+ data : {
825+ friendlyId : `deployment_oldest_${ environment . id } ` ,
826+ contentHash : "hash_oldest" ,
827+ version : oldest . worker . version ,
828+ shortCode : "dep_oldest" ,
829+ type : "MANAGED" ,
830+ status : "DEPLOYED" ,
831+ projectId : project . id ,
832+ environmentId : environment . id ,
833+ workerId : oldest . worker . id ,
834+ createdAt : new Date ( "2026-07-30T12:00:00.000Z" ) ,
835+ } ,
836+ } ) ;
837+
838+ expect ( oldestDeployment . id > newestDeployment . id ) . toBe ( true ) ;
839+ expect ( oldestDeployment . createdAt < newestDeployment . createdAt ) . toBe ( true ) ;
840+
841+ const resolver = new ControlPlaneResolver ( {
842+ controlPlaneReplica : prisma14 ,
843+ controlPlanePrimary : prisma14 ,
844+ cache : new ControlPlaneCache ( ) ,
845+ splitEnabled : ( ) => true ,
846+ } ) ;
847+
848+ const resolved = await resolver . resolveWorkerVersion ( {
849+ environmentId : environment . id ,
850+ type : "PRODUCTION" ,
851+ } ) ;
852+
853+ expect ( resolved ) . not . toBeNull ( ) ;
854+ expect ( resolved ! . deployment ! . id ) . toBe ( newestDeployment . id ) ;
855+ expect ( resolved ! . worker . id ) . toBe ( newest . worker . id ) ;
856+ }
857+ ) ;
0 commit comments