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
6.5 KiB192 linesraw
1import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
2
3// node env unit project has no DOM, install a minimal localStorage backed by a Map
4beforeAll(() => {
5	const store = new Map<string, string>();
6	const polyfill: Storage = {
7		clear: () => store.clear(),
8		getItem: (k) => (store.has(k) ? store.get(k)! : null),
9		key: (i) => Array.from(store.keys())[i] ?? null,
10		get length() {
11			return store.size;
12		},
13		removeItem: (k) => {
14			store.delete(k);
15		},
16		setItem: (k, v) => {
17			store.set(k, String(v));
18		}
19	};
20
21	(globalThis as unknown as { localStorage: Storage }).localStorage = polyfill;
22});
23
24import { STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX } from '$lib/constants';
25import { ChatService } from '$lib/services/chat.service';
26
27describe('ChatService stream resume', () => {
28	beforeEach(() => {
29		localStorage.clear();
30	});
31	afterEach(() => {
32		localStorage.clear();
33	});
34
35	it('returns null when no state exists for the conversation', () => {
36		expect(ChatService.getStreamState('conv-a')).toBeNull();
37	});
38
39	it('saves and reads back the byte count', () => {
40		ChatService.saveStreamState('conv-a', 4242);
41		const got = ChatService.getStreamState('conv-a');
42
43		expect(got).not.toBeNull();
44		expect(got!.bytesReceived).toBe(4242);
45		expect(typeof got!.updatedAt).toBe('number');
46	});
47
48	it('overwrites the previous byte count on a new save for the same conversation', () => {
49		ChatService.saveStreamState('conv-a', 100);
50		ChatService.saveStreamState('conv-a', 200);
51		const got = ChatService.getStreamState('conv-a');
52
53		expect(got!.bytesReceived).toBe(200);
54	});
55
56	it('keeps states for distinct conversations isolated', () => {
57		ChatService.saveStreamState('conv-a', 10);
58		ChatService.saveStreamState('conv-b', 20);
59		expect(ChatService.getStreamState('conv-a')!.bytesReceived).toBe(10);
60		expect(ChatService.getStreamState('conv-b')!.bytesReceived).toBe(20);
61	});
62
63	it('clears the state for a given conversation', () => {
64		ChatService.saveStreamState('conv-a', 10);
65		ChatService.clearStreamState('conv-a');
66		expect(ChatService.getStreamState('conv-a')).toBeNull();
67	});
68
69	it('ignores empty conversation id on save', () => {
70		ChatService.saveStreamState('', 1);
71		expect(ChatService.getStreamState('')).toBeNull();
72	});
73
74	it('returns null on corrupted storage payload', () => {
75		localStorage.setItem(`${STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX}conv-a`, '{not-json');
76		expect(ChatService.getStreamState('conv-a')).toBeNull();
77	});
78
79	it('persists the model alongside the byte count', () => {
80		ChatService.saveStreamState('conv-a', 10, 'model-x');
81		expect(ChatService.getStreamState('conv-a')!.model).toBe('model-x');
82	});
83
84	it('stores a null model when none is provided', () => {
85		ChatService.saveStreamState('conv-a', 10);
86		expect(ChatService.getStreamState('conv-a')!.model).toBeNull();
87	});
88
89	it('overwrites the model on a new save for the same conversation', () => {
90		ChatService.saveStreamState('conv-a', 10, 'model-x');
91		ChatService.saveStreamState('conv-a', 20, 'model-y');
92		expect(ChatService.getStreamState('conv-a')!.model).toBe('model-y');
93	});
94
95	describe('throttled saves (per-chunk path)', () => {
96		// unique conversation ids: the throttle tracker is module state and
97		// outlives beforeEach's localStorage.clear()
98		let counter = 0;
99
100		const freshConv = () => `conv-throttle-${++counter}`;
101
102		it('writes immediately when no write was recorded for the conversation', () => {
103			const conv = freshConv();
104
105			ChatService.saveStreamStateThrottled(conv, 100);
106			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(100);
107		});
108
109		it('holds a save pending when it lands inside the interval, flush forces it out', () => {
110			const conv = freshConv();
111
112			ChatService.saveStreamStateThrottled(conv, 100);
113			ChatService.saveStreamStateThrottled(conv, 200);
114			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(100);
115
116			ChatService.flushStreamState(conv);
117			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(200);
118		});
119
120		it('flush is a no-op when nothing is pending', () => {
121			const conv = freshConv();
122
123			ChatService.saveStreamStateThrottled(conv, 100);
124			ChatService.flushStreamState(conv);
125			ChatService.flushStreamState(conv);
126			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(100);
127		});
128
129		it('an immediate save resets the throttle window', () => {
130			const conv = freshConv();
131
132			ChatService.saveStreamStateThrottled(conv, 100);
133			ChatService.saveStreamState(conv, 150);
134			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(150);
135
136			ChatService.saveStreamStateThrottled(conv, 200);
137			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(150);
138
139			ChatService.flushStreamState(conv);
140			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(200);
141		});
142
143		it('clearStreamState drops the pending throttled state', () => {
144			const conv = freshConv();
145
146			ChatService.saveStreamStateThrottled(conv, 100);
147			ChatService.saveStreamStateThrottled(conv, 200);
148			ChatService.clearStreamState(conv);
149			expect(ChatService.getStreamState(conv)).toBeNull();
150
151			ChatService.flushStreamState(conv);
152			expect(ChatService.getStreamState(conv)).toBeNull();
153		});
154	});
155
156	describe('resumeStreamIdentity', () => {
157		it('appends the persisted model so the resume key matches the frozen POST identity', () => {
158			ChatService.saveStreamState('conv-a', 10, 'model-x');
159			expect(
160				ChatService.resumeStreamIdentity('conv-a', ChatService.getStreamState('conv-a'), 'dropdown')
161			).toBe('conv-a::model-x');
162		});
163
164		it('keeps the bare conv id when the persisted model is null', () => {
165			ChatService.saveStreamState('conv-a', 10);
166			expect(
167				ChatService.resumeStreamIdentity('conv-a', ChatService.getStreamState('conv-a'), 'dropdown')
168			).toBe('conv-a');
169		});
170
171		it('falls back to the current model only when no state is persisted', () => {
172			expect(ChatService.resumeStreamIdentity('conv-a', null, 'dropdown')).toBe('conv-a::dropdown');
173		});
174
175		it('ignores the fallback when a state exists, the persisted value is authoritative', () => {
176			ChatService.saveStreamState('conv-a', 10, 'model-x');
177			expect(
178				ChatService.resumeStreamIdentity('conv-a', ChatService.getStreamState('conv-a'), 'dropdown')
179			).toBe('conv-a::model-x');
180		});
181
182		it('falls back when a legacy state has no model field', () => {
183			localStorage.setItem(
184				`${STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX}conv-a`,
185				JSON.stringify({ bytesReceived: 10, updatedAt: 1 })
186			);
187			expect(
188				ChatService.resumeStreamIdentity('conv-a', ChatService.getStreamState('conv-a'), 'dropdown')
189			).toBe('conv-a::dropdown');
190		});
191	});
192});