step.test.ts
This file
Path
apps/platform/test/step.test.ts
165 lines · 4.8 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–165why, one step back1import { describe, expect, expectTypeOf, it } from "@effect/vitest";2import { LandedSha } from "@repo/protocol/ids";3import { StaleBase } from "@repo/protocol/refusal";4import type { WorkflowStep } from "cloudflare:workers";5import { DateTime, Effect, Option, Ref, Schema } from "effect";67import { memoSteps, StepRunner, workflowSteps } from "../src/workflows/step.ts";8import type {9 WorkflowStepRetries,10 WorkflowSteps,11} from "../src/workflows/step.ts";1213const sha = LandedSha.make("a".repeat(40));1415const Landing = Schema.Struct({16 at: Schema.DateTimeUtcFromString,17 sha: Schema.OptionFromOptionalKey(LandedSha),18});1920const landing = {21 at: DateTime.makeUnsafe("2026-10-08T12:00:00Z"),22 sha: Option.some(sha),23};2425const fakeWorkflowStep = () => {26 const recorded = new Map<string, Promise<Schema.Json>>();27 const retries: WorkflowStepRetries[] = [];2829 const steps: WorkflowSteps = {30 do: (name, config, body) => {31 if (config.retries !== undefined) {32 retries.push(config.retries);33 }3435 const kept = recorded.get(name) ?? body();3637 recorded.set(name, kept);3839 return kept;40 },41 };4243 const record = (name: string) =>44 Effect.promise(() => recorded.get(name) ?? Promise.resolve(null));4546 return { record, retries, steps };47};4849const counted = (runs: Ref.Ref<number>) =>50 Effect.as(51 Ref.update(runs, (n) => n + 1),52 landing53 );5455describe(StepRunner, () => {56 it("runs over a Workflow's own steps", () => {57 expectTypeOf<WorkflowStep>().toExtend<WorkflowSteps>();58 });5960 it.effect("records a step's value encoded, and decodes it back", () =>61 Effect.gen(function* () {62 const fake = fakeWorkflowStep();63 const runs = yield* Ref.make(0);6465 const value = yield* StepRunner.use(({ step }) =>66 step("land#1", Landing, counted(runs))67 ).pipe(Effect.provide(workflowSteps(fake.steps)));6869 expect(value).toStrictEqual(landing);70 expect(yield* fake.record("land#1")).toStrictEqual({71 at: "2026-10-08T12:00:00.000Z",72 sha,73 });74 })75 );7677 it.effect("returns a replayed step's record without running it again", () =>78 Effect.gen(function* () {79 const fake = fakeWorkflowStep();80 const runs = yield* Ref.make(0);8182 const run = StepRunner.use(({ step }) =>83 step("land#1", Landing, counted(runs))84 ).pipe(Effect.provide(workflowSteps(fake.steps)));8586 expect([yield* run, yield* run]).toStrictEqual([landing, landing]);87 expect(yield* Ref.get(runs)).toBe(1);88 })89 );9091 it.effect("hands the engine a step's retries", () =>92 Effect.gen(function* () {93 const fake = fakeWorkflowStep();94 const retry = { backoff: "exponential", delayMs: 500, limit: 3 } as const;9596 yield* StepRunner.use(({ step }) =>97 step("merge#1", Landing, Effect.succeed(landing), retry)98 ).pipe(Effect.provide(workflowSteps(fake.steps)));99100 expect(fake.retries).toStrictEqual([101 { backoff: "exponential", delay: 500, limit: 3 },102 ]);103 })104 );105106 it.effect("fails with a step's own refusal once the engine gives up", () =>107 Effect.gen(function* () {108 const fake = fakeWorkflowStep();109 const stale = new StaleBase({ base: sha, conflicts: [], main: sha });110111 const failure = yield* StepRunner.use(({ step }) =>112 step("land#1", Landing, Effect.fail(stale))113 ).pipe(Effect.provide(workflowSteps(fake.steps)), Effect.flip);114115 expect(failure).toStrictEqual(stale);116 })117 );118});119120describe(memoSteps, () => {121 it.effect("returns the recorded value on a second run", () =>122 Effect.gen(function* () {123 const steps = yield* memoSteps();124 const runs = yield* Ref.make(0);125126 const run = StepRunner.use(({ step }) =>127 step("land#1", Landing, counted(runs))128 ).pipe(Effect.provide(steps));129130 expect([yield* run, yield* run]).toStrictEqual([landing, landing]);131 expect(yield* Ref.get(runs)).toBe(1);132 })133 );134135 it.effect(136 "keeps a step that failed past its retries failed: a replay does not run it again, and dies as the engine's does",137 () =>138 Effect.gen(function* () {139 const steps = yield* memoSteps();140 const runs = yield* Ref.make(0);141 const stale = new StaleBase({ base: sha, conflicts: [], main: sha });142143 const run = StepRunner.use(({ step }) =>144 step(145 "land#1",146 Landing,147 Effect.andThen(148 Ref.update(runs, (n) => n + 1),149 Effect.fail(stale)150 ),151 { backoff: "constant", delayMs: 0, limit: 1 }152 )153 ).pipe(154 Effect.provide(steps),155 Effect.flip,156 Effect.catchDefect(() => Effect.succeed("died"))157 );158159 expect({160 runs: [yield* run, yield* run],161 tries: yield* Ref.get(runs),162 }).toStrictEqual({ runs: [stale, "died"], tries: 2 });163 })164 );165});