Open History to see the last commit
1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { scopeThreadRef } from "@t3tools/client-runtime/environment";
3import { describe, expect, it } from "vite-plus/test";
4import { formatInlineContextReference } from "../../lib/composerContextReferences";
5import { threadContextRecord, threadContextReference } from "../../lib/composerContextRecords";
6import {
7 approvalRedirectNotice,
8 loadBalancingSendWarning,
9 multipleModelSendBlock,
10 referencedThreadContexts,
11} from "./composerSend.logic";
12
13const local = EnvironmentId.make("local");
14const remote = EnvironmentId.make("remote");
15const localThread = threadContextRecord(scopeThreadRef(local, ThreadId.make("a")), "Local");
16const remoteThread = threadContextRecord(scopeThreadRef(remote, ThreadId.make("b")), "Remote");
17
18describe("referencedThreadContexts", () => {
19 it("keeps only the attached threads the prompt still links", () => {
20 const prompt = `see ${formatInlineContextReference(threadContextReference(remoteThread))}`;
21 expect(
22 referencedThreadContexts({ prompt, threadContexts: [localThread, remoteThread] }),
23 ).toEqual([remoteThread]);
24 });
25});
26
27describe("loadBalancingSendWarning", () => {
28 it("asks the user to wait while resource checks run", () => {
29 expect(
30 loadBalancingSendWarning({
31 pending: true,
32 threadContexts: [localThread, remoteThread],
33 candidateEnvironmentIds: [],
34 }).title,
35 ).toBe("Checking machine resources");
36 });
37
38 it("blames the attached threads when no machine can read all of them", () => {
39 expect(
40 loadBalancingSendWarning({
41 pending: false,
42 threadContexts: [localThread, remoteThread],
43 candidateEnvironmentIds: [local, remote],
44 }).description,
45 ).toMatch(/No machine can read every attached thread/);
46 });
47
48 it("blames resources when one machine can read every attached thread", () => {
49 expect(
50 loadBalancingSendWarning({
51 pending: false,
52 threadContexts: [localThread],
53 candidateEnvironmentIds: [remote, local],
54 }).description,
55 ).toMatch(/No eligible machine has available resources/);
56 });
57});
58
59describe("multipleModelSendBlock", () => {
60 const ready = {
61 selectionCount: 2,
62 serverSupportsWorktreeBootstrap: true,
63 isLocalDraftThread: true,
64 isGitRepo: true,
65 branch: "main",
66 };
67
68 it("lets a new Git draft with a branch and models start", () => {
69 expect(multipleModelSendBlock(ready)).toBeNull();
70 });
71
72 it("asks for a server update before checking the draft", () => {
73 expect(
74 multipleModelSendBlock({
75 ...ready,
76 serverSupportsWorktreeBootstrap: false,
77 isGitRepo: false,
78 }),
79 ).toBe("server-outdated");
80 });
81
82 it.each([
83 { isLocalDraftThread: false },
84 { isGitRepo: false },
85 { branch: null },
86 { selectionCount: 0 },
87 ])("needs a new Git thread with a branch and models (%o)", (override) => {
88 expect(multipleModelSendBlock({ ...ready, ...override })).toBe("needs-new-git-thread");
89 });
90});
91
92describe("approvalRedirectNotice", () => {
93 const settled = {
94 draftChanged: false,
95 declined: true,
96 approvalResponding: false,
97 approvalArrived: false,
98 };
99
100 it("says the decline went through when the draft changed after it", () => {
101 expect(approvalRedirectNotice({ ...settled, draftChanged: true })).toBe(
102 "Approval declined. Your message changed, so it wasn't sent.",
103 );
104 expect(approvalRedirectNotice({ ...settled, draftChanged: true, declined: false })).toBe(
105 "Your message changed, so it wasn't sent.",
106 );
107 });
108
109 it("explains an approval already being answered", () => {
110 expect(
111 approvalRedirectNotice({ ...settled, declined: false, approvalResponding: true }),
112 ).toMatch(/already being answered/);
113 });
114
115 it("explains an approval that arrived during the decline", () => {
116 expect(approvalRedirectNotice({ ...settled, approvalArrived: true })).toMatch(
117 /Another approval arrived/,
118 );
119 });
120
121 it("stays quiet when the decline failed or the user left the thread", () => {
122 expect(approvalRedirectNotice({ ...settled, declined: false })).toBeNull();
123 expect(approvalRedirectNotice(settled)).toBeNull();
124 });
125});