1- import { expect , test } from "bun:test" ;
1+ import { expect , spyOn , test } from "bun:test" ;
22import { createCommandCodeAdapter } from "../../src/adapters/command-code" ;
33import { createTestTranslatorBudget } from "../helpers/translator-budget" ;
44import type { AdapterEvent } from "../../src/types" ;
@@ -8,6 +8,41 @@ const content = "é한😀".repeat(32);
88const line = JSON . stringify ( { type : "text-delta" , text : content } ) ;
99const encoder = new TextEncoder ( ) ;
1010
11+ for ( const ending of [ "\n" , "" ] as const ) {
12+ test ( `Command Code bounds fragmented NDJSON measurement and scan work with ${ JSON . stringify ( ending ) } termination` , async ( ) => {
13+ const text = "a" . repeat ( 256 * 256 ) ;
14+ const bytes = encoder . encode ( JSON . stringify ( { type : "text-delta" , text } ) + ending ) ;
15+ const budget = createTestTranslatorBudget ( { maxTurnBytes : bytes . length } ) ;
16+ const source = input ( bytes , 256 ) ;
17+ const encode = TextEncoder . prototype . encode ;
18+ const indexOf = String . prototype . indexOf ;
19+ let measuredChars = 0 ;
20+ let scannedChars = 0 ;
21+ const measuring = spyOn ( TextEncoder . prototype , "encode" ) . mockImplementation ( function ( value = "" ) {
22+ measuredChars += value . length ;
23+ return Reflect . apply ( encode , this , [ value ] ) ;
24+ } ) ;
25+ const scanning = spyOn ( String . prototype , "indexOf" ) . mockImplementation ( function ( search , position = 0 ) {
26+ if ( search === "\n" ) scannedChars += Math . max ( 0 , this . length - position ) ;
27+ return Reflect . apply ( indexOf , this , [ search , position ] ) ;
28+ } ) ;
29+ const events : AdapterEvent [ ] = [ ] ;
30+ try {
31+ for await ( const event of adapter ( ) . parseStream ( source . response , budget ) ) events . push ( event ) ;
32+ } finally {
33+ measuring . mockRestore ( ) ;
34+ scanning . mockRestore ( ) ;
35+ }
36+ expect ( events ) . toEqual ( [
37+ { type : "text_delta" , text } ,
38+ { type : "done" , usage : undefined , stopReason : undefined } ,
39+ ] ) ;
40+ expect ( budget . snapshot ( ) ) . toMatchObject ( { currentBytes : 0 , highWaterBytes : bytes . length , overflows : 0 } ) ;
41+ expect ( measuredChars ) . toBeLessThanOrEqual ( 4 * bytes . length ) ;
42+ expect ( scannedChars ) . toBeLessThanOrEqual ( 2 * bytes . length ) ;
43+ } ) ;
44+ }
45+
1146function input ( bytes : Uint8Array , chunkSize : number , beforeRead ?: ( offset : number ) => void ) {
1247 let offset = 0 ;
1348 let cancelled = false ;
@@ -24,6 +59,26 @@ function input(bytes: Uint8Array, chunkSize: number, beforeRead?: (offset: numbe
2459 return { response : new Response ( body ) , cancelled : ( ) => cancelled } ;
2560}
2661
62+ for ( const maxTurnBytes of [ 2 , 3 ] ) {
63+ test ( `Command Code accounts for EOF decoder replacement bytes with a ${ maxTurnBytes } -byte budget` , async ( ) => {
64+ const budget = createTestTranslatorBudget ( { maxTurnBytes } ) ;
65+ const source = input ( Uint8Array . of ( 0xe2 ) , 1 ) ; // Incomplete UTF-8 becomes a 3-byte U+FFFD at EOF.
66+ const events : AdapterEvent [ ] = [ ] ;
67+ const consume = async ( ) => {
68+ for await ( const event of adapter ( ) . parseStream ( source . response , budget ) ) events . push ( event ) ;
69+ } ;
70+ if ( maxTurnBytes === 2 ) {
71+ await expect ( consume ( ) ) . rejects . toMatchObject ( { code : "translation_buffer_limit" , kind : "live_transient" } ) ;
72+ expect ( events ) . toEqual ( [ ] ) ;
73+ } else {
74+ await consume ( ) ;
75+ // Malformed NDJSON retains the existing skip-and-complete behavior.
76+ expect ( events ) . toEqual ( [ { type : "done" , usage : undefined , stopReason : undefined } ] ) ;
77+ }
78+ expect ( budget . snapshot ( ) ) . toMatchObject ( { currentBytes : 0 , overflows : maxTurnBytes === 2 ? 1 : 0 } ) ;
79+ } ) ;
80+ }
81+
2782for ( const ending of [ "\n" , "" ] as const ) {
2883 for ( const fragmented of [ false , true ] ) {
2984 test ( `Command Code admits an exact-limit ${ fragmented ? "fragmented" : "whole" } record with ${ JSON . stringify ( ending ) } termination` , async ( ) => {
0 commit comments