char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
521a64cd0
char/aliceai-t5-moe
1import { CONFIG_LOCALSTORAGE_KEY } from '$lib/constants' ; 2import { settingsStore } from '$lib/stores/settings/index.svelte' ; 3import { validateApiKey } from '$lib/utils/api-key-validation' ; 4import { beforeEach , describe , expect , it } from 'vitest' ; 5 6function fakeFetch ( status :number , capture :{ auth ?:string | null } = {}) { 7return ( async ( _url :RequestInfo | URL , init ?:RequestInit ) => { 8capture . auth = ( init ?. headers as Record < string , string >)?.[ 'Authorization' ] ?? null ; 9 10return new Response ( status === 200 ?'{}' :'Unauthorized' , { status}); 11}) as typeof globalThis . fetch ; 12} 13 14const is401 = ( err :unknown ) => 15typeof err === 'object' && err !== null && 'status' in err && err . status === 401 ; 16 17describe ( 'api key validation surfaces the splash' , () => { 18beforeEach (() => { 19localStorage . removeItem ( CONFIG_LOCALSTORAGE_KEY ); 20settingsStore . initialize (); 21}); 22 23it ( 'keyed server, no stored key: throws 401 so the splash shows (onboarding)' , async () => { 24await expect ( validateApiKey ( fakeFetch ( 401 ))). rejects . toSatisfy ( is401 ); 25}); 26 27it ( 'keyed server, wrong stored key: throws 401 so the splash shows' , async () => { 28settingsStore . updateConfig ( 'apiKey' , 'wrong-key' ); 29await expect ( validateApiKey ( fakeFetch ( 401 ))). rejects . toSatisfy ( is401 ); 30}); 31 32it ( 'open server, no stored key: passes silently' , async () => { 33await expect ( validateApiKey ( fakeFetch ( 200 ))). resolves . toBeUndefined (); 34}); 35 36it ( 'valid stored key: passes and sends the bearer header' , async () => { 37settingsStore . updateConfig ( 'apiKey' , 'sk-good' ); 38const capture :{ auth ?:string | null } = {}; 39 40await expect ( validateApiKey ( fakeFetch ( 200 , capture ))). resolves . toBeUndefined (); 41expect ( capture . auth ). toBe ( 'Bearer sk-good' ); 42}); 43});