import * as Cloudflare from "@alchemy.run/cloudflare";
import { expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as Data from "effect/Data";
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 pathe from "pathe";
import { cloneFixture } from "../Utils/Fixture.ts";
// `dev: true` runs local providers behind the RPC sidecar proxy by default,
// matching the process topology of the real `alchemy dev` command. The
// Worker and Consumer lifecycles (vite child, consumer wiring, worker
// restart hooks) all run in the sidecar process.
const { test } = Test.make({
providers: Cloudflare.providers(),
dev: true,
});
const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
const queueFixtureDir = pathe.resolve(import.meta.dirname, "vite-queue-fixture");
const cronFixtureDir = pathe.resolve(import.meta.dirname, "vite-cron-fixture");
const tailFixtureDir = pathe.resolve(import.meta.dirname, "vite-tail-fixture");
// Keep the temp clone under the workspace so Vite can express the project
// root relative to cwd (see the note on `tempRoot` in Vite.test.ts).
const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
const fixtureEntries = ["index.html", "package.json", "vite.config.ts", "worker.ts"];
class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{
status: number;
}> {}
const getJsonReady = (url: string) =>
Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const res = yield* client.get(url).pipe(
Effect.flatMap((res) =>
res.status === 200
? Effect.succeed(res)
: Effect.fail(new WorkerNotReady({ status: res.status })),
),
Effect.retry({
while: (e): e is WorkerNotReady => e instanceof WorkerNotReady,
schedule: Schedule.max([
Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("2 seconds")]),
Schedule.recurs(15),
]),
}),
);
return yield* res.json;
}).pipe(Effect.orDie);
/**
* Regression test for #1109: a `Queues.Consumer` registered against a
* `Website.Vite` worker in dev. The Consumer depends on the site's
* `workerName`, so the Vite child always boots before the consumer wiring
* lands in `LocalRuntimeState` — the consumer's reconcile must then restart
* the child through the `workerRestarts` hook (which `runVite` previously
* never registered). Pre-fix, the producer binding resolved to the dev
* registry's drop-and-warn stub: `queue()` never ran and `send()` hung.
*/
test.provider(
"Vite dev: queue consumer on the site worker receives produced messages",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
const rootDir = yield* cloneFixture(queueFixtureDir, {
prefix: "alchemy-vite-queue-",
tempRoot,
entries: fixtureEntries,
});
const deployed = yield* stack.deploy(
Effect.gen(function* () {
const queue = yield* Cloudflare.Queues.Queue("ViteJobsQueue");
const site = yield* Cloudflare.Website.Vite("ViteQueueSite", {
rootDir,
main: "worker.ts",
workersDev: true,
compatibility: {
date: "2024-09-23",
flags: ["nodejs_compat"],
},
memo: { include: fixtureEntries },
assets: { runWorkerFirst: true },
dev: { port: 0 },
env: { QUEUE: queue },
});
yield* Cloudflare.Queues.Consumer("ViteJobsConsumer", {
queueId: queue.queueId,
scriptName: site.workerName,
settings: { batchSize: 5, maxRetries: 2, maxWaitTimeMs: 500 },
});
return { queue, site };
}),
);
// The queue is emulated (`dev:` id — proof no cloud call ran) and the
// site serves from the local dev proxy.
expect(deployed.queue.queueId).toMatch(/^dev:/);
expect(deployed.site.url).toMatch(/^http:\/\/localhost:\d+$/);
// `send()` resolves (pre-fix it hung forever against the registry's
// drop stub)...
const sent = (yield* getJsonReady(`${deployed.site.url}/api/send?text=vite-queue-hello`).pipe(
Effect.timeout("60 seconds"),
)) as { sent: string };
expect(sent.sent).toBe("vite-queue-hello");
// ...and the broker delivers to the fixture's queue() handler.
const received = yield* getJsonReady(`${deployed.site.url}/api/received`).pipe(
Effect.map((body) => (body as { received: string[] }).received),
Effect.repeat({
schedule: Schedule.spaced("500 millis"),
until: (received) => received.includes("vite-queue-hello"),
times: 30,
}),
);
expect(received).toContain("vite-queue-hello");
yield* stack.destroy();
}).pipe(logLevel),
{
tags: [
"provider:cloudflare",
"provider:cloudflare:queue",
"provider:cloudflare:website",
"local",
],
timeout: 240_000,
},
);
/** Regression test for cron configuration dropped by the Vite child path. */
test.provider(
"Vite dev: automatically invokes scheduled handlers",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
const rootDir = yield* cloneFixture(cronFixtureDir, {
prefix: "alchemy-vite-cron-",
tempRoot,
entries: fixtureEntries,
});
const site = yield* stack.deploy(
Cloudflare.Website.Vite("ViteCronSite", {
rootDir,
main: "worker.ts",
workersDev: true,
compatibility: {
date: "2024-09-23",
flags: ["nodejs_compat"],
},
memo: { include: fixtureEntries },
assets: { runWorkerFirst: true },
dev: { port: 0 },
// Effect Cron supports seconds for fast local feedback; five-field
// production expressions use the same runtime path.
crons: ["* * * * * *"],
}),
);
const scheduledCount = yield* getJsonReady(`${site.url}/api/scheduled`).pipe(
Effect.map((body) => (body as { scheduledCount: number }).scheduledCount),
Effect.repeat({
schedule: Schedule.spaced("500 millis"),
until: (count) => count > 0,
times: 20,
}),
);
expect(scheduledCount).toBeGreaterThan(0);
yield* stack.destroy();
}).pipe(logLevel),
{
tags: ["provider:cloudflare", "provider:cloudflare:website", "local"],
timeout: 240_000,
},
);
/** Regression test for tail consumers dropped by the Vite child path (#2231). */
test.provider(
"Vite dev: tail and streaming tail consumers receive the site's events",
(stack) =>
Effect.gen(function* () {
yield* stack.destroy();
const rootDir = yield* cloneFixture(tailFixtureDir, {
prefix: "alchemy-vite-tail-",
tempRoot,
entries: fixtureEntries,
});
const { site, consumer } = yield* stack.deploy(
Effect.gen(function* () {
const events = yield* Cloudflare.KV.Namespace("ViteTailEvents");
const consumer = yield* Cloudflare.Worker("ViteTailConsumer", {
main: pathe.resolve(import.meta.dirname, "fixtures/tail-consumer.ts"),
env: { EVENTS: events },
dev: { port: 0 },
});
const site = yield* Cloudflare.Website.Vite("ViteTailSite", {
rootDir,
main: "worker.ts",
workersDev: true,
compatibility: { date: "2024-09-23" },
memo: { include: fixtureEntries },
assets: { runWorkerFirst: true },
dev: { port: 0 },
tailConsumers: [consumer],
streamingTailConsumers: [consumer],
});
return { site, consumer };
}),
);
// Each poll drives the site again: the consumer may register with the
// dev registry after the site's first requests.
const client = yield* HttpClient.HttpClient;
const recorded = yield* Effect.gen(function* () {
yield* client.get(`${site.url}/api/log`).pipe(Effect.flatMap((res) => res.text));
const res = yield* client.get(`${consumer.url}/events`);
return (yield* res.json) as { tail: string[]; stream: string[] };
}).pipe(
Effect.orDie,
Effect.repeat({
schedule: Schedule.spaced("1 second"),
until: ({ tail, stream }) =>
tail.some((batch) => batch.includes("vite-tail-marker")) &&
stream.some((session) => session.includes("vite-tail-marker")),
times: 30,
}),
);
expect(recorded.tail.some((batch) => batch.includes("vite-tail-marker"))).toBe(true);
expect(recorded.stream.some((session) => session.includes("vite-tail-marker"))).toBe(true);
yield* stack.destroy();
}).pipe(logLevel),
{
tags: ["provider:cloudflare", "provider:cloudflare:kv", "provider:cloudflare:website", "local"],
timeout: 240_000,
},
);