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 { Region } from "@distilled.cloud/aws/Region"; |
| 6 | import * as s3 from "@distilled.cloud/aws/s3"; |
| 7 | import * as s3control from "@distilled.cloud/aws/s3-control"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import S3ControlBindingsFunctionLive, { |
| 14 | S3ControlBindingsFunction, |
| 15 | } from "./fixtures/bindings-handler.ts"; |
| 16 | import S3ControlMrapBindingsFunctionLive, { |
| 17 | S3ControlMrapBindingsFunction, |
| 18 | SECOND_REGION, |
| 19 | secondRegionBucketName, |
| 20 | } from "./fixtures/mrap-bindings-handler.ts"; |
| 21 | |
| 22 | const testOptions = { providers: AWS.providers() }; |
| 23 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 24 | const sharedStack = Core.scratchStack(testOptions, "S3ControlBindings"); |
| 25 | const mrapStack = Core.scratchStack(testOptions, "S3ControlMrapBindings"); |
| 26 | const ACCOUNT_ID = "391965393224"; |
| 27 | |
| 28 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 29 | // over 60s on a fresh deploy. |
| 30 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 31 | |
| 32 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 33 | readonly status: number; |
| 34 | readonly body: string; |
| 35 | }> {} |
| 36 | |
| 37 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 38 | // (cold re-init, IAM propagation on the freshly attached policy). Retry only |
| 39 | // 5xx; a genuine 4xx/assertion failure surfaces immediately. |
| 40 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 41 | HttpClient.execute(request).pipe( |
| 42 | Effect.flatMap((response) => |
| 43 | response.status >= 500 |
| 44 | ? response.text.pipe( |
| 45 | Effect.flatMap((body) => |
| 46 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 47 | ), |
| 48 | ) |
| 49 | : Effect.succeed(response), |
| 50 | ), |
| 51 | Effect.retry({ |
| 52 | while: (e): boolean => e._tag === "TransientUpstream", |
| 53 | schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]), |
| 54 | }), |
| 55 | ); |
| 56 | |
| 57 | const getJson = (baseUrl: string, path: string) => |
| 58 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 59 | |
| 60 | const awaitReady = (readinessUrl: string) => |
| 61 | HttpClient.get(readinessUrl).pipe( |
| 62 | Effect.flatMap((response) => |
| 63 | response.status === 200 |
| 64 | ? Effect.succeed(response) |
| 65 | : Effect.flatMap(response.text, (body) => |
| 66 | Effect.fail(new Error(`Function not ready: ${response.status} ${body.slice(0, 300)}`)), |
| 67 | ), |
| 68 | ), |
| 69 | Effect.tapError((error) => |
| 70 | Effect.logWarning(`S3Control test setup: fixture not ready yet (${String(error)})`), |
| 71 | ), |
| 72 | Effect.retry({ schedule: readinessPolicy }), |
| 73 | ); |
| 74 | |
| 75 | let baseUrl: string; |
| 76 | |
| 77 | describe.sequential( |
| 78 | "AWS.S3Control bindings", |
| 79 | { |
| 80 | tags: [ |
| 81 | "provider:aws", |
| 82 | "provider:aws:iam", |
| 83 | "provider:aws:lambda", |
| 84 | "provider:aws:s3", |
| 85 | "provider:aws:s3control", |
| 86 | "live", |
| 87 | ], |
| 88 | }, |
| 89 | () => { |
| 90 | beforeAll( |
| 91 | Effect.gen(function* () { |
| 92 | yield* Effect.logInfo("S3Control test setup: destroying previous resources"); |
| 93 | yield* sharedStack.destroy(); |
| 94 | |
| 95 | yield* Effect.logInfo("S3Control test setup: deploying fixture"); |
| 96 | const attrs = yield* sharedStack.deploy( |
| 97 | Effect.gen(function* () { |
| 98 | return yield* S3ControlBindingsFunction; |
| 99 | }).pipe(Effect.provide(S3ControlBindingsFunctionLive)), |
| 100 | ); |
| 101 | |
| 102 | expect(attrs.functionUrl).toBeTruthy(); |
| 103 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 104 | |
| 105 | yield* awaitReady(`${baseUrl}/bindings`); |
| 106 | }), |
| 107 | { timeout: 240_000 }, |
| 108 | ); |
| 109 | |
| 110 | afterAll(sharedStack.destroy(), { timeout: 240_000 }); |
| 111 | |
| 112 | describe("binding registration", () => { |
| 113 | test.provider("all nine capabilities initialize in the runtime", (_stack) => |
| 114 | Effect.gen(function* () { |
| 115 | const response = (yield* getJson(baseUrl, "/bindings")) as { bound: string[] }; |
| 116 | expect(response.bound).toHaveLength(9); |
| 117 | }), |
| 118 | ); |
| 119 | }); |
| 120 | |
| 121 | describe("GetAccessPoint", () => { |
| 122 | test.provider("reads the access point's live configuration", (_stack) => |
| 123 | Effect.gen(function* () { |
| 124 | const live = (yield* getJson(baseUrl, "/access-point")) as { |
| 125 | name: string | undefined; |
| 126 | bucket: string | undefined; |
| 127 | networkOrigin: string | undefined; |
| 128 | }; |
| 129 | expect(live.name).toBeTruthy(); |
| 130 | expect(live.bucket).toBeTruthy(); |
| 131 | expect(live.networkOrigin).toBe("Internet"); |
| 132 | }), |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | describe("GetAccessPointPolicy", () => { |
| 137 | test.provider("typed NoSuchAccessPointPolicy on a policyless access point", (_stack) => |
| 138 | Effect.gen(function* () { |
| 139 | const result = (yield* getJson(baseUrl, "/policy")) as { hasPolicy: boolean }; |
| 140 | expect(result.hasPolicy).toBe(false); |
| 141 | }), |
| 142 | ); |
| 143 | }); |
| 144 | |
| 145 | describe("GetAccessPointPolicyStatus", () => { |
| 146 | test.provider("reports the access point as non-public", (_stack) => |
| 147 | Effect.gen(function* () { |
| 148 | const result = (yield* getJson(baseUrl, "/policy-status")) as { |
| 149 | isPublic: boolean; |
| 150 | hasPolicy: boolean; |
| 151 | }; |
| 152 | expect(result.isPublic).toBe(false); |
| 153 | }), |
| 154 | ); |
| 155 | }); |
| 156 | |
| 157 | describe("ListAccessPoints", () => { |
| 158 | test.provider("finds the fixture access point by bucket", (_stack) => |
| 159 | Effect.gen(function* () { |
| 160 | const live = (yield* getJson(baseUrl, "/access-point")) as { name: string | undefined }; |
| 161 | const { names } = (yield* getJson(baseUrl, "/access-points")) as { names: string[] }; |
| 162 | expect(names).toContain(live.name); |
| 163 | }), |
| 164 | ); |
| 165 | }); |
| 166 | |
| 167 | describe("CreateJob + DescribeJob + UpdateJobPriority + UpdateJobStatus + ListJobs", () => { |
| 168 | test.provider( |
| 169 | "runs the batch job loop: create suspended, describe, bump priority, cancel, list", |
| 170 | (_stack) => |
| 171 | Effect.gen(function* () { |
| 172 | const result = (yield* getJson(baseUrl, "/job-lifecycle")) as { |
| 173 | ok: boolean; |
| 174 | tag?: string; |
| 175 | message?: string; |
| 176 | jobId: string; |
| 177 | settledStatus: string; |
| 178 | priorityOutcome: string; |
| 179 | cancelOutcome: string; |
| 180 | finalStatus: string; |
| 181 | listedJobIds: string[]; |
| 182 | }; |
| 183 | expect(result.ok, `job flow failed: ${result.tag}: ${result.message}`).toBe(true); |
| 184 | expect(result.jobId).toBeTruthy(); |
| 185 | // The job settles into a stable state (Suspended when awaiting |
| 186 | // confirmation; Failed/Complete when the generated manifest is |
| 187 | // empty). |
| 188 | expect(["Suspended", "Failed", "Complete", "Cancelled", "Ready", "Active"]).toContain( |
| 189 | result.settledStatus, |
| 190 | ); |
| 191 | // Both bindings round-trip: either the mutation applied, or AWS |
| 192 | // refused the transition with the TYPED tag (which also proves the |
| 193 | // distilled JobStatusTransitionForbidden patch end-to-end). |
| 194 | expect(["updated", "JobStatusTransitionForbidden"]).toContain(result.priorityOutcome); |
| 195 | expect(result.cancelOutcome).toBeTruthy(); |
| 196 | expect(result.finalStatus).toBeTruthy(); |
| 197 | expect(result.listedJobIds).toContain(result.jobId); |
| 198 | }), |
| 199 | { timeout: 180_000 }, |
| 200 | ); |
| 201 | }); |
| 202 | }, |
| 203 | ); |
| 204 | |
| 205 | // Multi-Region Access Point provisioning is asynchronous and routinely takes |
| 206 | // 5-15 minutes, so the hooks get a matching budget. Keep this explicitly |
| 207 | // gated; a hard-killed run must be audited by deterministic physical name before it is retried. |
| 208 | describe.skipIf(!!process.env.FAST).sequential( |
| 209 | "AWS.S3Control MRAP route bindings (slow)", |
| 210 | { |
| 211 | tags: [ |
| 212 | "provider:aws", |
| 213 | "provider:aws:lambda", |
| 214 | "provider:aws:s3", |
| 215 | "provider:aws:s3control", |
| 216 | "live", |
| 217 | ], |
| 218 | }, |
| 219 | () => { |
| 220 | let mrapBaseUrl: string; |
| 221 | |
| 222 | const secondBucket = secondRegionBucketName(mrapStack.stage); |
| 223 | const inSecondRegion = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 224 | Core.withProviders( |
| 225 | effect.pipe(Effect.provideService(Region, Effect.succeed(SECOND_REGION))), |
| 226 | testOptions, |
| 227 | "S3ControlMrapBindings", |
| 228 | ); |
| 229 | |
| 230 | beforeAll( |
| 231 | Effect.gen(function* () { |
| 232 | yield* mrapStack.destroy(); |
| 233 | yield* inSecondRegion( |
| 234 | s3 |
| 235 | .createBucket({ Bucket: secondBucket }) |
| 236 | .pipe(Effect.catchTag("BucketAlreadyOwnedByYou", () => Effect.void)), |
| 237 | ); |
| 238 | const attrs = yield* mrapStack.deploy( |
| 239 | Effect.gen(function* () { |
| 240 | return yield* S3ControlMrapBindingsFunction; |
| 241 | }).pipe(Effect.provide(S3ControlMrapBindingsFunctionLive)), |
| 242 | ); |
| 243 | expect(attrs.functionUrl).toBeTruthy(); |
| 244 | mrapBaseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 245 | yield* awaitReady(`${mrapBaseUrl}/routes`); |
| 246 | }), |
| 247 | { timeout: 1_200_000 }, |
| 248 | ); |
| 249 | |
| 250 | afterAll( |
| 251 | mrapStack.destroy().pipe( |
| 252 | Effect.andThen( |
| 253 | inSecondRegion( |
| 254 | // S3 still reports the deleted MRAP as attached for a while. |
| 255 | s3.deleteBucket({ Bucket: secondBucket }).pipe( |
| 256 | Effect.retry({ |
| 257 | while: (error) => error._tag === "BucketHasAccessPointsAttached", |
| 258 | schedule: Schedule.spaced("5 seconds"), |
| 259 | times: 24, |
| 260 | }), |
| 261 | Effect.catchTag("NoSuchBucket", () => Effect.void), |
| 262 | ), |
| 263 | ), |
| 264 | ), |
| 265 | ), |
| 266 | { timeout: 1_200_000 }, |
| 267 | ); |
| 268 | |
| 269 | describe("GetMultiRegionAccessPointRoutes + SubmitMultiRegionAccessPointRoutes", () => { |
| 270 | test.provider( |
| 271 | "reads the routes and re-submits them at 100%", |
| 272 | (_stack) => |
| 273 | Effect.gen(function* () { |
| 274 | const before = (yield* getJson(mrapBaseUrl, "/routes")) as { |
| 275 | mrapArn: string; |
| 276 | routes: { region: string; trafficDialPercentage: number }[]; |
| 277 | }; |
| 278 | expect(before.routes.length).toBeGreaterThan(0); |
| 279 | |
| 280 | const after = (yield* getJson(mrapBaseUrl, "/dial-100")) as { |
| 281 | routes: { region: string; trafficDialPercentage: number }[]; |
| 282 | }; |
| 283 | expect(after.routes.length).toBe(before.routes.length); |
| 284 | for (const route of after.routes) { |
| 285 | expect(route.trafficDialPercentage).toBe(100); |
| 286 | } |
| 287 | |
| 288 | // Verify the binding's mutation independently of the Lambda |
| 289 | // response, through the distilled S3Control client. |
| 290 | const live = yield* s3control |
| 291 | .getMultiRegionAccessPointRoutes({ AccountId: ACCOUNT_ID, Mrap: before.mrapArn }) |
| 292 | .pipe(Effect.provideService(Region, Effect.succeed("us-west-2"))); |
| 293 | expect(live.Routes?.length).toBe(before.routes.length); |
| 294 | for (const route of live.Routes ?? []) { |
| 295 | expect(route.TrafficDialPercentage).toBe(100); |
| 296 | } |
| 297 | }), |
| 298 | { timeout: 120_000 }, |
| 299 | ); |
| 300 | }); |
| 301 | }, |
| 302 | ); |