@@ -4,6 +4,7 @@ import { SecretProviderId, type SimlockDeviceHostConfig } from "@t3tools/contrac
44import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver" ;
55import * as HostProcess from "@t3tools/shared/HostProcess" ;
66import * as Effect from "effect/Effect" ;
7+ import * as Clock from "effect/Clock" ;
78import * as Fiber from "effect/Fiber" ;
89import * as Deferred from "effect/Deferred" ;
910import * as Exit from "effect/Exit" ;
@@ -56,6 +57,8 @@ const fakePool = (
5657 readonly entered : Deferred . Deferred < void > ;
5758 readonly proceed : Deferred . Deferred < void > ;
5859 } ;
60+ /** The gateway's `lease.maxTtlMs`: leases expire one TTL after their last renewal. */
61+ readonly maxTtlMs ?: number ;
5962 } = { } ,
6063) =>
6164 Effect . gen ( function * ( ) {
@@ -66,15 +69,28 @@ const fakePool = (
6669 let leases = 0 ;
6770 let renewStatus = 200 ;
6871 let catalog : unknown = CATALOG ;
72+ let leaseDeadline = Infinity ;
6973 const http = HttpClient . make ( ( request ) =>
70- Effect . sync ( ( ) => {
74+ Effect . gen ( function * ( ) {
75+ const now = yield * Clock . currentTimeMillis ;
7176 const url = new URL ( request . url ) ;
7277 requests . push ( `${ request . method } ${ url . pathname } ` ) ;
7378 const json = ( body : unknown , status = 200 ) =>
7479 HttpClientResponse . fromWeb ( request , Response . json ( body , { status } ) ) ;
7580 if ( request . headers . authorization !== "Bearer agent-token" )
7681 return json ( { error : { code : "UNAUTHENTICATED" , message : "Unknown bearer token" } } , 401 ) ;
7782 if ( url . pathname === "/v1/catalog" ) return json ( catalog ) ;
83+ if ( url . pathname . endsWith ( "/renew" ) && options . maxTtlMs !== undefined ) {
84+ if ( now >= leaseDeadline ) return json ( { } , 404 ) ;
85+ const body =
86+ request . body . _tag === "Uint8Array"
87+ ? ( JSON . parse ( new TextDecoder ( ) . decode ( request . body . body ) ) as { ttlMs ?: number } )
88+ : { } ;
89+ if ( ( body . ttlMs ?? 0 ) > options . maxTtlMs )
90+ return json ( { error : { code : "BAD_REQUEST" , message : "ttlMs exceeds maxTtlMs" } } , 400 ) ;
91+ leaseDeadline = now + ( body . ttlMs ?? options . maxTtlMs ) ;
92+ return json ( { } ) ;
93+ }
7894 if ( url . pathname . endsWith ( "/renew" ) ) return json ( { } , renewStatus ) ;
7995 if ( request . method === "DELETE" ) return json ( { } , 202 ) ;
8096 return json ( { } , 404 ) ;
@@ -114,6 +130,8 @@ const fakePool = (
114130 path . join ( dir , `${ session } .json` ) ,
115131 JSON . stringify ( { leaseId : `lease-${ ++ leases } ` } ) ,
116132 ) ;
133+ if ( options . maxTtlMs !== undefined )
134+ leaseDeadline = ( yield * Clock . currentTimeMillis ) + options . maxTtlMs ;
117135 }
118136 if ( command === "screenshot" ) yield * fs . writeFile ( args [ 1 ] ! , new Uint8Array ( [ 1 , 2 , 3 ] ) ) ;
119137 return {
@@ -475,6 +493,21 @@ describe("Simlock device host", () => {
475493 } ) . pipe ( Effect . scoped , Effect . provide ( layer ) ) ,
476494 ) ;
477495
496+ it . effect ( "keeps a watched lease renewed when the gateway caps lease TTLs" , ( ) =>
497+ Effect . gen ( function * ( ) {
498+ const pool = yield * fakePool ( { maxTtlMs : 5 * 60_000 } ) ;
499+ const host = yield * makeHost ( pool , config ( ) , yield * Scope . make ( ) ) ;
500+ const ready = yield * host . ensureReady ( ( ) => Effect . void ) ;
501+ const [ iphone ] = ( yield * ready . devices . list ) . devices ;
502+ const leaseId = yield * ready . devices . boot ( iphone ! ) ;
503+
504+ yield * TestClock . adjust ( "6 minutes" ) ;
505+ expect ( ( yield * ready . devices . list ) . devices . some ( ( device ) => device . id === leaseId ) ) . toBe (
506+ true ,
507+ ) ;
508+ } ) . pipe ( Effect . scoped , Effect . provide ( layer ) ) ,
509+ ) ;
510+
478511 it . effect ( "returns a lease whose open finishes after the host stopped" , ( ) =>
479512 Effect . gen ( function * ( ) {
480513 const holdOpen = {
0 commit comments