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 Core from "@alchemy.run/test/Core"; |
| 5 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 6 | import * as resourcegroups from "@distilled.cloud/aws/resource-groups"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import RGTestFunctionLive, { RGTestFunction } from "./handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "ResourceGroupsBindings"); |
| 17 | |
| 18 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 19 | // over 60s on a fresh deploy. |
| 20 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 21 | |
| 22 | let baseUrl: string; |
| 23 | let functionArn: string; |
| 24 | |
| 25 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 26 | readonly status: number; |
| 27 | readonly body: string; |
| 28 | }> {} |
| 29 | |
| 30 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 31 | // (cold re-init, IAM propagation on the freshly attached policy). Retry only |
| 32 | // 5xx; a genuine 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, body: object) => |
| 54 | send( |
| 55 | HttpClientRequest.post(`${baseUrl}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)), |
| 56 | ).pipe(Effect.flatMap((r) => r.json)); |
| 57 | |
| 58 | describe.sequential( |
| 59 | "ResourceGroups Bindings", |
| 60 | { |
| 61 | tags: [ |
| 62 | "provider:aws", |
| 63 | "provider:aws:iam", |
| 64 | "provider:aws:lambda", |
| 65 | "provider:aws:resourcegroups", |
| 66 | "live", |
| 67 | ], |
| 68 | }, |
| 69 | () => { |
| 70 | beforeAll( |
| 71 | Effect.gen(function* () { |
| 72 | yield* Effect.logInfo("RG test setup: destroying previous resources"); |
| 73 | yield* sharedStack.destroy(); |
| 74 | |
| 75 | yield* Effect.logInfo("RG test setup: deploying fixture"); |
| 76 | const attrs = yield* sharedStack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* RGTestFunction; |
| 79 | }).pipe(Effect.provide(RGTestFunctionLive)), |
| 80 | ); |
| 81 | |
| 82 | expect(attrs.functionUrl).toBeTruthy(); |
| 83 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 84 | functionArn = attrs.functionArn; |
| 85 | |
| 86 | const readinessUrl = `${baseUrl}/bindings`; |
| 87 | yield* Effect.logInfo(`RG test setup: probing readiness at ${readinessUrl}`); |
| 88 | yield* HttpClient.get(readinessUrl).pipe( |
| 89 | Effect.flatMap((response) => |
| 90 | response.status === 200 |
| 91 | ? Effect.succeed(response) |
| 92 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 93 | ), |
| 94 | Effect.tapError((error) => |
| 95 | Effect.logWarning(`RG test setup: fixture not ready yet (${String(error)})`), |
| 96 | ), |
| 97 | Effect.retry({ schedule: readinessPolicy }), |
| 98 | ); |
| 99 | }), |
| 100 | { timeout: 240_000 }, |
| 101 | ); |
| 102 | |
| 103 | afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), { timeout: 120_000 }); |
| 104 | |
| 105 | describe("binding registration", () => { |
| 106 | test.provider("all ten capabilities initialize in the runtime", (_stack) => |
| 107 | Effect.gen(function* () { |
| 108 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 109 | expect(response.bound).toHaveLength(10); |
| 110 | }), |
| 111 | ); |
| 112 | }); |
| 113 | |
| 114 | describe("ListGroupResources", () => { |
| 115 | test.provider("enumerates the bound group's members (injected group name)", (_stack) => |
| 116 | Effect.gen(function* () { |
| 117 | const response = (yield* getJson("/members")) as { arns: string[] }; |
| 118 | // The pool group starts (and stays) empty; the call succeeding |
| 119 | // proves the injected group + the tagging read-through grants. |
| 120 | expect(response.arns).toEqual([]); |
| 121 | }), |
| 122 | ); |
| 123 | }); |
| 124 | |
| 125 | describe("GroupResources / UngroupResources", () => { |
| 126 | test.provider("accepts a grouping request and reports the per-resource outcome", (_stack) => |
| 127 | Effect.gen(function* () { |
| 128 | // A Lambda ARN is not a capacity reservation, so the service |
| 129 | // accepts the request and reports the ARN as Failed with a typed |
| 130 | // error code — proving the full wire loop without cost. |
| 131 | const grouped = (yield* postJson("/group", { arn: functionArn })) as { |
| 132 | succeeded: string[]; |
| 133 | failedCodes: string[]; |
| 134 | }; |
| 135 | expect(grouped.succeeded).toEqual([]); |
| 136 | expect(grouped.failedCodes).toEqual(["ResourceArnValidationException"]); |
| 137 | |
| 138 | const ungrouped = (yield* postJson("/ungroup", { arn: functionArn })) as { |
| 139 | failedCodes: string[]; |
| 140 | }; |
| 141 | expect(ungrouped.failedCodes).toEqual(["ResourceArnValidationException"]); |
| 142 | }), |
| 143 | ); |
| 144 | }); |
| 145 | |
| 146 | describe("ListGroupingStatuses", () => { |
| 147 | test.provider("rejects non-application groups with the typed BadRequestException", (_stack) => |
| 148 | Effect.gen(function* () { |
| 149 | // Grouping statuses only exist for application groups, which |
| 150 | // CreateGroup cannot make — the typed rejection proves the wire. |
| 151 | const response = (yield* getJson("/grouping-statuses")) as { |
| 152 | errorTag?: string; |
| 153 | message?: string; |
| 154 | }; |
| 155 | expect(response.errorTag).toBe("BadRequestException"); |
| 156 | expect(response.message).toContain("application group"); |
| 157 | }), |
| 158 | ); |
| 159 | }); |
| 160 | |
| 161 | describe("SearchResources", () => { |
| 162 | test.provider("runs an ad-hoc tag query", (_stack) => |
| 163 | Effect.gen(function* () { |
| 164 | const response = (yield* getJson("/search")) as { arns: string[] }; |
| 165 | // Tag indexing is eventually consistent, so assert only that the |
| 166 | // query executed (grants held) and returned an array. |
| 167 | expect(Array.isArray(response.arns)).toBe(true); |
| 168 | }), |
| 169 | ); |
| 170 | }); |
| 171 | |
| 172 | describe("GetAccountSettings", () => { |
| 173 | test.provider("reads the group lifecycle events status", (_stack) => |
| 174 | Effect.gen(function* () { |
| 175 | const response = (yield* getJson("/account-settings")) as { status: string }; |
| 176 | expect(["ACTIVE", "INACTIVE", "IN_PROGRESS", "ERROR"]).toContain(response.status); |
| 177 | }), |
| 178 | ); |
| 179 | }); |
| 180 | |
| 181 | describe("ListTagSyncTasks", () => { |
| 182 | test.provider("enumerates the account's tag-sync tasks", (_stack) => |
| 183 | Effect.gen(function* () { |
| 184 | const response = (yield* getJson("/tag-sync-tasks")) as { count: number }; |
| 185 | expect(response.count).toBeGreaterThanOrEqual(0); |
| 186 | }), |
| 187 | ); |
| 188 | }); |
| 189 | |
| 190 | describe("StartTagSyncTask / GetTagSyncTask / CancelTagSyncTask", () => { |
| 191 | test.provider("tag-sync operations reach the service and fail with typed tags", (_stack) => |
| 192 | Effect.gen(function* () { |
| 193 | // Tag-sync is part of Group Lifecycle Events (GLE), which entered |
| 194 | // maintenance mode on 2026-07-30 and is closed to new customers: |
| 195 | // non-entitled accounts get ForbiddenException with the GLE notice. |
| 196 | // Entitled accounts get the application-group-only |
| 197 | // BadRequestException (application groups are myApplications-only, |
| 198 | // not creatable via CreateGroup). Either typed rejection proves |
| 199 | // IAM + wiring, including the iam:PassRole grant path. Retry while |
| 200 | // the freshly attached role policy is still propagating — resource- |
| 201 | // groups surfaces IAM denials as ForbiddenException "not |
| 202 | // authorized" (the service has no AccessDeniedException). |
| 203 | const started = (yield* postJson("/start-tag-sync", {}).pipe( |
| 204 | Effect.repeat({ |
| 205 | schedule: Schedule.spaced("3 seconds"), |
| 206 | until: (r: unknown) => |
| 207 | !(r as { message?: string }).message?.includes("not authorized"), |
| 208 | times: 8, |
| 209 | }), |
| 210 | )) as { errorTag?: string; message?: string }; |
| 211 | expect(["BadRequestException", "ForbiddenException"], started.message).toContain( |
| 212 | started.errorTag, |
| 213 | ); |
| 214 | if (started.errorTag === "ForbiddenException") { |
| 215 | expect(started.message).toContain("Group Lifecycle Events"); |
| 216 | } |
| 217 | |
| 218 | const got = (yield* getJson( |
| 219 | "/tag-sync-task?arn=arn:aws:resource-groups:us-west-2:000000000000:group/none/00000000-0000-0000-0000-000000000000", |
| 220 | )) as { errorTag?: string }; |
| 221 | expect(got.errorTag).toBeTruthy(); |
| 222 | expect(got.errorTag).not.toBe("UnknownAwsError"); |
| 223 | |
| 224 | const cancelled = (yield* postJson("/cancel-tag-sync", { |
| 225 | arn: "arn:aws:resource-groups:us-west-2:000000000000:group/none/00000000-0000-0000-0000-000000000000", |
| 226 | })) as { errorTag?: string }; |
| 227 | expect(cancelled.errorTag).toBeTruthy(); |
| 228 | expect(cancelled.errorTag).not.toBe("UnknownAwsError"); |
| 229 | }), |
| 230 | ); |
| 231 | }); |
| 232 | |
| 233 | describe("consumeGroupEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 234 | test.provider("the deploy created an EventBridge rule targeting the function", (_stack) => |
| 235 | Effect.gen(function* () { |
| 236 | // Out-of-band via distilled: the fixture's consumeGroupEvents must |
| 237 | // have materialized as a rule on the default bus with the Lambda |
| 238 | // as target. |
| 239 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 240 | TargetArn: functionArn, |
| 241 | }); |
| 242 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 243 | }), |
| 244 | ); |
| 245 | }); |
| 246 | |
| 247 | describe("typed error probes (distilled patch)", () => { |
| 248 | test.provider( |
| 249 | "listGroupingStatuses on a missing group is the typed NotFoundException", |
| 250 | (_stack) => |
| 251 | Effect.gen(function* () { |
| 252 | // Proves the distilled patch adding NotFoundException to |
| 253 | // ListGroupingStatuses' error union (previously UnknownAwsError). |
| 254 | const error = yield* resourcegroups |
| 255 | .listGroupingStatuses({ Group: "alchemy-rg-does-not-exist" }) |
| 256 | .pipe(Effect.flip); |
| 257 | expect(error._tag).toBe("NotFoundException"); |
| 258 | }), |
| 259 | ); |
| 260 | }); |
| 261 | }, |
| 262 | ); |