@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, 04:45 UTC

main
ts 120 lines 4.21 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { StorageLensConfiguration } from "@alchemy.run/aws/S3Control";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as s3control from "@distilled.cloud/aws/s3-control";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 const ACCOUNT_ID = "391965393224";
13
14 const findConfiguration = (configId: string) =>
15 s3control.getStorageLensConfiguration({ AccountId: ACCOUNT_ID, ConfigId: configId }).pipe(
16 Effect.map((r) => r.StorageLensConfiguration),
17 Effect.catchTag("NoSuchConfiguration", () => Effect.succeed(undefined)),
18 );
19
20 class ConfigurationStillExists extends Data.TaggedError("ConfigurationStillExists")<{
21 readonly configId: string;
22 }> {}
23
24 const assertConfigurationDeleted = (configId: string) =>
25 findConfiguration(configId).pipe(
26 Effect.flatMap((cfg) =>
27 cfg === undefined ? Effect.void : Effect.fail(new ConfigurationStillExists({ configId })),
28 ),
29 Effect.retry({
30 while: (e) => e._tag === "ConfigurationStillExists",
31 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
32 }),
33 );
34
35 test.provider(
36 "typed NoSuchConfiguration tag on a nonexistent configuration",
37 () =>
38 Effect.gen(function* () {
39 const result = yield* s3control
40 .getStorageLensConfiguration({
41 AccountId: ACCOUNT_ID,
42 ConfigId: "alchemy-does-not-exist-xyz",
43 })
44 .pipe(
45 Effect.map(() => "found" as const),
46 Effect.catchTag("NoSuchConfiguration", () => Effect.succeed("missing" as const)),
47 );
48 expect(result).toBe("missing");
49 }),
50 { tags: ["provider:aws", "provider:aws:s3control", "live"], timeout: 60_000 },
51 );
52
53 test.provider(
54 "create, update, delete storage lens configuration",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const lens = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* StorageLensConfiguration("TestLens", {
62 tags: { Environment: "test" },
63 });
64 }),
65 );
66
67 expect(lens.configId).toBeDefined();
68 expect(lens.storageLensArn).toContain(":storage-lens/");
69
70 // out-of-band verification via distilled
71 const live = yield* findConfiguration(lens.configId);
72 expect(live?.IsEnabled).toBe(true);
73 expect(live?.AccountLevel).toBeDefined();
74
75 const tags = yield* s3control
76 .getStorageLensConfigurationTagging({
77 AccountId: ACCOUNT_ID,
78 ConfigId: lens.configId,
79 })
80 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
81 expect(tags.Environment).toBe("test");
82 expect(tags["alchemy::id"]).toBe("TestLens");
83
84 // update: disable the dashboard + enable activity metrics + retag
85 const updated = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* StorageLensConfiguration("TestLens", {
88 isEnabled: false,
89 // account-level activity metrics require bucket-level activity
90 // metrics to be enabled as well (MissingBucketLevelActivityMetrics)
91 accountLevel: {
92 ActivityMetrics: { IsEnabled: true },
93 BucketLevel: { ActivityMetrics: { IsEnabled: true } },
94 },
95 tags: { Environment: "production" },
96 });
97 }),
98 );
99 expect(updated.configId).toBe(lens.configId);
100
101 const updatedLive = yield* findConfiguration(lens.configId);
102 expect(updatedLive?.IsEnabled).toBe(false);
103 expect(updatedLive?.AccountLevel?.ActivityMetrics?.IsEnabled).toBe(true);
104
105 const updatedTags = yield* s3control
106 .getStorageLensConfigurationTagging({
107 AccountId: ACCOUNT_ID,
108 ConfigId: lens.configId,
109 })
110 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
111 expect(updatedTags.Environment).toBe("production");
112
113 yield* stack.destroy();
114 yield* assertConfigurationDeleted(lens.configId);
115 }),
116 {
117 tags: ["provider:aws", "provider:aws:s3control", "live"],
118 timeout: 120_000,
119 },
120 );