1import * as AWS from "@/AWS";
2import * as Test from "@/Test/Alchemy";
3import { describe, expect } from "alchemy-test";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Path from "effect/Path";
7import * as pathe from "pathe";
8import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts";
9import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts";
10
11// `dev: true` runs local providers behind the RPC sidecar proxy by default,
12// matching the process topology of the real `alchemy dev` command.
13const { test } = Test.make({ providers: AWS.providers(), dev: true });
14
15const fixtureDir = pathe.resolve(
16 import.meta.dirname,
17 "fixtures",
18 "sveltekit-app",
19);
20
21// Clone under the alchemy package so `@sveltejs/kit`/`svelte`/`vite`
22// resolve from the workspace's hoisted node_modules (the fixture has no
23// node_modules).
24const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp");
25
26const fixtureEntries = [".gitignore", "package.json", "src", "static"];
27
28describe(
29 "AWS.Website.SvelteKit local",
30 { tags: ["provider:aws", "provider:aws:website", "local"] },
31 () => {
32 test.provider(
33 "dev runs SvelteKit's own dev server with no cloud resources",
34 (stack) =>
35 Effect.gen(function* () {
36 const fs = yield* FileSystem.FileSystem;
37 const path = yield* Path.Path;
38
39 yield* stack.destroy();
40
41 const rootDir = yield* cloneFixture(fixtureDir, {
42 prefix: "alchemy-sveltekit-aws-local-",
43 tempRoot,
44 entries: fixtureEntries,
45 });
46
47 const deployed = yield* stack.deploy(
48 Effect.gen(function* () {
49 const site = yield* AWS.Website.SvelteKit("SvelteKitSite", {
50 rootDir,
51 });
52 return { site };
53 }),
54 );
55
56 // The site is the framework's own dev server: a localhost URL and
57 // no cloud rows at all (proof no AWS call ran).
58 const url = deployed.site.url! as string;
59 expect(url).toMatch(
60 /^http:\/\/(localhost|127\.0\.0\.1|\[[0-9a-fA-F:]+\])/,
61 );
62 expect(deployed.site.distribution).toBeUndefined();
63 expect(deployed.site.server).toBeUndefined();
64 expect(deployed.site.bucket).toBeUndefined();
65
66 // SSR page served by the kit dev server (native HMR toolchain).
67 yield* expectUrlContains(`${url}/`, "SVELTEKIT_AWS_PAGE_MARKER", {
68 timeout: "120 seconds",
69 label: "dev SSR home page",
70 });
71 // Server API route through the dev server.
72 yield* expectUrlContains(
73 `${url}/api/hello?echo=dev`,
74 "SVELTEKIT_AWS_API_MARKER",
75 { label: "API route (dev)" },
76 );
77 yield* expectUrlContains(`${url}/api/hello?echo=dev`, "dev", {
78 label: "API route query echo (dev)",
79 });
80
81 // ── HMR: edit the API route in place. The stack is NOT re-applied —
82 // the kit/vite dev rebuild must pick the change up and serve it
83 // through the same URL ───────────────────────────────────────────
84 const helloPath = path.join(
85 rootDir,
86 "src/routes/api/hello/+server.ts",
87 );
88 const hello = yield* fs.readFileString(helloPath);
89 yield* fs.writeFileString(
90 helloPath,
91 hello.replace(
92 "SVELTEKIT_AWS_API_MARKER",
93 "SVELTEKIT_AWS_API_MARKER_V2",
94 ),
95 );
96 yield* expectUrlContains(
97 `${url}/api/hello?echo=dev`,
98 "SVELTEKIT_AWS_API_MARKER_V2",
99 { timeout: "90 seconds", label: "API route after HMR edit" },
100 );
101 // The route still round-trips its query after the reload.
102 yield* expectUrlContains(
103 `${url}/api/hello?echo=post-hmr`,
104 "post-hmr",
105 {
106 label: "API route query echo after HMR edit",
107 },
108 );
109
110 yield* stack.destroy();
111 }),
112 { timeout: 600_000 },
113 );
114 },
115);