|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Context, Effect, Layer, Predicate } from "effect"; |
| 3 | +import { HttpRouter, HttpServer } from "effect/unstable/http"; |
| 4 | +import { HttpApi, HttpApiBuilder } from "effect/unstable/httpapi"; |
| 5 | + |
| 6 | +import type { Executor } from "@executor-js/sdk"; |
| 7 | + |
| 8 | +import { ExecutionsApi } from "../executions/api"; |
| 9 | +import { ExecutionsHandlers } from "./executions"; |
| 10 | +import { ExecutionEngineService, ExecutorService } from "../services"; |
| 11 | + |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | +// Focused tests — spec execution-tombstones, AC4 (resume-time surface). |
| 14 | +// |
| 15 | +// When the daemon restarts, the paused fiber is gone. A resume must NOT read |
| 16 | +// as a generic "approval expired": if a tombstone exists for the execution |
| 17 | +// (written before the restart), the resume surfaces the honest |
| 18 | +// "interrupted — re-trigger" outcome (InterruptedExecutionError). |
| 19 | +// --------------------------------------------------------------------------- |
| 20 | + |
| 21 | +const stubExecutor = (record: { executionId: string; status: string } | null): Executor => |
| 22 | + // oxlint-disable-next-line executor/no-double-cast -- minimal executor double: executionRecords.get and pendingApprovals.consume are exercised |
| 23 | + ({ |
| 24 | + executionRecords: { |
| 25 | + get: () => Effect.succeed(record), |
| 26 | + put: () => Effect.void, |
| 27 | + sweepInterrupted: () => Effect.succeed({ interrupted: 0 }), |
| 28 | + }, |
| 29 | + // resumeFromPendingApproval consumes a stored approval before reaching |
| 30 | + // the tombstone check; absent approvals are the restart scenario. |
| 31 | + pendingApprovals: { |
| 32 | + consume: () => Effect.succeed(null), |
| 33 | + discard: () => Effect.void, |
| 34 | + put: () => Effect.void, |
| 35 | + }, |
| 36 | + }) as unknown as Executor; |
| 37 | + |
| 38 | +// The engine remembers nothing (fresh process): live resume returns null, and |
| 39 | +// there is no pending-approval record — this is the restart scenario. |
| 40 | +// oxlint-disable-next-line executor/no-double-cast -- minimal engine double: only resume's null return (fresh process) is exercised |
| 41 | +const emptyEngine = { |
| 42 | + resume: () => Effect.succeed(null), |
| 43 | +} as unknown as ExecutionEngineService["Service"]; |
| 44 | + |
| 45 | +const runResume = (executor: Executor) => { |
| 46 | + const handler = HttpRouter.toWebHandler( |
| 47 | + HttpApiBuilder.layer(HttpApi.make("executor").add(ExecutionsApi)).pipe( |
| 48 | + Layer.provide(ExecutionsHandlers), |
| 49 | + Layer.provide(Layer.succeed(ExecutorService)(executor)), |
| 50 | + Layer.provide(Layer.succeed(ExecutionEngineService)(emptyEngine)), |
| 51 | + Layer.provideMerge(HttpServer.layerServices), |
| 52 | + Layer.provideMerge(Layer.succeed(HttpRouter.RouterConfig)({ maxParamLength: 1000 })), |
| 53 | + ), |
| 54 | + { disableLogger: false }, |
| 55 | + ).handler; |
| 56 | + // The handler's inferred type demands a Context<ExecutorService | |
| 57 | + // ExecutionEngineService> second argument at the type level (a beta.59 |
| 58 | + // inference quirk of toWebHandler's ReqR) even though the layer above |
| 59 | + // provides both at runtime. Pass the runtime-provided context explicitly. |
| 60 | + // The handler's inferred type demands a Context<ExecutorService | |
| 61 | + // ExecutionEngineService> second argument at the type level (a beta.59 |
| 62 | + // inference quirk of toWebHandler's ReqR) even though the layer above |
| 63 | + // provides both at runtime. Passing the real services explicitly also |
| 64 | + // satisfies the runtime — the stubs here are self-sufficient. |
| 65 | + const context = Context.make(ExecutorService, executor).pipe( |
| 66 | + Context.add(ExecutionEngineService, emptyEngine), |
| 67 | + ); |
| 68 | + return handler( |
| 69 | + new Request("https://executor.test/executions/exec_1/resume", { |
| 70 | + method: "POST", |
| 71 | + headers: { "content-type": "application/json" }, |
| 72 | + body: JSON.stringify({ action: "accept" }), |
| 73 | + }), |
| 74 | + context, |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +describe("resume after daemon restart (tombstone path)", () => { |
| 79 | + it("surfaces interrupted (404 + InterruptedExecutionError) when a tombstone exists", async () => { |
| 80 | + const res = await runResume(stubExecutor({ executionId: "exec_1", status: "interrupted" })); |
| 81 | + expect(res.status).toBe(404); |
| 82 | + const body = (await res.json()) as { _tag?: string; executionId?: string }; |
| 83 | + expect(Predicate.isTagged(body, "InterruptedExecutionError")).toBe(true); |
| 84 | + expect(body.executionId).toBe("exec_1"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("surfaces interrupted for a stale paused tombstone (sweep missed it — not a live execution)", async () => { |
| 88 | + const res = await runResume(stubExecutor({ executionId: "exec_1", status: "paused" })); |
| 89 | + expect(res.status).toBe(404); |
| 90 | + expect(JSON.stringify(await res.json())).toContain("InterruptedExecutionError"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("falls through to approval-expired when no tombstone exists", async () => { |
| 94 | + const res = await runResume(stubExecutor(null)); |
| 95 | + // ApprovalExpiredError is annotated httpApiStatus: 410 (Gone). |
| 96 | + expect(res.status).toBe(410); |
| 97 | + expect(JSON.stringify(await res.json())).toContain("ApprovalExpiredError"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("completed tombstones do not resurrect (completed is immutable)", async () => { |
| 101 | + const res = await runResume(stubExecutor({ executionId: "exec_1", status: "completed" })); |
| 102 | + // No tombstone hit for completed (immutable) — falls through to expired (410). |
| 103 | + expect(res.status).toBe(410); |
| 104 | + expect(JSON.stringify(await res.json())).toContain("ApprovalExpiredError"); |
| 105 | + }); |
| 106 | +}); |
0 commit comments