Prepare Relay production infrastructure for the PlanetScale PS_20 HA topology, upgrade Effect and Alchemy to compatible betas, reconcile migration state, and preserve patched MCP session termination behavior.\n\nCo-authored-by: codex <[email protected]>
308 lines
10 KiB
TypeScript
308 lines
10 KiB
TypeScript
import * as Cloudflare from "alchemy/Cloudflare";
|
|
import * as Test from "alchemy/Test/Bun";
|
|
import { expect } from "bun:test";
|
|
import * as Effect from "effect/Effect";
|
|
import { cast } from "effect/Function";
|
|
import * as Schedule from "effect/Schedule";
|
|
import * as Schema from "effect/Schema";
|
|
import * as HttpBody from "effect/unstable/http/HttpBody";
|
|
import * as HttpClient from "effect/unstable/http/HttpClient";
|
|
import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
|
|
import assert from "node:assert";
|
|
import Stack from "../alchemy.run.ts";
|
|
import type { Message } from "../src/AsyncWorker.ts";
|
|
import { WORKFLOW_SECRET_VALUE } from "../src/NotifyWorkflow.ts";
|
|
|
|
const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
|
|
providers: Cloudflare.providers(),
|
|
state: Cloudflare.state(),
|
|
dev: true,
|
|
});
|
|
|
|
const stack = beforeAll(
|
|
deploy(Stack).pipe(
|
|
Effect.flatMap(
|
|
Effect.fn(function* ({ asyncWorker, effectWorker }) {
|
|
assert(typeof asyncWorker === "string");
|
|
assert(typeof effectWorker === "string");
|
|
yield* Effect.forEach([asyncWorker, effectWorker], (url) =>
|
|
HttpClient.get(url).pipe(
|
|
Effect.flatMap(HttpClientResponse.filterStatusOk),
|
|
Effect.retry({
|
|
schedule: Schedule.max([Schedule.spaced("250 millis"), Schedule.recurs(25)]),
|
|
}),
|
|
),
|
|
);
|
|
return { asyncWorker, effectWorker };
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
|
|
afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
|
|
|
|
test(
|
|
"deploys all workers with URLs",
|
|
Effect.gen(function* () {
|
|
const { asyncWorker, effectWorker } = yield* stack;
|
|
|
|
expect(asyncWorker).toBeString();
|
|
expect(effectWorker).toBeString();
|
|
}),
|
|
);
|
|
|
|
/**
|
|
* AsyncWorker exports a default fetch handler that calls the `Counter`
|
|
* Durable Object's `increment()` and returns `Hello, world! <n>`.
|
|
*
|
|
* Hitting the worker twice exercises the DO end-to-end and proves
|
|
* persistent state across requests — if the DO binding is missing or
|
|
* the class export is wrong, the first request fails outright.
|
|
*/
|
|
test(
|
|
"AsyncWorker increments the Counter Durable Object across requests",
|
|
Effect.gen(function* () {
|
|
const { asyncWorker } = yield* stack;
|
|
|
|
const first = yield* HttpClient.get(new URL("/counter", asyncWorker));
|
|
expect(first.status).toBe(200);
|
|
const firstBody = yield* first.text;
|
|
const firstMatch = firstBody.match(/^Hello, world! (\d+)$/);
|
|
expect(firstMatch).not.toBeNull();
|
|
const firstCount = Number(firstMatch![1]);
|
|
|
|
const second = yield* HttpClient.get(new URL("/counter", asyncWorker));
|
|
expect(second.status).toBe(200);
|
|
const secondBody = yield* second.text;
|
|
const secondMatch = secondBody.match(/^Hello, world! (\d+)$/);
|
|
expect(secondMatch).not.toBeNull();
|
|
const secondCount = Number(secondMatch![1]);
|
|
|
|
expect(secondCount).toBe(firstCount + 1);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
"AsyncWorker serves assets",
|
|
Effect.gen(function* () {
|
|
const { asyncWorker } = yield* stack;
|
|
const response = yield* HttpClient.get(new URL("/", asyncWorker));
|
|
expect(response.status).toBe(200);
|
|
const body = yield* response.text;
|
|
expect(body).toMatch("<h1>Hello, world!</h1>");
|
|
}),
|
|
);
|
|
|
|
test(
|
|
"AsyncWorker receives bindings, including variables and secrets",
|
|
Effect.gen(function* () {
|
|
const { asyncWorker } = yield* stack;
|
|
const response = yield* HttpClient.get(new URL("/env", asyncWorker));
|
|
expect(response.status).toBe(200);
|
|
const body = yield* response.json;
|
|
expect(body).toMatchObject({
|
|
MY_SECRET: "my-secret-abc123",
|
|
MY_VARIABLE: "my-variable-abc123",
|
|
COUNTER: {},
|
|
});
|
|
}),
|
|
);
|
|
|
|
test(
|
|
"AsyncWorker sends and receives messages on the queue",
|
|
Effect.gen(function* () {
|
|
const { asyncWorker } = yield* stack;
|
|
const body = { text: "hello", sentAt: Date.now() };
|
|
yield* HttpClient.post(new URL("/queue/send", asyncWorker), {
|
|
body: yield* HttpBody.json(body),
|
|
}).pipe(Effect.flatMap(HttpClientResponse.filterStatusOk));
|
|
const message = yield* HttpClient.get(
|
|
new URL("/queue/messages", asyncWorker),
|
|
).pipe(
|
|
Effect.flatMap(HttpClientResponse.filterStatusOk),
|
|
Effect.flatMap((res) => res.json),
|
|
Effect.map(cast<Schema.Json, Array<Message>>),
|
|
Effect.map((messages) =>
|
|
messages.find((m) => m.body.sentAt === body.sentAt),
|
|
),
|
|
Effect.filterOrFail(
|
|
(message) => message !== undefined,
|
|
() => ({ _tag: "MessageNotFound" }) as const,
|
|
),
|
|
Effect.retry({
|
|
while: (error) => error._tag === "MessageNotFound",
|
|
schedule: Schedule.max([Schedule.spaced("250 millis"), Schedule.recurs(25)]),
|
|
}),
|
|
);
|
|
expect(message).toMatchObject({
|
|
id: expect.any(String),
|
|
body,
|
|
});
|
|
}),
|
|
{ timeout: 10_000 },
|
|
);
|
|
|
|
/**
|
|
* EffectWorker binds a KV namespace via `Cloudflare.KV.ReadWriteNamespace(KV)`
|
|
* and returns the result of `kv.list()` as JSON. A successful response
|
|
* proves the Effect-style binding wired the runtime SDK and the
|
|
* `WorkerEnvironment` service was provisioned for the fetch handler.
|
|
*/
|
|
test(
|
|
"EffectWorker returns a KV list result via the Effect KV binding",
|
|
Effect.gen(function* () {
|
|
const { effectWorker } = yield* stack;
|
|
|
|
const response = yield* HttpClient.get(effectWorker);
|
|
expect(response.status).toBe(200);
|
|
|
|
const body = (yield* response.json) as {
|
|
keys: Array<{ name: string }>;
|
|
list_complete: boolean;
|
|
};
|
|
expect(Array.isArray(body.keys)).toBe(true);
|
|
expect(typeof body.list_complete).toBe("boolean");
|
|
}),
|
|
);
|
|
|
|
test(
|
|
"EffectWorker sends and receives messages on the queue",
|
|
Effect.gen(function* () {
|
|
const { effectWorker } = yield* stack;
|
|
const body = { text: "hello", sentAt: Date.now() };
|
|
yield* HttpClient.post(new URL("/queue/send", effectWorker), {
|
|
body: yield* HttpBody.json(body),
|
|
}).pipe(Effect.flatMap(HttpClientResponse.filterStatusOk));
|
|
const message = yield* HttpClient.get(
|
|
new URL("/queue/messages", effectWorker),
|
|
).pipe(
|
|
Effect.flatMap(HttpClientResponse.filterStatusOk),
|
|
Effect.flatMap((res) => res.json),
|
|
Effect.map(cast<Schema.Json, Array<Message>>),
|
|
Effect.map((messages) =>
|
|
messages.find((m) => m.body.sentAt === body.sentAt),
|
|
),
|
|
Effect.filterOrFail(
|
|
(message) => message !== undefined,
|
|
() => ({ _tag: "MessageNotFound" }) as const,
|
|
),
|
|
Effect.retry({
|
|
while: (error) => error._tag === "MessageNotFound",
|
|
schedule: Schedule.max([Schedule.spaced("250 millis"), Schedule.recurs(25)]),
|
|
}),
|
|
);
|
|
expect(message).toMatchObject({
|
|
id: expect.any(String),
|
|
body,
|
|
});
|
|
}),
|
|
{ timeout: 10_000 },
|
|
);
|
|
|
|
/**
|
|
* Both workers import `./modules/wasm-example.wasm`, which exports a
|
|
* single `add(a: number, b: number): number` function. Hitting `/wasm`
|
|
* instantiates the module and returns `add(3, 4)` as JSON, proving that
|
|
* the bundler ships the wasm asset to workerd and that runtime
|
|
* `WebAssembly.instantiate` works for both the raw async-handler and
|
|
* Effect-style entrypoints.
|
|
*/
|
|
test(
|
|
"AsyncWorker /wasm instantiates the wasm module and returns add(3, 4)",
|
|
Effect.gen(function* () {
|
|
const { asyncWorker } = yield* stack;
|
|
|
|
const response = yield* HttpClient.get(new URL("/wasm", asyncWorker));
|
|
expect(response.status).toBe(200);
|
|
const body = (yield* response.json) as { result: number };
|
|
expect(body.result).toBe(7);
|
|
}),
|
|
);
|
|
|
|
test(
|
|
"EffectWorker /wasm instantiates the wasm module and returns add(3, 4)",
|
|
Effect.gen(function* () {
|
|
const { effectWorker } = yield* stack;
|
|
|
|
const response = yield* HttpClient.get(new URL("/wasm", effectWorker));
|
|
expect(response.status).toBe(200);
|
|
const body = (yield* response.json) as { result: number };
|
|
expect(body.result).toBe(7);
|
|
}),
|
|
);
|
|
|
|
interface WorkflowStatus {
|
|
status: string;
|
|
output?: { text?: string; secret?: string; ts?: number };
|
|
error?: { name: string; message: string } | null;
|
|
}
|
|
|
|
/**
|
|
* Start a `NotifyWorkflow` instance through `workerUrl` and poll its status
|
|
* until the instance reaches a terminal state. Asserts the workflow ran the
|
|
* KV roundtrip task (`Processed: <message>`) and resolved the plantime-bound
|
|
* `Alchemy.Secret` at runtime (`output.secret === WORKFLOW_SECRET_VALUE`).
|
|
*/
|
|
const exerciseWorkflow = (workerUrl: string, label: string) =>
|
|
Effect.gen(function* () {
|
|
const roomId = `${label}-${Math.random().toString(36).slice(2, 10)}`;
|
|
|
|
const startResponse = yield* HttpClient.post(
|
|
new URL(`/workflow/start/${roomId}`, workerUrl),
|
|
);
|
|
expect(startResponse.status).toBe(200);
|
|
const { instanceId } = (yield* startResponse.json) as {
|
|
instanceId: string;
|
|
};
|
|
expect(instanceId).toBeString();
|
|
|
|
const statusUrl = new URL(`/workflow/status/${instanceId}`, workerUrl);
|
|
const fetchStatus = HttpClient.get(statusUrl).pipe(
|
|
Effect.flatMap((res) => res.json),
|
|
Effect.map((json) => json as unknown as WorkflowStatus),
|
|
);
|
|
const status = yield* fetchStatus.pipe(
|
|
Effect.repeat({
|
|
schedule: Schedule.spaced("2 seconds"),
|
|
until: (s: WorkflowStatus) =>
|
|
s.status === "complete" || s.status === "errored",
|
|
times: 60,
|
|
}),
|
|
);
|
|
|
|
expect(status.error).toBeFalsy();
|
|
expect(status.status).toBe("complete");
|
|
expect(status.output?.secret).toBe(WORKFLOW_SECRET_VALUE);
|
|
expect(status.output?.text).toBe("Processed: hello from workflow");
|
|
});
|
|
|
|
/**
|
|
* EffectWorker `/workflow/start/:roomId` kicks off `NotifyWorkflow`, which
|
|
* does a KV roundtrip task and resolves the `WORKFLOW_SECRET` `Alchemy.Secret`
|
|
* at runtime. The status route surfaces the workflow output so we can assert
|
|
* the workflow actually executed end-to-end (not just that it was scheduled).
|
|
*/
|
|
test(
|
|
"EffectWorker drives NotifyWorkflow to completion with secret + KV roundtrip",
|
|
Effect.gen(function* () {
|
|
const { effectWorker } = yield* stack;
|
|
yield* exerciseWorkflow(effectWorker, "effect");
|
|
}),
|
|
{ timeout: 60_000 },
|
|
);
|
|
|
|
test(
|
|
"EffectWorker fetches a URL in a sandbox",
|
|
Effect.gen(function* () {
|
|
const { effectWorker } = yield* stack;
|
|
const response = yield* HttpClient.get(new URL("/sandbox", effectWorker));
|
|
expect(response.status).toBe(200);
|
|
const body = yield* response.text;
|
|
// The container echoes its `GREETING` env var, proving env vars flow
|
|
// through to the container (via the application config on a live deploy,
|
|
// and via `ctx.container.start({ env })` in local dev).
|
|
expect(body).toBe("Hello from Sandbox container! GREETING=hello-from-env");
|
|
}),
|
|
{ timeout: 60_000 },
|
|
);
|