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 datazone from "@distilled.cloud/aws/datazone"; |
| 6 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 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 Redacted from "effect/Redacted"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import * as Stream from "effect/Stream"; |
| 14 | import DataZoneTestFunctionLive, { |
| 15 | BINDINGS_DOMAIN_NAME, |
| 16 | BINDINGS_PROJECT_NAME, |
| 17 | DataZoneTestFunction, |
| 18 | } from "./handler"; |
| 19 | |
| 20 | const testOptions = { providers: AWS.providers() }; |
| 21 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 22 | const sharedStack = Core.scratchStack(testOptions, "DataZoneBindings"); |
| 23 | |
| 24 | // Lambda function URL cold-start (DNS, IAM propagation, init) plus DataZone |
| 25 | // user-profile propagation can take well over 60s on a fresh deploy. |
| 26 | const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]); |
| 27 | |
| 28 | let baseUrl: string; |
| 29 | let functionArn: string; |
| 30 | let functionRoleArn: string; |
| 31 | |
| 32 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 33 | readonly status: number; |
| 34 | readonly body: string; |
| 35 | }> {} |
| 36 | |
| 37 | // Retry only 5xx (cold re-init, IAM/user-profile propagation the handler's |
| 38 | // `Effect.orDie` surfaces as a 500); a genuine 4xx surfaces immediately. |
| 39 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 40 | HttpClient.execute(request).pipe( |
| 41 | Effect.flatMap((response) => |
| 42 | response.status >= 500 |
| 43 | ? response.text.pipe( |
| 44 | Effect.flatMap((body) => |
| 45 | Effect.fail(new TransientUpstream({ status: response.status, body })), |
| 46 | ), |
| 47 | ) |
| 48 | : Effect.succeed(response), |
| 49 | ), |
| 50 | Effect.retry({ |
| 51 | while: (e) => e._tag === "TransientUpstream", |
| 52 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(8)]), |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | const getJson = (path: string) => |
| 57 | send(HttpClientRequest.get(`${baseUrl}${path}`)).pipe(Effect.flatMap((r) => r.json)); |
| 58 | |
| 59 | /** Resolve the fixture domain's id by its deterministic name. */ |
| 60 | const findDomainId = Effect.gen(function* () { |
| 61 | const pages = yield* datazone.listDomains.pages({}).pipe(Stream.runCollect); |
| 62 | const summary = Array.from(pages) |
| 63 | .flatMap((page) => page.items ?? []) |
| 64 | .find( |
| 65 | (s) => |
| 66 | (Redacted.isRedacted(s.name) ? Redacted.value(s.name) : s.name) === BINDINGS_DOMAIN_NAME && |
| 67 | s.status !== "DELETING" && |
| 68 | s.status !== "DELETED", |
| 69 | ); |
| 70 | return summary?.id; |
| 71 | }); |
| 72 | |
| 73 | describe.sequential( |
| 74 | "DataZone Bindings", |
| 75 | { tags: ["provider:aws", "provider:aws:datazone", "provider:aws:lambda", "live"] }, |
| 76 | () => { |
| 77 | beforeAll( |
| 78 | Effect.gen(function* () { |
| 79 | yield* Effect.logInfo("DataZone test setup: destroying previous resources"); |
| 80 | yield* sharedStack.destroy(); |
| 81 | |
| 82 | yield* Effect.logInfo("DataZone test setup: deploying fixture"); |
| 83 | const attrs = yield* sharedStack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | return yield* DataZoneTestFunction; |
| 86 | }).pipe(Effect.provide(DataZoneTestFunctionLive)), |
| 87 | ); |
| 88 | |
| 89 | expect(attrs.functionUrl).toBeTruthy(); |
| 90 | baseUrl = attrs.functionUrl!.replace(/\/+$/, ""); |
| 91 | functionArn = attrs.functionArn; |
| 92 | functionRoleArn = attrs.roleArn; |
| 93 | |
| 94 | const readinessUrl = `${baseUrl}/bindings`; |
| 95 | yield* Effect.logInfo(`DataZone test setup: probing readiness at ${readinessUrl}`); |
| 96 | yield* HttpClient.get(readinessUrl).pipe( |
| 97 | Effect.flatMap((response) => |
| 98 | response.status === 200 |
| 99 | ? Effect.succeed(response) |
| 100 | : Effect.fail(new Error(`Function not ready: ${response.status}`)), |
| 101 | ), |
| 102 | Effect.tapError((error) => |
| 103 | Effect.logWarning(`DataZone test setup: fixture not ready yet (${String(error)})`), |
| 104 | ), |
| 105 | Effect.retry({ schedule: readinessPolicy }), |
| 106 | ); |
| 107 | }), |
| 108 | { timeout: 600_000 }, |
| 109 | ); |
| 110 | |
| 111 | afterAll(sharedStack.destroy(), { timeout: 600_000 }); |
| 112 | |
| 113 | describe("user profile setup", () => { |
| 114 | // DataZone authorizes data-plane calls against a *user profile*, not |
| 115 | // just IAM — create one for the function's execution role before any |
| 116 | // route test calls the API through the bindings. Runs as the first |
| 117 | // sequential test (distilled calls need the provider credentials that |
| 118 | // `test.provider` supplies). |
| 119 | test.provider("creates a user profile for the function role", () => |
| 120 | Effect.gen(function* () { |
| 121 | const domainId = yield* findDomainId; |
| 122 | expect(domainId).toBeTruthy(); |
| 123 | const profile = yield* datazone |
| 124 | .createUserProfile({ |
| 125 | domainIdentifier: domainId!, |
| 126 | userIdentifier: functionRoleArn, |
| 127 | userType: "IAM_ROLE", |
| 128 | }) |
| 129 | .pipe( |
| 130 | // already exists from a previous run — resolve it instead. |
| 131 | Effect.catchTag("ValidationException", () => |
| 132 | datazone.getUserProfile({ |
| 133 | domainIdentifier: domainId!, |
| 134 | userIdentifier: functionRoleArn, |
| 135 | type: "IAM", |
| 136 | }), |
| 137 | ), |
| 138 | ); |
| 139 | expect(profile.id).toBeTruthy(); |
| 140 | |
| 141 | // Inventory search is project-scoped: the function's user profile |
| 142 | // must be a member of the fixture project. |
| 143 | const projects = yield* datazone.listProjects({ domainIdentifier: domainId! }); |
| 144 | const project = (projects.items ?? []).find( |
| 145 | (p) => |
| 146 | (Redacted.isRedacted(p.name) ? Redacted.value(p.name) : p.name) === |
| 147 | BINDINGS_PROJECT_NAME, |
| 148 | ); |
| 149 | expect(project).toBeTruthy(); |
| 150 | yield* datazone |
| 151 | .createProjectMembership({ |
| 152 | domainIdentifier: domainId!, |
| 153 | projectIdentifier: project!.id, |
| 154 | member: { userIdentifier: profile.id! }, |
| 155 | designation: "PROJECT_CONTRIBUTOR", |
| 156 | }) |
| 157 | .pipe( |
| 158 | // already a member from a previous run |
| 159 | Effect.catchTag("ValidationException", () => Effect.void), |
| 160 | ); |
| 161 | }), |
| 162 | ); |
| 163 | }); |
| 164 | |
| 165 | describe("binding registration", () => { |
| 166 | test.provider("all 30 domain capabilities initialize in the runtime", () => |
| 167 | Effect.gen(function* () { |
| 168 | const response = (yield* getJson("/bindings")) as { bound: string[] }; |
| 169 | expect(response.bound).toHaveLength(30); |
| 170 | expect(response.bound).toContain("search"); |
| 171 | expect(response.bound).toContain("acceptSubscriptionRequest"); |
| 172 | expect(response.bound).toContain("postLineageEvent"); |
| 173 | }), |
| 174 | ); |
| 175 | }); |
| 176 | |
| 177 | describe("Search", () => { |
| 178 | test.provider( |
| 179 | "searches the domain inventory (injected domain id)", |
| 180 | () => |
| 181 | Effect.gen(function* () { |
| 182 | const response = (yield* getJson("/search")) as { |
| 183 | ok: boolean; |
| 184 | totalMatchCount: number; |
| 185 | items: number; |
| 186 | error?: string; |
| 187 | }; |
| 188 | // fresh domain — no assets, but the call must succeed end-to-end. |
| 189 | expect(response).toMatchObject({ ok: true, items: 0 }); |
| 190 | }), |
| 191 | { timeout: 240_000 }, |
| 192 | ); |
| 193 | }); |
| 194 | |
| 195 | describe("SearchListings", () => { |
| 196 | test.provider("searches the published catalog", () => |
| 197 | Effect.gen(function* () { |
| 198 | const response = (yield* getJson("/listings")) as { ok: boolean; items: number }; |
| 199 | expect(response).toMatchObject({ ok: true, items: 0 }); |
| 200 | }), |
| 201 | ); |
| 202 | }); |
| 203 | |
| 204 | describe("ListSubscriptions", () => { |
| 205 | test.provider("lists approved subscriptions (empty in a fresh domain)", () => |
| 206 | Effect.gen(function* () { |
| 207 | const response = (yield* getJson("/subscriptions")) as { ok: boolean; items: number }; |
| 208 | expect(response).toMatchObject({ ok: true, items: 0 }); |
| 209 | }), |
| 210 | ); |
| 211 | }); |
| 212 | |
| 213 | describe("ListSubscriptionRequests", () => { |
| 214 | test.provider("lists pending requests (empty in a fresh domain)", () => |
| 215 | Effect.gen(function* () { |
| 216 | const response = (yield* getJson("/requests")) as { ok: boolean; items: number }; |
| 217 | expect(response).toMatchObject({ ok: true, items: 0 }); |
| 218 | }), |
| 219 | ); |
| 220 | }); |
| 221 | |
| 222 | describe("GetIamPortalLoginUrl", () => { |
| 223 | test.provider("mints a data portal sign-in URL", () => |
| 224 | Effect.gen(function* () { |
| 225 | const response = (yield* getJson("/portal")) as { |
| 226 | ok: boolean; |
| 227 | authCodeUrl?: string; |
| 228 | userProfileId?: string; |
| 229 | }; |
| 230 | expect(response.ok).toBe(true); |
| 231 | expect(response.authCodeUrl).toBeTruthy(); |
| 232 | }), |
| 233 | ); |
| 234 | }); |
| 235 | |
| 236 | describe("consumeDataZoneEvents", { tags: ["provider:aws:eventbridge"] }, () => { |
| 237 | test.provider("the deploy created an EventBridge rule targeting the function", () => |
| 238 | Effect.gen(function* () { |
| 239 | // Out-of-band via distilled: the fixture's consumeDataZoneEvents |
| 240 | // must have materialized as a rule on the default bus with the |
| 241 | // Lambda as target. |
| 242 | const { RuleNames } = yield* eventbridge.listRuleNamesByTarget({ |
| 243 | TargetArn: functionArn, |
| 244 | }); |
| 245 | expect((RuleNames ?? []).length).toBeGreaterThanOrEqual(1); |
| 246 | }), |
| 247 | ); |
| 248 | }); |
| 249 | }, |
| 250 | ); |