char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Stores split refactor (#27240)521a64cd0

char/aliceai-t5-moe
1.7 KiB43 linesraw
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 } = {}) {
7	return (async (_url: RequestInfo | URL, init?: RequestInit) => {
8		capture.auth = (init?.headers as Record<string, string>)?.['Authorization'] ?? null;
9
10		return new Response(status === 200 ? '{}' : 'Unauthorized', { status });
11	}) as typeof globalThis.fetch;
12}
13
14const is401 = (err: unknown) =>
15	typeof err === 'object' && err !== null && 'status' in err && err.status === 401;
16
17describe('api key validation surfaces the splash', () => {
18	beforeEach(() => {
19		localStorage.removeItem(CONFIG_LOCALSTORAGE_KEY);
20		settingsStore.initialize();
21	});
22
23	it('keyed server, no stored key: throws 401 so the splash shows (onboarding)', async () => {
24		await expect(validateApiKey(fakeFetch(401))).rejects.toSatisfy(is401);
25	});
26
27	it('keyed server, wrong stored key: throws 401 so the splash shows', async () => {
28		settingsStore.updateConfig('apiKey', 'wrong-key');
29		await expect(validateApiKey(fakeFetch(401))).rejects.toSatisfy(is401);
30	});
31
32	it('open server, no stored key: passes silently', async () => {
33		await expect(validateApiKey(fakeFetch(200))).resolves.toBeUndefined();
34	});
35
36	it('valid stored key: passes and sends the bearer header', async () => {
37		settingsStore.updateConfig('apiKey', 'sk-good');
38		const capture: { auth?: string | null } = {};
39
40		await expect(validateApiKey(fakeFetch(200, capture))).resolves.toBeUndefined();
41		expect(capture.auth).toBe('Bearer sk-good');
42	});
43});