@@ -2,6 +2,7 @@ import { createServer, type Server, type ServerResponse } from "node:http";
22import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
33import {
44 TriggerChatTransport ,
5+ type ChatTransportEndpointContext ,
56 type ChatTransportEvent ,
67 type TriggerChatTransportOptions ,
78} from "./chat.js" ;
@@ -31,6 +32,7 @@ describe("Stop delivery outcomes", () => {
3132 let inputs : InputRequest [ ] ;
3233 let outputs : ServerResponse [ ] ;
3334 let events : ChatTransportEvent [ ] ;
35+ let fetchContexts : ChatTransportEndpointContext [ ] ;
3436 let respond : ( input : InputRequest ) => void ;
3537 let inputSeq : number ;
3638
@@ -41,6 +43,10 @@ describe("Stop delivery outcomes", () => {
4143 accessToken : ( ) => "test-token" ,
4244 sessions : { chat : { publicAccessToken : "test-token" , lastEventId : "1" } } ,
4345 onEvent : ( event ) => events . push ( event ) ,
46+ fetch : ( url , init , context ) => {
47+ fetchContexts . push ( { ...context } ) ;
48+ return globalThis . fetch ( url , init ) ;
49+ } ,
4450 ...options ,
4551 } ) ;
4652 }
@@ -55,6 +61,7 @@ describe("Stop delivery outcomes", () => {
5561 inputs = [ ] ;
5662 outputs = [ ] ;
5763 events = [ ] ;
64+ fetchContexts = [ ] ;
5865 inputSeq = 10 ;
5966 respond = ( input ) => appendResponse ( input ) ;
6067 server = createServer ( async ( request , response ) => {
@@ -128,6 +135,7 @@ describe("Stop delivery outcomes", () => {
128135 await expect ( stop ( throwOnError ) ) . resolves . toBe ( true ) ;
129136 expect ( inputs ) . toHaveLength ( 1 ) ;
130137 expect ( inputs [ 0 ] ?. kind ) . toBe ( "stop" ) ;
138+ expect ( fetchContexts ) . toEqual ( [ { endpoint : "in" , chatId : "chat" , inputKind : "stop" } ] ) ;
131139 expect ( sendFailures ( ) ) . toHaveLength ( 0 ) ;
132140 }
133141 ) ;
@@ -199,6 +207,10 @@ describe("Stop delivery outcomes", () => {
199207 "Bearer test-token" ,
200208 "Bearer fresh-token" ,
201209 ] ) ;
210+ expect ( fetchContexts ) . toEqual ( [
211+ { endpoint : "in" , chatId : "chat" , inputKind : "stop" } ,
212+ { endpoint : "in" , chatId : "chat" , inputKind : "stop" } ,
213+ ] ) ;
202214 expect ( sendFailures ( ) ) . toHaveLength ( 0 ) ;
203215 }
204216 ) ;
@@ -315,6 +327,29 @@ describe("Stop delivery outcomes", () => {
315327 }
316328 ) ;
317329
330+ it . each ( [ "message" , "action" ] as const ) (
331+ "identifies %s input and consumer Stop without an output input kind" ,
332+ async ( kind ) => {
333+ const abort = new AbortController ( ) ;
334+ const reader = ( await send ( kind , abort . signal ) ) . getReader ( ) ;
335+ await vi . waitFor ( ( ) => expect ( outputs ) . toHaveLength ( 1 ) ) ;
336+ expect ( fetchContexts ) . toEqual ( [
337+ { endpoint : "in" , chatId : "chat" , inputKind : "message" } ,
338+ { endpoint : "out" , chatId : "chat" } ,
339+ ] ) ;
340+ abort . abort ( ) ;
341+ await expect ( reader . read ( ) ) . resolves . toEqual ( { done : true , value : undefined } ) ;
342+ await vi . waitFor ( ( ) =>
343+ expect ( inputs . map ( ( input ) => input . kind ) ) . toEqual ( [ "message" , "stop" ] )
344+ ) ;
345+ expect ( fetchContexts ) . toEqual ( [
346+ { endpoint : "in" , chatId : "chat" , inputKind : "message" } ,
347+ { endpoint : "out" , chatId : "chat" } ,
348+ { endpoint : "in" , chatId : "chat" , inputKind : "stop" } ,
349+ ] ) ;
350+ }
351+ ) ;
352+
318353 describe . each ( [ "message" , "action" ] as const ) ( "%s cancellation" , ( kind ) => {
319354 it ( "does not hold a tab claim during cancelled session creation" , async ( ) => {
320355 const token = deferred < string > ( ) ;
0 commit comments