import test from 'node:test'; import assert from 'node:assert/strict'; import { appendBoundedChunk, parseResponseBody } from './responseBody'; test('bounds streamed response accumulation before retaining an oversized chunk',()=>{const chunks:Buffer[]=[];let size=appendBoundedChunk(chunks,Buffer.from('1234'),0,5);assert.equal(size,4);assert.throws(()=>appendBoundedChunk(chunks,Buffer.from('67'),size,5),(e:any)=>e.code==='PROXY_RESPONSE_TOO_LARGE');assert.equal(chunks.length,1);}); test('parses valid JSON and safely returns malformed JSON as text',()=>{assert.deepEqual(parseResponseBody(Buffer.from('{"ok":true}'),'application/json'),{ok:true});assert.equal(parseResponseBody(Buffer.from('{bad'),'application/json'),'{bad');assert.equal(parseResponseBody(Buffer.from('hello'),'text/plain'),'hello');});