@@ -216,6 +216,76 @@ describe("TriggerChatTransport", function () {
216216 expect ( observedStreamPath ) . toBe ( "/realtime/v1/streams/run_stream_object/typed-stream-id" ) ;
217217 } ) ;
218218
219+ it ( "forwards preview branch and timeout headers to trigger and stream requests" , async function ( ) {
220+ let triggerBranchHeader : string | undefined ;
221+ let streamBranchHeader : string | undefined ;
222+ let streamTimeoutHeader : string | undefined ;
223+
224+ const server = await startServer ( function ( req , res ) {
225+ if ( req . method === "POST" && req . url === "/api/v1/tasks/chat-task/trigger" ) {
226+ const branchHeader = req . headers [ "x-trigger-branch" ] ;
227+ triggerBranchHeader = Array . isArray ( branchHeader ) ? branchHeader [ 0 ] : branchHeader ;
228+
229+ res . writeHead ( 200 , {
230+ "content-type" : "application/json" ,
231+ "x-trigger-jwt" : "pk_run_headers" ,
232+ } ) ;
233+ res . end ( JSON . stringify ( { id : "run_headers" } ) ) ;
234+ return ;
235+ }
236+
237+ if ( req . method === "GET" && req . url === "/realtime/v1/streams/run_headers/chat-stream" ) {
238+ const branchHeader = req . headers [ "x-trigger-branch" ] ;
239+ const timeoutHeader = req . headers [ "timeout-seconds" ] ;
240+
241+ streamBranchHeader = Array . isArray ( branchHeader ) ? branchHeader [ 0 ] : branchHeader ;
242+ streamTimeoutHeader = Array . isArray ( timeoutHeader ) ? timeoutHeader [ 0 ] : timeoutHeader ;
243+
244+ res . writeHead ( 200 , {
245+ "content-type" : "text/event-stream" ,
246+ } ) ;
247+ writeSSE (
248+ res ,
249+ "1-0" ,
250+ JSON . stringify ( { type : "text-start" , id : "headers_1" } )
251+ ) ;
252+ writeSSE (
253+ res ,
254+ "2-0" ,
255+ JSON . stringify ( { type : "text-end" , id : "headers_1" } )
256+ ) ;
257+ res . end ( ) ;
258+ return ;
259+ }
260+
261+ res . writeHead ( 404 ) ;
262+ res . end ( ) ;
263+ } ) ;
264+
265+ const transport = new TriggerChatTransport ( {
266+ task : "chat-task" ,
267+ stream : "chat-stream" ,
268+ accessToken : "pk_trigger" ,
269+ baseURL : server . url ,
270+ previewBranch : "feature-preview" ,
271+ timeoutInSeconds : 123 ,
272+ } ) ;
273+
274+ const stream = await transport . sendMessages ( {
275+ trigger : "submit-message" ,
276+ chatId : "chat-headers" ,
277+ messageId : undefined ,
278+ messages : [ ] ,
279+ abortSignal : undefined ,
280+ } ) ;
281+
282+ const chunks = await readChunks ( stream ) ;
283+ expect ( chunks ) . toHaveLength ( 2 ) ;
284+ expect ( triggerBranchHeader ) . toBe ( "feature-preview" ) ;
285+ expect ( streamBranchHeader ) . toBe ( "feature-preview" ) ;
286+ expect ( streamTimeoutHeader ) . toBe ( "123" ) ;
287+ } ) ;
288+
219289 it ( "triggers task and streams chunks with rich default payload" , async function ( ) {
220290 let receivedTriggerBody : Record < string , unknown > | undefined ;
221291
0 commit comments