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

QueuedRunsControl.browser.test.tsx

215 lines6.6 KB

1import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
2import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
3import { act, type ComponentProps } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, describe, 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}));
22​
23vi.mock(import("../../state/use-atom-command"), async (importOriginal) => ({
24 ...(await importOriginal()),
25 useAtomCommand: (() => async () => undefined) as never,
26}));
27​
28vi.mock(import("../../assets/assetUrls"), async (importOriginal) => ({
29 ...(await importOriginal()),
30 useAssetUrls: ((_environmentId: never, resources: ReadonlyArray<{ attachmentId: string }>) =>
31 resources.map((resource) => `https://assets.test/${resource.attachmentId}`)) as never,
32}));
33​
34import { QueuedRunsControl } from "./QueuedRunsControl";
35​
36const roots: Root[] = [];
37​
38afterEach(async () => {
39 await act(async () => {
40 for (const root of roots.splice(0)) root.unmount();
41 });
42 document.body.replaceChildren();
43 vi.unstubAllGlobals();
44});
45​
46const baseProps = {
47 environmentId: "environment:test" as never,
48 optimisticMessages: [],
49 threadId: "thread:test" as never,
50 editingRunId: null,
51 onEditQueuedRun: () => undefined,
52 onCancelEdit: () => undefined,
53} satisfies ComponentProps<typeof QueuedRunsControl>;
54​
55async function render(props: Partial<ComponentProps<typeof QueuedRunsControl>> = {}) {
56 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
57 const container = document.createElement("div");
58 document.body.append(container);
59 const root = createRoot(container);
60 roots.push(root);
61 await act(async () => root.render(<QueuedRunsControl {...baseProps} {...props} />));
62 return container;
64​
65const labels = (container: HTMLElement) =>
66 [...container.querySelectorAll("[aria-label]")].map((element) =>
67 element.getAttribute("aria-label"),
68 );
69​
70describe("QueuedRunsControl automatic completion delivery", () => {
71 it("does not render a queue control when only hidden delivery remains", async () => {
72 state.projection = {
73 projection: {
74 messages: [
75 {
76 delegatedCompletion: {
77 parentRunId: "run:parent",
78 generation: 1,
79 taskIds: ["task:child"],
80 },
81 id: "message:completion",
82 },
83 ],
84 },
85 };
86 state.workflow = {
87 activeRun: { id: "run:active" },
88 canPromoteToSteer: true,
89 canReorder: true,
90 queuedRuns: [],
91 };
92​
93 const view = await render();
94​
95 expect(view.childElementCount).toBe(0);
96 });
97});
98​
99describe("QueuedRunsControl attachments and edit mode", () => {
100 const workflowWithAttachment = () => ({
101 activeRun: { id: "run:active" },
102 canPromoteToSteer: true,
103 canReorder: true,
104 queuedRuns: [
105 {
106 run: { id: "run:queued", userMessageId: "message:queued" },
107 text: "Queued with a screenshot",
108 attachments: [
109 {
110 type: "image",
111 id: "attachment-1",
112 name: "screenshot.png",
113 mimeType: "image/png",
114 sizeBytes: 128,
115 },
116 ],
117 },
118 ],
119 });
120​
121 it("renders an attachment thumbnail on the queued row", async () => {
122 state.projection = { projection: { messages: [] } };
123 state.workflow = workflowWithAttachment();
124​
125 const view = await render();
126​
127 expect(view.querySelector("img")?.getAttribute("src")).toBe("https://assets.test/attachment-1");
128 expect(view.textContent).toContain("Queued with a screenshot");
129 expect(labels(view)).toContain("Edit queued message");
130 expect(labels(view)).toContain("Reorder queued message (drag, or press the arrow keys)");
131 expect(labels(view)).not.toContain("Move queued message up");
132 });
133​
134 it("drops the optimistic pending row once the projection holds its message", async () => {
135 state.projection = {
136 projection: { messages: [{ id: "message:acknowledged", text: "hello" }] },
137 };
138 state.workflow = {
139 activeRun: { id: "run:active" },
140 canPromoteToSteer: true,
141 canReorder: true,
142 queuedRuns: [],
143 };
144​
145 const view = await render({
146 optimisticMessages: [
147 {
148 id: "message:acknowledged" as never,
149 inputIntent: "queued_turn",
150 text: "hello",
151 attachments: [],
152 },
153 ],
154 });
155​
156 expect(view.childElementCount).toBe(0);
157 });
158​
159 it("keeps the original queued message visible while editing", async () => {
160 state.projection = { projection: { messages: [] } };
161 state.workflow = workflowWithAttachment();
162​
163 const view = await render({ editingRunId: "run:queued" as never });
164​
165 expect(view.textContent).toContain("Queued with a screenshot");
166 });
167});
168​
169it("previews assistant quotes as readable text and preserves the citation when editing", async () => {
170 const quotedText = "Cache the parsed config per worktree.";
171 const quote = serializeAssistantCitation({
172 version: 1,
173 environmentId: EnvironmentId.make("environment:test"),
174 threadId: ThreadId.make("thread:test"),
175 messageId: MessageId.make("message:assistant"),
176 text: quotedText,
177 start: 0,
178 end: quotedText.length,
179 prefix: "",
180 suffix: "",
181 });
182 const quoteAndText = `${quote}\n\nWhy per worktree?`;
183 state.projection = { projection: { messages: [] } };
184 state.workflow = {
185 activeRun: { id: "run:active" },
186 canPromoteToSteer: true,
187 canReorder: true,
188 queuedRuns: [
189 {
190 run: { id: "run:quote-only", userMessageId: "message:quote-only" },
191 text: quote,
192 attachments: [],
193 },
194 {
195 run: { id: "run:quote-and-text", userMessageId: "message:quote-and-text" },
196 text: quoteAndText,
197 attachments: [],
198 },
199 ],
200 };
201 const onEditQueuedRun = vi.fn();
202 await render({ onEditQueuedRun });
203​
204 await expect.element(page.getByText(quotedText, { exact: true })).toBeVisible();
205 await expect
206 .element(page.getByText(`${quotedText} Why per worktree?`, { exact: true }))
207 .toBeVisible();
208​
209 await act(async () => {
210 await page.getByRole("button", { name: "Edit queued message", exact: true }).nth(1).click();
211 });
212 expect(onEditQueuedRun).toHaveBeenCalledExactlyOnceWith(
213 expect.objectContaining({ text: quoteAndText }),
214 );
215});