| 1 | import * as AWS from "@/AWS"; |
| 2 | import * as Core from "@/Test/Core"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import { describe, expect } from "alchemy-test"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import AossBindingsFunctionLive, { |
| 11 | AossBindingsFunction, |
| 12 | } from "./bindings-handler"; |
| 13 | |
| 14 | const testOptions = { providers: AWS.providers() }; |
| 15 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 16 | const sharedStack = Core.scratchStack(testOptions, "AossBindings"); |
| 17 | |
| 18 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 19 | // over 60s on a fresh deploy under parallel-suite load. |
| 20 | const readinessPolicy = Schedule.max([ |
| 21 | Schedule.fixed("2 seconds"), |
| 22 | Schedule.recurs(75), |
| 23 | ]); |
| 24 | |
| 25 | let baseUrl: string; |
| 26 | |
| 27 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 28 | readonly status: number; |
| 29 | readonly body: string; |
| 30 | }> {} |
| 31 | |
| 32 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 33 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 34 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 35 | // 4xx/assertion failure surfaces immediately. |
| 36 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 37 | HttpClient.execute(request).pipe( |
| 38 | Effect.flatMap((response) => |
| 39 | response.status >= 500 |
| 40 | ? response.text.pipe( |
| 41 | Effect.flatMap((body) => |
| 42 | Effect.fail( |
| 43 | new TransientUpstream({ status: response.status, body }), |
| 44 | ), |
| 45 | ), |
| 46 | ) |
| 47 | : Effect.succeed(response), |
| 48 | ), |
| 49 | Effect.retry({ |
| 50 | while: (e) => e._tag === "TransientUpstream", |
| 51 | schedule: Schedule.max([ |
| 52 | Schedule.exponential("500 millis"), |
| 53 | Schedule.recurs(6), |
| 54 | ]), |
| 55 | }), |
| 56 | ); |
| 57 | |
| 58 | describe.sequential( |
| 59 | "OpenSearchServerless Bindings", |
| 60 | { |
| 61 | tags: [ |
| 62 | "provider:aws", |
| 63 | "provider:aws:batch", |
| 64 | "provider:aws:lambda", |
| 65 | "provider:aws:opensearchserverless", |
| 66 | "live", |
| 67 | ], |
| 68 | }, |
| 69 | () => { |
| 70 | beforeAll( |
| 71 | Effect.gen(function* () { |
| 72 | yield* Effect.logInfo("AOSS test setup: destroying previous resources"); |
| 73 | yield* sharedStack.destroy(); |
| 74 | |
| 75 | yield* Effect.logInfo("AOSS test setup: deploying fixture"); |
| 76 | const { functionUrl } = yield* sharedStack.deploy( |
| 77 | Effect.gen(function* () { |
| 78 | return yield* AossBindingsFunction; |
| 79 | }).pipe(Effect.provide(AossBindingsFunctionLive)), |
| 80 | ); |
| 81 | |
| 82 | expect(functionUrl).toBeTruthy(); |
| 83 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 84 | |
| 85 | const readinessUrl = `${baseUrl}/bindings`; |
| 86 | yield* Effect.logInfo( |
| 87 | `AOSS test setup: probing readiness at ${readinessUrl}`, |
| 88 | ); |
| 89 | yield* HttpClient.get(readinessUrl).pipe( |
| 90 | Effect.flatMap((response) => |
| 91 | response.status === 200 |
| 92 | ? Effect.succeed(response) |
| 93 | : Effect.fail( |
| 94 | new Error(`Function not ready: ${response.status}`), |
| 95 | ), |
| 96 | ), |
| 97 | Effect.tapError((error) => |
| 98 | Effect.logWarning( |
| 99 | `AOSS test setup: fixture not ready yet (${String(error)})`, |
| 100 | ), |
| 101 | ), |
| 102 | Effect.retry({ schedule: readinessPolicy }), |
| 103 | ); |
| 104 | }), |
| 105 | { timeout: 240_000 }, |
| 106 | ); |
| 107 | |
| 108 | afterAll(sharedStack.destroy(), { timeout: 240_000 }); |
| 109 | |
| 110 | describe("binding registration", () => { |
| 111 | test.provider("all 4 capabilities initialize in the runtime", (_stack) => |
| 112 | Effect.gen(function* () { |
| 113 | const response = yield* send( |
| 114 | HttpClientRequest.get(`${baseUrl}/bindings`), |
| 115 | ).pipe(Effect.flatMap((r) => r.json)); |
| 116 | expect((response as any).bound).toHaveLength(4); |
| 117 | }), |
| 118 | ); |
| 119 | }); |
| 120 | |
| 121 | describe("GetAccountSettings", () => { |
| 122 | test.provider("reads the account's capacity limits", (_stack) => |
| 123 | Effect.gen(function* () { |
| 124 | const response = (yield* send( |
| 125 | HttpClientRequest.get(`${baseUrl}/account-settings`), |
| 126 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 127 | // Limits may be unset on a fresh account — the call round-tripping |
| 128 | // proves the grant. |
| 129 | expect(response).toHaveProperty("capacityLimits"); |
| 130 | }), |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | describe("UpdateAccountSettings", () => { |
| 135 | test.provider("writes back the observed capacity limits", (_stack) => |
| 136 | Effect.gen(function* () { |
| 137 | const response = (yield* send( |
| 138 | HttpClientRequest.post(`${baseUrl}/account-settings/noop`), |
| 139 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 140 | expect( |
| 141 | response.capacityLimits?.maxIndexingCapacityInOCU, |
| 142 | ).toBeGreaterThan(0); |
| 143 | }), |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | describe("GetPoliciesStats", () => { |
| 148 | test.provider("counts the account's aoss policies", (_stack) => |
| 149 | Effect.gen(function* () { |
| 150 | const response = (yield* send( |
| 151 | HttpClientRequest.get(`${baseUrl}/policies-stats`), |
| 152 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 153 | expect(typeof response.total).toBe("number"); |
| 154 | }), |
| 155 | ); |
| 156 | }); |
| 157 | |
| 158 | describe("BatchGetEffectiveLifecyclePolicy", () => { |
| 159 | test.provider( |
| 160 | "resolves a nonexistent index through the error-detail channel", |
| 161 | (_stack) => |
| 162 | Effect.gen(function* () { |
| 163 | const response = (yield* send( |
| 164 | HttpClientRequest.post(`${baseUrl}/effective-lifecycle`), |
| 165 | ).pipe(Effect.flatMap((r) => r.json))) as any; |
| 166 | expect(response.details + response.errors).toBeGreaterThan(0); |
| 167 | }), |
| 168 | ); |
| 169 | }); |
| 170 | }, |
| 171 | ); |