Datasets:
File size: 6,187 Bytes
8d88d9b |
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 |
import type { Session } from "$lib/types/Session";
import type { User } from "$lib/types/User";
import type { Conversation } from "$lib/types/Conversation";
import { ObjectId } from "mongodb";
import { deleteConversations } from "./09-delete-empty-conversations";
import { afterAll, afterEach, beforeAll, describe, expect, test } from "vitest";
import { collections } from "$lib/server/database";
type Message = Conversation["messages"][number];
const userData = {
_id: new ObjectId(),
createdAt: new Date(),
updatedAt: new Date(),
username: "new-username",
name: "name",
avatarUrl: "https://example.com/avatar.png",
hfUserId: "9999999999",
} satisfies User;
Object.freeze(userData);
const sessionForUser = {
_id: new ObjectId(),
createdAt: new Date(),
updatedAt: new Date(),
userId: userData._id,
sessionId: "session-id-9999999999",
expiresAt: new Date(Date.now() + 1000 * 60 * 60 * 24),
} satisfies Session;
Object.freeze(sessionForUser);
const userMessage = {
from: "user",
id: "user-message-id",
content: "Hello, how are you?",
} satisfies Message;
const assistantMessage = {
from: "assistant",
id: "assistant-message-id",
content: "I'm fine, thank you!",
} satisfies Message;
const systemMessage = {
from: "system",
id: "system-message-id",
content: "This is a system message",
} satisfies Message;
const conversationBase = {
_id: new ObjectId(),
createdAt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
updatedAt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
model: "model-id",
embeddingModel: "embedding-model-id",
title: "title",
messages: [],
} satisfies Conversation;
describe.sequential("Deleting discarded conversations", async () => {
test("a conversation with no messages should get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
sessionId: sessionForUser.sessionId,
});
const result = await deleteConversations(collections);
expect(result).toBe(1);
});
test("a conversation with no messages that is less than 1 hour old should not get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
sessionId: sessionForUser.sessionId,
createdAt: new Date(Date.now() - 30 * 60 * 1000),
});
const result = await deleteConversations(collections);
expect(result).toBe(0);
});
test("a conversation with only system messages should get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
sessionId: sessionForUser.sessionId,
messages: [systemMessage],
});
const result = await deleteConversations(collections);
expect(result).toBe(1);
});
test("a conversation with a user message should not get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
sessionId: sessionForUser.sessionId,
messages: [userMessage],
});
const result = await deleteConversations(collections);
expect(result).toBe(0);
});
test("a conversation with an assistant message should not get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
sessionId: sessionForUser.sessionId,
messages: [assistantMessage],
});
const result = await deleteConversations(collections);
expect(result).toBe(0);
});
test("a conversation with a mix of messages should not get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
sessionId: sessionForUser.sessionId,
messages: [systemMessage, userMessage, assistantMessage, userMessage, assistantMessage],
});
const result = await deleteConversations(collections);
expect(result).toBe(0);
});
test("a conversation with a userId and no sessionId should not get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
messages: [userMessage, assistantMessage],
userId: userData._id,
});
const result = await deleteConversations(collections);
expect(result).toBe(0);
});
test("a conversation with no userId or sessionId should get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
messages: [userMessage, assistantMessage],
});
const result = await deleteConversations(collections);
expect(result).toBe(1);
});
test("a conversation with a sessionId that exists should not get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
messages: [userMessage, assistantMessage],
sessionId: sessionForUser.sessionId,
});
const result = await deleteConversations(collections);
expect(result).toBe(0);
});
test("a conversation with a userId and a sessionId that doesn't exist should NOT get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
userId: userData._id,
messages: [userMessage, assistantMessage],
sessionId: new ObjectId().toString(),
});
const result = await deleteConversations(collections);
expect(result).toBe(0);
});
test("a conversation with only a sessionId that doesn't exist, should get deleted", async () => {
await collections.conversations.insertOne({
...conversationBase,
messages: [userMessage, assistantMessage],
sessionId: new ObjectId().toString(),
});
const result = await deleteConversations(collections);
expect(result).toBe(1);
});
test("many conversations should get deleted", async () => {
const conversations = Array.from({ length: 10010 }, () => ({
...conversationBase,
_id: new ObjectId(),
}));
await collections.conversations.insertMany(conversations);
const result = await deleteConversations(collections);
expect(result).toBe(10010);
});
});
beforeAll(async () => {
await collections.users.insertOne(userData);
await collections.sessions.insertOne(sessionForUser);
});
afterAll(async () => {
await collections.users.deleteOne({
_id: userData._id,
});
await collections.sessions.deleteOne({
_id: sessionForUser._id,
});
await collections.conversations.deleteMany({});
});
afterEach(async () => {
await collections.conversations.deleteMany({
_id: { $in: [conversationBase._id] },
});
});
|