Open History to see the last commit
1import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
2import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
3import {
4 formatComposerContextReference,
5 toComposerContextId,
6} from "@t3tools/shared/composerContextReferences";
7import * as DateTime from "effect/DateTime";
8import type { ComponentProps } from "react";
9import { expect, it, vi } from "vite-plus/test";
10import { page } from "vite-plus/test/browser";
11
12const state = vi.hoisted(() => ({
13 projection: null as unknown,
14 workflow: null as unknown,
15}));
16
17vi.mock(import("@t3tools/client-runtime/state/thread-workflows"), async (importOriginal) => ({
18 ...(await importOriginal()),
19 deriveThreadQueueWorkflowState: (() => state.workflow) as never,
20}));
21
22vi.mock(import("../../state/entities"), async (importOriginal) => ({
23 ...(await importOriginal()),
24 useThreadProjection: (() => state.projection) as never,
25 // The linked environment is connected, but its thread is not in the loaded shell.
26 useQuietThreadShell: ((target: { readonly environmentId: string } | null) =>
27 target === null || target.environmentId === "environment:linked"
28 ? null
29 : { title: "Release lead" }) as never,
30}));
31
32vi.mock(import("../../state/environments"), async (importOriginal) => ({
33 ...(await importOriginal()),
34 useEnvironment: ((environmentId: string | null) =>
35 environmentId === "environment:linked" ? { environmentId } : null) as never,
36}));
37
38vi.mock(import("../../state/query"), async (importOriginal) => ({
39 ...(await importOriginal()),
40 useEnvironmentQuery: (() => ({
41 data: { tasks: [{ id: "task:webhook", title: "PR dispatcher" }] },
42 })) as never,
43}));
44
45vi.mock(import("../../state/use-atom-command"), async (importOriginal) => ({
46 ...(await importOriginal()),
47 useAtomCommand: (() => async () => undefined) as never,
48}));
49
50vi.mock(import("../../assets/assetUrls"), async (importOriginal) => ({
51 ...(await importOriginal()),
52 useAssetUrls: ((_environmentId: never, resources: ReadonlyArray<{ attachmentId: string }>) =>
53 resources.map((resource) => `https://assets.test/${resource.attachmentId}`)) as never,
54}));
55
56import "~/index.css";
57import { render as renderUi } from "../../../test/render";
58import { QueuedRunsControl } from "./QueuedRunsControl";
59
60function setup() {
61 state.projection = null;
62 state.workflow = null;
63}
64
65const baseProps = {
66 environmentId: "environment:test" as never,
67 optimisticMessages: [],
68 threadId: "thread:test" as never,
69 editingRunId: null,
70 editBlockedReason: null,
71 onEditQueuedRun: () => undefined,
72 onCancelEdit: () => undefined,
73 timestampFormat: "locale",
74 onOpenThread: () => undefined,
75} satisfies ComponentProps<typeof QueuedRunsControl>;
76
77async function renderQueue(props: Partial<ComponentProps<typeof QueuedRunsControl>> = {}) {
78 return renderUi(<QueuedRunsControl {...baseProps} {...props} />);
79}
80
81it("previews assistant quotes as readable text and preserves the citation when editing", async () => {
82 setup();
83 const quotedText = "Cache the parsed config per worktree.";
84 const quote = serializeAssistantCitation({
85 version: 1,
86 environmentId: EnvironmentId.make("environment:test"),
87 threadId: ThreadId.make("thread:test"),
88 messageId: MessageId.make("message:assistant"),
89 text: quotedText,
90 start: 0,
91 end: quotedText.length,
92 prefix: "",
93 suffix: "",
94 });
95 const quoteAndText = `${quote}\n\nWhy per worktree?`;
96 const inlineImage = formatComposerContextReference({
97 kind: "image",
98 contextId: toComposerContextId("header"),
99 label: "header.png",
100 });
101 state.projection = { projection: { messages: [] } };
102 state.workflow = {
103 activeRun: { id: "run:active" },
104 canPromoteToSteer: true,
105 queuedRuns: [
106 {
107 run: { id: "run:quote-only", userMessageId: "message:quote-only" },
108 text: quote,
109 attachments: [],
110 },
111 {
112 run: { id: "run:quote-and-text", userMessageId: "message:quote-and-text" },
113 text: quoteAndText,
114 attachments: [],
115 },
116 {
117 run: { id: "run:inline-image", userMessageId: "message:inline-image" },
118 text: `Make the header look like ${inlineImage} and keep the logo.`,
119 attachments: [{ type: "image", id: "attachment:header", name: "header.png" }],
120 },
121 ],
122 };
123 const onEditQueuedRun = vi.fn();
124 await using _screen = await renderQueue({ onEditQueuedRun });
125
126 await expect.element(page.getByText(quotedText, { exact: true })).toBeVisible();
127 await expect
128 .element(page.getByText(`${quotedText} Why per worktree?`, { exact: true }))
129 .toBeVisible();
130 await expect
131 .element(
132 page.getByText("Make the header look like header.png and keep the logo.", { exact: true }),
133 )
134 .toBeVisible();
135
136 await page.getByRole("button", { name: "Edit queued message", exact: true }).nth(1).click();
137 expect(onEditQueuedRun).toHaveBeenCalledExactlyOnceWith(
138 expect.objectContaining({ text: quoteAndText }),
139 );
140});
141
142it("shows who sent a queued message and folds automation and agent prompts", async () => {
143 setup();
144 const message = (id: string, fields: Record<string, unknown>) => ({
145 id,
146 role: "user",
147 createdAt: DateTime.makeUnsafe("2026-10-27T12:00:00.000Z"),
148 ...fields,
149 });
150 state.projection = { projection: { messages: [] } };
151 state.workflow = {
152 activeRun: { id: "run:active" },
153 canPromoteToSteer: true,
154 queuedRuns: [
155 [
156 "run:automation",
157 "GitHub labeled a pull request.\nFollow the runbook.",
158 {
159 createdBy: "agent",
160 scheduledTaskId: "task:webhook",
161 },
162 ],
163 [
164 "run:agent",
165 "Review finished.",
166 {
167 createdBy: "agent",
168 creationSource: "provider",
169 senderThreadId: "thread:lead",
170 },
171 ],
172 [
173 "run:linked",
174 "Linked review finished.",
175 {
176 createdBy: "agent",
177 creationSource: "mcp",
178 linkedSender: {
179 environmentId: "environment:linked",
180 threadId: "thread:linked-lead",
181 title: "Title when sent",
182 },
183 },
184 ],
185 [
186 "run:server",
187 "Continue where you left off.",
188 {
189 createdBy: "agent",
190 creationSource: "server",
191 },
192 ],
193 ].map(([runId, text, fields]) => ({
194 run: { id: runId, userMessageId: `${runId}:message` },
195 text,
196 attachments: [],
197 message: message(`${runId}:message`, { text, ...(fields as object) }),
198 })),
199 };
200 const onOpenThread = vi.fn();
201 await using _screen = await renderQueue({ onOpenThread });
202 // Four rows collapse the queue.
203 await page.getByRole("button", { name: "Show all 4 queued" }).click();
204
205 const automation = page.getByRole("button", { name: /PR dispatcher/ });
206 await expect.element(automation).toHaveAttribute("aria-expanded", "false");
207 await expect.element(page.getByText(/Follow the runbook/)).not.toBeInTheDocument();
208 await automation.click();
209 await expect.element(page.getByText(/Follow the runbook/)).toBeVisible();
210 await expect.element(page.getByRole("link", { name: "Open automation" })).toBeInTheDocument();
211
212 await expect.element(page.getByRole("button", { name: /Release lead/ })).toBeVisible();
213 await page.getByRole("button", { name: "Open thread" }).nth(0).click({ force: true });
214 expect(onOpenThread).toHaveBeenCalledExactlyOnceWith("thread:lead", "environment:test");
215 // A sender in a connected linked environment keeps the title it sent and opens there.
216 await expect.element(page.getByRole("button", { name: /Title when sent/ })).toBeVisible();
217 await page.getByRole("button", { name: "Open thread" }).nth(1).click({ force: true });
218 expect(onOpenThread).toHaveBeenLastCalledWith("thread:linked-lead", "environment:linked");
219
220 await expect.element(page.getByText("Sent by T3 Code")).toBeVisible();
221 await expect.element(page.getByText("Continue where you left off.")).toBeVisible();
222});
223
224it("stays inside the timeline scroller instead of growing the chat column", async () => {
225 setup();
226 state.projection = { projection: { messages: [] } };
227 state.workflow = {
228 activeRun: { id: "run:active" },
229 canPromoteToSteer: true,
230 queuedRuns: [
231 {
232 run: { id: "run:queued", userMessageId: "message:queued" },
233 text: "Later",
234 attachments: [],
235 },
236 ],
237 };
238 // Mirrors ChatView: a scrollable column, a positioned wrapper, then a non-positioned scroller.
239 await using screen = await renderUi(
240 <div data-testid="column" style={{ height: 200, overflowY: "auto" }}>
241 <div style={{ position: "relative", height: "100%" }}>
242 <div style={{ height: "100%", overflowY: "auto" }}>
243 <div style={{ height: 1000 }} />
244 <QueuedRunsControl {...baseProps} />
245 </div>
246 </div>
247 </div>,
248 );
249 await expect.element(page.getByText("Later", { exact: true })).toBeInTheDocument();
250 const column = screen.getByTestId("column").element();
251 expect(column.scrollHeight).toBe(column.clientHeight);
252});