| 1 | import * as AWS from "@/AWS"; |
| 2 | import { TargetGroup } from "@/AWS/ELBv2"; |
| 3 | import * as Provider from "@/Provider"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import * as elbv2 from "@distilled.cloud/aws/elastic-load-balancing-v2"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { getDefaultVpc } from "../DefaultVpc.ts"; |
| 9 | |
| 10 | const { test } = Test.make({ providers: AWS.providers() }); |
| 11 | |
| 12 | // Canonical `list()` test for an account/region-scoped resource: a target group |
| 13 | // only needs a VPC id, so reuse the default VPC in the account/region (avoids |
| 14 | // the per-region VPC limit), deploy a TargetGroup, resolve the provider from |
| 15 | // context with the typed `findProvider` helper, call `list()` (which |
| 16 | // exhaustively paginates describeTargetGroups in the account/region), and assert |
| 17 | // the deployed target group appears in the result. |
| 18 | test.provider( |
| 19 | "list enumerates the deployed target group", |
| 20 | (stack) => |
| 21 | Effect.gen(function* () { |
| 22 | yield* stack.destroy(); |
| 23 | |
| 24 | const defaultVpc = yield* getDefaultVpc; |
| 25 | const deployed = yield* stack.deploy( |
| 26 | Effect.gen(function* () { |
| 27 | const targetGroup = yield* TargetGroup("ListTargetGroup", { |
| 28 | vpcId: defaultVpc.vpcId, |
| 29 | port: 80, |
| 30 | protocol: "HTTP", |
| 31 | targetType: "ip", |
| 32 | // Duration.Input props — verified on the wire below. |
| 33 | healthCheckInterval: "45 seconds", |
| 34 | healthCheckTimeout: "20 seconds", |
| 35 | }); |
| 36 | |
| 37 | return { targetGroup }; |
| 38 | }), |
| 39 | ); |
| 40 | |
| 41 | expect(deployed.targetGroup.targetGroupArn).toBeDefined(); |
| 42 | |
| 43 | // Out-of-band: Duration.Input health-check props reach the wire as |
| 44 | // whole seconds. |
| 45 | const observed = yield* elbv2 |
| 46 | .describeTargetGroups({ |
| 47 | TargetGroupArns: [deployed.targetGroup.targetGroupArn], |
| 48 | }) |
| 49 | .pipe(Effect.map((r) => r.TargetGroups?.[0])); |
| 50 | expect(observed?.HealthCheckIntervalSeconds).toBe(45); |
| 51 | expect(observed?.HealthCheckTimeoutSeconds).toBe(20); |
| 52 | |
| 53 | const provider = yield* Provider.findProvider(TargetGroup); |
| 54 | const all = yield* provider.list(); |
| 55 | |
| 56 | expect( |
| 57 | all.some( |
| 58 | (tg) => tg.targetGroupArn === deployed.targetGroup.targetGroupArn, |
| 59 | ), |
| 60 | ).toBe(true); |
| 61 | |
| 62 | yield* stack.destroy(); |
| 63 | |
| 64 | // Out-of-band: the target group is gone after destroy. |
| 65 | const after = yield* elbv2 |
| 66 | .describeTargetGroups({ |
| 67 | TargetGroupArns: [deployed.targetGroup.targetGroupArn], |
| 68 | }) |
| 69 | .pipe( |
| 70 | Effect.map((r) => r.TargetGroups?.length ?? 0), |
| 71 | Effect.catchTag("TargetGroupNotFoundException", () => |
| 72 | Effect.succeed(0), |
| 73 | ), |
| 74 | ); |
| 75 | expect(after).toBe(0); |
| 76 | }), |
| 77 | { |
| 78 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:elbv2", "live"], |
| 79 | timeout: 240_000, |
| 80 | }, |
| 81 | ); |