Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

queued-run-edit.test.ts

82 lines2.4 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 ChatAttachmentId,
4 ComposerContextId,
5 MessageId,
6 RunId,
7 type ChatAttachment,
8 type OrchestrationMessageContext,
9} from "@t3tools/contracts";
10​
11import { resolveQueuedEditPayload, type QueuedRunEdit } from "./queued-run-edit";
12​
13const image = (id: string): ChatAttachment => ({
14 type: "image",
15 id: ChatAttachmentId.make(id),
16 name: `${id}.png`,
17 mimeType: "image/png",
18 sizeBytes: 10,
19});
20​
21function edit(attachments: ReadonlyArray<ChatAttachment>): QueuedRunEdit {
22 return {
23 runId: RunId.make("run-1"),
24 messageId: MessageId.make("message-1"),
25 originalText: "original",
26 existingAttachments: attachments,
27 };
29​
30function context(attachmentIds: ReadonlyArray<string>): OrchestrationMessageContext {
31 return {
32 version: 1,
33 records: attachmentIds.map((attachmentId, index) => ({
34 version: 1,
35 kind: "image",
36 contextId: ComposerContextId.make(`ctx-${index}`),
37 label: `${attachmentId}.png`,
38 attachmentId,
39 name: `${attachmentId}.png`,
40 mimeType: "image/png",
41 sizeBytes: 10,
42 })),
43 };
45​
46describe("resolveQueuedEditPayload", () => {
47 it("resends the kept server attachments ahead of the newly uploaded ones", () => {
48 const payload = resolveQueuedEditPayload({
49 edit: edit([image("kept")]),
50 draftContext: undefined,
51 draftAttachments: [{ id: "draft-1" }],
52 uploaded: [image("uploaded")],
53 });
54​
55 expect(payload.attachments.map((attachment) => attachment.id)).toEqual(["kept", "uploaded"]);
56 });
57​
58 it("rebinds context records from the draft's ids to the uploaded ones", () => {
59 const payload = resolveQueuedEditPayload({
60 edit: edit([]),
61 draftContext: context(["draft-1"]),
62 draftAttachments: [{ id: "draft-1" }],
63 uploaded: [image("uploaded")],
64 });
65​
66 expect(payload.context?.records).toEqual([
67 expect.objectContaining({ attachmentId: "uploaded" }),
68 ]);
69 });
70​
71 it("drops the record of an attachment the user removed", () => {
72 const payload = resolveQueuedEditPayload({
73 edit: edit([image("kept")]),
74 draftContext: context(["kept", "gone"]),
75 draftAttachments: [],
76 uploaded: [],
77 });
78​
79 expect(payload.attachments.map((attachment) => attachment.id)).toEqual(["kept"]);
80 expect(payload.context?.records).toEqual([expect.objectContaining({ attachmentId: "kept" })]);
81 });
82});