| 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 CurTestFunctionLive, { CurTestFunction, REPORT_NAME } from "./handler"; |
| 11 | |
| 12 | const testOptions = { providers: AWS.providers() }; |
| 13 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 14 | const sharedStack = Core.scratchStack(testOptions, "CurBindings"); |
| 15 | |
| 16 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 17 | // over 60s on a fresh deploy. |
| 18 | const readinessPolicy = Schedule.max([ |
| 19 | Schedule.fixed("2 seconds"), |
| 20 | Schedule.recurs(75), |
| 21 | ]); |
| 22 | |
| 23 | let baseUrl: 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 that the |
| 32 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 33 | // 4xx/assertion failure surfaces immediately. |
| 34 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 35 | HttpClient.execute(request).pipe( |
| 36 | Effect.flatMap((response) => |
| 37 | response.status >= 500 |
| 38 | ? response.text.pipe( |
| 39 | Effect.flatMap((body) => |
| 40 | Effect.fail( |
| 41 | new TransientUpstream({ status: response.status, body }), |
| 42 | ), |
| 43 | ), |
| 44 | ) |
| 45 | : Effect.succeed(response), |
| 46 | ), |
| 47 | Effect.retry({ |
| 48 | while: (e) => e._tag === "TransientUpstream", |
| 49 | schedule: Schedule.max([ |
| 50 | Schedule.exponential("500 millis"), |
| 51 | Schedule.recurs(6), |
| 52 | ]), |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | const getJson = (path: string) => |
| 57 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe( |
| 58 | Effect.flatMap((r) => r.json), |
| 59 | ); |
| 60 | |
| 61 | describe.sequential( |
| 62 | "CostAndUsageReport Bindings", |
| 63 | { |
| 64 | tags: [ |
| 65 | "provider:aws", |
| 66 | "provider:aws:costandusagereport", |
| 67 | "provider:aws:lambda", |
| 68 | "provider:aws:s3", |
| 69 | "live", |
| 70 | ], |
| 71 | }, |
| 72 | () => { |
| 73 | beforeAll( |
| 74 | Effect.gen(function* () { |
| 75 | yield* Effect.logInfo("CUR test setup: destroying previous resources"); |
| 76 | yield* sharedStack.destroy(); |
| 77 | |
| 78 | yield* Effect.logInfo("CUR test setup: deploying fixture"); |
| 79 | const { functionUrl } = yield* sharedStack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | return yield* CurTestFunction; |
| 82 | }).pipe(Effect.provide(CurTestFunctionLive)), |
| 83 | ); |
| 84 | |
| 85 | expect(functionUrl).toBeTruthy(); |
| 86 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 87 | |
| 88 | const readinessUrl = `${baseUrl}/bindings`; |
| 89 | yield* Effect.logInfo( |
| 90 | `CUR test setup: probing readiness at ${readinessUrl}`, |
| 91 | ); |
| 92 | yield* HttpClient.get(readinessUrl).pipe( |
| 93 | Effect.flatMap((response) => |
| 94 | response.status === 200 |
| 95 | ? Effect.succeed(response) |
| 96 | : Effect.fail( |
| 97 | new Error(`Function not ready: ${response.status}`), |
| 98 | ), |
| 99 | ), |
| 100 | Effect.tapError((error) => |
| 101 | Effect.logWarning( |
| 102 | `CUR test setup: fixture not ready yet (${String(error)})`, |
| 103 | ), |
| 104 | ), |
| 105 | Effect.retry({ schedule: readinessPolicy }), |
| 106 | ); |
| 107 | }), |
| 108 | { timeout: 240_000 }, |
| 109 | ); |
| 110 | |
| 111 | afterAll(sharedStack.destroy(), { timeout: 180_000 }); |
| 112 | |
| 113 | describe("binding registration", () => { |
| 114 | test.provider("both capabilities initialize in the runtime", (_stack) => |
| 115 | Effect.gen(function* () { |
| 116 | const response = yield* getJson("/bindings"); |
| 117 | expect((response as any).bound).toEqual([ |
| 118 | "describeReportDefinitions", |
| 119 | "listReportTags", |
| 120 | ]); |
| 121 | }), |
| 122 | ); |
| 123 | }); |
| 124 | |
| 125 | describe("DescribeReportDefinitions", () => { |
| 126 | test.provider( |
| 127 | "lists the fixture report definition from the runtime", |
| 128 | (_stack) => |
| 129 | Effect.gen(function* () { |
| 130 | const response = (yield* getJson("/reports")) as any; |
| 131 | expect(response.count).toBeGreaterThan(0); |
| 132 | expect(response.names).toContain(REPORT_NAME); |
| 133 | }), |
| 134 | { timeout: 60_000 }, |
| 135 | ); |
| 136 | }); |
| 137 | |
| 138 | describe("ListTagsForResource", () => { |
| 139 | test.provider( |
| 140 | "reads the fixture report's tags (proving ReportName injection + the report-scoped grant)", |
| 141 | (_stack) => |
| 142 | Effect.gen(function* () { |
| 143 | const response = (yield* getJson("/report-tags")) as any; |
| 144 | expect(response.tags.fixture).toBe("cur-bindings"); |
| 145 | // The provider brands the report with internal alchemy tags. |
| 146 | expect( |
| 147 | Object.keys(response.tags).some((k) => k.startsWith("alchemy:")), |
| 148 | ).toBe(true); |
| 149 | }), |
| 150 | { timeout: 60_000 }, |
| 151 | ); |
| 152 | }); |
| 153 | }, |
| 154 | ); |