char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { parseSseJsonStream } from '$lib/utils/sse' ; 2import { describe , expect , it } from 'vitest' ; 3 4function makeSseResponse ( events :string []) :Response { 5const body = events . join ( '\n\n' ) + '\n\n' ; 6 7return new Response ( body , { 8headers :{ 'content-type' :'text/event-stream' }, 9status :200 10}); 11} 12 13describe ( 'parseSseJsonStream' , () => { 14it ( 'yields parsed data for each record' , async () => { 15const response = makeSseResponse ([ 'data: {"chunk": "a"}' , 'data: {"chunk": "b"}' ]); 16const collected :unknown [] = []; 17 18for await ( const ev of parseSseJsonStream ( response )) { 19collected . push ( ev . data ); 20} 21expect ( collected ). toEqual ([{ chunk :'a' }, { chunk :'b' }]); 22}); 23 24it ( 'stops on [DONE] sentinel' , async () => { 25const response = makeSseResponse ([ 26'data: {"chunk": "a"}' , 27'data: [DONE]' , 28'data: {"chunk": "after-done"}' 29]); 30const collected :unknown [] = []; 31 32for await ( const ev of parseSseJsonStream ( response )) { 33collected . push ( ev . data ); 34} 35expect ( collected ). toEqual ([{ chunk :'a' }]); 36}); 37 38it ( 'skips malformed JSON records' , async () => { 39const response = makeSseResponse ([ 40'data: {"chunk": "ok"}' , 41'data: {not-json}' , 42'data: {"chunk": "also-ok"}' 43]); 44const collected :unknown [] = []; 45 46for await ( const ev of parseSseJsonStream ( response )) { 47collected . push ( ev . data ); 48} 49expect ( collected ). toEqual ([{ chunk :'ok' }, { chunk :'also-ok' }]); 50}); 51 52it ( 'handles records split across multiple chunks (partial last line)' , async () => { 53const full = 'data: {"chunk": "x"}\n\ndata: {"chunk": "y"}\n\n' ; 54const stream = new ReadableStream < Uint8Array >({ 55start ( controller ) { 56const enc = new TextEncoder (); 57 58controller . enqueue ( enc . encode ( full . slice ( 0 , full . length / 2 ))); 59controller . enqueue ( enc . encode ( full . slice ( full . length / 2 ))); 60controller . close (); 61} 62}); 63const response = new Response ( stream , { 64headers :{ 'content-type' :'text/event-stream' }, 65status :200 66}); 67const collected :unknown [] = []; 68 69for await ( const ev of parseSseJsonStream ( response )) { 70collected . push ( ev . data ); 71} 72expect ( collected ). toEqual ([{ chunk :'x' }, { chunk :'y' }]); 73}); 74 75it ( 'returns immediately if response has no body' , async () => { 76const response = new Response ( null , { status :200 }); 77const collected :unknown [] = []; 78 79for await ( const ev of parseSseJsonStream ( response )) { 80collected . push ( ev . data ); 81} 82expect ( collected ). toEqual ([]); 83}); 84});