@@ -2,6 +2,7 @@ import { describe, it, expect, afterEach } from "vitest";
22import * as http from "node:http" ;
33import type { Fixture } from "../types.js" ;
44import { createServer , type ServerInstance } from "../server.js" ;
5+ import { buildBedrockStreamTextEvents } from "../bedrock.js" ;
56
67// --- helpers ---
78
@@ -504,3 +505,69 @@ describe("POST /api/generate (reasoning streaming)", () => {
504505 expect ( reasoningChunks ) . toHaveLength ( 0 ) ;
505506 } ) ;
506507} ) ;
508+
509+ // ─── Bedrock streaming reasoning: unit test ─────────────────────────────────
510+
511+ describe ( "buildBedrockStreamTextEvents (reasoning)" , ( ) => {
512+ it ( "emits thinking block events before text block events" , ( ) => {
513+ const events = buildBedrockStreamTextEvents ( "The answer." , 100 , "Step by step." ) ;
514+ const types = events . map ( ( e ) => e . eventType ) ;
515+
516+ // messageStart → thinking block → text block → messageStop
517+ expect ( types [ 0 ] ) . toBe ( "messageStart" ) ;
518+
519+ // Thinking block at index 0
520+ expect ( events [ 1 ] ) . toEqual ( {
521+ eventType : "contentBlockStart" ,
522+ payload : { contentBlockIndex : 0 , start : { type : "thinking" } } ,
523+ } ) ;
524+ expect ( events [ 2 ] ) . toEqual ( {
525+ eventType : "contentBlockDelta" ,
526+ payload : {
527+ contentBlockIndex : 0 ,
528+ delta : { type : "thinking_delta" , thinking : "Step by step." } ,
529+ } ,
530+ } ) ;
531+ expect ( events [ 3 ] ) . toEqual ( {
532+ eventType : "contentBlockStop" ,
533+ payload : { contentBlockIndex : 0 } ,
534+ } ) ;
535+
536+ // Text block at index 1
537+ expect ( events [ 4 ] ) . toEqual ( {
538+ eventType : "contentBlockStart" ,
539+ payload : { contentBlockIndex : 1 , start : { } } ,
540+ } ) ;
541+ expect ( events [ 5 ] ) . toEqual ( {
542+ eventType : "contentBlockDelta" ,
543+ payload : {
544+ contentBlockIndex : 1 ,
545+ delta : { type : "text_delta" , text : "The answer." } ,
546+ } ,
547+ } ) ;
548+ expect ( events [ 6 ] ) . toEqual ( {
549+ eventType : "contentBlockStop" ,
550+ payload : { contentBlockIndex : 1 } ,
551+ } ) ;
552+
553+ expect ( events [ 7 ] ) . toEqual ( {
554+ eventType : "messageStop" ,
555+ payload : { stopReason : "end_turn" } ,
556+ } ) ;
557+ } ) ;
558+
559+ it ( "no thinking block when reasoning is absent" , ( ) => {
560+ const events = buildBedrockStreamTextEvents ( "Hello." , 100 ) ;
561+ const types = events . map ( ( e ) => e . eventType ) ;
562+
563+ // messageStart → text block at index 0 → messageStop
564+ expect ( types ) . toEqual ( [
565+ "messageStart" ,
566+ "contentBlockStart" ,
567+ "contentBlockDelta" ,
568+ "contentBlockStop" ,
569+ "messageStop" ,
570+ ] ) ;
571+ expect ( ( events [ 1 ] . payload as { contentBlockIndex : number } ) . contentBlockIndex ) . toBe ( 0 ) ;
572+ } ) ;
573+ } ) ;
0 commit comments