| 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, expectUrlOk } 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 | /** |
| 16 | * SolidStart 2's dev SSR does not run under Bun. |
| 17 | * |
| 18 | * `solid-start-dev-server` answers every request through |
| 19 | * `sendNodeResponse(res, webRes)` from `srvx/node`; under Bun that writes the |
| 20 | * literal string `[object Object]` (HTTP 200, 15 bytes) instead of the |
| 21 | * rendered document. Under Node the same code path returns the SSR HTML. |
| 22 | * Verified directly against the framework integration: |
| 23 | * |
| 24 | * node -> STATUS 200 LEN 1070 "<!DOCTYPE html><html lang=\"en\">..." |
| 25 | * bun -> STATUS 200 LEN 15 "[object Object]" |
| 26 | * |
| 27 | * `alchemy dev` hosts local providers in a Bun sidecar |
| 28 | * (`bun run src/AWS/Website/ServerLocal.ts`), so the body assertions below |
| 29 | * cannot pass until the upstream `srvx`/Bun gap is closed. Everything the |
| 30 | * dev provider itself owns — the server starting, its address, and the |
| 31 | * absence of cloud resources — is asserted unconditionally in the first test. |
| 32 | * |
| 33 | * Set `ALCHEMY_TEST_SOLIDSTART_DEV_SSR=1` to run the body assertions on a |
| 34 | * Node-hosted sidecar. |
| 35 | */ |
| 36 | const runDevSsr = process.env.ALCHEMY_TEST_SOLIDSTART_DEV_SSR === "1"; |
| 37 | |
| 38 | const fixtureDir = pathe.resolve( |
| 39 | import.meta.dirname, |
| 40 | "fixtures", |
| 41 | "solidstart-app", |
| 42 | ); |
| 43 | |
| 44 | // Clone under the alchemy package so `@solidjs/start`, `vite`, and |
| 45 | // `@solidjs/vite-plugin-nitro-2` resolve from the workspace's hoisted |
| 46 | // node_modules (the fixture has no node_modules). |
| 47 | const tempRoot = pathe.resolve(import.meta.dirname, "../../../.tmp"); |
| 48 | |
| 49 | const fixtureEntries = [ |
| 50 | ".gitignore", |
| 51 | "package.json", |
| 52 | "vite.config.ts", |
| 53 | "src", |
| 54 | "public", |
| 55 | ]; |
| 56 | |
| 57 | describe( |
| 58 | "AWS.Website.SolidStart local", |
| 59 | { tags: ["provider:aws", "provider:aws:website", "local"] }, |
| 60 | () => { |
| 61 | test.provider( |
| 62 | "dev runs SolidStart's own Vite dev server with no cloud resources", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | yield* stack.destroy(); |
| 66 | |
| 67 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 68 | prefix: "alchemy-solidstart-aws-local-", |
| 69 | tempRoot, |
| 70 | entries: fixtureEntries, |
| 71 | }); |
| 72 | |
| 73 | const deployed = yield* stack.deploy( |
| 74 | Effect.gen(function* () { |
| 75 | const site = yield* AWS.Website.SolidStart("SolidStartSite", { |
| 76 | rootDir, |
| 77 | env: { |
| 78 | SOLIDSTART_ENV_MARKER: "solidstart-aws-dev-env-marker", |
| 79 | }, |
| 80 | }); |
| 81 | return { site }; |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | // The site is the framework's own dev server: a localhost URL and |
| 86 | // no cloud rows at all (proof no AWS call ran). |
| 87 | const url = deployed.site.url! as string; |
| 88 | expect(url).toMatch( |
| 89 | /^http:\/\/(localhost|127\.0\.0\.1|\[[0-9a-fA-F:]+\])/, |
| 90 | ); |
| 91 | // The URL is an origin, not a directory — appending a path must not |
| 92 | // produce a double slash (SolidStart runs `appType: "custom"`, so its |
| 93 | // router sees the raw pathname and `//about` is a 404). |
| 94 | expect(url.endsWith("/")).toBe(false); |
| 95 | expect(deployed.site.distribution).toBeUndefined(); |
| 96 | expect(deployed.site.server).toBeUndefined(); |
| 97 | expect(deployed.site.bucket).toBeUndefined(); |
| 98 | |
| 99 | // The dev server is reachable from another process. |
| 100 | expect(yield* expectUrlOk(`${url}/`)).toBe(200); |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | }), |
| 104 | { timeout: 600_000 }, |
| 105 | ); |
| 106 | |
| 107 | // Body-level assertions: gated on a Node-hosted dev sidecar (see the note |
| 108 | // on `runDevSsr` above). |
| 109 | test.provider.skipIf(!runDevSsr)( |
| 110 | "dev serves SSR, the project's vite.config.ts, injected env, and HMR", |
| 111 | (stack) => |
| 112 | Effect.gen(function* () { |
| 113 | const fs = yield* FileSystem.FileSystem; |
| 114 | const path = yield* Path.Path; |
| 115 | |
| 116 | yield* stack.destroy(); |
| 117 | |
| 118 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 119 | prefix: "alchemy-solidstart-aws-local-ssr-", |
| 120 | tempRoot, |
| 121 | entries: fixtureEntries, |
| 122 | }); |
| 123 | |
| 124 | const deployed = yield* stack.deploy( |
| 125 | Effect.gen(function* () { |
| 126 | const site = yield* AWS.Website.SolidStart("SolidStartSite", { |
| 127 | rootDir, |
| 128 | env: { |
| 129 | SOLIDSTART_ENV_MARKER: "solidstart-aws-dev-env-marker", |
| 130 | }, |
| 131 | }); |
| 132 | return { site }; |
| 133 | }), |
| 134 | ); |
| 135 | |
| 136 | const url = deployed.site.url! as string; |
| 137 | |
| 138 | // SSR page served by the SolidStart dev server (native HMR). |
| 139 | yield* expectUrlContains(`${url}/`, "SOLIDSTART_AWS_PAGE_MARKER", { |
| 140 | timeout: "120 seconds", |
| 141 | label: "dev SSR home page", |
| 142 | }); |
| 143 | // The fixture's own vite.config.ts applied in dev too. |
| 144 | yield* expectUrlContains( |
| 145 | `${url}/`, |
| 146 | "config:solidstart-aws-user-config-loaded", |
| 147 | { label: "user vite.config.ts applied (dev)" }, |
| 148 | ); |
| 149 | // server.environment reaches the dev server's process env — the |
| 150 | // same values the Lambda gets on deploy (dev/live parity). |
| 151 | yield* expectUrlContains( |
| 152 | `${url}/`, |
| 153 | "env:solidstart-aws-dev-env-marker", |
| 154 | { label: "server.environment injected into dev server" }, |
| 155 | ); |
| 156 | // API route through the dev server. |
| 157 | yield* expectUrlContains( |
| 158 | `${url}/api/hello?echo=dev`, |
| 159 | "SOLIDSTART_AWS_API_MARKER", |
| 160 | { label: "API route (dev)" }, |
| 161 | ); |
| 162 | |
| 163 | // ── HMR: edit the API route in place. The stack is NOT re-applied — |
| 164 | // vite's dev rebuild must pick the change up and serve it through |
| 165 | // the same URL ─────────────────────────────────────────────────── |
| 166 | const helloPath = path.join(rootDir, "src/routes/api/hello.ts"); |
| 167 | const hello = yield* fs.readFileString(helloPath); |
| 168 | yield* fs.writeFileString( |
| 169 | helloPath, |
| 170 | hello.replace( |
| 171 | "SOLIDSTART_AWS_API_MARKER", |
| 172 | "SOLIDSTART_AWS_API_MARKER_V2", |
| 173 | ), |
| 174 | ); |
| 175 | yield* expectUrlContains( |
| 176 | `${url}/api/hello?echo=dev`, |
| 177 | "SOLIDSTART_AWS_API_MARKER_V2", |
| 178 | { timeout: "90 seconds", label: "API route after HMR edit" }, |
| 179 | ); |
| 180 | |
| 181 | yield* stack.destroy(); |
| 182 | }), |
| 183 | { timeout: 600_000 }, |
| 184 | ); |
| 185 | }, |
| 186 | ); |