| 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 CodeConnectionsTestFunctionLive, { |
| 11 | CodeConnectionsTestFunction, |
| 12 | fixtureConnectionName, |
| 13 | } from "./handler"; |
| 14 | |
| 15 | const testOptions = { providers: AWS.providers() }; |
| 16 | const { test, beforeAll, afterAll } = Test.make(testOptions); |
| 17 | const sharedStack = Core.scratchStack(testOptions, "CodeConnectionsBindings"); |
| 18 | |
| 19 | // Lambda function URL cold-start (DNS, IAM propagation, init) can take well |
| 20 | // over 60s on a fresh deploy. |
| 21 | const readinessPolicy = Schedule.max([ |
| 22 | Schedule.fixed("2 seconds"), |
| 23 | Schedule.recurs(75), |
| 24 | ]); |
| 25 | |
| 26 | let baseUrl: string; |
| 27 | |
| 28 | class TransientUpstream extends Data.TaggedError("TransientUpstream")<{ |
| 29 | readonly status: number; |
| 30 | readonly body: string; |
| 31 | }> {} |
| 32 | |
| 33 | // The shared Lambda fixture occasionally answers a transient 5xx under load |
| 34 | // (cold re-init, IAM propagation on the freshly attached policy that the |
| 35 | // handler's `Effect.orDie` surfaces as a 500). Retry only 5xx; a genuine |
| 36 | // 4xx/assertion failure surfaces immediately. |
| 37 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 38 | HttpClient.execute(request).pipe( |
| 39 | Effect.flatMap((response) => |
| 40 | response.status >= 500 |
| 41 | ? response.text.pipe( |
| 42 | Effect.flatMap((body) => |
| 43 | Effect.fail( |
| 44 | new TransientUpstream({ status: response.status, body }), |
| 45 | ), |
| 46 | ), |
| 47 | ) |
| 48 | : Effect.succeed(response), |
| 49 | ), |
| 50 | Effect.retry({ |
| 51 | while: (e) => e._tag === "TransientUpstream", |
| 52 | schedule: Schedule.max([ |
| 53 | Schedule.exponential("500 millis"), |
| 54 | Schedule.recurs(6), |
| 55 | ]), |
| 56 | }), |
| 57 | ); |
| 58 | |
| 59 | describe.sequential( |
| 60 | "CodeConnections Bindings", |
| 61 | { |
| 62 | tags: [ |
| 63 | "provider:aws", |
| 64 | "provider:aws:codeconnections", |
| 65 | "provider:aws:connection", |
| 66 | "provider:aws:lambda", |
| 67 | "live", |
| 68 | ], |
| 69 | }, |
| 70 | () => { |
| 71 | beforeAll( |
| 72 | Effect.gen(function* () { |
| 73 | yield* Effect.logInfo( |
| 74 | "CodeConnections test setup: destroying previous resources", |
| 75 | ); |
| 76 | yield* sharedStack.destroy(); |
| 77 | |
| 78 | yield* Effect.logInfo("CodeConnections test setup: deploying fixture"); |
| 79 | const { functionUrl } = yield* sharedStack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | return yield* CodeConnectionsTestFunction; |
| 82 | }).pipe(Effect.provide(CodeConnectionsTestFunctionLive)), |
| 83 | ); |
| 84 | |
| 85 | expect(functionUrl).toBeTruthy(); |
| 86 | baseUrl = functionUrl!.replace(/\/+$/, ""); |
| 87 | |
| 88 | const readinessUrl = `${baseUrl}/bindings`; |
| 89 | yield* Effect.logInfo( |
| 90 | `CodeConnections 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 | `CodeConnections 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: 120_000 }); |
| 112 | |
| 113 | describe("binding registration", () => { |
| 114 | test.provider("all capabilities initialize in the runtime", (_stack) => |
| 115 | Effect.gen(function* () { |
| 116 | const response = yield* send( |
| 117 | HttpClientRequest.get(`${baseUrl}/bindings`), |
| 118 | ).pipe(Effect.flatMap((r) => r.json)); |
| 119 | expect((response as any).bound).toHaveLength(4); |
| 120 | }), |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | describe("GetConnection", () => { |
| 125 | test.provider("reads the fixture connection's live state", (_stack) => |
| 126 | Effect.gen(function* () { |
| 127 | const response = yield* send( |
| 128 | HttpClientRequest.get(`${baseUrl}/connection`), |
| 129 | ).pipe(Effect.flatMap((r) => r.json)); |
| 130 | expect((response as any).name).toBe(fixtureConnectionName); |
| 131 | // The OAuth handshake is a manual console step, so the fixture |
| 132 | // connection stays PENDING. |
| 133 | expect((response as any).status).toBe("PENDING"); |
| 134 | expect((response as any).providerType).toBe("GitHub"); |
| 135 | }), |
| 136 | ); |
| 137 | }); |
| 138 | |
| 139 | describe("ListConnections", () => { |
| 140 | test.provider( |
| 141 | "lists the fixture connection among the account's connections", |
| 142 | (_stack) => |
| 143 | Effect.gen(function* () { |
| 144 | const response = yield* send( |
| 145 | HttpClientRequest.get(`${baseUrl}/connections`), |
| 146 | ).pipe(Effect.flatMap((r) => r.json)); |
| 147 | expect((response as any).names).toContain(fixtureConnectionName); |
| 148 | }), |
| 149 | ); |
| 150 | }); |
| 151 | |
| 152 | describe("ListHosts", () => { |
| 153 | test.provider( |
| 154 | "enumerates the account's hosts (empty unless a self-managed provider is registered)", |
| 155 | (_stack) => |
| 156 | Effect.gen(function* () { |
| 157 | const response = yield* send( |
| 158 | HttpClientRequest.get(`${baseUrl}/hosts`), |
| 159 | ).pipe(Effect.flatMap((r) => r.json)); |
| 160 | expect(Array.isArray((response as any).names)).toBe(true); |
| 161 | }), |
| 162 | ); |
| 163 | }); |
| 164 | |
| 165 | describe("ListRepositoryLinks", () => { |
| 166 | test.provider( |
| 167 | "enumerates the account's repository links (empty unless Git sync is configured)", |
| 168 | (_stack) => |
| 169 | Effect.gen(function* () { |
| 170 | const response = yield* send( |
| 171 | HttpClientRequest.get(`${baseUrl}/repository-links`), |
| 172 | ).pipe(Effect.flatMap((r) => r.json)); |
| 173 | expect(Array.isArray((response as any).repositories)).toBe(true); |
| 174 | }), |
| 175 | ); |
| 176 | }); |
| 177 | }, |
| 178 | ); |