File size: 6,634 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';

// node env unit project has no DOM, install a minimal localStorage backed by a Map
beforeAll(() => {
	const store = new Map<string, string>();
	const polyfill: Storage = {
		clear: () => store.clear(),
		getItem: (k) => (store.has(k) ? store.get(k)! : null),
		key: (i) => Array.from(store.keys())[i] ?? null,
		get length() {
			return store.size;
		},
		removeItem: (k) => {
			store.delete(k);
		},
		setItem: (k, v) => {
			store.set(k, String(v));
		}
	};

	(globalThis as unknown as { localStorage: Storage }).localStorage = polyfill;
});

import { STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX } from '$lib/constants';
import { ChatService } from '$lib/services/chat.service';

describe('ChatService stream resume', () => {
	beforeEach(() => {
		localStorage.clear();
	});
	afterEach(() => {
		localStorage.clear();
	});

	it('returns null when no state exists for the conversation', () => {
		expect(ChatService.getStreamState('conv-a')).toBeNull();
	});

	it('saves and reads back the byte count', () => {
		ChatService.saveStreamState('conv-a', 4242);
		const got = ChatService.getStreamState('conv-a');

		expect(got).not.toBeNull();
		expect(got!.bytesReceived).toBe(4242);
		expect(typeof got!.updatedAt).toBe('number');
	});

	it('overwrites the previous byte count on a new save for the same conversation', () => {
		ChatService.saveStreamState('conv-a', 100);
		ChatService.saveStreamState('conv-a', 200);
		const got = ChatService.getStreamState('conv-a');

		expect(got!.bytesReceived).toBe(200);
	});

	it('keeps states for distinct conversations isolated', () => {
		ChatService.saveStreamState('conv-a', 10);
		ChatService.saveStreamState('conv-b', 20);
		expect(ChatService.getStreamState('conv-a')!.bytesReceived).toBe(10);
		expect(ChatService.getStreamState('conv-b')!.bytesReceived).toBe(20);
	});

	it('clears the state for a given conversation', () => {
		ChatService.saveStreamState('conv-a', 10);
		ChatService.clearStreamState('conv-a');
		expect(ChatService.getStreamState('conv-a')).toBeNull();
	});

	it('ignores empty conversation id on save', () => {
		ChatService.saveStreamState('', 1);
		expect(ChatService.getStreamState('')).toBeNull();
	});

	it('returns null on corrupted storage payload', () => {
		localStorage.setItem(`${STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX}conv-a`, '{not-json');
		expect(ChatService.getStreamState('conv-a')).toBeNull();
	});

	it('persists the model alongside the byte count', () => {
		ChatService.saveStreamState('conv-a', 10, 'model-x');
		expect(ChatService.getStreamState('conv-a')!.model).toBe('model-x');
	});

	it('stores a null model when none is provided', () => {
		ChatService.saveStreamState('conv-a', 10);
		expect(ChatService.getStreamState('conv-a')!.model).toBeNull();
	});

	it('overwrites the model on a new save for the same conversation', () => {
		ChatService.saveStreamState('conv-a', 10, 'model-x');
		ChatService.saveStreamState('conv-a', 20, 'model-y');
		expect(ChatService.getStreamState('conv-a')!.model).toBe('model-y');
	});

	describe('throttled saves (per-chunk path)', () => {
		// unique conversation ids: the throttle tracker is module state and
		// outlives beforeEach's localStorage.clear()
		let counter = 0;

		const freshConv = () => `conv-throttle-${++counter}`;

		it('writes immediately when no write was recorded for the conversation', () => {
			const conv = freshConv();

			ChatService.saveStreamStateThrottled(conv, 100);
			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(100);
		});

		it('holds a save pending when it lands inside the interval, flush forces it out', () => {
			const conv = freshConv();

			ChatService.saveStreamStateThrottled(conv, 100);
			ChatService.saveStreamStateThrottled(conv, 200);
			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(100);

			ChatService.flushStreamState(conv);
			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(200);
		});

		it('flush is a no-op when nothing is pending', () => {
			const conv = freshConv();

			ChatService.saveStreamStateThrottled(conv, 100);
			ChatService.flushStreamState(conv);
			ChatService.flushStreamState(conv);
			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(100);
		});

		it('an immediate save resets the throttle window', () => {
			const conv = freshConv();

			ChatService.saveStreamStateThrottled(conv, 100);
			ChatService.saveStreamState(conv, 150);
			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(150);

			ChatService.saveStreamStateThrottled(conv, 200);
			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(150);

			ChatService.flushStreamState(conv);
			expect(ChatService.getStreamState(conv)!.bytesReceived).toBe(200);
		});

		it('clearStreamState drops the pending throttled state', () => {
			const conv = freshConv();

			ChatService.saveStreamStateThrottled(conv, 100);
			ChatService.saveStreamStateThrottled(conv, 200);
			ChatService.clearStreamState(conv);
			expect(ChatService.getStreamState(conv)).toBeNull();

			ChatService.flushStreamState(conv);
			expect(ChatService.getStreamState(conv)).toBeNull();
		});
	});

	describe('resumeStreamIdentity', () => {
		it('appends the persisted model so the resume key matches the frozen POST identity', () => {
			ChatService.saveStreamState('conv-a', 10, 'model-x');
			expect(
				ChatService.resumeStreamIdentity('conv-a', ChatService.getStreamState('conv-a'), 'dropdown')
			).toBe('conv-a::model-x');
		});

		it('keeps the bare conv id when the persisted model is null', () => {
			ChatService.saveStreamState('conv-a', 10);
			expect(
				ChatService.resumeStreamIdentity('conv-a', ChatService.getStreamState('conv-a'), 'dropdown')
			).toBe('conv-a');
		});

		it('falls back to the current model only when no state is persisted', () => {
			expect(ChatService.resumeStreamIdentity('conv-a', null, 'dropdown')).toBe('conv-a::dropdown');
		});

		it('ignores the fallback when a state exists, the persisted value is authoritative', () => {
			ChatService.saveStreamState('conv-a', 10, 'model-x');
			expect(
				ChatService.resumeStreamIdentity('conv-a', ChatService.getStreamState('conv-a'), 'dropdown')
			).toBe('conv-a::model-x');
		});

		it('falls back when a legacy state has no model field', () => {
			localStorage.setItem(
				`${STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX}conv-a`,
				JSON.stringify({ bytesReceived: 10, updatedAt: 1 })
			);
			expect(
				ChatService.resumeStreamIdentity('conv-a', ChatService.getStreamState('conv-a'), 'dropdown')
			).toBe('conv-a::dropdown');
		});
	});
});