@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 293 lines 10.6 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Vault } from "@alchemy.run/aws/Glacier";
3 import { Topic } from "@alchemy.run/aws/SNS";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as glacier from "@distilled.cloud/aws/glacier";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Result from "effect/Result";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 // Ungated typed-error probe: prove the distilled error union carries the
15 // not-found tag this provider's read/delete/sync paths depend on. Runs in
16 // every CI pass at near-zero cost.
17 test.provider(
18 "describeVault on a nonexistent vault fails with ResourceNotFoundException",
19 () =>
20 Effect.gen(function* () {
21 const error = yield* Effect.flip(
22 glacier.describeVault({
23 accountId: "-",
24 vaultName: "alchemy-nonexistent-glacier-vault-probe",
25 }),
26 );
27 expect(error._tag).toBe("ResourceNotFoundException");
28 }),
29 { tags: ["provider:aws", "provider:aws:glacier", "live"] },
30 );
31
32 // Ungated entitlement probe: AWS rejects the vault-based S3 Glacier API on
33 // accounts created after its 2025 deprecation ("This API is no longer
34 // supported for new accounts. Please use S3 Glacier storage classes
35 // instead.") with the typed NoLongerSupportedException. On an entitled
36 // (older) account the create succeeds — clean the probe vault up and move
37 // on; the full lifecycle below is what exercises entitled accounts.
38 test.provider(
39 "createVault either succeeds (entitled account) or fails with the typed NoLongerSupportedException",
40 () =>
41 Effect.gen(function* () {
42 const vaultName = "alchemy-glacier-entitlement-probe";
43 const created = yield* Effect.result(glacier.createVault({ accountId: "-", vaultName }));
44 if (Result.isFailure(created)) {
45 expect(created.failure._tag).toBe("NoLongerSupportedException");
46 return;
47 }
48 yield* glacier
49 .deleteVault({ accountId: "-", vaultName })
50 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
51 }),
52 { tags: ["provider:aws", "provider:aws:glacier", "live"] },
53 );
54
55 class VaultStillExists extends Data.TaggedError("VaultStillExists")<{ vaultName: string }> {}
56
57 // Typed wait-until-gone: DescribeVault must return the typed
58 // ResourceNotFoundException once deletion has propagated.
59 const assertVaultDeleted = (vaultName: string) =>
60 Effect.gen(function* () {
61 const exists = yield* glacier.describeVault({ accountId: "-", vaultName }).pipe(
62 Effect.map(() => true),
63 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(false)),
64 );
65 if (exists) {
66 yield* Effect.fail(new VaultStillExists({ vaultName }));
67 }
68 }).pipe(
69 Effect.retry({
70 while: (e) => e._tag === "VaultStillExists",
71 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
72 }),
73 );
74
75 const denyArchiveDeletes = (vaultArn: string) => ({
76 Version: "2012-10-17",
77 Statement: [
78 {
79 Sid: "deny-archive-deletes",
80 Effect: "Deny",
81 Principal: "*",
82 Action: ["glacier:DeleteArchive"],
83 Resource: [vaultArn],
84 },
85 ],
86 });
87
88 test.provider.skipIf(!process.env.AWS_TEST_GLACIER)(
89 "create vault with tags + notifications, sync access policy, remove config, destroy",
90 (stack) =>
91 Effect.gen(function* () {
92 yield* stack.destroy();
93
94 // Step 1 — vault with tags and a notification configuration.
95 const step1 = yield* stack.deploy(
96 Effect.gen(function* () {
97 const topic = yield* Topic("VaultEvents");
98 const vault = yield* Vault("Backups", {
99 tags: { fixture: "glacier-vault" },
100 notificationConfig: { snsTopic: topic.topicArn, events: ["ArchiveRetrievalCompleted"] },
101 });
102 return { topic, vault };
103 }),
104 );
105
106 expect(step1.vault.vaultName).toBeDefined();
107 expect(step1.vault.vaultArn).toContain(":vaults/");
108 expect(step1.vault.creationDate).toBeDefined();
109
110 // Out-of-band verification via distilled.
111 const described = yield* glacier.describeVault({
112 accountId: "-",
113 vaultName: step1.vault.vaultName,
114 });
115 expect(described.VaultARN).toBe(step1.vault.vaultArn);
116
117 const notifications = yield* glacier.getVaultNotifications({
118 accountId: "-",
119 vaultName: step1.vault.vaultName,
120 });
121 expect(notifications.vaultNotificationConfig?.SNSTopic).toBe(step1.topic.topicArn);
122 expect(notifications.vaultNotificationConfig?.Events).toEqual(["ArchiveRetrievalCompleted"]);
123
124 const tags = yield* glacier.listTagsForVault({
125 accountId: "-",
126 vaultName: step1.vault.vaultName,
127 });
128 expect(tags.Tags?.fixture).toBe("glacier-vault");
129 expect(tags.Tags?.["alchemy::id"]).toBeDefined();
130
131 // No access policy yet.
132 const noPolicy = yield* Effect.flip(
133 glacier.getVaultAccessPolicy({ accountId: "-", vaultName: step1.vault.vaultName }),
134 );
135 expect(noPolicy._tag).toBe("ResourceNotFoundException");
136
137 // Step 2 — add an access policy (the vault ARN is deterministic given
138 // the engine-generated name, so we thread the step-1 output through),
139 // change the notification events, and update the tags.
140 const vaultArn = step1.vault.vaultArn;
141 const step2 = yield* stack.deploy(
142 Effect.gen(function* () {
143 const topic = yield* Topic("VaultEvents");
144 const vault = yield* Vault("Backups", {
145 tags: { fixture: "glacier-vault", stage: "two" },
146 accessPolicy: denyArchiveDeletes(vaultArn),
147 notificationConfig: {
148 snsTopic: topic.topicArn,
149 events: ["ArchiveRetrievalCompleted", "InventoryRetrievalCompleted"],
150 },
151 });
152 return { vault };
153 }),
154 );
155 expect(step2.vault.vaultArn).toBe(vaultArn);
156
157 const policy = yield* glacier.getVaultAccessPolicy({
158 accountId: "-",
159 vaultName: step2.vault.vaultName,
160 });
161 expect(policy.policy?.Policy).toContain("deny-archive-deletes");
162
163 const updatedNotifications = yield* glacier.getVaultNotifications({
164 accountId: "-",
165 vaultName: step2.vault.vaultName,
166 });
167 expect([...(updatedNotifications.vaultNotificationConfig?.Events ?? [])].sort()).toEqual([
168 "ArchiveRetrievalCompleted",
169 "InventoryRetrievalCompleted",
170 ]);
171
172 const updatedTags = yield* glacier.listTagsForVault({
173 accountId: "-",
174 vaultName: step2.vault.vaultName,
175 });
176 expect(updatedTags.Tags?.stage).toBe("two");
177
178 // Step 3 — remove the access policy and notification configuration;
179 // the reconciler must delete both against observed state.
180 yield* stack.deploy(
181 Effect.gen(function* () {
182 yield* Topic("VaultEvents");
183 const vault = yield* Vault("Backups", { tags: { fixture: "glacier-vault" } });
184 return { vault };
185 }),
186 );
187
188 const removedPolicy = yield* Effect.flip(
189 glacier.getVaultAccessPolicy({ accountId: "-", vaultName: step1.vault.vaultName }),
190 );
191 expect(removedPolicy._tag).toBe("ResourceNotFoundException");
192
193 const removedNotifications = yield* Effect.flip(
194 glacier.getVaultNotifications({ accountId: "-", vaultName: step1.vault.vaultName }),
195 );
196 expect(removedNotifications._tag).toBe("ResourceNotFoundException");
197
198 const prunedTags = yield* glacier.listTagsForVault({
199 accountId: "-",
200 vaultName: step1.vault.vaultName,
201 });
202 expect(prunedTags.Tags?.stage).toBeUndefined();
203
204 yield* stack.destroy();
205 yield* assertVaultDeleted(step1.vault.vaultName);
206 }),
207 { tags: ["provider:aws", "provider:aws:glacier", "provider:aws:sns", "live"], timeout: 240_000 },
208 );
209
210 test.provider.skipIf(!process.env.AWS_TEST_GLACIER)(
211 "vault lock: initiate in-progress, then abort on removal",
212 (stack) =>
213 Effect.gen(function* () {
214 yield* stack.destroy();
215
216 // Step 1 — plain vault (we need its ARN to author the lock policy).
217 const step1 = yield* stack.deploy(
218 Effect.gen(function* () {
219 return yield* Vault("LockedVault");
220 }),
221 );
222
223 // Step 2 — initiate the vault lock. It must observe as InProgress and
224 // must never be completed by the provider.
225 const vaultArn = step1.vaultArn;
226 yield* stack.deploy(
227 Effect.gen(function* () {
228 return yield* Vault("LockedVault", { lockPolicy: denyArchiveDeletes(vaultArn) });
229 }),
230 );
231
232 const lock = yield* glacier.getVaultLock({ accountId: "-", vaultName: step1.vaultName });
233 expect(lock.State).toBe("InProgress");
234 expect(lock.Policy).toContain("deny-archive-deletes");
235
236 // Reconcile is idempotent while the lock is in progress.
237 yield* stack.deploy(
238 Effect.gen(function* () {
239 return yield* Vault("LockedVault", { lockPolicy: denyArchiveDeletes(vaultArn) });
240 }),
241 );
242
243 // Step 3 — removing the prop aborts the in-progress lock.
244 yield* stack.deploy(
245 Effect.gen(function* () {
246 return yield* Vault("LockedVault");
247 }),
248 );
249 const aborted = yield* Effect.flip(
250 glacier.getVaultLock({ accountId: "-", vaultName: step1.vaultName }),
251 );
252 expect(aborted._tag).toBe("ResourceNotFoundException");
253
254 yield* stack.destroy();
255 yield* assertVaultDeleted(step1.vaultName);
256 }),
257 { tags: ["provider:aws", "provider:aws:glacier", "live"], timeout: 240_000 },
258 );
259
260 test.provider.skipIf(!process.env.AWS_TEST_GLACIER)(
261 "renaming a vault replaces it",
262 (stack) =>
263 Effect.gen(function* () {
264 yield* stack.destroy();
265
266 const original = yield* stack.deploy(
267 Effect.gen(function* () {
268 return yield* Vault("Renamed");
269 }),
270 );
271
272 const renamed = yield* stack.deploy(
273 Effect.gen(function* () {
274 return yield* Vault("Renamed", { vaultName: "alchemy-test-glacier-vault-renamed" });
275 }),
276 );
277
278 expect(renamed.vaultName).toBe("alchemy-test-glacier-vault-renamed");
279 expect(renamed.vaultArn).not.toBe(original.vaultArn);
280
281 // The new vault exists; the old one was deleted by the replacement.
282 const described = yield* glacier.describeVault({
283 accountId: "-",
284 vaultName: renamed.vaultName,
285 });
286 expect(described.VaultARN).toBe(renamed.vaultArn);
287 yield* assertVaultDeleted(original.vaultName);
288
289 yield* stack.destroy();
290 yield* assertVaultDeleted(renamed.vaultName);
291 }),
292 { tags: ["provider:aws", "provider:aws:glacier", "live"], timeout: 240_000 },
293 );