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