t3-code-android-nightly/.repos/alchemy-effect/examples/hetzner-website-sveltekit/test/integ.test.ts
Julius Marminge e3c85ead63
chore(refs): sync Effect and Alchemy references to rc.115 and beta.78 (#12327)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-17 23:21:25 -07:00

140 lines
4.5 KiB
TypeScript

import * as Alchemy from "alchemy";
import * as Hetzner from "alchemy/Hetzner";
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";
// A fresh Hetzner deployment (and the Hetzner Server behind it) can serve
// transient 404/5xx responses while it propagates. `Test.getWhenReady`
// fails on that cold-start window and retries until the site serves a
// real response.
const { getWhenReady } = Test;
class AssetNotReady extends Data.TaggedError("AssetNotReady")<{
body: string;
}> {}
// While the asset manifest and Hetzner edge caches are still
// propagating, a 200 body can be stale — the status alone can't
// distinguish "not yet" from "served", so retry until the body matches.
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.max([
Schedule.min([
Schedule.exponential("500 millis"),
Schedule.spaced("3 seconds"),
]),
Schedule.recurs(20),
]),
}),
);
const hasCreds = !!process.env.HCLOUD_TOKEN;
const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
providers: Hetzner.providers(),
state: Alchemy.localState(),
profile: process.env.ALCHEMY_PROFILE,
});
// The first deploy runs the full SvelteKit build AND creates a Hetzner
// distribution (~5-10 minutes), so give the hook far more headroom than
// the default 120s.
if (!hasCreds) {
test.skip("skipped without HCLOUD_TOKEN", Effect.void);
} else {
const stack = beforeAll(deploy(Stack).pipe(Effect.tap(Console.log)), {
timeout: 600_000,
});
// Deleting the Hetzner deployment takes several minutes too.
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 server-rendered home page",
Effect.gen(function* () {
const url = yield* base;
const res = yield* getWhenReady(url);
expect(res.status).toBe(200);
const html = yield* res.text;
expect(html).toContain("SvelteKit on Hetzner");
// The `GREETING` env value from alchemy.run.ts, read via `process.env`
// in the server `load` — proves the Hetzner Server rendered it at request time.
expect(html).toContain("Hello from SvelteKit on Hetzner!");
}),
{ timeout: 180_000 },
);
test(
"serves the prerendered about page",
Effect.gen(function* () {
const url = yield* base;
const res = yield* getWhenReady(`${url}/about`);
expect(res.status).toBe(200);
}),
{ timeout: 180_000 },
);
test(
"compiles tailwind from vite.config.ts",
Effect.gen(function* () {
const url = yield* base;
const res = yield* getWhenReady(url);
expect(res.status).toBe(200);
const html = yield* res.text;
// The page markup uses Tailwind utilities...
expect(html).toContain("text-3xl");
// ...and links the stylesheet Vite emitted via the project-owned
// vite.config.ts (the @tailwindcss/vite plugin), proving Alchemy loaded
// the config file natively instead of the programmatic fallback.
const match = html.match(/\/_app\/immutable\/assets\/[^"']+\.css/);
expect(match).not.toBeNull();
const css = yield* getBodyWhenReady(`${url}${match![0]}`, ".text-3xl");
expect(css).toContain(".text-3xl");
expect(css).toContain(".font-bold");
}),
{ timeout: 180_000 },
);
test(
"serves a static asset from static/",
Effect.gen(function* () {
const url = yield* base;
const body = yield* getBodyWhenReady(
`${url}/robots.txt`,
"User-agent: *",
);
expect(body).toContain("User-agent: *");
}),
{ timeout: 180_000 },
);
}