char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { 2extractJsonRpcMethods , 3formatDiagnosticErrorMessage , 4getRequestBody , 5getRequestMethod , 6getRequestUrl , 7summarizeRequestBody 8} from '$lib/utils/request-helpers' ; 9import { describe , expect , it } from 'vitest' ; 10 11describe ( 'getRequestUrl' , () => { 12it ( 'returns a plain string input as-is' , () => { 13expect ( getRequestUrl ( 'https://example.com/mcp' )). toBe ( 'https://example.com/mcp' ); 14}); 15 16it ( 'returns href from a URL object' , () => { 17expect ( getRequestUrl ( new URL ( 'https://example.com/mcp' ))). toBe ( 'https://example.com/mcp' ); 18}); 19 20it ( 'returns url from a Request object' , () => { 21const req = new Request ( 'https://example.com/mcp' ); 22 23expect ( getRequestUrl ( req )). toBe ( 'https://example.com/mcp' ); 24}); 25}); 26 27describe ( 'getRequestMethod' , () => { 28it ( 'prefers method from init' , () => { 29expect ( getRequestMethod ( 'https://example.com' , { method :'POST' })). toBe ( 'POST' ); 30}); 31 32it ( 'falls back to Request.method' , () => { 33const req = new Request ( 'https://example.com' , { method :'PUT' }); 34 35expect ( getRequestMethod ( req )). toBe ( 'PUT' ); 36}); 37 38it ( 'falls back to baseInit.method' , () => { 39expect ( getRequestMethod ( 'https://example.com' , undefined , { method :'DELETE' })). toBe ( 'DELETE' ); 40}); 41 42it ( 'defaults to GET' , () => { 43expect ( getRequestMethod ( 'https://example.com' )). toBe ( 'GET' ); 44}); 45}); 46 47describe ( 'getRequestBody' , () => { 48it ( 'returns body from init' , () => { 49expect ( getRequestBody ( 'https://example.com' , { body :'payload' })). toBe ( 'payload' ); 50}); 51 52it ( 'returns undefined when no body is present' , () => { 53expect ( getRequestBody ( 'https://example.com' )). toBeUndefined (); 54}); 55}); 56 57describe ( 'summarizeRequestBody' , () => { 58it ( 'returns empty for null' , () => { 59expect ( summarizeRequestBody ( null )). toEqual ({ kind :'empty' }); 60}); 61 62it ( 'returns empty for undefined' , () => { 63expect ( summarizeRequestBody ( undefined )). toEqual ({ kind :'empty' }); 64}); 65 66it ( 'returns string kind with size' , () => { 67expect ( summarizeRequestBody ( 'hello' )). toEqual ({ kind :'string' , size :5 }); 68}); 69 70it ( 'returns blob kind with size' , () => { 71const blob = new Blob ([ 'abc' ]); 72 73expect ( summarizeRequestBody ( blob )). toEqual ({ kind :'blob' , size :3 }); 74}); 75 76it ( 'returns formdata kind' , () => { 77expect ( summarizeRequestBody ( new FormData ())). toEqual ({ kind :'formdata' }); 78}); 79 80it ( 'returns arraybuffer kind with size' , () => { 81expect ( summarizeRequestBody ( new ArrayBuffer ( 8 ))). toEqual ({ kind :'arraybuffer' , size :8 }); 82}); 83}); 84 85describe ( 'formatDiagnosticErrorMessage' , () => { 86it ( 'appends CORS hint for Failed to fetch' , () => { 87expect ( formatDiagnosticErrorMessage ( new TypeError ( 'Failed to fetch' ))). toBe ( 88'Failed to fetch (check CORS?)' 89); 90}); 91 92it ( 'passes through other error messages unchanged' , () => { 93expect ( formatDiagnosticErrorMessage ( new Error ( 'timeout' ))). toBe ( 'timeout' ); 94}); 95 96it ( 'handles non-Error values' , () => { 97expect ( formatDiagnosticErrorMessage ( 'some string' )). toBe ( 'some string' ); 98}); 99}); 100 101describe ( 'extractJsonRpcMethods' , () => { 102it ( 'extracts methods from a JSON-RPC array' , () => { 103const body = JSON . stringify ([ 104{ id :1 , jsonrpc :'2.0' , method :'initialize' }, 105{ jsonrpc :'2.0' , method :'notifications/initialized' } 106]); 107 108expect ( extractJsonRpcMethods ( body )). toEqual ([ 'initialize' , 'notifications/initialized' ]); 109}); 110 111it ( 'extracts method from a single JSON-RPC message' , () => { 112const body = JSON . stringify ({ id :1 , jsonrpc :'2.0' , method :'tools/list' }); 113 114expect ( extractJsonRpcMethods ( body )). toEqual ([ 'tools/list' ]); 115}); 116 117it ( 'returns undefined for non-string body' , () => { 118expect ( extractJsonRpcMethods ( null )). toBeUndefined (); 119expect ( extractJsonRpcMethods ( undefined )). toBeUndefined (); 120}); 121 122it ( 'returns undefined for invalid JSON' , () => { 123expect ( extractJsonRpcMethods ( 'not json' )). toBeUndefined (); 124}); 125 126it ( 'returns undefined when no methods found' , () => { 127expect ( extractJsonRpcMethods ( JSON . stringify ({ foo :'bar' }))). toBeUndefined (); 128}); 129});