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

ThreadWorkflowsPanel.test.tsx

200 lines6.1 KB

1import { act, cloneElement, type ReactElement } from "react";
2import { create, type ReactTestRenderer } from "react-test-renderer";
3import { EnvironmentId, ThreadId } from "@t3tools/contracts";
4import * as DateTime from "effect/DateTime";
5import { afterEach, expect, it, vi } from "vite-plus/test";
6​
7const state = vi.hoisted(() => ({
8 projection: null as unknown,
9}));
10​
11vi.mock("../../state/entities", () => ({
12 useThreadProjection: () => (state.projection === null ? null : { projection: state.projection }),
13}));
14​
15import { ThreadWorkflowsPanel } from "./ThreadWorkflowsPanel";
16​
17let renderer: ReactTestRenderer;
18​
19afterEach(async () => {
20 await act(async () => renderer?.unmount());
21 state.projection = null;
22});
23​
24const startedAt = DateTime.makeUnsafe("2026-09-16T12:00:00Z");
25const updatedAt = DateTime.makeUnsafe("2026-09-16T12:01:00Z");
26const completedAt = DateTime.makeUnsafe("2026-09-16T12:02:00Z");
27​
28const workflowBlock = {
29 kind: "workflow" as const,
30 workflowName: "pi_workflow_phase_pending_fix",
31 phases: [
32 { index: 0, title: "Plan" },
33 { index: 1, title: "Implement" },
34 { index: 2, title: "Verify" },
35 ],
36 phaseIndex: null,
37 phaseTitle: null,
38 agentIndex: null,
39 runId: "pi_workflow_phase_pending_fix-mu5n6tcx-i253rw",
40};
41​
42const coordinator = {
43 id: "coordinator",
44 title: null,
45 prompt: "Run the workflow",
46 model: null,
47 status: "running" as const,
48 result: null,
49 startedAt,
50 completedAt: null,
51 updatedAt,
52 workflow: workflowBlock,
53};
54​
55function member(input: {
56 readonly id: string;
57 readonly title: string;
58 readonly status: "pending" | "running" | "completed" | "failed";
59 readonly phaseIndex: number | null;
60 readonly agentIndex?: number;
61}) {
62 return {
63 id: input.id,
64 title: input.title,
65 prompt: input.title,
66 model: null,
67 status: input.status,
68 result: null,
69 parentNodeId: "coordinator",
70 startedAt,
71 completedAt: input.status === "completed" || input.status === "failed" ? completedAt : null,
72 updatedAt,
73 workflow: {
74 ...workflowBlock,
75 kind: "workflow_agent" as const,
76 phaseIndex: input.phaseIndex,
77 phaseTitle:
78 input.phaseIndex === null ? null : (workflowBlock.phases[input.phaseIndex]?.title ?? null),
79 agentIndex: input.agentIndex ?? 0,
80 },
81 };
83​
84const panel = (
85 <ThreadWorkflowsPanel
86 environmentId={EnvironmentId.make("test")}
87 threadId={ThreadId.make("thread")}
88 />
89);
90​
91async function render(projection: unknown) {
92 state.projection = projection;
93 await act(async () => {
94 renderer = create(panel);
95 });
96 return () =>
97 renderer.root
98 .findAll((node) => typeof node.type === "string")
99 .flatMap((node) => node.children.filter((child) => typeof child === "string"))
100 .join(" ")
101 .replace(/\s+/g, " ");
103​
104it("renders the workflow name with its run suffix, phases, and members", async () => {
105 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
106 const text = await render({
107 subagents: [
108 coordinator,
109 member({ id: "plan", title: "Plan the fix", status: "completed", phaseIndex: 0 }),
110 member({ id: "implement", title: "Implement the fix", status: "running", phaseIndex: 1 }),
111 ],
112 });
113 expect(text()).toContain("Workflows · 1");
114 expect(text()).toContain("pi_workflow_phase_pending_fix · mu5n6tcx-i253rw");
115 expect(text()).toContain("Plan");
116 expect(text()).toContain("Implement");
117 expect(text()).toContain("Verify");
118 expect(text()).toContain("Plan the fix");
119 expect(text()).toContain("Implement the fix");
120});
121​
122it("keeps an unreached phase pending while the run is live, then marks it not reached", async () => {
123 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
124 const projection = {
125 subagents: [
126 coordinator,
127 member({ id: "plan", title: "Plan the fix", status: "completed", phaseIndex: 0 }),
128 member({ id: "implement", title: "Implement the fix", status: "running", phaseIndex: 1 }),
129 ],
130 };
131 const text = await render(projection);
132 expect(text()).toContain("Waiting to start");
133 expect(text()).not.toContain("Not reached");
134 expect(text()).not.toContain("not reached");
135​
136 state.projection = {
137 subagents: [
138 { ...coordinator, status: "completed", completedAt },
139 member({ id: "plan", title: "Plan the fix", status: "completed", phaseIndex: 0 }),
140 member({ id: "implement", title: "Implement the fix", status: "completed", phaseIndex: 1 }),
141 ],
142 };
143 await act(async () => renderer.update(cloneElement(panel)));
144 expect(text()).toContain("not reached");
145 expect(text()).toContain("Not reached");
146 expect(text()).not.toContain("Waiting to start");
147});
148​
149it("never paints a terminal state on a live run's unanswered phase", async () => {
150 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
151 const text = await render({
152 subagents: [
153 { ...coordinator, status: "pending", startedAt: null },
154 member({ id: "plan", title: "Plan the fix", status: "pending", phaseIndex: 0 }),
155 ],
156 });
157 expect(text()).toContain("Plan the fix");
158 expect(text()).toContain("Waiting to start");
159 expect(text()).not.toContain("not reached");
160 expect(text()).not.toContain("Not reached");
161});
162​
163it("shows members whose phase is unknown instead of dropping them", async () => {
164 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
165 const text = await render({
166 subagents: [
167 coordinator,
168 member({ id: "orphan", title: "Unknown phase worker", status: "running", phaseIndex: 7 }),
169 ],
170 });
171 expect(text()).toContain("Unphased");
172 expect(text()).toContain("Unknown phase worker");
173});
174​
175it("renders nothing for a thread without workflow subagents", async () => {
176 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
177 const text = await render({
178 subagents: [
179 {
180 id: "plain",
181 title: "Plain subagent",
182 prompt: "Do work",
183 model: null,
184 status: "running",
185 result: null,
186 startedAt,
187 completedAt: null,
188 updatedAt,
189 },
190 ],
191 });
192 expect(text()).not.toContain("Workflows");
193 expect(text()).not.toContain("Plain subagent");
194});
195​
196it("renders nothing without a projection", async () => {
197 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
198 await render(null);
199 expect(renderer.toJSON()).toBeNull();
200});