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

composerSend.logic.test.ts

184 lines5.9 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { describe, expect, it } from "vite-plus/test";
4import type { ComposerThreadDraftState } from "../../composerDraftStore";
5import { formatInlineContextReference } from "../../lib/composerContextReferences";
6import { threadContextReference } from "../../lib/composerContextRecords";
7import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
8import {
9 approvalRedirectNotice,
10 composerDraftContentChanged,
11 loadBalancingSendWarning,
12 multipleModelSendBlock,
13 referencedThreadContexts,
14} from "./composerSend.logic";
15​
16const local = EnvironmentId.make("local");
17const remote = EnvironmentId.make("remote");
18const localThread = threadContextRecord(scopeThreadRef(local, ThreadId.make("a")), "Local");
19const remoteThread = threadContextRecord(scopeThreadRef(remote, ThreadId.make("b")), "Remote");
20​
21it("detects draft edits during upload preparation without treating upload metadata as an edit", () => {
22 const image = {
23 type: "image" as const,
24 id: "image",
25 name: "a.png",
26 mimeType: "image/png",
27 sizeBytes: 1,
28 previewUrl: "blob:image",
29 file: new File(["before"], "a.png", { type: "image/png" }),
30 };
31 const draft: ComposerThreadDraftState = {
32 prompt: "before",
33 images: [image],
34 files: [
35 {
36 type: "file",
37 id: "file",
38 name: "a.pdf",
39 mimeType: "application/pdf",
40 sizeBytes: 1,
41 file: null,
42 },
43 ],
44 nonPersistedImageIds: [],
45 persistedAttachments: [],
46 terminalContexts: [],
47 previewAnnotations: [],
48 reviewComments: [],
49 threadContexts: [],
50 environmentContexts: [],
51 replyTo: null,
52 modelSelectionByProvider: {},
53 activeProvider: null,
54 runtimeMode: null,
55 interactionMode: null,
56 };
57 expect(
58 composerDraftContentChanged(draft, {
59 ...draft,
60 files: [{ ...draft.files[0]!, uploadedAttachmentId: "upload", uploadEnvironmentId: local }],
61 }),
62 ).toBe(false);
63 expect(composerDraftContentChanged(draft, { ...draft, prompt: "after" })).toBe(true);
64 expect(composerDraftContentChanged(draft, { ...draft, files: [] })).toBe(true);
65 expect(composerDraftContentChanged(draft, { ...draft, threadContexts: [localThread] })).toBe(
66 true,
67 );
68 expect(composerDraftContentChanged(draft, { ...draft, images: [{ ...image }] })).toBe(false);
69 expect(
70 composerDraftContentChanged(draft, {
71 ...draft,
72 images: [{ ...image, file: new File(["after"], "a.png", { type: "image/png" }) }],
73 }),
74 ).toBe(true);
75});
76​
77describe("referencedThreadContexts", () => {
78 it("keeps only the attached threads the prompt still links", () => {
79 const prompt = `see ${formatInlineContextReference(threadContextReference(remoteThread))}`;
80 expect(
81 referencedThreadContexts({ prompt, threadContexts: [localThread, remoteThread] }),
82 ).toEqual([remoteThread]);
83 });
84});
85​
86describe("loadBalancingSendWarning", () => {
87 it("asks the user to wait while resource checks run", () => {
88 expect(
89 loadBalancingSendWarning({
90 pending: true,
91 threadContexts: [localThread, remoteThread],
92 candidateEnvironmentIds: [],
93 }).title,
94 ).toBe("Checking machine resources");
95 });
96​
97 it("blames the attached threads when no machine can read all of them", () => {
98 expect(
99 loadBalancingSendWarning({
100 pending: false,
101 threadContexts: [localThread, remoteThread],
102 candidateEnvironmentIds: [local, remote],
103 }).description,
104 ).toMatch(/No machine can read every attached thread/);
105 });
106​
107 it("blames resources when one machine can read every attached thread", () => {
108 expect(
109 loadBalancingSendWarning({
110 pending: false,
111 threadContexts: [localThread],
112 candidateEnvironmentIds: [remote, local],
113 }).description,
114 ).toMatch(/No eligible machine has available resources/);
115 });
116});
117​
118describe("multipleModelSendBlock", () => {
119 const ready = {
120 selectionCount: 2,
121 serverSupportsWorktreeBootstrap: true,
122 isLocalDraftThread: true,
123 isGitRepo: true,
124 branch: "main",
125 };
126​
127 it("lets a new Git draft with a branch and models start", () => {
128 expect(multipleModelSendBlock(ready)).toBeNull();
129 });
130​
131 it("asks for a server update before checking the draft", () => {
132 expect(
133 multipleModelSendBlock({
134 ...ready,
135 serverSupportsWorktreeBootstrap: false,
136 isGitRepo: false,
137 }),
138 ).toBe("server-outdated");
139 });
140​
141 it.each([
142 { isLocalDraftThread: false },
143 { isGitRepo: false },
144 { branch: null },
145 { selectionCount: 0 },
146 ])("needs a new Git thread with a branch and models (%o)", (override) => {
147 expect(multipleModelSendBlock({ ...ready, ...override })).toBe("needs-new-git-thread");
148 });
149});
150​
151describe("approvalRedirectNotice", () => {
152 const settled = {
153 draftChanged: false,
154 declined: true,
155 approvalResponding: false,
156 approvalArrived: false,
157 };
158​
159 it("says the decline went through when the draft changed after it", () => {
160 expect(approvalRedirectNotice({ ...settled, draftChanged: true })).toBe(
161 "Approval declined. Your message changed, so it wasn't sent.",
162 );
163 expect(approvalRedirectNotice({ ...settled, draftChanged: true, declined: false })).toBe(
164 "Your message changed, so it wasn't sent.",
165 );
166 });
167​
168 it("explains an approval already being answered", () => {
169 expect(
170 approvalRedirectNotice({ ...settled, declined: false, approvalResponding: true }),
171 ).toMatch(/already being answered/);
172 });
173​
174 it("explains an approval that arrived during the decline", () => {
175 expect(approvalRedirectNotice({ ...settled, approvalArrived: true })).toMatch(
176 /Another approval arrived/,
177 );
178 });
179​
180 it("stays quiet when the decline failed or the user left the thread", () => {
181 expect(approvalRedirectNotice({ ...settled, declined: false })).toBeNull();
182 expect(approvalRedirectNotice(settled)).toBeNull();
183 });
184});