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

QueuedRunsControl.browser.test.tsx

233 lines8.2 KB

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