Skip to content
TypeScript
14.4 KB
Raw Blame
import * as Cloudflare from "@alchemy.run/cloudflare";
import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as workers from "@distilled.cloud/cloudflare/workers";
import * as workflows from "@distilled.cloud/cloudflare/workflows";
import * as Provider from "alchemy/Provider";
import * as Effect from "effect/Effect";
import * as HttpClient from "effect/http/HttpClient";
import { MinimumLogLevel } from "effect/References";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import LimitsWorkflowWorker from "./fixtures/workflow-limits/limits-worker.ts";
import { STEP_LIMIT } from "./fixtures/workflow-limits/limits-workflow.ts";
import ScheduledWorkflowWorker from "./fixtures/workflow-schedules/scheduled-worker.ts";
import { YEARLY_CRON } from "./fixtures/workflow-schedules/scheduled-workflow.ts";
import Stack from "./fixtures/workflow/stack.ts";
import WorkflowTestWorker from "./fixtures/workflow/workflow-worker.ts";

const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
  providers: Cloudflare.providers(),
});

const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");

const stack = beforeAll(
  deploy(Stack).pipe(
    // Let the freshly-deployed worker (and its Workflow binding) settle before
    // the first run so a step doesn't error mid-propagation.
    Effect.tap(Effect.sleep("5 seconds")),
  ),
);
afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));

interface WorkflowStatus {
  status: string;
  output?: {
    greeting: string;
    envBindingCount: number;
    workflowName: string;
    stepAttempt: number;
    instanceId: string;
  };
  error?: { message?: string } | null;
  rollback?: {
    outcome: "complete" | "failed";
    error: { message?: string } | null;
  } | null;
}

const isTerminal = (status: WorkflowStatus) =>
  status.status === "complete" || status.status === "errored" || status.status === "terminated";

const waitForStatus = (
  client: HttpClient.HttpClient,
  url: string,
  id: string,
  until: (status: WorkflowStatus) => boolean = isTerminal,
  times = 30,
) =>
  client.get(`${url}/workflow/status/${id}`).pipe(
    // The status endpoint transiently returns a 500 (HTML error page, not
    // JSON) while the freshly-deployed worker's Workflow binding is still
    // propagating. Only decode JSON on a 200; treat any other status as a
    // non-terminal "pending" so the poll keeps swinging instead of dying
    // on a JSON decode error.
    Effect.flatMap((res) =>
      res.status === 200
        ? res.json.pipe(Effect.map((json) => json as unknown as WorkflowStatus))
        : Effect.succeed({ status: "pending" } as WorkflowStatus),
    ),
    Effect.repeat({
      schedule: Schedule.spaced("2 seconds"),
      until,
      times,
    }),
  );

// Start a fresh workflow instance and poll until it reaches a terminal state.
// A transient `errored` during edge/binding propagation fails this effect so
// the caller can retry with a brand-new instance.
const runWorkflowToCompletion = (url: string) =>
  Effect.gen(function* () {
    const client = yield* HttpClient.HttpClient;

    // Cloudflare's edge takes a few seconds to start serving a fresh
    // workers.dev URL, so retry until it returns 200 (a fresh URL also
    // returns 404 transiently, which is not an HTTP error so Effect.retry
    // does not catch it unless we explicitly fail on non-200).
    const startRes = yield* client.post(`${url}/workflow/start/world`).pipe(
      Effect.flatMap((res) =>
        res.status === 200
          ? Effect.succeed(res)
          : Effect.fail(new Error(`Worker not ready: ${res.status}`)),
      ),
      Effect.retry({
        // Cap the exponential at 3s — uncapped, 15 retries grow past 30s of
        // sleep after only six attempts and blow the test timeout.
        schedule: Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("3 seconds")]),
        times: 15,
      }),
    );
    const { instanceId } = (yield* startRes.json) as { instanceId: string };
    expect(instanceId).toBeTypeOf("string");

    const lastStatus = yield* waitForStatus(client, url, instanceId);

    // Surface a non-complete terminal state as a failure so the outer retry
    // can take another swing (a fresh worker occasionally errors a step while
    // its bindings are still propagating).
    if (lastStatus.status !== "complete") {
      return yield* Effect.fail(
        new Error(`workflow ${lastStatus.status}: ${JSON.stringify(lastStatus.error)}`),
      );
    }
    return lastStatus;
  });

test(
  "deployed worker can run a workflow to completion",
  Effect.gen(function* () {
    const out = yield* stack;
    const url = out.url;
    expect(url).toBeTypeOf("string");

    const lastStatus = yield* runWorkflowToCompletion(url).pipe(
      Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 2 }),
    );

    expect(lastStatus.status).toBe("complete");
    expect(lastStatus.error).toBeFalsy();
    expect(lastStatus.output?.greeting).toBe("Hello, world!");
    // `event.workflowName` carries the account-global *physical* name, which
    // is derived from the host Worker's unique name (stack-id-stage prefix).
    expect(lastStatus.output?.workflowName).toMatch(/^workflowbindingstack-/);
    expect(lastStatus.output?.stepAttempt).toBe(1);
    expect(lastStatus.rollback).toBeNull();
    // The body yields `WorkerEnvironment` — if the regression from PR #71 ever
    // returns, the body dies on the first yield and `output` is undefined.
    expect(lastStatus.output?.envBindingCount).toBeGreaterThan(0);
  }).pipe(logLevel),
  {
    tags: [
      "provider:cloudflare",
      "provider:cloudflare:worker",
      "provider:cloudflare:workflow",
      "live",
    ],
    timeout: 180_000,
  },
);

test(
  "workflow can wait for and receive external events",
  Effect.gen(function* () {
    const { url } = yield* stack;
    const client = yield* HttpClient.HttpClient;

    const { instanceId, lastStatus } = yield* Effect.gen(function* () {
      const { instanceId } = yield* client.post(`${url}/workflow/wait/world`).pipe(
        Effect.flatMap((res) =>
          res.status === 200
            ? res.json.pipe(
                Effect.flatMap((body) => {
                  const instanceId = (body as { instanceId?: unknown }).instanceId;
                  return typeof instanceId === "string"
                    ? Effect.succeed({ instanceId })
                    : Effect.fail(new Error("Worker returned no workflow id"));
                }),
              )
            : Effect.fail(new Error(`Worker not ready: ${res.status}`)),
        ),
        Effect.retry({
          schedule: Schedule.exponential("500 millis"),
          times: 15,
        }),
      );

      // Cloudflare reports an instance parked in `waitForEvent` as `running`
      // (`waiting` is reserved for sleeps), so the wait step itself is not
      // observable through the coarse instance status. Wait for the instance
      // to start, then deliver the event.
      const runningStatus = yield* waitForStatus(
        client,
        url,
        instanceId,
        (status) => status.status === "running" || isTerminal(status),
      );
      if (runningStatus.status !== "running") {
        return yield* Effect.fail(
          new Error(`workflow ${runningStatus.status}: ${JSON.stringify(runningStatus.error)}`),
        );
      }

      // An event sent in the gap before the `waitForEvent` step registers can
      // be missed, so re-send until the workflow acknowledges it by reaching a
      // terminal status.
      const lastStatus = yield* Effect.gen(function* () {
        const sendRes = yield* client.post(`${url}/workflow/send/${instanceId}/external-ok`);
        if (sendRes.status !== 200) {
          return yield* Effect.fail(new Error(`sendEvent failed: ${sendRes.status}`));
        }
        const status = yield* waitForStatus(client, url, instanceId, isTerminal, 5);
        return isTerminal(status)
          ? status
          : yield* Effect.fail(new Error(`workflow still ${status.status}`));
      }).pipe(Effect.retry({ times: 3 }));
      return { instanceId, lastStatus };
    }).pipe(
      // A just-deployed workflow can error its first instance while the
      // binding propagates. Retry the scenario with a fresh instance.
      Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 2 }),
    );
    expect(lastStatus.status).toBe("complete");
    expect(lastStatus.error).toBeFalsy();
    expect(lastStatus.output?.greeting).toBe("external-ok");
    expect(lastStatus.output?.instanceId).toBe(instanceId);
  }).pipe(logLevel),
  {
    tags: [
      "provider:cloudflare",
      "provider:cloudflare:worker",
      "provider:cloudflare:workflow",
      "live",
    ],
    timeout: 180_000,
  },
);

// Canonical `list()` test (account collection): deploy the worker+workflow
// fixture (which creates a `WorkflowResource` named "TestWorkflow"), then
// enumerate every workflow in the account via the typed provider and assert the
// deployed one is present. Bracket with destroy so the test is isolated.
test.provider(
  "list enumerates the deployed workflow",
  (stack) =>
    Effect.gen(function* () {
      yield* stack.destroy();

      yield* stack.deploy(
        Effect.gen(function* () {
          return yield* WorkflowTestWorker;
        }),
      );

      const provider = yield* Provider.findProvider(Cloudflare.Workflows.WorkflowResource);
      const all = yield* provider.list();

      // Physical names are derived from the host Worker name (which carries
      // the stack-id-stage prefix), not the exported class name.
      expect(all.some((w) => w.workflowName.startsWith("workflowbindingstack-"))).toBe(true);

      yield* stack.destroy();
    }).pipe(logLevel),
  {
    tags: [
      "provider:cloudflare",
      "provider:cloudflare:worker",
      "provider:cloudflare:workflow",
      "live",
    ],
    timeout: 120_000,
  },
);

// ---------------------------------------------------------------------------
// Per-workflow limits: deploy a workflow declared with a step limit through the
// Effect-native form, then read it back out-of-band from the versions API (the
// only read that surfaces `limits`) to confirm it was applied.
// ---------------------------------------------------------------------------

// Physical workflow names are derived from the host Worker name and class, so
// read the name off the deployed binding rather than assuming the class name.
const readWorkflowName = (scriptName: string) =>
  Effect.gen(function* () {
    const { accountId } = yield* yield* CloudflareEnvironment;
    const settings = yield* workers.getScriptScriptAndVersionSetting({
      accountId,
      scriptName,
    });
    const binding = (settings.bindings ?? []).find(
      (b): b is Extract<typeof b, { type: "workflow" }> => b.type === "workflow",
    );
    return binding === undefined
      ? yield* Effect.fail(new Error(`no workflow binding on '${scriptName}'`))
      : binding.workflowName;
  });

// Read the applied step limit out-of-band via the versions API, retrying until
// it propagates (bounded, so a missing limit fails fast).
const waitForAppliedStepLimit = (workflowName: string, expected: number) =>
  Effect.gen(function* () {
    const { accountId } = yield* yield* CloudflareEnvironment;
    const versions = yield* workflows.listVersions
      .items({ accountId, workflowName })
      .pipe(Stream.runCollect);
    return Array.from(versions)
      .map((v) => v.limits?.steps ?? undefined)
      .find((steps) => steps !== undefined);
  }).pipe(
    Effect.flatMap((steps) =>
      steps === expected
        ? Effect.succeed(steps)
        : Effect.fail(new Error(`steps limit not applied yet: ${steps}`)),
    ),
    Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 15 }),
  );

test.provider(
  "effect-native workflow applies a per-workflow step limit",
  (scratch) =>
    Effect.gen(function* () {
      yield* scratch.destroy();

      const deployed = yield* scratch.deploy(
        Effect.gen(function* () {
          return { worker: yield* LimitsWorkflowWorker };
        }),
      );

      const workflowName = yield* readWorkflowName(deployed.worker.workerName);
      const applied = yield* waitForAppliedStepLimit(workflowName, STEP_LIMIT);
      expect(applied).toBe(STEP_LIMIT);

      yield* scratch.destroy();
    }).pipe(logLevel),
  {
    tags: [
      "provider:cloudflare",
      "provider:cloudflare:worker",
      "provider:cloudflare:workflow",
      "live",
    ],
    timeout: 120_000,
  },
);

// ---------------------------------------------------------------------------
// #1473 regression: native Workflow schedules on the Effect-native form,
// driven by the file-based `fixtures/workflow-schedules` worker. Deploy a
// workflow declared with `schedules`, then read it back out-of-band from
// getWorkflow (the read that surfaces `schedules`) to confirm it was
// applied. The cron is yearly so the test does not wait for a fire.
// ---------------------------------------------------------------------------

const waitForAppliedSchedules = (workflowName: string, expected: string[]) =>
  Effect.gen(function* () {
    const { accountId } = yield* yield* CloudflareEnvironment;
    const workflow = yield* workflows.getWorkflow({
      accountId,
      workflowName,
    });
    return (workflow.schedules ?? []).map((s) => s.cron);
  }).pipe(
    Effect.flatMap((crons) =>
      crons.length === expected.length && crons.every((cron, index) => cron === expected[index])
        ? Effect.succeed(crons)
        : Effect.fail(new Error(`schedules not applied yet: ${JSON.stringify(crons)}`)),
    ),
    Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 15 }),
  );

test.provider(
  "effect-native workflow applies native cron schedules",
  (scratch) =>
    Effect.gen(function* () {
      yield* scratch.destroy();

      const deployed = yield* scratch.deploy(
        Effect.gen(function* () {
          return { worker: yield* ScheduledWorkflowWorker };
        }),
      );

      const workflowName = yield* readWorkflowName(deployed.worker.workerName);
      const applied = yield* waitForAppliedSchedules(workflowName, [YEARLY_CRON]);
      expect(applied).toEqual([YEARLY_CRON]);

      yield* scratch.destroy();
    }).pipe(logLevel),
  {
    tags: [
      "provider:cloudflare",
      "provider:cloudflare:worker",
      "provider:cloudflare:workflow",
      "live",
    ],
    timeout: 120_000,
  },
);