The best way to control Claude Code, Codex and more from any computer or phone
| 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 Core from "@alchemy.run/test/Core"; |
| 5 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import FisTestFunctionLive, { FisTestFunction } from "./handler"; |
| 12 | |
| 13 | const testOptions = { providers: AWS.providers() }; |
| 14 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 15 | const sharedStack = Core.scratchStack(testOptions, "FISBindings"); |
| 16 | |
| 17 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 18 | // over 60s on a fresh deploy. |
| 19 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 20 | |
| 21 | let baseUrl: string; |
| 22 | let functionArn: string; |
| 23 | |
| 24 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 25 | readonly status: number; |
| 26 | readonly body: string; |
| 27 | }> {} |
| 28 | |
| 29 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 30 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 31 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 32 | // 4xx/assertion failure surfaces immediately. |
| 33 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 34 | HttpClient.execute(request).pipe( |
| 35 | Effect.flatMap((response) => |
| 36 | response.status >= 500 |
| 37 | ? response.text.pipe( |
| 38 | Effect.flatMap((body) => |
| 39 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 40 | ), |
| 41 | ) |
| 42 | : Effect.succeed(response), |
| 43 | ), |
| 44 | Effect.retry({ |
| 45 | while: (e) => e._tag === "TransientUpstream", |
| 46 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | const getJson = (path: string) => |
| 51 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 52 | |
| 53 | const postJson = (path: string) => |
| 54 | send(HttpClientRequest.post(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 55 | |
| 56 | describe.sequential( |
| 57 | "FIS Bindings", |
| 58 | { tags: ["provider:aws", "provider:aws:fis", "provider:aws:iam", "provider:aws:lambda", "live"] }, |
| 59 | () => { |
| 60 | beforeAll( |
| 61 | Effect.gen(function* () { |
| 62 | yield* Effect.logInfo("FIS test setup: destroying previous resources"); |
| 63 | yield* sharedStack.destroy(); |
| 64 | |
| 65 | yield* Effect.logInfo("FIS test setup: deploying fixture"); |
| 66 | const attrs = yield* sharedStack.deploy( |
| 67 | Effect.gen(function* () { |
| 68 | return yield* FisTestFunction; |
| 69 | }).pipe(Effect.provide(FisTestFunctionLive)), |
| 70 | ); |
| 71 | |
| 72 | expect(attrs.functionUrl).toBeTruthy(); |
| 73 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 74 | functionArn = attrs.functionArn; |
| 75 | |
| 76 | const readinessUrl = `${baseUrl}/bindings`; |
| 77 | yield* Effect.logInfo(`FIS test setup: probing readiness at ${readinessUrl}`); |
| 78 | yield* HttpClient.get(readinessUrl).pipe( |
| 79 | Effect.flatMap((response) => |
| 80 | response.status === 200 |
| 81 | ? Effect.succeed(response) |
| 82 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 83 | ), |
| 84 | Effect.tapError((error) => |
| 85 | Effect.logWarning(`FIS test setup: fixture not ready yet (${String(error)})`), |
| 86 | ), |
| 87 | Effect.retry({ schedule: readinessPolicy }), |
| 88 | ); |
| 89 | }), |
| 90 | { timeout: 240_000 }, |
| 91 | ); |
| 92 | |
| 93 | afterAll(sharedStack.destroy(), { timeout: 120_000 }); |
| 94 | |
| 95 | describe("binding registration", () => { |
| 96 | test.provider("all capabilities initialize in the runtime", (_stack) => |
| 97 | Effect.gen(function* () { |
| 98 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 99 | expect(response.bound).toHaveLength(15); |
| 100 | }), |
| 101 | ); |
| 102 | }); |
| 103 | |
| 104 | describe("GetExperimentTemplate + ListExperimentTemplates", () => { |
| 105 | test.provider( |
| 106 | "reads the bound template (injected id) and finds it in the listing", |
| 107 | (_stack) => |
| 108 | Effect.gen(function* () { |
| 109 | const template = (yield* getJson("/template")) as { id: string; actions: string[] }; |
| 110 | expect(template.id).toMatch(/^EXT/); |
| 111 | expect(template.actions).toContain("Wait"); |
| 112 | |
| 113 | const listing = (yield* getJson("/templates")) as { ids: string[] }; |
| 114 | expect(listing.ids).toContain(template.id); |
| 115 | }), |
| 116 | { timeout: 60_000 }, |
| 117 | ); |
| 118 | }); |
| 119 | |
| 120 | describe("StartExperiment + GetExperiment + ListExperiments + StopExperiment", () => { |
| 121 | test.provider( |
| 122 | "runs the harmless wait experiment end to end and stops it", |
| 123 | (_stack) => |
| 124 | Effect.gen(function* () { |
| 125 | const template = (yield* getJson("/template")) as { id: string }; |
| 126 | |
| 127 | // start — the bound template's id is injected by the binding. |
| 128 | const started = (yield* postJson("/experiments")) as { id: string; status: string }; |
| 129 | expect(started.id).toMatch(/^EXP/); |
| 130 | expect(["pending", "initiating", "running"]).toContain(started.status); |
| 131 | |
| 132 | // read it back and see it in the template-scoped listing. |
| 133 | const read = (yield* getJson(`/experiment?id=${started.id}`)) as { |
| 134 | id: string; |
| 135 | templateId: string; |
| 136 | status: string; |
| 137 | }; |
| 138 | expect(read.id).toBe(started.id); |
| 139 | expect(read.templateId).toBe(template.id); |
| 140 | |
| 141 | const listing = (yield* getJson(`/experiments?templateId=${template.id}`)) as { |
| 142 | ids: string[]; |
| 143 | }; |
| 144 | expect(listing.ids).toContain(started.id); |
| 145 | |
| 146 | // the wait-only experiment has no targets to resolve. |
| 147 | const resolved = (yield* getJson(`/resolved-targets?id=${started.id}`)) as { |
| 148 | count: number; |
| 149 | }; |
| 150 | expect(resolved.count).toBe(0); |
| 151 | |
| 152 | // stop it so nothing lingers past the test. |
| 153 | const stopped = (yield* postJson(`/stop?id=${started.id}`)) as { status: string }; |
| 154 | expect(["stopping", "stopped"]).toContain(stopped.status); |
| 155 | }), |
| 156 | { timeout: 120_000 }, |
| 157 | ); |
| 158 | }); |
| 159 | |
| 160 | describe("GetAction + ListActions", () => { |
| 161 | test.provider("reads the aws:fis:wait action catalog entry", (_stack) => |
| 162 | Effect.gen(function* () { |
| 163 | const action = (yield* getJson("/action?id=aws:fis:wait")) as { id: string }; |
| 164 | expect(action.id).toBe("aws:fis:wait"); |
| 165 | |
| 166 | const listing = (yield* getJson("/actions")) as { ids: string[] }; |
| 167 | expect(listing.ids).toContain("aws:fis:wait"); |
| 168 | }), |
| 169 | ); |
| 170 | }); |
| 171 | |
| 172 | describe("GetTargetResourceType + ListTargetResourceTypes", () => { |
| 173 | test.provider("reads the aws:ec2:instance target resource type", (_stack) => |
| 174 | Effect.gen(function* () { |
| 175 | const type = (yield* getJson("/target-resource-type?type=aws:ec2:instance")) as { |
| 176 | resourceType: string; |
| 177 | }; |
| 178 | expect(type.resourceType).toBe("aws:ec2:instance"); |
| 179 | |
| 180 | const listing = (yield* getJson("/target-resource-types")) as { resourceTypes: string[] }; |
| 181 | expect(listing.resourceTypes).toContain("aws:ec2:instance"); |
| 182 | }), |
| 183 | ); |
| 184 | }); |
| 185 | |
| 186 | describe("GetSafetyLever", () => { |
| 187 | test.provider("reads the account's default safety lever", (_stack) => |
| 188 | Effect.gen(function* () { |
| 189 | const lever = (yield* getJson("/safety-lever")) as { id: string; status: string }; |
| 190 | expect(lever.id).toBe("default"); |
| 191 | // Anything but disengaged would halt every experiment in the |
| 192 | // account, so the suite asserts the steady state. |
| 193 | expect(lever.status).toBe("disengaged"); |
| 194 | }), |
| 195 | ); |
| 196 | }); |
| 197 | |
| 198 | describe("consumeExperimentEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 199 | test.provider("the deploy created an EventBridge rule targeting the function", (_stack) => |
| 200 | Effect.gen(function* () { |
| 201 | // Out-of-band via distilled: the fixture's consumeExperimentEvents |
| 202 | // must have materialized as a rule on the default bus with the |
| 203 | // Lambda as target. |
| 204 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 205 | TargetArn: functionArn, |
| 206 | }); |
| 207 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 208 | }), |
| 209 | ); |
| 210 | }); |
| 211 | }, |
| 212 | ); |