import * as AWS from "@alchemy.run/aws";
import { describe, expect } from "@alchemy.run/test";
import * as Test from "@alchemy.run/test/Alchemy";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as pathe from "pathe";
import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts";
import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts";
// `dev: true` runs local providers behind the RPC sidecar proxy by default,
// matching the process topology of the real `alchemy dev` command.
const { test } = Test.make({ providers: AWS.providers(), dev: true });
const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "sveltekit-app");
// Clone under the alchemy package so `@sveltejs/kit`/`svelte`/`vite`
// resolve from the workspace's hoisted node_modules (the fixture has no
// node_modules).
const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
const fixtureEntries = [".gitignore", "package.json", "src", "static"];
describe(
"AWS.Website.SvelteKit local",
{ tags: ["provider:aws", "provider:aws:website", "local"] },
() => {
test.provider(
"dev runs SvelteKit's own dev server with no cloud resources",
(stack) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
yield* stack.destroy();
const rootDir = yield* cloneFixture(fixtureDir, {
prefix: "alchemy-sveltekit-aws-local-",
tempRoot,
entries: fixtureEntries,
});
const deployed = yield* stack.deploy(
Effect.gen(function* () {
const site = yield* AWS.Website.SvelteKit("SvelteKitSite", { rootDir });
return { site };
}),
);
// The site is the framework's own dev server: a localhost URL and
// no cloud rows at all (proof no AWS call ran).
const url = deployed.site.url! as string;
expect(url).toMatch(/^http:\/\/(localhost|127\.0\.0\.1|\[[0-9a-fA-F:]+\])/);
expect(deployed.site.distribution).toBeUndefined();
expect(deployed.site.server).toBeUndefined();
expect(deployed.site.bucket).toBeUndefined();
// SSR page served by the kit dev server (native HMR toolchain).
yield* expectUrlContains(`${url}/`, "SVELTEKIT_AWS_PAGE_MARKER", {
timeout: "120 seconds",
label: "dev SSR home page",
});
// Server API route through the dev server.
yield* expectUrlContains(`${url}/api/hello?echo=dev`, "SVELTEKIT_AWS_API_MARKER", {
label: "API route (dev)",
});
yield* expectUrlContains(`${url}/api/hello?echo=dev`, "dev", {
label: "API route query echo (dev)",
});
// ── HMR: edit the API route in place. The stack is NOT re-applied —
// the kit/vite dev rebuild must pick the change up and serve it
// through the same URL ───────────────────────────────────────────
const helloPath = path.join(rootDir, "src/routes/api/hello/+server.ts");
const hello = yield* fs.readFileString(helloPath);
yield* fs.writeFileString(
helloPath,
hello.replace("SVELTEKIT_AWS_API_MARKER", "SVELTEKIT_AWS_API_MARKER_V2"),
);
yield* expectUrlContains(`${url}/api/hello?echo=dev`, "SVELTEKIT_AWS_API_MARKER_V2", {
timeout: "90 seconds",
label: "API route after HMR edit",
});
// The route still round-trips its query after the reload.
yield* expectUrlContains(`${url}/api/hello?echo=post-hmr`, "post-hmr", {
label: "API route query echo after HMR edit",
});
yield* stack.destroy();
}),
{ timeout: 600_000 },
);
},
);