The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 6 | import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"; |
| 7 | import { describe, expect, it } from "vitest"; |
| 8 | import { |
| 9 | LAMBDA_ADAPTER_FILE_NAME, |
| 10 | SERVE_ENTRY_NAME, |
| 11 | makeAwsTarget, |
| 12 | makeLambdaEntrySource, |
| 13 | target, |
| 14 | } from "../aws.ts"; |
| 15 | import { loadPrerenderPairs, seedStoreFromPrerender } from "../cache/seed.ts"; |
| 16 | |
| 17 | describe("makeAwsTarget", () => { |
| 18 | it( |
| 19 | "renders from an isolated Lambda directory without project dependencies", |
| 20 | () => |
| 21 | Effect.runPromise( |
| 22 | Effect.scoped( |
| 23 | Effect.gen(function* () { |
| 24 | const fs = yield* FileSystem.FileSystem; |
| 25 | const path = yield* Path.Path; |
| 26 | const root = yield* path.fromFileUrl( |
| 27 | new URL("../../../../../examples/aws-website-vinext/", import.meta.url), |
| 28 | ); |
| 29 | const configPath = path.join(root, "vite.config.ts"); |
| 30 | const configBefore = yield* fs.readFileString(configPath); |
| 31 | expect(configBefore).not.toContain("@alchemy.run/frontend-frameworks"); |
| 32 | const built = yield* makeAwsTarget({ streaming: false }).build!({ |
| 33 | root, |
| 34 | framework: "vinext", |
| 35 | }); |
| 36 | expect(yield* fs.readFileString(configPath)).toBe(configBefore); |
| 37 | expect( |
| 38 | yield* fs.readFileString(path.join(built.distDirectory!, "server/index.js")), |
| 39 | ).toContain("CACHE_BUCKET_NAME"); |
| 40 | const directory = yield* fs.makeTempDirectoryScoped({ |
| 41 | prefix: "vinext-lambda-", |
| 42 | }); |
| 43 | yield* fs.copy(path.join(built.distDirectory!, "server"), directory); |
| 44 | const prerender = yield* Effect.promise(() => loadPrerenderPairs(directory)); |
| 45 | expect(prerender.routeCount).toBeGreaterThan(0); |
| 46 | expect(prerender.pairs.some((pair) => pair.key.includes("/isr"))).toBe(true); |
| 47 | const values = new Map<string, string>(); |
| 48 | const store = { |
| 49 | getText: (key: string) => Effect.runPromise(Effect.sync(() => values.get(key))), |
| 50 | putText: (key: string, value: string) => |
| 51 | Effect.runPromise( |
| 52 | Effect.sync(() => { |
| 53 | values.set(key, value); |
| 54 | }), |
| 55 | ), |
| 56 | delete: (key: string) => |
| 57 | Effect.runPromise( |
| 58 | Effect.sync(() => { |
| 59 | values.delete(key); |
| 60 | }), |
| 61 | ), |
| 62 | }; |
| 63 | yield* Effect.promise(() => seedStoreFromPrerender(store, directory)); |
| 64 | const key = prerender.pairs[0]!.key; |
| 65 | values.set(key, "refreshed at runtime"); |
| 66 | yield* Effect.promise(() => seedStoreFromPrerender(store, directory)); |
| 67 | expect(values.get(key)).toBe("refreshed at runtime"); |
| 68 | const spawner = yield* ChildProcessSpawner; |
| 69 | const node = yield* Effect.sync(() => process.execPath); |
| 70 | const child = yield* spawner.spawn( |
| 71 | ChildProcess.make( |
| 72 | node, |
| 73 | [ |
| 74 | "--input-type=module", |
| 75 | "--eval", |
| 76 | ` |
| 77 | import assert from "node:assert/strict"; |
| 78 | import { handler } from "./serve-aws-lambda.mjs"; |
| 79 | for (const route of ["/", "/isr", "/api/hello"]) { |
| 80 | const response = await handler({ rawPath: route, rawQueryString: "name=Alchemy", requestContext: { domainName: "localhost", http: { method: "GET" } } }); |
| 81 | assert.equal(response.statusCode, 200); |
| 82 | assert.ok(response.body.length > 0); |
| 83 | if (route === "/") assert.match(response.body, /vinext on AWS/); |
| 84 | if (route === "/api/hello") assert.equal(JSON.parse(response.body).greeting, "isolated Lambda"); |
| 85 | } |
| 86 | `, |
| 87 | ], |
| 88 | { |
| 89 | cwd: directory, |
| 90 | env: { GREETING: "isolated Lambda" }, |
| 91 | stdout: "inherit", |
| 92 | stderr: "inherit", |
| 93 | }, |
| 94 | ), |
| 95 | ); |
| 96 | expect(yield* child.exitCode).toBe(0); |
| 97 | }), |
| 98 | ).pipe(Effect.provide(NodeServices.layer)), |
| 99 | ), |
| 100 | 120_000, |
| 101 | ); |
| 102 | it("declares the aws platform and a wholesale vinext build (not OpenNext)", () => { |
| 103 | const aws = makeAwsTarget(); |
| 104 | expect(aws.platform).toBe("aws"); |
| 105 | expect(aws.build).toBeTypeOf("function"); |
| 106 | expect(aws.bundle?.conditions).toContain("node"); |
| 107 | expect(aws.bundle?.external).toContain("@aws-sdk/"); |
| 108 | expect(aws.bundle?.external ?? []).not.toContain("cloudflare:"); |
| 109 | }); |
| 110 | |
| 111 | it("wraps the App Router fetch handler as a streaming Lambda handler", () => { |
| 112 | const source = makeLambdaEntrySource(true); |
| 113 | expect(SERVE_ENTRY_NAME).toBe("server/serve-aws-lambda.mjs"); |
| 114 | expect(LAMBDA_ADAPTER_FILE_NAME).toBe("vinext-aws-lambda.mjs"); |
| 115 | expect(source).toContain('import rsc from "./index.js"'); |
| 116 | expect(source).toContain("toLambdaHandler"); |
| 117 | expect(source).toContain("export const handler"); |
| 118 | expect(source).toContain('hostRuntime: "node"'); |
| 119 | expect(source).not.toContain("toBufferedLambdaHandler"); |
| 120 | expect(source).not.toContain("opennext"); |
| 121 | expect(source).not.toContain("vinext/server/fetch-handler"); |
| 122 | expect(source).not.toContain("startProdServer"); |
| 123 | }); |
| 124 | |
| 125 | it("can emit the buffered Lambda wrapper", () => { |
| 126 | const source = makeLambdaEntrySource(false); |
| 127 | expect(source).toContain("toBufferedLambdaHandler"); |
| 128 | expect(source).not.toContain("toLambdaHandler"); |
| 129 | }); |
| 130 | |
| 131 | it("exposes the named `target` module export as the factory", () => { |
| 132 | expect(target).toBe(makeAwsTarget); |
| 133 | }); |
| 134 | }); |