The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import * as pathe from "pathe"; |
| 8 | import { cloneFixture } from "../../Cloudflare/Utils/Fixture.ts"; |
| 9 | import { expectUrlContains } from "../../Cloudflare/Utils/Http.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | // Gated with the rest of the AWS.Website suites: the CloudFront lifecycle |
| 14 | // dominates the runtime (create ~5-15 min, destroy ~5-15 min). |
| 15 | const runLive = !process.env.FAST; |
| 16 | |
| 17 | const fixtureDir = pathe.resolve(import.meta.dirname, "fixtures", "solidstart-app"); |
| 18 | |
| 19 | // Clone under the alchemy package so `@solidjs/start`, `vite`, and |
| 20 | // `@solidjs/vite-plugin-nitro-2` resolve from the workspace's hoisted |
| 21 | // node_modules (the fixture has no node_modules). |
| 22 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 23 | |
| 24 | const fixtureEntries = [".gitignore", "package.json", "vite.config.ts", "src", "public"]; |
| 25 | |
| 26 | // Under the floci runner the standalone composite deploys only the framework |
| 27 | // dev server (no Lambda/S3/CloudFront), so the standalone test below is |
| 28 | // live-only; the shared-Router test runs in both modes because the Router |
| 29 | // pipeline deploys fully against the emulator, which serves the router's |
| 30 | // edge on a local plain-HTTP port. |
| 31 | const runEmulated = process.env.ALCHEMY_TEST_DEV === "1"; |
| 32 | |
| 33 | describe.skipIf(!runLive)( |
| 34 | "AWS.Website.SolidStart", |
| 35 | { tags: ["provider:aws", "provider:aws:cloudfront", "provider:aws:website", "live"] }, |
| 36 | () => { |
| 37 | test.provider.skipIf(runEmulated)( |
| 38 | "deploys SSR on a Lambda Web Function with S3 assets behind CloudFront", |
| 39 | (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | yield* stack.destroy(); |
| 42 | |
| 43 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 44 | prefix: "alchemy-solidstart-aws-", |
| 45 | tempRoot, |
| 46 | entries: fixtureEntries, |
| 47 | }); |
| 48 | |
| 49 | const deployed = yield* stack.deploy( |
| 50 | Effect.gen(function* () { |
| 51 | const site = yield* AWS.Website.SolidStart("SolidStartSite", { |
| 52 | rootDir, |
| 53 | forceDestroy: true, |
| 54 | invalidation: { paths: "all", wait: true }, |
| 55 | // Forwarded into the nitro plugin the integration appends — |
| 56 | // proves nitro options reach the build. |
| 57 | nitro: { prerender: { routes: ["/prerendered"] } }, |
| 58 | }); |
| 59 | return { site }; |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | const url = deployed.site.url! as string; |
| 64 | expect(url).toMatch(/^https:\/\//); |
| 65 | expect(deployed.site.serverUrl).toBeDefined(); |
| 66 | // The server is a Lambda Web Function (the default). |
| 67 | expect(deployed.site.serverUrl).toContain(".lambda-web."); |
| 68 | yield* Effect.log(`site url: ${url} | server url: ${deployed.site.serverUrl}`); |
| 69 | |
| 70 | // The Lambda Function URL serves the SSR page directly — isolates |
| 71 | // server-function health from the CloudFront edge routing. |
| 72 | yield* expectUrlContains(`${deployed.site.serverUrl!}`, "SOLIDSTART_AWS_PAGE_MARKER", { |
| 73 | timeout: "120 seconds", |
| 74 | label: "SSR direct from Lambda URL", |
| 75 | }); |
| 76 | |
| 77 | // SSR page rendered by the Lambda through CloudFront. |
| 78 | yield* expectUrlContains(`${url}/`, "SOLIDSTART_AWS_PAGE_MARKER", { |
| 79 | timeout: "180 seconds", |
| 80 | label: "SSR home page", |
| 81 | }); |
| 82 | // The fixture's own vite.config.ts applied (its `define` marker). |
| 83 | yield* expectUrlContains(`${url}/`, "config:solidstart-aws-user-config-loaded", { |
| 84 | label: "user vite.config.ts applied", |
| 85 | }); |
| 86 | // API route through the streaming Function URL origin. |
| 87 | yield* expectUrlContains(`${url}/api/hello?echo=roundtrip`, "SOLIDSTART_AWS_API_MARKER", { |
| 88 | label: "API route", |
| 89 | }); |
| 90 | yield* expectUrlContains(`${url}/api/hello?echo=roundtrip`, "roundtrip", { |
| 91 | label: "API route query echo", |
| 92 | }); |
| 93 | // Public file served from S3 via the KV file manifest. |
| 94 | yield* expectUrlContains(`${url}/robots.txt`, "solidstart-aws-robots-marker", { |
| 95 | label: "public asset from S3", |
| 96 | }); |
| 97 | // Prerendered into `.output/public` at build time (via the `nitro` |
| 98 | // prop's `prerender.routes`) and served from S3 by exact match at |
| 99 | // the edge — no Lambda invocation. |
| 100 | yield* expectUrlContains(`${url}/prerendered`, "SOLIDSTART_AWS_PRERENDERED_MARKER", { |
| 101 | label: "prerendered page from S3", |
| 102 | }); |
| 103 | |
| 104 | const distributionId = deployed.site.distribution!.distributionId; |
| 105 | |
| 106 | if (!process.env.NO_DESTROY) { |
| 107 | yield* stack.destroy(); |
| 108 | yield* assertDistributionDeleted(distributionId); |
| 109 | } |
| 110 | }), |
| 111 | { timeout: 2_400_000 }, |
| 112 | ); |
| 113 | |
| 114 | test.provider( |
| 115 | "serves SSR through a shared Router distribution with Lambda env applied", |
| 116 | (stack) => |
| 117 | Effect.gen(function* () { |
| 118 | yield* stack.destroy(); |
| 119 | |
| 120 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 121 | prefix: "alchemy-solidstart-aws-router-", |
| 122 | tempRoot, |
| 123 | entries: fixtureEntries, |
| 124 | }); |
| 125 | |
| 126 | const deployed = yield* stack.deploy( |
| 127 | Effect.gen(function* () { |
| 128 | const router = yield* AWS.Website.Router("FrontDoor", { |
| 129 | invalidation: { paths: "all", wait: true }, |
| 130 | }); |
| 131 | const site = yield* AWS.Website.SolidStart("SolidStartSite", { |
| 132 | rootDir, |
| 133 | forceDestroy: true, |
| 134 | domain: { router }, |
| 135 | env: { SOLIDSTART_ENV_MARKER: "solidstart-aws-live-env-marker" }, |
| 136 | }); |
| 137 | return { router, site }; |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | const url = deployed.router.url as string; |
| 142 | // `https://{id}.cloudfront.net` live; the emulator serves the |
| 143 | // router's edge on a local plain-HTTP port. |
| 144 | expect(url).toMatch(runEmulated ? /^http:\/\/localhost:\d+/ : /^https:\/\//); |
| 145 | |
| 146 | // SSR through the ROUTER's distribution (the site registered |
| 147 | // itself in the router's KV store — no site-owned distribution). |
| 148 | expect(deployed.site.distribution).toBeUndefined(); |
| 149 | // Generous budget: a fresh router distribution's KVS association + |
| 150 | // function propagation can lag past 180s on first serve. |
| 151 | yield* expectUrlContains(`${url}/`, "SOLIDSTART_AWS_PAGE_MARKER", { |
| 152 | timeout: "300 seconds", |
| 153 | label: "SSR via router", |
| 154 | }); |
| 155 | // Lambda env applied on deploy (parity with the dev-server |
| 156 | // injection asserted in SolidStart.local.test.ts). |
| 157 | yield* expectUrlContains(`${url}/`, "env:solidstart-aws-live-env-marker", { |
| 158 | label: "server.environment on the Lambda", |
| 159 | }); |
| 160 | // The router's defaultTTL-0 cache policy must not cache SSR |
| 161 | // responses: the API route round-trips with distinct query strings, |
| 162 | // which a day-long cached body would break. |
| 163 | yield* expectUrlContains(`${url}/api/hello?echo=router-one`, "router-one", { |
| 164 | label: "API via router (query one)", |
| 165 | }); |
| 166 | yield* expectUrlContains(`${url}/api/hello?echo=router-two`, "router-two", { |
| 167 | label: "API via router (query two)", |
| 168 | }); |
| 169 | // Static asset from S3 through the router's edge function. |
| 170 | yield* expectUrlContains(`${url}/robots.txt`, "solidstart-aws-robots-marker", { |
| 171 | label: "public asset via router", |
| 172 | }); |
| 173 | |
| 174 | const distributionId = deployed.router.distributionId as string; |
| 175 | |
| 176 | if (!process.env.NO_DESTROY) { |
| 177 | yield* stack.destroy(); |
| 178 | yield* assertDistributionDeleted(distributionId); |
| 179 | } |
| 180 | }), |
| 181 | { timeout: 2_400_000 }, |
| 182 | ); |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | const assertDistributionDeleted = (distributionId: string) => |
| 187 | cloudfront.getDistribution({ Id: distributionId }).pipe( |
| 188 | Effect.flatMap(() => Effect.fail(new Error("DistributionStillExists"))), |
| 189 | Effect.catchTag("NoSuchDistribution", () => Effect.void), |
| 190 | Effect.retry({ |
| 191 | while: (error): boolean => |
| 192 | error instanceof Error && error.message === "DistributionStillExists", |
| 193 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(60)]), |
| 194 | }), |
| 195 | ); |