| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Test from "@/Test/Alchemy"; |
| 3 | import { describe, expect } from "alchemy-test"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import * as pathe from "pathe"; |
| 8 | import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts"; |
| 9 | import { 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. |
| 13 | const { test } = Test.make({ providers: AWS.providers(), dev: true }); |
| 14 | |
| 15 | const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "astro-app"); |
| 16 | |
| 17 | // Clone under the alchemy package so `astro` resolves from the workspace's |
| 18 | // hoisted node_modules (the fixture has no node_modules). |
| 19 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 20 | |
| 21 | const fixtureEntries = [ |
| 22 | ".gitignore", |
| 23 | "package.json", |
| 24 | "astro.config.mjs", |
| 25 | "src", |
| 26 | "public", |
| 27 | ]; |
| 28 | |
| 29 | describe( |
| 30 | "AWS.Website.Astro local", |
| 31 | { tags: ["provider:aws", "provider:aws:website", "local"] }, |
| 32 | () => { |
| 33 | test.provider( |
| 34 | "dev runs Astro's own dev server with no cloud resources", |
| 35 | (stack) => |
| 36 | Effect.gen(function* () { |
| 37 | const fs = yield* FileSystem.FileSystem; |
| 38 | const path = yield* Path.Path; |
| 39 | |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 43 | prefix: "alchemy-astro-aws-local-", |
| 44 | tempRoot, |
| 45 | entries: fixtureEntries, |
| 46 | }); |
| 47 | |
| 48 | const deployed = yield* stack.deploy( |
| 49 | Effect.gen(function* () { |
| 50 | const site = yield* AWS.Website.Astro("AstroSite", { |
| 51 | rootDir, |
| 52 | // Pin the dev server to a deterministic port — `dev.port` |
| 53 | // flows through Server's port probe into the framework kit. |
| 54 | dev: { port: 43117 }, |
| 55 | }); |
| 56 | return { site }; |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | // The site is the framework's own dev server: a localhost URL and |
| 61 | // no cloud rows at all (proof no AWS call ran). |
| 62 | const url = deployed.site.url! as string; |
| 63 | expect(url).toMatch( |
| 64 | /^http:\/\/(localhost|127\.0\.0\.1|\[[0-9a-fA-F:]+\])/, |
| 65 | ); |
| 66 | // The preferred `dev.port` was honored. |
| 67 | expect(url).toContain(":43117"); |
| 68 | expect(deployed.site.distribution).toBeUndefined(); |
| 69 | expect(deployed.site.server).toBeUndefined(); |
| 70 | expect(deployed.site.bucket).toBeUndefined(); |
| 71 | |
| 72 | // SSR page served by the astro dev server (native HMR toolchain). |
| 73 | yield* expectUrlContains(`${url}/`, "ASTRO_AWS_PAGE_MARKER", { |
| 74 | timeout: "120 seconds", |
| 75 | label: "dev SSR home page", |
| 76 | }); |
| 77 | // API route through the dev server. |
| 78 | yield* expectUrlContains( |
| 79 | `${url}/api/hello?echo=dev`, |
| 80 | "ASTRO_AWS_API_MARKER", |
| 81 | { label: "API route (dev)" }, |
| 82 | ); |
| 83 | yield* expectUrlContains(`${url}/api/hello?echo=dev`, "dev", { |
| 84 | label: "API route query echo (dev)", |
| 85 | }); |
| 86 | |
| 87 | // ── HMR: edit the API route in place. The stack is NOT re-applied — |
| 88 | // astro's dev rebuild must pick the change up and serve it through |
| 89 | // the same URL ─────────────────────────────────────────────────── |
| 90 | const helloPath = path.join(rootDir, "src/pages/api/hello.ts"); |
| 91 | const hello = yield* fs.readFileString(helloPath); |
| 92 | yield* fs.writeFileString( |
| 93 | helloPath, |
| 94 | hello.replace("ASTRO_AWS_API_MARKER", "ASTRO_AWS_API_MARKER_V2"), |
| 95 | ); |
| 96 | yield* expectUrlContains( |
| 97 | `${url}/api/hello?echo=dev`, |
| 98 | "ASTRO_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 | ); |