t3-code-android-nightly/.repos/alchemy-effect/examples/fly-website-vinext/test/integ.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

115 lines
3 KiB
TypeScript

import * as Alchemy from "alchemy";
import * as Fly from "alchemy/Fly";
import * as Test from "alchemy/Test/Bun";
import { expect } from "bun:test";
import * as Console from "effect/Console";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import Stack from "../alchemy.run.ts";
const { getWhenReady } = Test;
class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
body: string;
}> {}
const getBodyWhenReady = (url: string, expected: string) =>
Effect.gen(function* () {
const res = yield* getWhenReady(url);
expect(res.status).toBe(200);
const body = yield* res.text;
if (!body.includes(expected)) {
return yield* Effect.fail(new AssetNotReady({ body }));
}
return body;
}).pipe(
Effect.retry({
while: (error) => error instanceof AssetNotReady,
schedule: Schedule.min([
Schedule.exponential("500 millis"),
Schedule.spaced("3 seconds"),
]),
times: 10,
}),
);
const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
providers: Fly.providers(),
state: Alchemy.localState(),
profile: process.env.ALCHEMY_PROFILE,
});
// Pre-deploy cleanup must not close the suite's shared runtime scope.
const stack = beforeAll(
Effect.sync(Test.defaultStage).pipe(
Effect.flatMap((stage) => Alchemy.destroy({ stack: Stack, stage })),
Effect.andThen(deploy(Stack)),
Effect.tap(Console.log),
),
{
timeout: 600_000,
},
);
afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
timeout: 600_000,
});
const base = Effect.map(stack, ({ url }) => {
if (!url) throw new Error("expected the site to expose a url");
return String(url).replace(/\/+$/, "");
});
test(
"deploys and exposes a url",
Effect.gen(function* () {
const { url } = yield* stack;
expect(url).toBeString();
}),
{ timeout: 180_000 },
);
test(
"serves the home page",
Effect.gen(function* () {
const url = yield* base;
const html = yield* getBodyWhenReady(url, "Hello from vinext on Fly!");
expect(html).toContain("vinext on Fly");
}),
{ timeout: 180_000 },
);
test(
"serves the ISR page",
Effect.gen(function* () {
const url = yield* base;
const html = yield* getBodyWhenReady(`${url}/isr`, "ISR");
expect(html).toContain("revalidate 60s");
}),
{ timeout: 180_000 },
);
test(
"serves the dynamic API route",
Effect.gen(function* () {
const url = yield* base;
const res = yield* getWhenReady(`${url}/api/hello?name=Alchemy`);
expect(res.status).toBe(200);
const body = yield* res.json;
expect(body).toEqual({
name: "Alchemy",
greeting: "Hello from vinext on Fly!",
});
}),
{ timeout: 180_000 },
);
test(
"serves a static asset from public/",
Effect.gen(function* () {
const url = yield* base;
const body = yield* getBodyWhenReady(`${url}/robots.txt`, "User-agent: *");
expect(body).toContain("User-agent: *");
}),
{ timeout: 180_000 },
);