@@ -70,16 +70,72 @@ describe('native Keystone transport', () => {
7070 expect ( proxy ) . toMatchObject ( { ok : false , kind : 'invalid' } ) ;
7171 expect ( JSON . stringify ( proxy ) ) . not . toContain ( 'Bad Gateway' ) ;
7272 } ) ;
73- it ( 'preserves a completed-versus-cancel race status' , async ( ) => {
74- const fetcher = vi
75- . fn ( )
76- . mockResolvedValue ( json ( { schemaVersion : 1 , run : { status : 'completed' } } ) ) ;
77- const result = await createNativeKeystoneTransport ( fetcher ) . page . cancelRun (
78- 'investigation' ,
79- 'run' ,
80- ) ;
81- expect ( result ) . toEqual ( { ok : true , value : { status : 'completed' } } ) ;
82- expect ( fetcher . mock . calls [ 0 ] [ 1 ] . credentials ) . toBe ( 'include' ) ;
73+ it . each ( [
74+ [ 'queued' , 'queued' ] ,
75+ [ 'running' , 'resolving_scope' ] ,
76+ ] as const ) (
77+ 'accepts a cancellation acknowledgement while the native run is still %s' ,
78+ async ( nativeStatus , browserStatus ) => {
79+ const fetcher = vi
80+ . fn ( )
81+ . mockResolvedValue ( json ( { schemaVersion : 1 , run : { status : nativeStatus } } ) ) ;
82+ const result = await createNativeKeystoneTransport ( fetcher ) . page . cancelRun (
83+ 'investigation' ,
84+ 'run' ,
85+ ) ;
86+ // Cancellation may be acknowledged before the coordinator finishes teardown.
87+ // Preserve a nonterminal result until the terminal event arrives over SSE.
88+ expect ( result ) . toEqual ( { ok : true , value : { status : browserStatus } } ) ;
89+ expect ( fetcher . mock . calls [ 0 ] [ 0 ] ) . toBe (
90+ '/api/prism/v1/keystone/native/investigations/investigation/runs/run/cancel' ,
91+ ) ;
92+ expect ( fetcher . mock . calls [ 0 ] [ 1 ] ) . toMatchObject ( {
93+ method : 'POST' ,
94+ credentials : 'include' ,
95+ } ) ;
96+ } ,
97+ ) ;
98+ it . each ( [ 'completed' , 'partial' , 'failed' , 'cancelled' , 'interrupted' ] as const ) (
99+ 'preserves the native %s status when cancellation races with termination' ,
100+ async ( status ) => {
101+ const fetcher = vi . fn ( ) . mockResolvedValue ( json ( { schemaVersion : 1 , run : { status } } ) ) ;
102+ const result = await createNativeKeystoneTransport ( fetcher ) . page . cancelRun (
103+ 'investigation' ,
104+ 'run' ,
105+ ) ;
106+ expect ( result ) . toEqual ( { ok : true , value : { status } } ) ;
107+ expect ( fetcher . mock . calls [ 0 ] [ 1 ] . credentials ) . toBe ( 'include' ) ;
108+ } ,
109+ ) ;
110+ it . each ( [
111+ null ,
112+ { } ,
113+ { schemaVersion : 2 , run : { status : 'running' } } ,
114+ { schemaVersion : 1 } ,
115+ { schemaVersion : 1 , run : null } ,
116+ { schemaVersion : 1 , run : { } } ,
117+ ...[
118+ null ,
119+ 1 ,
120+ true ,
121+ { } ,
122+ [ ] ,
123+ '' ,
124+ 'unknown' ,
125+ 'draft' ,
126+ 'resolving_scope' ,
127+ 'awaiting_confirmation' ,
128+ 'querying' ,
129+ 'composing' ,
130+ ] . map ( ( status ) => ( {
131+ schemaVersion : 1 ,
132+ run : { status } ,
133+ } ) ) ,
134+ ] ) ( 'rejects an invalid native cancellation response: %j' , async ( body ) => {
135+ const result = await createNativeKeystoneTransport (
136+ vi . fn ( ) . mockResolvedValue ( json ( body ) ) ,
137+ ) . page . cancelRun ( 'investigation' , 'run' ) ;
138+ expect ( result . ok ) . toBe ( false ) ;
83139 } ) ;
84140 it ( 'creates only the selected export evidence and pins server hash' , async ( ) => {
85141 const scope = {
0 commit comments