@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 197 lines 8.63 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { EventDataStore } from "@alchemy.run/aws/CloudTrail";
3 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as cloudtrail from "@distilled.cloud/aws/cloudtrail";
7 import type * as Duration from "effect/Duration";
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: a well-formed ARN pointing at a nonexistent
15 // store must surface the typed not-found the provider's read/delete paths
16 // depend on.
17 test.provider(
18 "getEventDataStore on a nonexistent store fails with EventDataStoreNotFoundException",
19 () =>
20 Effect.gen(function* () {
21 const { accountId, region } = yield* AWSEnvironment.current;
22 const error = yield* Effect.flip(
23 cloudtrail.getEventDataStore({
24 EventDataStore: `arn:aws:cloudtrail:${region}:${accountId}:eventdatastore/00000000-0000-0000-0000-000000000000`,
25 }),
26 );
27 expect(error._tag).toBe("EventDataStoreNotFoundException");
28 }),
29 { tags: ["provider:aws", "provider:aws:cloudtrail", "live"] },
30 );
31
32 // Ungated entitlement probe: CloudTrail Lake stopped accepting NEW
33 // customers (InvalidParameterException "CloudTrail Lake is no longer
34 // accepting new customers..."), which the distilled patch surfaces as the
35 // typed `CloudTrailLakeOnboardingClosed` tag. On a non-onboarded account
36 // this proves the patch; on an onboarded account the create succeeds and
37 // is immediately deleted (PENDING_DELETION incurs no cost).
38 const PROBE_STORE_NAME = "alchemy-test-cloudtrail-lake-probe";
39
40 // Idempotent, name-keyed cleanup for the out-of-band probe store. Because
41 // the probe's name is deterministic, this reclaims a leftover from a
42 // previously-killed run as well as the one created by this run. A store
43 // already in PENDING_DELETION is considered cleaned (Lake deletion always
44 // goes through a 7-day, zero-cost PENDING_DELETION window — there is no
45 // harder delete).
46 const deleteProbeStoreIfExists = Effect.gen(function* () {
47 const stores = yield* cloudtrail.listEventDataStores({});
48 const existing = (stores.EventDataStores ?? []).find(
49 (s) => s.Name === PROBE_STORE_NAME && s.Status !== "PENDING_DELETION",
50 );
51 if (existing?.EventDataStoreArn !== undefined) {
52 yield* cloudtrail.deleteEventDataStore({ EventDataStore: existing.EventDataStoreArn }).pipe(
53 // A store still finishing creation can transiently conflict.
54 Effect.retry({
55 while: (e) => e._tag === "ConflictException",
56 schedule: Schedule.exponential("2 seconds"),
57 times: 8,
58 }),
59 Effect.catchTag(
60 ["EventDataStoreNotFoundException", "InactiveEventDataStoreException"],
61 () => Effect.void,
62 ),
63 );
64 }
65 });
66
67 test.provider(
68 "createEventDataStore is either typed onboarding-closed or a real create",
69 () =>
70 Effect.gen(function* () {
71 // Pre-clean: reclaim a leftover probe store from a killed prior run
72 // so the deterministic name is free and the test converges.
73 yield* deleteProbeStoreIfExists;
74 const attempt = yield* Effect.result(
75 cloudtrail.createEventDataStore({
76 Name: PROBE_STORE_NAME,
77 MultiRegionEnabled: false,
78 RetentionPeriod: 7,
79 TerminationProtectionEnabled: false,
80 }),
81 );
82 if (Result.isFailure(attempt)) {
83 expect([
84 "CloudTrailLakeOnboardingClosed",
85 // A prior probe's store still pending deletion holds the name on
86 // an onboarded account.
87 "EventDataStoreAlreadyExistsException",
88 ]).toContain(attempt.failure._tag);
89 }
90 // Onboarded-account success path: the ensuring finalizer below
91 // deletes the store (PENDING_DELETION incurs no cost).
92 }).pipe(
93 // Guaranteed cleanup on success, failure, AND interruption. Keyed on
94 // the deterministic name, so it also covers the create-succeeded-but-
95 // fiber-killed-before-response window on the next run's pre-clean.
96 // orDie (not swallow): a failed cleanup must fail the test loudly —
97 // a green run must imply zero leftovers.
98 Effect.ensuring(deleteProbeStoreIfExists.pipe(Effect.orDie)),
99 ),
100 { tags: ["provider:aws", "provider:aws:cloudtrail", "live"] },
101 );
102
103 const STORE_NAME = "alchemy-test-cloudtrail-eds";
104
105 // A deleted store sits in PENDING_DELETION for 7 days (no cost) and keeps
106 // its name; the provider restores such a store instead of creating a
107 // duplicate, which is what makes this test re-runnable day after day.
108 //
109 // GATED: CloudTrail Lake is closed to new customers (see the ungated probe
110 // above) — the full lifecycle only runs on an account that was onboarded
111 // to Lake before the closure. Set AWS_TEST_CLOUDTRAIL_LAKE=1 to run it.
112 test.provider.skipIf(!process.env.AWS_TEST_CLOUDTRAIL_LAKE)(
113 "create event data store, update retention, delete (PENDING_DELETION)",
114 (stack) =>
115 Effect.gen(function* () {
116 yield* stack.destroy();
117
118 const make = (props: {
119 retentionPeriod: Duration.Input;
120 tags?: Record<string, string>;
121 ingestionEnabled?: boolean;
122 }) =>
123 Effect.gen(function* () {
124 const store = yield* EventDataStore("Lake", {
125 name: STORE_NAME,
126 multiRegionEnabled: false,
127 retentionPeriod: props.retentionPeriod,
128 terminationProtectionEnabled: false,
129 ingestionEnabled: props.ingestionEnabled,
130 tags: props.tags,
131 });
132 return { store };
133 });
134
135 // 1. Create with the 7-day minimum retention.
136 const { store } = yield* stack.deploy(
137 make({ retentionPeriod: "7 days", tags: { fixture: "cloudtrail-eds" } }),
138 );
139 expect(store.name).toBe(STORE_NAME);
140 expect(store.eventDataStoreArn).toContain(":eventdatastore/");
141 expect(["CREATED", "ENABLED", "STARTING_INGESTION"]).toContain(store.status);
142
143 // Out-of-band verification via distilled.
144 const observed = yield* cloudtrail.getEventDataStore({
145 EventDataStore: store.eventDataStoreArn,
146 });
147 expect(observed.Name).toBe(STORE_NAME);
148 expect(observed.RetentionPeriod).toBe(7);
149 expect(observed.TerminationProtectionEnabled).toBe(false);
150 expect(observed.MultiRegionEnabled).toBe(false);
151 const tags = yield* cloudtrail.listTags({ ResourceIdList: [store.eventDataStoreArn] });
152 const tagRecord = Object.fromEntries(
153 (tags.ResourceTagList?.[0]?.TagsList ?? []).map((t) => [t.Key, t.Value]),
154 );
155 expect(tagRecord.fixture).toBe("cloudtrail-eds");
156 expect(tagRecord["alchemy::id"]).toBe("Lake");
157
158 // 2. Update retention in place — the ARN is the stable identity.
159 const { store: updated } = yield* stack.deploy(
160 make({ retentionPeriod: "14 days", tags: { team: "audit" } }),
161 );
162 expect(updated.eventDataStoreArn).toBe(store.eventDataStoreArn);
163 const observedAfter = yield* cloudtrail.getEventDataStore({
164 EventDataStore: store.eventDataStoreArn,
165 });
166 expect(observedAfter.RetentionPeriod).toBe(14);
167 const tagsAfter = yield* cloudtrail.listTags({ ResourceIdList: [store.eventDataStoreArn] });
168 const tagRecordAfter = Object.fromEntries(
169 (tagsAfter.ResourceTagList?.[0]?.TagsList ?? []).map((t) => [t.Key, t.Value]),
170 );
171 expect(tagRecordAfter.team).toBe("audit");
172 expect(tagRecordAfter.fixture).toBeUndefined();
173
174 // 2b. Stop ingestion — the store's Status converges to
175 // STOPPED_INGESTION; collected events stay queryable.
176 const { store: stopped } = yield* stack.deploy(
177 make({ retentionPeriod: "14 days", tags: { team: "audit" }, ingestionEnabled: false }),
178 );
179 expect(stopped.eventDataStoreArn).toBe(store.eventDataStoreArn);
180 const observedStopped = yield* cloudtrail.getEventDataStore({
181 EventDataStore: store.eventDataStoreArn,
182 });
183 expect(["STOPPING_INGESTION", "STOPPED_INGESTION"]).toContain(observedStopped.Status);
184
185 // 3. Delete — schedules PENDING_DELETION (7-day wait, zero cost);
186 // do NOT wait for the store to disappear.
187 yield* stack.destroy();
188 const afterDelete = yield* cloudtrail
189 .getEventDataStore({ EventDataStore: store.eventDataStoreArn })
190 .pipe(
191 Effect.map((r) => r.Status ?? "UNKNOWN"),
192 Effect.catchTag("EventDataStoreNotFoundException", () => Effect.succeed("GONE" as const)),
193 );
194 expect(["PENDING_DELETION", "GONE"]).toContain(afterDelete);
195 }),
196 { tags: ["provider:aws", "provider:aws:cloudtrail", "live"], timeout: 180_000 },
197 );