Open History to see the last commit
1import {
2 EnvironmentResourceNotFoundError,
3 OrchestrationDispatchCommandError,
4 OrchestrationGetSnapshotError,
5 OrchestrationThreadNotFoundError,
6 ThreadId,
7} from "@t3tools/contracts";
8import * as Cause from "effect/Cause";
9import { describe, expect, it } from "vite-plus/test";
10
11import {
12 findHttpThreadNotFoundError,
13 hasTerminalThreadNotFoundFailure,
14 isTerminalThreadNotFoundError,
15 wasBootstrapThreadDeleted,
16 wasBootstrapThreadNotCreated,
17} from "./orchestration.ts";
18
19const THREAD_ID = ThreadId.make("thread-1");
20const httpThreadNotFound = new EnvironmentResourceNotFoundError({
21 code: "not_found",
22 reason: "thread_not_found",
23 traceId: "trace-thread-not-found",
24});
25const socketThreadNotFound = new OrchestrationThreadNotFoundError({ threadId: THREAD_ID });
26
27describe("terminal thread-not-found classification", () => {
28 it("recognizes the authoritative HTTP and WebSocket errors", () => {
29 expect(isTerminalThreadNotFoundError(httpThreadNotFound)).toBe(true);
30 expect(isTerminalThreadNotFoundError(socketThreadNotFound)).toBe(true);
31 expect(
32 isTerminalThreadNotFoundError(
33 new OrchestrationGetSnapshotError({
34 message: `Thread ${THREAD_ID} was not found`,
35 cause: THREAD_ID,
36 }),
37 ),
38 ).toBe(false);
39 expect(isTerminalThreadNotFoundError(new Error("connection lost"))).toBe(false);
40 });
41
42 it("finds the HTTP error anywhere in a combined cause", () => {
43 const cause = Cause.combine(Cause.fail(socketThreadNotFound), Cause.fail(httpThreadNotFound));
44
45 expect(findHttpThreadNotFoundError(cause)).toBe(httpThreadNotFound);
46 expect(findHttpThreadNotFoundError(Cause.fail(socketThreadNotFound))).toBeUndefined();
47 });
48
49 it("treats either transport error as terminal without accepting defects", () => {
50 expect(hasTerminalThreadNotFoundFailure(Cause.fail(httpThreadNotFound))).toBe(true);
51 expect(hasTerminalThreadNotFoundFailure(Cause.fail(socketThreadNotFound))).toBe(true);
52 expect(hasTerminalThreadNotFoundFailure(Cause.die(socketThreadNotFound))).toBe(false);
53 });
54});
55
56describe("wasBootstrapThreadDeleted", () => {
57 it("accepts only a confirmed deleted bootstrap thread", () => {
58 expect(
59 wasBootstrapThreadDeleted(
60 new OrchestrationDispatchCommandError({
61 message: "Failed to create worktree.",
62 bootstrapThreadDisposition: "deleted",
63 }),
64 ),
65 ).toBe(true);
66 expect(
67 wasBootstrapThreadDeleted(
68 new OrchestrationDispatchCommandError({ message: "Failed to create worktree." }),
69 ),
70 ).toBe(false);
71 expect(wasBootstrapThreadDeleted(new Error("connection lost"))).toBe(false);
72 });
73});
74
75describe("wasBootstrapThreadNotCreated", () => {
76 it("accepts only a confirmed never-created bootstrap thread", () => {
77 const notCreated = new OrchestrationDispatchCommandError({
78 message: "A separate worktree requires a base commit.",
79 bootstrapThreadDisposition: "not-created",
80 });
81 expect(wasBootstrapThreadNotCreated(notCreated)).toBe(true);
82 expect(wasBootstrapThreadDeleted(notCreated)).toBe(false);
83 expect(
84 wasBootstrapThreadNotCreated(
85 new OrchestrationDispatchCommandError({
86 message: "Failed to create worktree.",
87 bootstrapThreadDisposition: "deleted",
88 }),
89 ),
90 ).toBe(false);
91 expect(
92 wasBootstrapThreadNotCreated(
93 new OrchestrationDispatchCommandError({
94 message: "Failed to create worktree.",
95 }),
96 ),
97 ).toBe(false);
98 expect(wasBootstrapThreadNotCreated(new Error("connection lost"))).toBe(false);
99 });
100});