The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 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 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", "vite-app"); |
| 16 | |
| 17 | // Clone under the alchemy package so `vite` 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 = [".gitignore", "package.json", "index.html", "src", "public"]; |
| 22 | |
| 23 | describe( |
| 24 | "AWS.Website.Vite local", |
| 25 | { tags: ["provider:aws", "provider:aws:website", "local"] }, |
| 26 | () => { |
| 27 | test.provider( |
| 28 | "dev runs Vite's own dev server with no cloud resources", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | const fs = yield* FileSystem.FileSystem; |
| 32 | const path = yield* Path.Path; |
| 33 | |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | const rootDir = yield* cloneFixture(fixtureDir, { |
| 37 | prefix: "alchemy-vite-aws-local-", |
| 38 | tempRoot, |
| 39 | entries: fixtureEntries, |
| 40 | }); |
| 41 | |
| 42 | const deployed = yield* stack.deploy( |
| 43 | Effect.gen(function* () { |
| 44 | const site = yield* AWS.Website.Vite("ViteSite", { rootDir }); |
| 45 | return { site }; |
| 46 | }), |
| 47 | ); |
| 48 | |
| 49 | // The site is Vite's own dev server: a localhost URL and no cloud |
| 50 | // rows at all (proof no AWS call ran — and specifically NOT a |
| 51 | // *.cloudfront.net URL). |
| 52 | const url = deployed.site.url! as string; |
| 53 | expect(url).toMatch(/^http:\/\/(localhost|127\.0\.0\.1|\[[0-9a-fA-F:]+\])/); |
| 54 | expect(deployed.site.distribution).toBeUndefined(); |
| 55 | expect(deployed.site.server).toBeUndefined(); |
| 56 | expect(deployed.site.bucket).toBeUndefined(); |
| 57 | expect(deployed.site.files).toBeUndefined(); |
| 58 | |
| 59 | // The page serves through Vite's dev server (index.html transformed |
| 60 | // on the fly, module scripts served from source). |
| 61 | yield* expectUrlContains(`${url}/`, "VITE_AWS_PAGE_MARKER", { |
| 62 | timeout: "120 seconds", |
| 63 | label: "dev index page", |
| 64 | }); |
| 65 | // Source modules serve straight from src/ (no build ran). |
| 66 | yield* expectUrlContains(`${url}/src/main.ts`, "VITE_AWS_MODULE_MARKER", { |
| 67 | label: "dev module source", |
| 68 | }); |
| 69 | |
| 70 | // ── HMR surface: edit index.html in place. The stack is NOT |
| 71 | // re-applied — Vite's dev server serves the transformed html per |
| 72 | // request, so the same URL must show the new marker ─────────────── |
| 73 | const indexPath = path.join(rootDir, "index.html"); |
| 74 | const index = yield* fs.readFileString(indexPath); |
| 75 | yield* fs.writeFileString( |
| 76 | indexPath, |
| 77 | index.replace("VITE_AWS_PAGE_MARKER", "VITE_AWS_PAGE_MARKER_V2"), |
| 78 | ); |
| 79 | yield* expectUrlContains(`${url}/`, "VITE_AWS_PAGE_MARKER_V2", { |
| 80 | timeout: "90 seconds", |
| 81 | label: "index page after edit", |
| 82 | }); |
| 83 | |
| 84 | yield* stack.destroy(); |
| 85 | }), |
| 86 | { timeout: 600_000 }, |
| 87 | ); |
| 88 | |
| 89 | // #1654: a dev proxy target in `vite.config.*` comes from another |
| 90 | // resource's `Output`, handed to the dev server through `env`. |
| 91 | test.provider( |
| 92 | "dev applies resource-resolved env to the Vite dev server", |
| 93 | (stack) => |
| 94 | Effect.gen(function* () { |
| 95 | const fs = yield* FileSystem.FileSystem; |
| 96 | const path = yield* Path.Path; |
| 97 | |
| 98 | yield* stack.destroy(); |
| 99 | |
| 100 | const apiDir = yield* cloneFixture(fixtureDir, { |
| 101 | prefix: "alchemy-vite-aws-local-api-", |
| 102 | tempRoot, |
| 103 | entries: fixtureEntries, |
| 104 | }); |
| 105 | const webDir = yield* cloneFixture(fixtureDir, { |
| 106 | prefix: "alchemy-vite-aws-local-web-", |
| 107 | tempRoot, |
| 108 | entries: [...fixtureEntries, "vite.proxy.config.mjs"], |
| 109 | }); |
| 110 | // The proxied site serves a distinct robots.txt so a hit through |
| 111 | // the proxy can't be confused with the frontend's own file. |
| 112 | yield* fs.writeFileString( |
| 113 | path.join(apiDir, "public", "robots.txt"), |
| 114 | "VITE_AWS_PROXY_TARGET_MARKER\n", |
| 115 | ); |
| 116 | |
| 117 | const deployed = yield* stack.deploy( |
| 118 | Effect.gen(function* () { |
| 119 | const api = yield* AWS.Website.Vite("ApiSite", { rootDir: apiDir }); |
| 120 | const web = yield* AWS.Website.Vite("WebSite", { |
| 121 | rootDir: webDir, |
| 122 | config: "vite.proxy.config.mjs", |
| 123 | env: { |
| 124 | API_PROXY_TARGET: api.url, |
| 125 | VITE_SITE_ENV: "VITE_AWS_DEV_ENV_MARKER", |
| 126 | }, |
| 127 | }); |
| 128 | return { api, web }; |
| 129 | }), |
| 130 | ); |
| 131 | |
| 132 | const url = deployed.web.url! as string; |
| 133 | expect(url).toMatch(/^http:\/\/(localhost|127\.0\.0\.1|\[[0-9a-fA-F:]+\])/); |
| 134 | expect(deployed.web.distribution).toBeUndefined(); |
| 135 | |
| 136 | // vite.config read the resolved Output from process.env: `/api` |
| 137 | // proxies to the other site's dev server. |
| 138 | yield* expectUrlContains(`${url}/api/robots.txt`, "VITE_AWS_PROXY_TARGET_MARKER", { |
| 139 | timeout: "120 seconds", |
| 140 | label: "dev proxy to resource-resolved target", |
| 141 | }); |
| 142 | // `VITE_*` entries reach the client through `import.meta.env`. |
| 143 | yield* expectUrlContains(`${url}/src/main.ts`, "VITE_AWS_DEV_ENV_MARKER", { |
| 144 | label: "VITE_* env in dev module", |
| 145 | }); |
| 146 | |
| 147 | yield* stack.destroy(); |
| 148 | }), |
| 149 | { timeout: 600_000 }, |
| 150 | ); |
| 151 | }, |
| 152 | ); |