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

turnItemPresentation.test.ts

112 lines3.7 KB

1import {
2 ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
3 RunId,
4 ThreadId,
5 TurnItemId,
6 type OrchestrationV2Run,
7 type OrchestrationV2TurnItem,
8} from "@t3tools/contracts";
9import * as DateTime from "effect/DateTime";
10import { describe, expect, it } from "vite-plus/test";
11​
12import {
13 turnItemIsWorkspacePreparation,
14 workspacePreparationRetryRunIds,
15} from "./turnItemPresentation.ts";
16​
17function command(input: string): OrchestrationV2TurnItem {
18 const now = DateTime.makeUnsafe("2026-08-03T00:00:00.000Z");
19 return {
20 id: TurnItemId.make("item-command"),
21 threadId: ThreadId.make("thread-1"),
22 runId: RunId.make("run-1"),
23 nodeId: null,
24 providerThreadId: null,
25 providerTurnId: null,
26 nativeItemRef: null,
27 parentItemId: null,
28 ordinal: 1,
29 status: "completed",
30 title: "Workspace ready",
31 startedAt: now,
32 completedAt: now,
33 updatedAt: now,
34 type: "command_execution",
35 input,
36 output: "Workspace preparation completed.",
37 exitCode: 0,
38 };
40​
41function preparationFailure(
42 status: OrchestrationV2TurnItem["status"],
43 code: string | null = ORCHESTRATION_V2_WORKSPACE_PREPARATION_FAILURE_CODE,
44): OrchestrationV2TurnItem {
45 const {
46 input: _input,
47 output: _output,
48 exitCode: _exitCode,
49 ...base
50 } = command("") as Extract<OrchestrationV2TurnItem, { type: "command_execution" }>;
51 return {
52 ...base,
53 id: TurnItemId.make("item-error"),
54 status,
55 title: "Workspace preparation failed",
56 type: "error",
57 failure: { class: "validation_error", message: "fetch failed", code, retryable: false },
58 };
60​
61const worktree = { type: "worktree", baseRef: "main" } as const;
62function run(
63 status: OrchestrationV2Run["status"],
64 workspacePreparation?: typeof worktree,
65): Pick<OrchestrationV2Run, "id" | "status" | "startedAt" | "workspacePreparation"> {
66 return {
67 id: RunId.make("run-1"),
68 status,
69 startedAt: null,
70 ...(workspacePreparation === undefined ? {} : { workspacePreparation }),
71 };
73​
74describe("turnItemIsWorkspacePreparation", () => {
75 it("hides a preparation failure only once a retry cancelled it", () => {
76 expect(turnItemIsWorkspacePreparation(preparationFailure("failed"))).toBe(false);
77 expect(turnItemIsWorkspacePreparation(preparationFailure("cancelled"))).toBe(true);
78 expect(turnItemIsWorkspacePreparation(preparationFailure("cancelled", null))).toBe(false);
79 });
80});
81​
82describe("workspacePreparationRetryRunIds", () => {
83 it("offers a retry for an interrupted checkout before the provider starts", () => {
84 const stopped = run("interrupted", worktree);
85 const interrupted = { ...command("Preparing workspace"), status: "interrupted" as const };
86 expect([...workspacePreparationRetryRunIds([stopped], [interrupted])]).toEqual([stopped.id]);
87 expect(
88 workspacePreparationRetryRunIds(
89 [{ ...stopped, startedAt: DateTime.makeUnsafe("2026-08-03T00:00:00.000Z") }],
90 [interrupted],
91 ).size,
92 ).toBe(0);
93 expect(workspacePreparationRetryRunIds([stopped], [command("Preparing workspace")]).size).toBe(
94 0,
95 );
96 });
97​
98 it("offers a retry while the run still ends in its failed preparation", () => {
99 const failure = [preparationFailure("failed")];
100 expect([...workspacePreparationRetryRunIds([run("failed", worktree)], failure)]).toEqual([
101 RunId.make("run-1"),
102 ]);
103 expect(workspacePreparationRetryRunIds([run("preparing", worktree)], failure).size).toBe(0);
104 expect(workspacePreparationRetryRunIds([run("failed")], failure).size).toBe(0);
105 expect(
106 workspacePreparationRetryRunIds(
107 [run("failed", worktree)],
108 [preparationFailure("failed", "provider_crashed")],
109 ).size,
110 ).toBe(0);
111 });
112});