@@ -668,9 +668,11 @@ describe("skill upload store", () => {
668668 } ) ;
669669
670670 it ( "renews the install lease and preserves an expired leased upload" , async ( ) => {
671+ let now = 1000 ;
671672 const { databasePath, store } = await makeStore ( {
672673 installLeaseHeartbeatMs : 10 ,
673674 installLeaseMs : 100 ,
675+ now : ( ) => now ,
674676 } ) ;
675677 const archive = Buffer . from ( "abc" ) ;
676678 const committed = await store . begin ( {
@@ -687,47 +689,53 @@ describe("skill upload store", () => {
687689
688690 const entered = deferred ( ) ;
689691 const release = deferred ( ) ;
692+ vi . useFakeTimers ( ) ;
690693 const pinned = store . withCommittedUpload ( committed . uploadId , async ( ) => {
691694 entered . resolve ( ) ;
692695 await release . promise ;
693696 } ) ;
694- await entered . promise ;
695- const db = stateDatabase ( databasePath ) ;
696- const initialHeartbeat = (
697- db
698- . prepare (
699- "SELECT heartbeat_at FROM state_leases WHERE scope = 'skill-upload-install' AND lease_key = ?" ,
700- )
701- . get ( committed . uploadId ) as { heartbeat_at : number }
702- ) . heartbeat_at ;
703- await new Promise < void > ( ( resolve ) => {
704- setTimeout ( resolve , 40 ) ;
705- } ) ;
706- const renewedHeartbeat = (
707- db
708- . prepare (
709- "SELECT heartbeat_at FROM state_leases WHERE scope = 'skill-upload-install' AND lease_key = ?" ,
710- )
711- . get ( committed . uploadId ) as { heartbeat_at : number }
712- ) . heartbeat_at ;
713- expect ( renewedHeartbeat ) . toBeGreaterThan ( initialHeartbeat ) ;
714-
715- db . prepare ( "UPDATE skill_uploads SET expires_at = ? WHERE upload_id = ?" ) . run (
716- Date . now ( ) - 1 ,
717- committed . uploadId ,
718- ) ;
719- expect (
720- deleteExpiredSkillUploadUnlessLeased ( {
721- uploadId : committed . uploadId ,
722- nowMs : Date . now ( ) ,
723- options : { path : databasePath } ,
724- } ) ,
725- ) . toBe ( "leased" ) ;
726- expect ( uploadCount ( databasePath ) ) . toBe ( 1 ) ;
727- expect ( installLeaseCount ( databasePath , committed . uploadId ) ) . toBe ( 1 ) ;
728-
729- release . resolve ( ) ;
730- await pinned ;
697+ try {
698+ await entered . promise ;
699+ const db = stateDatabase ( databasePath ) ;
700+ const initialHeartbeat = (
701+ db
702+ . prepare (
703+ "SELECT heartbeat_at FROM state_leases WHERE scope = 'skill-upload-install' AND lease_key = ?" ,
704+ )
705+ . get ( committed . uploadId ) as { heartbeat_at : number }
706+ ) . heartbeat_at ;
707+ now += 10 ;
708+ await vi . advanceTimersByTimeAsync ( 10 ) ;
709+ const renewedHeartbeat = (
710+ db
711+ . prepare (
712+ "SELECT heartbeat_at FROM state_leases WHERE scope = 'skill-upload-install' AND lease_key = ?" ,
713+ )
714+ . get ( committed . uploadId ) as { heartbeat_at : number }
715+ ) . heartbeat_at ;
716+ expect ( renewedHeartbeat ) . toBeGreaterThan ( initialHeartbeat ) ;
717+
718+ db . prepare ( "UPDATE skill_uploads SET expires_at = ? WHERE upload_id = ?" ) . run (
719+ now - 1 ,
720+ committed . uploadId ,
721+ ) ;
722+ expect (
723+ deleteExpiredSkillUploadUnlessLeased ( {
724+ uploadId : committed . uploadId ,
725+ nowMs : now ,
726+ options : { path : databasePath } ,
727+ } ) ,
728+ ) . toBe ( "leased" ) ;
729+ expect ( uploadCount ( databasePath ) ) . toBe ( 1 ) ;
730+ expect ( installLeaseCount ( databasePath , committed . uploadId ) ) . toBe ( 1 ) ;
731+ } finally {
732+ release . resolve ( ) ;
733+ try {
734+ await pinned ;
735+ } finally {
736+ vi . useRealTimers ( ) ;
737+ }
738+ }
731739 expect ( installLeaseCount ( databasePath , committed . uploadId ) ) . toBe ( 0 ) ;
732740 } ) ;
733741
0 commit comments