The best way to control Claude Code, Codex and more from any computer or phone

main
ts 129 lines 5.16 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import type { SubnetId } from "@alchemy.run/aws/EC2/Subnet";
3 import { DBSubnetGroup } from "@alchemy.run/aws/RDS/DBSubnetGroup";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as EC2 from "@distilled.cloud/aws/ec2";
7 import * as RDS from "@distilled.cloud/aws/rds";
8 import * as Provider from "alchemy/Provider";
9 import * as Effect from "effect/Effect";
10 import { driftOf, repairDrift, waitForDrift } from "../drift.ts";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Default read-only path (AWS account/region-scoped collection): resolve the
15 // provider via the typed `Provider.findProvider(DBSubnetGroup)` so `list()`'s
16 // element type is the exact `DBSubnetGroup["Attributes"]` shape, then call
17 // `list()` without deploying. This always runs and exercises the full
18 // `describeDBSubnetGroups` pagination + Attributes-mapping code, asserting a
19 // well-typed `Attributes[]`.
20 //
21 // We do NOT deploy here because a DB subnet group needs subnets spanning >= 2
22 // AZs, and the shared testing account has NO default VPC, is at its 5-VPC limit,
23 // and (verified live) currently exposes only a single available subnet in a
24 // single AZ — so there is no reusable VPC to carve a multi-AZ group from. The
25 // deploy-backed assertion is gated below behind AWS_TEST_RDS_DBSUBNETGROUP=1 for
26 // accounts that have multi-AZ subnets available.
27 test.provider(
28 "list returns well-typed DBSubnetGroup attributes",
29 () =>
30 Effect.gen(function* () {
31 const provider = yield* Provider.findProvider(DBSubnetGroup);
32 const all = yield* provider.list();
33
34 expect(Array.isArray(all)).toBe(true);
35 for (const g of all) {
36 expect(typeof g.dbSubnetGroupName).toBe("string");
37 expect(Array.isArray(g.subnetIds)).toBe(true);
38 expect(typeof g.tags).toBe("object");
39 }
40 }),
41 { tags: ["provider:aws", "provider:aws:rds", "live"] },
42 );
43
44 // Deploy-backed list test. A DB subnet group requires subnets in >= 2
45 // distinct AZs; the account's DEFAULT VPC provides one default subnet per AZ
46 // (subnets don't count against the VPC limit, and the default VPC is never
47 // torn down by a concurrent suite). Deploys a subnet group over two of them,
48 // resolves the provider via the typed `Provider.findProvider(DBSubnetGroup)`,
49 // lists, and asserts presence — bracketed by `stack.destroy()`.
50 test.provider(
51 "list enumerates the deployed DB subnet group",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const subnetsResult = yield* EC2.describeSubnets({
57 Filters: [{ Name: "default-for-az", Values: ["true"] }],
58 });
59 const available = (subnetsResult.Subnets ?? []).filter(
60 (s) => s.State === "available" && !!s.SubnetId && !!s.VpcId && !!s.AvailabilityZone,
61 );
62
63 // Group available subnets by VPC, keeping one subnet per AZ, then pick a
64 // VPC with subnets in >= 2 distinct AZs.
65 const byVpc = new Map<string, Map<string, string>>();
66 for (const s of available) {
67 let byAz = byVpc.get(s.VpcId!);
68 if (!byAz) {
69 byAz = new Map();
70 byVpc.set(s.VpcId!, byAz);
71 }
72 if (!byAz.has(s.AvailabilityZone!)) {
73 byAz.set(s.AvailabilityZone!, s.SubnetId!);
74 }
75 }
76
77 let chosen: string[] | undefined;
78 for (const byAz of byVpc.values()) {
79 if (byAz.size >= 2) {
80 chosen = Array.from(byAz.values()).slice(0, 2);
81 break;
82 }
83 }
84
85 if (!chosen) {
86 // Exact reason for a clean skip:
87 // "the default VPC has no available subnets in >= 2 distinct AZs".
88 return yield* Effect.fail(
89 new Error("the default VPC has no available subnets in >= 2 distinct AZs"),
90 );
91 }
92
93 const subnetIds = chosen as SubnetId[];
94
95 const group = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* DBSubnetGroup("ListDBSubnetGroup", {
98 description: "Alchemy list() test subnet group",
99 subnetIds,
100 tags: { team: "declared" },
101 });
102 }),
103 );
104
105 // #2688: `read` must observe the live tags, not carry the persisted
106 // ones.
107 expect((yield* driftOf(stack, "ListDBSubnetGroup"))?.action).toBe("unchanged");
108 yield* RDS.addTagsToResource({
109 ResourceName: group.dbSubnetGroupArn!,
110 Tags: [{ Key: "team", Value: "out-of-band" }],
111 });
112 expect((yield* waitForDrift(stack, "ListDBSubnetGroup", "drifted"))?.attr?.tags?.team).toBe(
113 "out-of-band",
114 );
115 yield* repairDrift(stack);
116 expect((yield* driftOf(stack, "ListDBSubnetGroup"))?.action).toBe("unchanged");
117 const tags = yield* RDS.listTagsForResource({ ResourceName: group.dbSubnetGroupArn! });
118 expect(tags.TagList?.find((t) => t.Key === "team")?.Value).toBe("declared");
119
120 const provider = yield* Provider.findProvider(DBSubnetGroup);
121 const all = yield* provider.list();
122
123 expect(Array.isArray(all)).toBe(true);
124 expect(all.some((g) => g.dbSubnetGroupName === group.dbSubnetGroupName)).toBe(true);
125
126 yield* stack.destroy();
127 }),
128 { tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"] },
129 );