Spaces:
Build error
Build error
File size: 3,767 Bytes
e6addfc |
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 |
import { collections } from "$lib/server/database";
import { ObjectId } from "mongodb";
import { describe, expect, it } from "vitest";
import {
insertLegacyConversation,
insertLinearBranchConversation,
insertSideBranchesConversation,
} from "./treeHelpers.spec";
import { buildSubtree } from "./buildSubtree";
describe("buildSubtree", () => {
it("a subtree in a legacy conversation should be just a slice", async () => {
const convId = await insertLegacyConversation();
const conv = await collections.conversations.findOne({ _id: new ObjectId(convId) });
if (!conv) throw new Error("Conversation not found");
// check middle
const id = conv.messages[2].id;
const subtree = buildSubtree(conv, id);
expect(subtree).toEqual(conv.messages.slice(0, 3));
// check zero
const id2 = conv.messages[0].id;
const subtree2 = buildSubtree(conv, id2);
expect(subtree2).toEqual(conv.messages.slice(0, 1));
//check full length
const id3 = conv.messages[conv.messages.length - 1].id;
const subtree3 = buildSubtree(conv, id3);
expect(subtree3).toEqual(conv.messages);
});
it("a subtree in a linear branch conversation should be the ancestors and the message", async () => {
const convId = await insertLinearBranchConversation();
const conv = await collections.conversations.findOne({ _id: new ObjectId(convId) });
if (!conv) throw new Error("Conversation not found");
// check middle
const id = conv.messages[1].id;
const subtree = buildSubtree(conv, id);
expect(subtree).toEqual([conv.messages[0], conv.messages[1]]);
// check zero
const id2 = conv.messages[0].id;
const subtree2 = buildSubtree(conv, id2);
expect(subtree2).toEqual([conv.messages[0]]);
//check full length
const id3 = conv.messages[conv.messages.length - 1].id;
const subtree3 = buildSubtree(conv, id3);
expect(subtree3).toEqual(conv.messages);
});
it("should throw an error if the message is not found", async () => {
const convId = await insertLinearBranchConversation();
const conv = await collections.conversations.findOne({ _id: new ObjectId(convId) });
if (!conv) throw new Error("Conversation not found");
const id = "not-a-real-id-test";
expect(() => buildSubtree(conv, id)).toThrow("Message not found");
});
it("should throw an error if the ancestor is not found", async () => {
const convId = await insertLinearBranchConversation();
const conv = await collections.conversations.findOne({ _id: new ObjectId(convId) });
if (!conv) throw new Error("Conversation not found");
const id = "1-1-1-1-2";
conv.messages[1].ancestors = ["not-a-real-id-test"];
expect(() => buildSubtree(conv, id)).toThrow("Ancestor not found");
});
it("should work on empty conversations", () => {
const conv = {
_id: new ObjectId(),
rootMessageId: undefined,
messages: [],
};
const subtree = buildSubtree(conv, "not-a-real-id-test");
expect(subtree).toEqual([]);
});
it("should work for conversation with subtrees", async () => {
const convId = await insertSideBranchesConversation();
const conv = await collections.conversations.findOne({ _id: new ObjectId(convId) });
if (!conv) throw new Error("Conversation not found");
const subtree = buildSubtree(conv, "1-1-1-1-2");
expect(subtree).toEqual([conv.messages[0], conv.messages[1]]);
const subtree2 = buildSubtree(conv, "1-1-1-1-4");
expect(subtree2).toEqual([
conv.messages[0],
conv.messages[1],
conv.messages[2],
conv.messages[3],
]);
const subtree3 = buildSubtree(conv, "1-1-1-1-6");
expect(subtree3).toEqual([conv.messages[0], conv.messages[4], conv.messages[5]]);
const subtree4 = buildSubtree(conv, "1-1-1-1-7");
expect(subtree4).toEqual([conv.messages[0], conv.messages[4], conv.messages[6]]);
});
});
|