File size: 2,137 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
import { ObjectId } from "mongodb";
import { collections } from "$lib/server/database";
import type { User } from "$lib/types/User";
import type { Session } from "$lib/types/Session";
import type { Conversation } from "$lib/types/Conversation";

export function createTestLocals(overrides?: Partial<App.Locals>): App.Locals {
	return {
		sessionId: "test-session-id",
		isAdmin: false,
		user: undefined,
		token: undefined,
		...overrides,
	};
}

export async function createTestUser(): Promise<{
	user: User;
	session: Session;
	locals: App.Locals;
}> {
	const userId = new ObjectId();
	const sessionId = `test-session-${userId.toString()}`;

	const user: User = {
		_id: userId,
		createdAt: new Date(),
		updatedAt: new Date(),
		username: `user-${userId.toString().slice(0, 8)}`,
		name: "Test User",
		avatarUrl: "https://example.com/avatar.png",
		hfUserId: `hf-${userId.toString()}`,
	};

	const session: Session = {
		_id: new ObjectId(),
		createdAt: new Date(),
		updatedAt: new Date(),
		userId,
		sessionId,
		expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
	};

	await collections.users.insertOne(user);
	await collections.sessions.insertOne(session);

	return {
		user,
		session,
		locals: {
			user,
			sessionId,
			isAdmin: false,
			token: undefined,
		},
	};
}

export async function createTestConversation(
	locals: App.Locals,
	overrides?: Partial<Conversation>
): Promise<Conversation> {
	const conv: Conversation = {
		_id: new ObjectId(),
		title: "Test Conversation",
		model: "test-model",
		messages: [],
		createdAt: new Date(),
		updatedAt: new Date(),
		...(locals.user ? { userId: locals.user._id } : { sessionId: locals.sessionId }),
		...overrides,
	};

	await collections.conversations.insertOne(conv);
	return conv;
}

export async function cleanupTestData() {
	await collections.conversations.deleteMany({});
	await collections.abortedGenerations.deleteMany({});
	await collections.users.deleteMany({});
	await collections.sessions.deleteMany({});
	await collections.settings.deleteMany({});
	await collections.sharedConversations.deleteMany({});
	await collections.reports.deleteMany({});
}