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

main
ts 153 lines 6.08 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { EIP } from "@alchemy.run/aws/EC2";
3 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
4 import { Protection, ProtectionGroup } from "@alchemy.run/aws/Shield";
5 import { describe, expect } from "@alchemy.run/test";
6 import * as Test from "@alchemy.run/test/Alchemy";
7 import * as shield from "@distilled.cloud/aws/shield";
8 import * as Output from "alchemy/Output";
9 import * as Effect from "effect/Effect";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // Shield Advanced costs $3,000/month with a mandatory 1-year auto-renewing
14 // commitment, so the testing account is NOT subscribed and must never be. The
15 // ungated probes below prove the distilled wiring and the patched typed
16 // `SubscriptionNotFound` entitlement tag against the real API at near-zero
17 // cost; the full lifecycle only runs on an already-subscribed account with
18 // AWS_TEST_SHIELD_ADVANCED=1.
19 describe("AWS.Shield", { tags: ["provider:aws", "provider:aws:shield", "live"] }, () => {
20 test.provider(
21 "getSubscriptionState succeeds without a subscription",
22 () =>
23 Effect.gen(function* () {
24 // Available to every account, subscribed or not — proves endpoint
25 // resolution (shield pins to us-east-1) and response decoding.
26 const state = yield* shield.getSubscriptionState({});
27 expect(["ACTIVE", "INACTIVE"]).toContain(state.SubscriptionState);
28 }),
29 { timeout: 60_000 },
30 );
31
32 test.provider(
33 "describeSubscription on a non-subscribed account fails with typed SubscriptionNotFound",
34 () =>
35 Effect.gen(function* () {
36 // The entitlement probe: the wire error is ResourceNotFoundException
37 // "The subscription does not exist." — patched in distilled to the
38 // synthetic typed tag `SubscriptionNotFound`.
39 const error = yield* Effect.flip(shield.describeSubscription({}));
40 expect(error._tag).toBe("SubscriptionNotFound");
41 }),
42 { timeout: 60_000 },
43 );
44
45 test.provider(
46 "describeProtection without a subscription fails with typed SubscriptionNotFound",
47 () =>
48 Effect.gen(function* () {
49 const error = yield* Effect.flip(
50 shield.describeProtection({
51 ProtectionId: "00000000-0000-0000-0000-000000000000",
52 }),
53 );
54 expect(error._tag).toBe("SubscriptionNotFound");
55 }),
56 { timeout: 60_000 },
57 );
58
59 test.provider(
60 "listProtections without a subscription fails with typed SubscriptionNotFound",
61 () =>
62 Effect.gen(function* () {
63 const error = yield* Effect.flip(shield.listProtections({}));
64 expect(error._tag).toBe("SubscriptionNotFound");
65 }),
66 { timeout: 60_000 },
67 );
68
69 test.provider(
70 "describeProtectionGroup for a nonexistent group fails with typed ResourceNotFoundException",
71 () =>
72 Effect.gen(function* () {
73 // Unlike the other operations, protection-group reads report the
74 // missing group itself even on a non-subscribed account.
75 const error = yield* Effect.flip(
76 shield.describeProtectionGroup({
77 ProtectionGroupId: "alchemy-nonexistent-probe",
78 }),
79 );
80 expect(error._tag).toBe("ResourceNotFoundException");
81 }),
82 { timeout: 60_000 },
83 );
84
85 // Full lifecycle — requires an account with an ACTIVE Shield Advanced
86 // subscription. NEVER subscribe the testing account: deploying the
87 // Shield.Subscription resource starts a $3,000/month 1-year commitment.
88 test.provider.skipIf(!process.env.AWS_TEST_SHIELD_ADVANCED)(
89 "protection + protection group lifecycle on a subscribed account",
90 (stack) =>
91 Effect.gen(function* () {
92 yield* stack.destroy();
93
94 const deploy = (aggregation: "SUM" | "MAX") =>
95 stack.deploy(
96 Effect.gen(function* () {
97 const { accountId, region } = yield* AWSEnvironment.current;
98 const eip = yield* EIP("ProtectedEip", {});
99 const protection = yield* Protection("EipProtection", {
100 resourceArn: Output.interpolate`arn:aws:ec2:${region}:${accountId}:eip-allocation/${eip.allocationId}`,
101 tags: { fixture: "shield" },
102 });
103 const group = yield* ProtectionGroup("AllGroup", {
104 aggregation,
105 pattern: "ALL",
106 tags: { fixture: "shield" },
107 });
108 return { protection, group };
109 }),
110 );
111
112 const { protection, group } = yield* deploy("SUM");
113 expect(protection.protectionId).toBeDefined();
114 expect(protection.protectionArn).toContain(":protection/");
115 expect(protection.healthCheckIds).toEqual([]);
116 expect(group.aggregation).toBe("SUM");
117 expect(group.pattern).toBe("ALL");
118
119 // Out-of-band verification via distilled.
120 const observed = yield* shield.describeProtection({
121 ProtectionId: protection.protectionId,
122 });
123 expect(observed.Protection?.ResourceArn).toBe(protection.resourceArn);
124 const observedGroup = yield* shield.describeProtectionGroup({
125 ProtectionGroupId: group.protectionGroupId,
126 });
127 expect(observedGroup.ProtectionGroup.Aggregation).toBe("SUM");
128
129 // Update the group's aggregation in place (no replacement).
130 const updated = yield* deploy("MAX");
131 expect(updated.group.protectionGroupId).toBe(group.protectionGroupId);
132 expect(updated.group.aggregation).toBe("MAX");
133
134 // Destroy and verify both are gone out-of-band.
135 yield* stack.destroy();
136 const goneProtection = yield* Effect.flip(
137 shield.describeProtection({
138 ProtectionId: protection.protectionId,
139 }),
140 );
141 expect(["ResourceNotFoundException", "SubscriptionNotFound"]).toContain(
142 goneProtection._tag,
143 );
144 const goneGroup = yield* Effect.flip(
145 shield.describeProtectionGroup({
146 ProtectionGroupId: group.protectionGroupId,
147 }),
148 );
149 expect(goneGroup._tag).toBe("ResourceNotFoundException");
150 }),
151 { tags: ["provider:aws:ec2"], timeout: 300_000 },
152 );
153 });