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 { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as apprunner from "@distilled.cloud/aws/apprunner"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import AppRunnerTestFunctionLive, { AppRunnerTestFunction } from "./fixtures/handler"; |
| 10 | |
| 11 | // App Runner closed to new customers on 2026-04-30; accounts that weren't |
| 12 | // already customers get "Account <id> can't create new App Runner services". |
| 13 | // Set AWS_TEST_APPRUNNER_SERVICES=1 on an account that can still create them. |
| 14 | const appRunnerServices = !!process.env.AWS_TEST_APPRUNNER_SERVICES; |
| 15 | |
| 16 | const { test } = Test.make({ providers: AWS.providers() }); |
| 17 | |
| 18 | /** |
| 19 | * POST a fixture route and decode its JSON body. |
| 20 | * |
| 21 | * Each binding is granted by its OWN inline policy on the Lambda role, and |
| 22 | * those propagate independently — a call can be rejected for seconds after |
| 23 | * a sibling binding's call already works, so every route retries. On a |
| 24 | * non-200 the fixture's body (the rendered cause) goes into the failure |
| 25 | * message; decoding it blind used to surface as a TypeError on a missing |
| 26 | * field, which said nothing about what App Runner refused. |
| 27 | */ |
| 28 | const postJson = <T>(url: string) => |
| 29 | HttpClient.execute(HttpClientRequest.post(url)).pipe( |
| 30 | Effect.flatMap((response) => |
| 31 | response.text.pipe( |
| 32 | Effect.flatMap((body) => |
| 33 | response.status === 200 |
| 34 | ? Effect.try({ |
| 35 | try: () => JSON.parse(body) as T, |
| 36 | catch: () => new Error(`POST ${url} returned unparseable body: ${body}`), |
| 37 | }) |
| 38 | : Effect.fail(new Error(`POST ${url} returned ${response.status}: ${body}`)), |
| 39 | ), |
| 40 | ), |
| 41 | ), |
| 42 | Effect.retry({ |
| 43 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]), |
| 44 | }), |
| 45 | ); |
| 46 | |
| 47 | /** Poll the service status out-of-band until it settles to `expected`. */ |
| 48 | const waitForStatus = (serviceArn: string, expected: string) => |
| 49 | apprunner.describeService({ ServiceArn: serviceArn }).pipe( |
| 50 | Effect.flatMap((r) => |
| 51 | (r.Service.Status ?? "").toUpperCase() === expected |
| 52 | ? Effect.void |
| 53 | : Effect.fail(new Error(`service is ${r.Service.Status}, waiting for ${expected}`)), |
| 54 | ), |
| 55 | Effect.retry({ |
| 56 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(36)]), |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | // The fixture deploys a real App Runner service (3-5 min provisioning, |
| 61 | // bills while running) plus a Lambda wired to it through the four runtime |
| 62 | // bindings, so the whole flow is skipped under `--fast` and always |
| 63 | // destroys what it created. |
| 64 | test.provider.skipIf(!!process.env.FAST || !appRunnerServices)( |
| 65 | "a Lambda manages an App Runner service through the runtime bindings", |
| 66 | (stack) => |
| 67 | Effect.gen(function* () { |
| 68 | // Clean slate in case a previous run died mid-flight. |
| 69 | yield* stack.destroy(); |
| 70 | |
| 71 | const { functionUrl } = yield* stack.deploy( |
| 72 | Effect.gen(function* () { |
| 73 | return yield* AppRunnerTestFunction; |
| 74 | }).pipe(Effect.provide(AppRunnerTestFunctionLive)), |
| 75 | ); |
| 76 | expect(functionUrl).toBeTruthy(); |
| 77 | const baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 78 | |
| 79 | // Readiness probe — fresh function URLs take seconds to serve 200s, |
| 80 | // and the fresh IAM role's apprunner permissions propagate eventually. |
| 81 | yield* HttpClient.get(`${baseUrl}/ping`).pipe( |
| 82 | Effect.flatMap((response) => |
| 83 | response.status === 200 |
| 84 | ? Effect.succeed(response) |
| 85 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 86 | ), |
| 87 | Effect.retry({ |
| 88 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(60)]), |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | // ListOperations: the CREATE_SERVICE operation is on the record. |
| 93 | const operations = yield* HttpClient.get(`${baseUrl}/operations`).pipe( |
| 94 | Effect.flatMap((response) => response.json), |
| 95 | Effect.map( |
| 96 | (json) => |
| 97 | json as { |
| 98 | operations: { id?: string; type?: string; status?: string }[]; |
| 99 | }, |
| 100 | ), |
| 101 | // The Lambda role's apprunner:ListOperations grant propagates |
| 102 | // eventually — a 500 body parses as not-matching and retries. |
| 103 | Effect.filterOrFail( |
| 104 | (json) => Array.isArray(json.operations), |
| 105 | () => new Error("operations not yet listable"), |
| 106 | ), |
| 107 | Effect.retry({ |
| 108 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]), |
| 109 | }), |
| 110 | ); |
| 111 | const createOp = operations.operations.find((op) => op.type === "CREATE_SERVICE"); |
| 112 | expect(createOp).toBeDefined(); |
| 113 | expect(createOp?.status).toBe("SUCCEEDED"); |
| 114 | |
| 115 | // DescribeCustomDomains: no domain is associated, so the list is |
| 116 | // empty but the DNSTarget (the service's default endpoint) resolves — |
| 117 | // proving the binding's wiring and IAM grant. |
| 118 | const customDomains = yield* HttpClient.get(`${baseUrl}/custom-domains`).pipe( |
| 119 | Effect.flatMap((response) => response.json), |
| 120 | Effect.map( |
| 121 | (json) => |
| 122 | json as { |
| 123 | dnsTarget?: string; |
| 124 | customDomains: { domainName?: string; status?: string }[]; |
| 125 | }, |
| 126 | ), |
| 127 | Effect.filterOrFail( |
| 128 | (json) => Array.isArray(json.customDomains), |
| 129 | () => new Error("custom domains not yet describable"), |
| 130 | ), |
| 131 | Effect.retry({ |
| 132 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(8)]), |
| 133 | }), |
| 134 | ); |
| 135 | expect(customDomains.customDomains).toEqual([]); |
| 136 | expect(customDomains.dnsTarget).toContain("awsapprunner.com"); |
| 137 | |
| 138 | // PauseService: the service settles to PAUSED. |
| 139 | const paused = yield* postJson<{ |
| 140 | serviceArn: string; |
| 141 | operationId?: string; |
| 142 | }>(`${baseUrl}/pause`); |
| 143 | expect(paused.serviceArn).toContain(":service/alchemy-test-apprunner-bind/"); |
| 144 | yield* waitForStatus(paused.serviceArn, "PAUSED"); |
| 145 | |
| 146 | // ResumeService: the service settles back to RUNNING. |
| 147 | const resumed = yield* postJson<{ |
| 148 | serviceArn: string; |
| 149 | operationId?: string; |
| 150 | }>(`${baseUrl}/resume`); |
| 151 | expect(resumed.serviceArn).toBe(paused.serviceArn); |
| 152 | yield* waitForStatus(paused.serviceArn, "RUNNING"); |
| 153 | |
| 154 | // StartDeployment: returns an operation id; the deployment then shows |
| 155 | // up in ListOperations. (The provider's delete waits for the service |
| 156 | // to settle, so the in-flight deployment doesn't block destroy.) |
| 157 | const deployed = yield* postJson<{ operationId?: string }>(`${baseUrl}/deploy`); |
| 158 | expect(deployed.operationId).toBeTruthy(); |
| 159 | |
| 160 | const afterDeploy = yield* HttpClient.get(`${baseUrl}/operations`).pipe( |
| 161 | Effect.flatMap((response) => response.json), |
| 162 | Effect.map((json) => json as { operations: { id?: string; type?: string }[] }), |
| 163 | ); |
| 164 | expect(afterDeploy.operations.some((op) => op.id === deployed.operationId)).toBe(true); |
| 165 | |
| 166 | // Destroy immediately — App Runner services bill while running. |
| 167 | yield* stack.destroy(); |
| 168 | const after = yield* apprunner.describeService({ ServiceArn: paused.serviceArn }).pipe( |
| 169 | Effect.map((r) => (r.Service.Status ?? "UNKNOWN").toUpperCase()), |
| 170 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("GONE" as const)), |
| 171 | ); |
| 172 | expect(["GONE", "DELETED"]).toContain(after); |
| 173 | }), |
| 174 | // create (~3-5 min) + pause (~1-2 min) + resume (~1-2 min) + delete |
| 175 | // (~2-3 min), one sequential test. |
| 176 | { |
| 177 | tags: ["provider:aws", "provider:aws:apprunner", "provider:aws:lambda", "live"], |
| 178 | timeout: 1_200_000, |
| 179 | }, |
| 180 | ); |