@@ -97,11 +97,14 @@ describe("Crabbox project snapshot provisioning", () => {
9797 expect ( calls . filter ( ( { argv } ) => argv [ 1 ] === "inspect" ) ) . toHaveLength ( 2 ) ;
9898 } ) ;
9999
100- it . each ( [ "setup" , "readiness" ] as const ) (
100+ it . each ( [ "grant" , " setup", "readiness" ] as const ) (
101101 "preserves runtime %s failure ownership without starting capture or enrollment" ,
102102 async ( failure ) => {
103103 const events : string [ ] = [ ] ;
104104 const { options, observe } = projectOptions ( events ) ;
105+ if ( failure === "grant" ) {
106+ options . prepareNodeRuntime . mockRejectedValueOnce ( new Error ( "runtime grant failed" ) ) ;
107+ }
105108 let installed = false ;
106109 const { provider, calls } = createWarmProvider ( ( call ) => {
107110 observe ( call ) ;
@@ -120,7 +123,7 @@ describe("Crabbox project snapshot provisioning", () => {
120123 expect ( options . beginNodeEnrollment ) . not . toHaveBeenCalled ( ) ;
121124 expect ( calls . some ( ( { argv } ) => argv [ 2 ] === "create" ) ) . toBe ( false ) ;
122125 expect ( calls . filter ( ( { argv } ) => argv [ 1 ] === "stop" ) ) . toHaveLength (
123- failure === "setup " ? 1 : 0 ,
126+ failure === "readiness " ? 0 : 1 ,
124127 ) ;
125128 expect ( listCrabboxWarmImages ( ) . every ( ( image ) => ! image . capture ) ) . toBe ( true ) ;
126129 if ( failure === "readiness" ) {
@@ -131,6 +134,54 @@ describe("Crabbox project snapshot provisioning", () => {
131134 } ,
132135 ) ;
133136
137+ it . each ( [ "resolve" , "reject" ] as const ) (
138+ "fences a runtime grant that will %s after project ownership changes" ,
139+ async ( outcome ) => {
140+ const events : string [ ] = [ ] ;
141+ const controller = new AbortController ( ) ;
142+ const { options, observe } = projectOptions ( events , controller ) ;
143+ const { provider, calls } = createWarmProvider ( observe ) ;
144+ let current = true ;
145+ const closed = new DOMException ( "Project owner changed" , "AbortError" ) ;
146+ options . project . assertCurrent = ( ) => {
147+ if ( ! current ) {
148+ controller . abort ( closed ) ;
149+ }
150+ controller . signal . throwIfAborted ( ) ;
151+ } ;
152+ options . prepareNodeRuntime . mockImplementationOnce ( async ( ) => {
153+ current = false ;
154+ expect ( controller . signal . aborted ) . toBe ( false ) ;
155+ if ( outcome === "reject" ) {
156+ throw closed ;
157+ }
158+ return {
159+ nodeBootstrap : createNodeBootstrapFixture ( ) ,
160+ signal : new AbortController ( ) . signal ,
161+ } ;
162+ } ) ;
163+
164+ await expect (
165+ provider . provision ( PROFILE , `stale-grant-${ outcome } ` , options ) ,
166+ ) . rejects . toMatchObject ( {
167+ name : "AbortError" ,
168+ } ) ;
169+
170+ expect ( events ) . not . toContain ( "runtime-install" ) ;
171+ expect ( calls . some ( ( { argv } ) => argv [ 1 ] === "stop" || argv [ 2 ] === "create" ) ) . toBe ( false ) ;
172+ expect ( options . beginNodeEnrollment ) . not . toHaveBeenCalled ( ) ;
173+ expect ( listCrabboxWarmImages ( ) [ 0 ] ) . toMatchObject ( {
174+ allocations : {
175+ [ operationLeaseId ( `stale-grant-${ outcome } ` ) ] : {
176+ phase : "prepared" ,
177+ choice : { kind : "cold" } ,
178+ } ,
179+ } ,
180+ } ) ;
181+ expect ( listCrabboxWarmImages ( ) [ 0 ] ?. capture ) . toBeUndefined ( ) ;
182+ } ,
183+ ) ;
184+
134185 it . each ( [ "aborted" , "uncertain" ] as const ) (
135186 "does not enroll after an %s native capture" ,
136187 async ( failure ) => {
0 commit comments