Open History to see the last commit
1import {
2 EventId,
3 ProviderInstanceId,
4 ThreadId,
5 TurnId,
6 type OrchestrationLatestTurn,
7 type OrchestrationSession,
8 type OrchestrationThreadActivity,
9} from "@v12/contracts";
10import { renderToStaticMarkup } from "react-dom/server";
11import { describe, expect, it } from "vite-plus/test";
12
13import {
14 deriveOpenThreadActivityPresentation,
15 formatOpenThreadActivityElapsed,
16 OpenThreadActivityStatus,
17} from "./OpenThreadActivityStatus";
18
19const latestTurn: OrchestrationLatestTurn = {
20 turnId: TurnId.make("turn-1"),
21 state: "running",
22 requestedAt: "2026-07-15T00:00:00.000Z",
23 startedAt: "2026-07-15T00:00:01.000Z",
24 completedAt: null,
25 assistantMessageId: null,
26};
27
28const runningSession: OrchestrationSession = {
29 threadId: ThreadId.make("thread-1"),
30 status: "running",
31 providerName: "Codex",
32 providerInstanceId: ProviderInstanceId.make("codex"),
33 runtimeMode: "full-access",
34 activeTurnId: TurnId.make("turn-1"),
35 lastError: null,
36 updatedAt: "2026-07-15T00:00:01.000Z",
37};
38
39const activity: OrchestrationThreadActivity = {
40 id: EventId.make("activity-1"),
41 tone: "tool",
42 kind: "tool.started",
43 summary: "Running typecheck",
44 payload: {},
45 turnId: TurnId.make("turn-1"),
46 sequence: 3,
47 createdAt: "2026-07-15T00:00:03.000Z",
48};
49
50function derive(
51 overrides: Partial<Parameters<typeof deriveOpenThreadActivityPresentation>[0]> = {},
52) {
53 return deriveOpenThreadActivityPresentation({
54 session: runningSession,
55 latestTurn,
56 activities: [activity],
57 queued: false,
58 running: true,
59 activeWorkStartedAt: latestTurn.startedAt,
60 pendingApprovalCreatedAt: null,
61 pendingUserInputCreatedAt: null,
62 error: null,
63 ...overrides,
64 });
65}
66
67describe("deriveOpenThreadActivityPresentation", () => {
68 it("shows running work with its latest useful activity", () => {
69 expect(derive()).toMatchObject({
70 phase: "running",
71 label: "Running",
72 startedAt: "2026-07-15T00:00:01.000Z",
73 latestActivity: "Running typecheck",
74 });
75 });
76
77 it("puts blocking user states ahead of the underlying running session", () => {
78 expect(derive({ pendingApprovalCreatedAt: "2026-07-15T00:00:05.000Z" })).toMatchObject({
79 phase: "waiting_for_approval",
80 label: "Waiting for approval",
81 });
82 expect(derive({ pendingUserInputCreatedAt: "2026-07-15T00:00:06.000Z" })).toMatchObject({
83 phase: "waiting_for_input",
84 label: "Waiting for input",
85 });
86 });
87
88 it("shows local dispatch as queued until the server acknowledges it", () => {
89 expect(derive({ session: null, queued: true, running: false })).toMatchObject({
90 phase: "queued",
91 label: "Queued",
92 });
93 });
94
95 it("clears a stale running turn after a reconnect snapshot confirms no session", () => {
96 expect(derive({ session: null, running: false })).toBeNull();
97 });
98
99 it("uses a settled session when a quick run has no materialized latest turn", () => {
100 expect(
101 derive({
102 session: { ...runningSession, status: "ready", activeTurnId: null },
103 latestTurn: null,
104 running: false,
105 }),
106 ).toMatchObject({ phase: "complete", label: "Complete" });
107 });
108
109 it("lets a settled reconnect snapshot clear stale blockers and local work", () => {
110 expect(
111 derive({
112 session: { ...runningSession, status: "ready", activeTurnId: null },
113 pendingApprovalCreatedAt: "2026-07-15T00:00:05.000Z",
114 queued: true,
115 }),
116 ).toMatchObject({ phase: "complete", label: "Complete" });
117
118 expect(
119 derive({
120 session: { ...runningSession, status: "stopped", activeTurnId: null },
121 pendingUserInputCreatedAt: "2026-07-15T00:00:06.000Z",
122 running: true,
123 }),
124 ).toBeNull();
125 });
126
127 it("lets a failed server snapshot clear a stale waiting state", () => {
128 expect(
129 derive({
130 session: {
131 ...runningSession,
132 status: "error",
133 activeTurnId: null,
134 lastError: "Provider exited",
135 },
136 pendingApprovalCreatedAt: "2026-07-15T00:00:05.000Z",
137 }),
138 ).toMatchObject({ phase: "failed", label: "Failed" });
139 });
140
141 it("shows failures and settled completion", () => {
142 expect(derive({ error: "Provider exited" })).toMatchObject({
143 phase: "failed",
144 label: "Failed",
145 });
146 expect(
147 derive({
148 session: { ...runningSession, status: "ready", activeTurnId: null },
149 latestTurn: {
150 ...latestTurn,
151 state: "completed",
152 completedAt: "2026-07-15T00:02:01.000Z",
153 },
154 running: false,
155 }),
156 ).toMatchObject({ phase: "complete", label: "Complete" });
157 });
158});
159
160describe("formatOpenThreadActivityElapsed", () => {
161 it("formats live and completed durations", () => {
162 expect(
163 formatOpenThreadActivityElapsed({
164 startedAt: "2026-07-15T00:00:00.000Z",
165 endedAt: null,
166 nowMs: Date.parse("2026-07-15T00:01:05.000Z"),
167 }),
168 ).toBe("1m 05s");
169 expect(
170 formatOpenThreadActivityElapsed({
171 startedAt: "2026-07-15T00:00:00.000Z",
172 endedAt: "2026-07-15T01:02:00.000Z",
173 nowMs: 0,
174 }),
175 ).toBe("1h 02m");
176 });
177});
178
179describe("OpenThreadActivityStatus", () => {
180 it("renders an accessible quiet checkout-toolbar status", () => {
181 const markup = renderToStaticMarkup(
182 <OpenThreadActivityStatus status={derive()} variant="toolbar" />,
183 );
184
185 expect(markup).toContain('role="status"');
186 expect(markup).toContain('aria-label="Running. Running typecheck"');
187 expect(markup).toContain("Running");
188 expect(markup).not.toContain(">Running typecheck<");
189 expect(markup).toContain('title="Running typecheck"');
190 expect(markup).not.toContain("rounded-full");
191 });
192});