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, 04:45 UTC
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import * as AWS from "@alchemy.run/aws"; |
| 3 | import { expect } from "@alchemy.run/test"; |
| 4 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 5 | import * as LambdaWeb from "@distilled.cloud/aws/lambda-web"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import EffectWebFunctionLive, { EffectWebFunction } from "./fixtures/web/effect-web.ts"; |
| 10 | |
| 11 | const asyncServerMain = fileURLToPath(new URL("./fixtures/web/async-server.ts", import.meta.url)); |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | const tags = ["provider:aws", "provider:aws:lambda", "live"]; |
| 16 | |
| 17 | class UnexpectedStatus extends Error {} |
| 18 | |
| 19 | /** GET `url` until it answers 200 with a body satisfying `until`. */ |
| 20 | const getTextUntil = (url: string, until: (body: string) => boolean = () => true) => |
| 21 | HttpClient.get(url).pipe( |
| 22 | Effect.flatMap((response) => |
| 23 | response.status === 200 |
| 24 | ? response.text |
| 25 | : Effect.fail(new UnexpectedStatus(`${url} returned ${response.status}`)), |
| 26 | ), |
| 27 | Effect.retry({ schedule: Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(30)]) }), |
| 28 | Effect.repeat({ schedule: Schedule.spaced("2 seconds"), until, times: 30 }), |
| 29 | ); |
| 30 | |
| 31 | const listEndpointNames = (functionName: string) => |
| 32 | LambdaWeb.listWebFunctionEndpoints({ functionName }).pipe( |
| 33 | Effect.map((r) => r.endpoints.map((e) => e.endpointName).sort()), |
| 34 | ); |
| 35 | |
| 36 | test.provider( |
| 37 | "additional endpoint: create, follow revisions, update, rename, remove", |
| 38 | (stack) => |
| 39 | Effect.gen(function* () { |
| 40 | yield* stack.destroy(); |
| 41 | |
| 42 | const deploy = (options: { |
| 43 | greeting: string; |
| 44 | webhooks?: Partial<AWS.Lambda.WebFunctionEndpointProps>; |
| 45 | }) => |
| 46 | stack.deploy( |
| 47 | Effect.gen(function* () { |
| 48 | const api = yield* AWS.Lambda.WebFunction("Doors", { |
| 49 | main: asyncServerMain, |
| 50 | authType: "ApplicationManaged", |
| 51 | env: { GREETING: options.greeting }, |
| 52 | }); |
| 53 | const webhooks = options.webhooks |
| 54 | ? yield* AWS.Lambda.WebFunctionEndpoint("Webhooks", { |
| 55 | function: api, |
| 56 | authType: "ApplicationManaged", |
| 57 | ...options.webhooks, |
| 58 | }) |
| 59 | : undefined; |
| 60 | return { api, webhooks }; |
| 61 | }), |
| 62 | ); |
| 63 | |
| 64 | // Create: two doors onto the same code, told apart by the header. |
| 65 | const created = yield* deploy({ greeting: "one", webhooks: { rateLimit: 100 } }); |
| 66 | const api = created.api; |
| 67 | const webhooks = created.webhooks!; |
| 68 | expect(webhooks.url).not.toBe(api.url); |
| 69 | expect(webhooks.endpointName).toBe("Webhooks"); |
| 70 | expect(webhooks.revisionId).toBe(api.revisionId); |
| 71 | expect(yield* getTextUntil(`${api.url}/endpoint`)).toBe("default"); |
| 72 | expect(yield* getTextUntil(`${webhooks.url}/endpoint`)).toBe("Webhooks"); |
| 73 | expect(yield* listEndpointNames(api.functionName)).toEqual(["Webhooks", "default"]); |
| 74 | const observed = yield* LambdaWeb.getWebFunctionEndpoint({ |
| 75 | functionName: api.functionName, |
| 76 | endpointName: "Webhooks", |
| 77 | }); |
| 78 | expect(observed.throttleConfig?.rateLimit).toBe(100); |
| 79 | expect(observed.revisionWeights).toEqual([{ revisionId: api.revisionId, weight: 100 }]); |
| 80 | |
| 81 | // A new revision moves both doors; the endpoint updates in place. |
| 82 | const updated = yield* deploy({ greeting: "two", webhooks: { rateLimit: 200 } }); |
| 83 | expect(updated.api.revisionId).not.toBe(api.revisionId); |
| 84 | expect(updated.webhooks!.url).toBe(webhooks.url); |
| 85 | expect(updated.webhooks!.revisionId).toBe(updated.api.revisionId); |
| 86 | expect(yield* getTextUntil(webhooks.url, (body) => body === "greeting=two")).toBe( |
| 87 | "greeting=two", |
| 88 | ); |
| 89 | const updatedObserved = yield* LambdaWeb.getWebFunctionEndpoint({ |
| 90 | functionName: api.functionName, |
| 91 | endpointName: "Webhooks", |
| 92 | }); |
| 93 | expect(updatedObserved.throttleConfig?.rateLimit).toBe(200); |
| 94 | |
| 95 | // Renaming is a new endpoint with a new URL; the old one is removed. |
| 96 | const renamed = yield* deploy({ greeting: "two", webhooks: { endpointName: "hooks" } }); |
| 97 | expect(renamed.webhooks!.endpointName).toBe("hooks"); |
| 98 | expect(renamed.webhooks!.url).not.toBe(webhooks.url); |
| 99 | expect(yield* listEndpointNames(api.functionName)).toEqual(["default", "hooks"]); |
| 100 | expect(yield* getTextUntil(`${renamed.webhooks!.url}/endpoint`)).toBe("hooks"); |
| 101 | |
| 102 | // Removing the resource removes only that endpoint. |
| 103 | const removed = yield* deploy({ greeting: "two" }); |
| 104 | expect(removed.api.url).toBe(api.url); |
| 105 | expect(yield* listEndpointNames(api.functionName)).toEqual(["default"]); |
| 106 | |
| 107 | // Endpoint type and Regions are immutable: changing them replaces it. |
| 108 | yield* deploy({ greeting: "two", webhooks: {} }); |
| 109 | const multiRegion = yield* stack.plan( |
| 110 | Effect.gen(function* () { |
| 111 | const fn = yield* AWS.Lambda.WebFunction("Doors", { |
| 112 | main: asyncServerMain, |
| 113 | authType: "ApplicationManaged", |
| 114 | env: { GREETING: "two" }, |
| 115 | }); |
| 116 | return yield* AWS.Lambda.WebFunctionEndpoint("Webhooks", { |
| 117 | function: fn, |
| 118 | authType: "ApplicationManaged", |
| 119 | regions: ["us-east-1"], |
| 120 | }); |
| 121 | }), |
| 122 | ); |
| 123 | expect(multiRegion.resources.Webhooks).toMatchObject({ action: "replace" }); |
| 124 | |
| 125 | yield* stack.destroy(); |
| 126 | const gone = yield* LambdaWeb.getWebFunction({ functionName: api.functionName }).pipe( |
| 127 | Effect.map(() => false), |
| 128 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)), |
| 129 | ); |
| 130 | expect(gone).toBe(true); |
| 131 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 132 | { tags, timeout: 360_000 }, |
| 133 | ); |
| 134 | |
| 135 | test.provider( |
| 136 | "takes a class-form WebFunction directly", |
| 137 | (stack) => |
| 138 | Effect.gen(function* () { |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const { api, webhooks } = yield* stack.deploy( |
| 142 | Effect.gen(function* () { |
| 143 | const webhooks = yield* AWS.Lambda.WebFunctionEndpoint("Webhooks", { |
| 144 | // the class declaration itself, not a yielded instance |
| 145 | function: EffectWebFunction, |
| 146 | authType: "ApplicationManaged", |
| 147 | }); |
| 148 | const api = yield* EffectWebFunction; |
| 149 | return { api, webhooks }; |
| 150 | }).pipe(Effect.provide(EffectWebFunctionLive)), |
| 151 | ); |
| 152 | expect(webhooks.functionName).toBe(api.functionName); |
| 153 | expect(webhooks.revisionId).toBe(api.revisionId); |
| 154 | expect(yield* getTextUntil(webhooks.url)).toBe("hello from effect"); |
| 155 | |
| 156 | yield* stack.destroy(); |
| 157 | expect( |
| 158 | yield* listEndpointNames(api.functionName).pipe(Effect.orElseSucceed(() => [])), |
| 159 | ).toEqual([]); |
| 160 | }).pipe(Effect.onError(() => stack.destroy().pipe(Effect.ignore))), |
| 161 | { tags, timeout: 240_000 }, |
| 162 | ); |