File size: 6,723 Bytes
ded72f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { describe, it, expect, beforeEach } from "vitest";
import superjson from "superjson";
import { collections } from "$lib/server/database";
import { createTestLocals, createTestUser, cleanupTestData } from "./testHelpers";
import { GET as userGET } from "../../../../routes/api/v2/user/+server";
import {
	GET as settingsGET,
	POST as settingsPOST,
} from "../../../../routes/api/v2/user/settings/+server";

async function parseResponse<T = unknown>(res: Response): Promise<T> {
	return superjson.parse(await res.text()) as T;
}

function mockRequestEvent(locals: App.Locals, overrides?: Record<string, unknown>) {
	return {
		locals,
		url: new URL("http://localhost"),
		request: new Request("http://localhost"),
		...overrides,
	} as Parameters<typeof userGET>[0];
}

describe("GET /api/v2/user", () => {
	beforeEach(async () => {
		await cleanupTestData();
	}, 20000);

	it("returns user info for authenticated user", async () => {
		const { user, locals } = await createTestUser();

		const res = await userGET(mockRequestEvent(locals));
		const data = await parseResponse<Record<string, unknown>>(res);

		expect(data).not.toBeNull();
		expect(data).toMatchObject({
			id: user._id.toString(),
			username: user.username,
			avatarUrl: user.avatarUrl,
			isAdmin: false,
			isEarlyAccess: false,
		});
	});

	it("returns null for unauthenticated user", async () => {
		const locals = createTestLocals();

		const res = await userGET(mockRequestEvent(locals));
		const data = await parseResponse(res);

		expect(data).toBeNull();
	});
});

describe("GET /api/v2/user/settings", () => {
	beforeEach(async () => {
		await cleanupTestData();
	}, 20000);

	it("returns default settings when none exist", async () => {
		const { locals } = await createTestUser();

		const res = await settingsGET(mockRequestEvent(locals));
		const data = await parseResponse<Record<string, unknown>>(res);

		expect(data).toMatchObject({
			welcomeModalSeen: false,
			welcomeModalSeenAt: null,
			streamingMode: "smooth",
			directPaste: false,
			shareConversationsWithModelAuthors: true,
			customPrompts: {},
			multimodalOverrides: {},
			toolsOverrides: {},
			providerOverrides: {},
		});
	});

	it("returns stored settings with canonical streaming mode", async () => {
		const { user, locals } = await createTestUser();

		await collections.settings.insertOne({
			userId: user._id,
			shareConversationsWithModelAuthors: false,
			activeModel: "custom-model",
			streamingMode: "raw",
			directPaste: true,
			hapticsEnabled: true,
			customPrompts: { "my-model": "Be helpful" },
			multimodalOverrides: {},
			toolsOverrides: {},
			hidePromptExamples: {},
			providerOverrides: {},
			welcomeModalSeenAt: new Date("2024-01-01"),
			createdAt: new Date(),
			updatedAt: new Date(),
		});

		const res = await settingsGET(mockRequestEvent(locals));
		const data = await parseResponse<Record<string, unknown>>(res);

		expect(data).toMatchObject({
			welcomeModalSeen: true,
			shareConversationsWithModelAuthors: false,
			streamingMode: "raw",
			directPaste: true,
			customPrompts: { "my-model": "Be helpful" },
		});
	});

	it("maps legacy stored streamingMode=final to smooth", async () => {
		const { user, locals } = await createTestUser();

		const legacySettingsWithFinal = {
			userId: user._id,
			shareConversationsWithModelAuthors: true,
			activeModel: "custom-model",
			streamingMode: "final",
			directPaste: false,
			customPrompts: {},
			multimodalOverrides: {},
			toolsOverrides: {},
			hidePromptExamples: {},
			providerOverrides: {},
			createdAt: new Date(),
			updatedAt: new Date(),
		};

		await collections.settings.insertOne(
			legacySettingsWithFinal as unknown as Parameters<typeof collections.settings.insertOne>[0]
		);

		const res = await settingsGET(mockRequestEvent(locals));
		const data = await parseResponse<Record<string, unknown>>(res);

		expect(data).toMatchObject({
			streamingMode: "smooth",
		});
	});
});

describe("POST /api/v2/user/settings", () => {
	beforeEach(async () => {
		await cleanupTestData();
	}, 20000);

	it("creates settings with upsert", async () => {
		const { user, locals } = await createTestUser();

		const body = {
			shareConversationsWithModelAuthors: false,
			activeModel: "test-model",
			customPrompts: {},
			multimodalOverrides: {},
			toolsOverrides: {},
			providerOverrides: {},
			streamingMode: "raw",
			directPaste: false,
			hidePromptExamples: {},
		};

		const res = await settingsPOST(
			mockRequestEvent(locals, {
				request: new Request("http://localhost", {
					method: "POST",
					body: JSON.stringify(body),
					headers: { "Content-Type": "application/json" },
				}),
			})
		);

		expect(res.status).toBe(200);

		const stored = await collections.settings.findOne({ userId: user._id });
		expect(stored).not.toBeNull();
		expect(stored?.shareConversationsWithModelAuthors).toBe(false);
		expect(stored?.streamingMode).toBe("raw");
		expect(stored?.createdAt).toBeInstanceOf(Date);
		expect(stored?.updatedAt).toBeInstanceOf(Date);
	});

	it("sets welcomeModalSeenAt when welcomeModalSeen is true", async () => {
		const { user, locals } = await createTestUser();

		const body = {
			welcomeModalSeen: true,
			shareConversationsWithModelAuthors: true,
			activeModel: "test-model",
			customPrompts: {},
			multimodalOverrides: {},
			toolsOverrides: {},
			providerOverrides: {},
			streamingMode: "smooth",
			directPaste: false,
			hidePromptExamples: {},
		};

		await settingsPOST(
			mockRequestEvent(locals, {
				request: new Request("http://localhost", {
					method: "POST",
					body: JSON.stringify(body),
					headers: { "Content-Type": "application/json" },
				}),
			})
		);

		const stored = await collections.settings.findOne({ userId: user._id });
		expect(stored).not.toBeNull();
		expect(stored?.welcomeModalSeenAt).toBeInstanceOf(Date);
	});

	it("validates body with Zod and applies defaults for missing fields", async () => {
		const { user, locals } = await createTestUser();

		// POST with minimal body — Zod defaults should fill in the rest
		const body = {};

		const res = await settingsPOST(
			mockRequestEvent(locals, {
				request: new Request("http://localhost", {
					method: "POST",
					body: JSON.stringify(body),
					headers: { "Content-Type": "application/json" },
				}),
			})
		);

		expect(res.status).toBe(200);

		const stored = await collections.settings.findOne({ userId: user._id });
		expect(stored).not.toBeNull();
		// Zod defaults should be applied
		expect(stored?.shareConversationsWithModelAuthors).toBe(true);
		expect(stored?.streamingMode).toBe("smooth");
		expect(stored?.directPaste).toBe(false);
		expect(stored?.customPrompts).toEqual({});
	});
});