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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166import { describe, expect, expectTypeOf, it } from "@effect/vitest";import { LandedSha } from "@repo/protocol/ids";import { StaleBase } from "@repo/protocol/refusal";import type { WorkflowStep } from "cloudflare:workers";import { DateTime, Effect, Option, Ref, Schema } from "effect";
import { memoSteps, StepRunner, workflowSteps } from "../src/workflows/step.ts";import type { WorkflowStepRetries, WorkflowSteps,} from "../src/workflows/step.ts";
const sha = LandedSha.make("a".repeat(40));
const Landing = Schema.Struct({ at: Schema.DateTimeUtcFromString, sha: Schema.OptionFromOptionalKey(LandedSha),});
const landing = { at: DateTime.makeUnsafe("2026-10-08T12:00:00Z"), sha: Option.some(sha),};
const fakeWorkflowStep = () => { const recorded = new Map<string, Promise<Schema.Json>>(); const retries: WorkflowStepRetries[] = [];
const steps: WorkflowSteps = { do: (name, config, body) => { if (config.retries !== undefined) { retries.push(config.retries); }
const kept = recorded.get(name) ?? body();
recorded.set(name, kept);
return kept; }, };
const record = (name: string) => Effect.promise(() => recorded.get(name) ?? Promise.resolve(null));
return { record, retries, steps };};
const counted = (runs: Ref.Ref<number>) => Effect.as( Ref.update(runs, (n) => n + 1), landing );
describe(StepRunner, () => { it("runs over a Workflow's own steps", () => { expectTypeOf<WorkflowStep>().toExtend<WorkflowSteps>(); });
it.effect("records a step's value encoded, and decodes it back", () => Effect.gen(function* () { const fake = fakeWorkflowStep(); const runs = yield* Ref.make(0);
const value = yield* StepRunner.use(({ step }) => step("land#1", Landing, counted(runs)) ).pipe(Effect.provide(workflowSteps(fake.steps)));
expect(value).toStrictEqual(landing); expect(yield* fake.record("land#1")).toStrictEqual({ at: "2026-10-08T12:00:00.000Z", sha, }); }) );
it.effect("returns a replayed step's record without running it again", () => Effect.gen(function* () { const fake = fakeWorkflowStep(); const runs = yield* Ref.make(0);
const run = StepRunner.use(({ step }) => step("land#1", Landing, counted(runs)) ).pipe(Effect.provide(workflowSteps(fake.steps)));
expect([yield* run, yield* run]).toStrictEqual([landing, landing]); expect(yield* Ref.get(runs)).toBe(1); }) );
it.effect("hands the engine a step's retries", () => Effect.gen(function* () { const fake = fakeWorkflowStep(); const retry = { backoff: "exponential", delayMs: 500, limit: 3 } as const;
yield* StepRunner.use(({ step }) => step("merge#1", Landing, Effect.succeed(landing), retry) ).pipe(Effect.provide(workflowSteps(fake.steps)));
expect(fake.retries).toStrictEqual([ { backoff: "exponential", delay: 500, limit: 3 }, ]); }) );
it.effect("fails with a step's own refusal once the engine gives up", () => Effect.gen(function* () { const fake = fakeWorkflowStep(); const stale = new StaleBase({ base: sha, conflicts: [], main: sha });
const failure = yield* StepRunner.use(({ step }) => step("land#1", Landing, Effect.fail(stale)) ).pipe(Effect.provide(workflowSteps(fake.steps)), Effect.flip);
expect(failure).toStrictEqual(stale); }) );});
describe(memoSteps, () => { it.effect("returns the recorded value on a second run", () => Effect.gen(function* () { const steps = yield* memoSteps(); const runs = yield* Ref.make(0);
const run = StepRunner.use(({ step }) => step("land#1", Landing, counted(runs)) ).pipe(Effect.provide(steps));
expect([yield* run, yield* run]).toStrictEqual([landing, landing]); expect(yield* Ref.get(runs)).toBe(1); }) );
it.effect( "keeps a step that failed past its retries failed: a replay does not run it again, and dies as the engine's does", () => Effect.gen(function* () { const steps = yield* memoSteps(); const runs = yield* Ref.make(0); const stale = new StaleBase({ base: sha, conflicts: [], main: sha });
const run = StepRunner.use(({ step }) => step( "land#1", Landing, Effect.andThen( Ref.update(runs, (n) => n + 1), Effect.fail(stale) ), { backoff: "constant", delayMs: 0, limit: 1 } ) ).pipe( Effect.provide(steps), Effect.flip, Effect.catchDefect(() => Effect.succeed("died")) );
expect({ runs: [yield* run, yield* run], tries: yield* Ref.get(runs), }).toStrictEqual({ runs: [stale, "died"], tries: 2 }); }) );});