char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { parseHeadersToArray , serializeHeaders } from '$lib/utils/headers' ; 2import { describe , expect , it } from 'vitest' ; 3 4/** 5* Tests for the header serialization helpers used by the MCP server form 6* (custom header rows) and the new Authorization/Bearer-token flow. 7*/ 8describe ( 'parseHeadersToArray' , () => { 9it ( 'returns an empty array for empty or whitespace-only input' , () => { 10expect ( parseHeadersToArray ( '' )). toEqual ([]); 11expect ( parseHeadersToArray ( ' ' )). toEqual ([]); 12expect ( parseHeadersToArray ( undefined as unknown as string )). toEqual ([]); 13}); 14 15it ( 'returns an empty array for invalid JSON input' , () => { 16expect ( parseHeadersToArray ( '{not-json' )). toEqual ([]); 17expect ( parseHeadersToArray ( '[]' )). toEqual ([]); 18expect ( parseHeadersToArray ( '"plain-string"' )). toEqual ([]); 19}); 20 21it ( 'converts an object into ordered key/value pairs' , () => { 22expect ( parseHeadersToArray ( '{"X-Foo":"bar","Authorization":"Bearer abc"}' )). toEqual ([ 23{ key :'X-Foo' , value :'bar' }, 24{ key :'Authorization' , value :'Bearer abc' } 25]); 26}); 27 28it ( 'stringifies non-string values' , () => { 29expect ( parseHeadersToArray ( '{"count":"42","flag":"true"}' )). toEqual ([ 30{ key :'count' , value :'42' }, 31{ key :'flag' , value :'true' } 32]); 33}); 34}); 35 36describe ( 'serializeHeaders' , () => { 37it ( 'returns an empty string when there are no valid pairs' , () => { 38expect ( serializeHeaders ([])). toBe ( '' ); 39expect ( serializeHeaders ([{ key :'' , value :'value' }])). toBe ( '' ); 40expect ( serializeHeaders ([{ key :' ' , value :'value' }])). toBe ( '' ); 41}); 42 43it ( 'returns an empty string when every pair has a blank key' , () => { 44expect ( 45serializeHeaders ([ 46{ key :'' , value :'drop-me' }, 47{ key :' ' , value :'drop-me-too' }, 48{ key :'\t' , value :'tab-key' } 49]) 50). toBe ( '' ); 51}); 52 53it ( 'drops pairs with empty keys but keeps the rest' , () => { 54expect ( 55serializeHeaders ([ 56{ key :'' , value :'drop-me' }, 57{ key :'X-Keep' , value :'ok' } 58]) 59). toBe ( '{"X-Keep":"ok"}' ); 60}); 61 62it ( 'trims keys before serializing' , () => { 63expect ( serializeHeaders ([{ key :' X-Space ' , value :'ok' }])). toBe ( '{"X-Space":"ok"}' ); 64}); 65 66it ( 'preserves the input order of surviving pairs' , () => { 67const serialized = serializeHeaders ([ 68{ key :'X-C' , value :'3' }, 69{ key :'X-A' , value :'1' }, 70{ key :'X-B' , value :'2' } 71]); 72 73// Object key order follows insertion order in modern JS engines, so 74// the serialized JSON writes keys in our input order. 75expect ( JSON . parse ( serialized )). toEqual ({ 'X-A' :'1' , 'X-B' :'2' , 'X-C' :'3' }); 76}); 77}); 78 79describe ( 'parseHeadersToArray / serializeHeaders roundtrip' , () => { 80it ( 'serializes back to an equal header object after a parse' , () => { 81const original = JSON . stringify ({ 82'Content-Type' :'application/json' , 83'X-Trace-Id' :'abc-123' 84}); 85const roundtrip = serializeHeaders ( parseHeadersToArray ( original )); 86 87expect ( JSON . parse ( roundtrip )). toEqual ( JSON . parse ( original )); 88}); 89 90it ( 'drops rows whose keys are blank after trimming during serialization' , () => { 91const pairs = parseHeadersToArray ( '{"X-Keep":"ok","":"drop-me"}' ); 92 93// parseHeadersToArray keeps raw key strings (the consumer is expected to 94// filter blanks, not the parser); serialization must strip them. 95expect ( pairs ). toEqual ([ 96{ key :'X-Keep' , value :'ok' }, 97{ key :'' , value :'drop-me' } 98]); 99expect ( serializeHeaders ( pairs )). toBe ( '{"X-Keep":"ok"}' ); 100}); 101 102it ( 'preserves upstream keys untouched (does not lowercase them)' , () => { 103const upperCased = '{"Authorization":"Bearer xyz"}' ; 104const parsed = parseHeadersToArray ( upperCased ); 105 106expect ( parsed ). toEqual ([{ key :'Authorization' , value :'Bearer xyz' }]); 107}); 108 109it ( 'bearer-token write survives a re-parse when paired with regular custom headers' , () => { 110// The McpServerForm bearer UI writes {Authorization: `Bearer <token>`} 111// into the same headers string as the custom KV section. The round 112// trip below mirrors the exact shape the form produces so a future 113// refactor of either code path cannot silently change the on-disk key. 114const pairs = [ 115{ key :'X-Trace-Id' , value :'abc-123' }, 116{ key :'Authorization' , value :'Bearer super-secret' } 117]; 118const serialized = serializeHeaders ( pairs ); 119 120expect ( serialized ). toBe ( '{"X-Trace-Id":"abc-123","Authorization":"Bearer super-secret"}' ); 121expect ( parseHeadersToArray ( serialized )). toEqual ( pairs ); 122}); 123});