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

QueuedRunsControl.browser.test.tsx

164 lines4.9 KB

1import { act, type ComponentProps } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, describe, expect, it, vi } from "vite-plus/test";
4​
5const state = vi.hoisted(() => ({
6 projection: null as unknown,
7 workflow: null as unknown,
8}));
9​
10vi.mock(import("@t3tools/client-runtime/state/thread-workflows"), async (importOriginal) => ({
11 ...(await importOriginal()),
12 deriveThreadQueueWorkflowState: (() => state.workflow) as never,
13}));
14​
15vi.mock(import("../../state/entities"), async (importOriginal) => ({
16 ...(await importOriginal()),
17 useThreadProjection: (() => state.projection) as never,
18}));
19​
20vi.mock(import("../../state/use-atom-command"), async (importOriginal) => ({
21 ...(await importOriginal()),
22 useAtomCommand: (() => async () => undefined) as never,
23}));
24​
25vi.mock(import("../../assets/assetUrls"), async (importOriginal) => ({
26 ...(await importOriginal()),
27 useAssetUrls: ((_environmentId: never, resources: ReadonlyArray<{ attachmentId: string }>) =>
28 resources.map((resource) => `https://assets.test/${resource.attachmentId}`)) as never,
29}));
30​
31import { QueuedRunsControl } from "./QueuedRunsControl";
32​
33const roots: Root[] = [];
34​
35afterEach(async () => {
36 await act(async () => {
37 for (const root of roots.splice(0)) root.unmount();
38 });
39 document.body.replaceChildren();
40 vi.unstubAllGlobals();
41});
42​
43const baseProps = {
44 environmentId: "environment:test" as never,
45 optimisticMessages: [],
46 threadId: "thread:test" as never,
47 editingRunId: null,
48 onEditQueuedRun: () => undefined,
49 onCancelEdit: () => undefined,
50} satisfies ComponentProps<typeof QueuedRunsControl>;
51​
52async function render(props: Partial<ComponentProps<typeof QueuedRunsControl>> = {}) {
53 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
54 const container = document.createElement("div");
55 document.body.append(container);
56 const root = createRoot(container);
57 roots.push(root);
58 await act(async () => root.render(<QueuedRunsControl {...baseProps} {...props} />));
59 return container;
61​
62const labels = (container: HTMLElement) =>
63 [...container.querySelectorAll("[aria-label]")].map((element) =>
64 element.getAttribute("aria-label"),
65 );
66​
67describe("QueuedRunsControl automatic completion delivery", () => {
68 it("does not render a queue control when only hidden delivery remains", async () => {
69 state.projection = {
70 projection: {
71 messages: [
72 {
73 delegatedCompletion: {
74 parentRunId: "run:parent",
75 generation: 1,
76 taskIds: ["task:child"],
77 },
78 id: "message:completion",
79 },
80 ],
81 },
82 };
83 state.workflow = {
84 activeRun: { id: "run:active" },
85 canPromoteToSteer: true,
86 canReorder: true,
87 queuedRuns: [],
88 };
89​
90 const view = await render();
91​
92 expect(view.childElementCount).toBe(0);
93 });
94});
95​
96describe("QueuedRunsControl attachments and edit mode", () => {
97 const workflowWithAttachment = () => ({
98 activeRun: { id: "run:active" },
99 canPromoteToSteer: true,
100 canReorder: true,
101 queuedRuns: [
102 {
103 run: { id: "run:queued", userMessageId: "message:queued" },
104 text: "Queued with a screenshot",
105 attachments: [
106 {
107 type: "image",
108 id: "attachment-1",
109 name: "screenshot.png",
110 mimeType: "image/png",
111 sizeBytes: 128,
112 },
113 ],
114 },
115 ],
116 });
117​
118 it("renders an attachment thumbnail on the queued row", async () => {
119 state.projection = { projection: { messages: [] } };
120 state.workflow = workflowWithAttachment();
121​
122 const view = await render();
123​
124 expect(view.querySelector("img")?.getAttribute("src")).toBe("https://assets.test/attachment-1");
125 expect(view.textContent).toContain("Queued with a screenshot");
126 expect(labels(view)).toContain("Edit queued message");
127 expect(labels(view)).toContain("Reorder queued message (drag, or press the arrow keys)");
128 expect(labels(view)).not.toContain("Move queued message up");
129 });
130​
131 it("drops the optimistic pending row once the projection holds its message", async () => {
132 state.projection = {
133 projection: { messages: [{ id: "message:acknowledged", text: "hello" }] },
134 };
135 state.workflow = {
136 activeRun: { id: "run:active" },
137 canPromoteToSteer: true,
138 canReorder: true,
139 queuedRuns: [],
140 };
141​
142 const view = await render({
143 optimisticMessages: [
144 {
145 id: "message:acknowledged" as never,
146 inputIntent: "queued_turn",
147 text: "hello",
148 attachments: [],
149 },
150 ],
151 });
152​
153 expect(view.childElementCount).toBe(0);
154 });
155​
156 it("keeps the original queued message visible while editing", async () => {
157 state.projection = { projection: { messages: [] } };
158 state.workflow = workflowWithAttachment();
159​
160 const view = await render({ editingRunId: "run:queued" as never });
161​
162 expect(view.textContent).toContain("Queued with a screenshot");
163 });
164});