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

main
ts 109 lines 4.33 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { AggregationAuthorization } from "@alchemy.run/aws/Config";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as config from "@distilled.cloud/aws/config-service";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import * as Stream from "effect/Stream";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // A fixed, syntactically valid aggregator identity. The authorization is a
14 // pure grant — the target account does not need to exist.
15 const AGGREGATOR_ACCOUNT = "123456789012";
16
17 const findAuthorization = (accountId: string, region: string) =>
18 config.describeAggregationAuthorizations.items({}).pipe(
19 Stream.filter(
20 (auth) => auth.AuthorizedAccountId === accountId && auth.AuthorizedAwsRegion === region,
21 ),
22 Stream.runHead,
23 Effect.map((head) => (head._tag === "Some" ? head.value : undefined)),
24 );
25
26 class AuthorizationStillExists extends Data.TaggedError("AuthorizationStillExists")<{
27 readonly region: string;
28 }> {}
29
30 const assertAuthorizationDeleted = (accountId: string, region: string) =>
31 findAuthorization(accountId, region).pipe(
32 Effect.flatMap((auth) =>
33 auth === undefined ? Effect.void : Effect.fail(new AuthorizationStillExists({ region })),
34 ),
35 Effect.retry({
36 while: (e) => e._tag === "AuthorizationStillExists",
37 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
38 }),
39 );
40
41 test.provider(
42 "create, update tags, replace on region change, delete",
43 (stack) =>
44 Effect.gen(function* () {
45 yield* stack.destroy();
46
47 const auth = yield* stack.deploy(
48 Effect.gen(function* () {
49 return yield* AggregationAuthorization("TestAuth", {
50 authorizedAccountId: AGGREGATOR_ACCOUNT,
51 authorizedAwsRegion: "us-east-1",
52 tags: { Environment: "test" },
53 });
54 }),
55 );
56
57 expect(auth.authorizedAccountId).toBe(AGGREGATOR_ACCOUNT);
58 expect(auth.authorizedAwsRegion).toBe("us-east-1");
59 expect(auth.aggregationAuthorizationArn).toContain(":aggregation-authorization/");
60
61 // Out-of-band verification via distilled.
62 const created = yield* findAuthorization(AGGREGATOR_ACCOUNT, "us-east-1");
63 expect(created?.AggregationAuthorizationArn).toBe(auth.aggregationAuthorizationArn);
64 const tags = yield* config
65 .listTagsForResource({
66 ResourceArn: auth.aggregationAuthorizationArn,
67 })
68 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
69 expect(tags.Environment).toBe("test");
70 expect(tags["alchemy::id"]).toBe("TestAuth");
71
72 // Tags update in place (Put ignores tags on an existing authorization,
73 // so this exercises the TagResource sync path).
74 const updated = yield* stack.deploy(
75 Effect.gen(function* () {
76 return yield* AggregationAuthorization("TestAuth", {
77 authorizedAccountId: AGGREGATOR_ACCOUNT,
78 authorizedAwsRegion: "us-east-1",
79 tags: { Environment: "prod" },
80 });
81 }),
82 );
83 expect(updated.aggregationAuthorizationArn).toBe(auth.aggregationAuthorizationArn);
84 const updatedTags = yield* config
85 .listTagsForResource({
86 ResourceArn: auth.aggregationAuthorizationArn,
87 })
88 .pipe(Effect.map((r) => Object.fromEntries((r.Tags ?? []).map((t) => [t.Key, t.Value]))));
89 expect(updatedTags.Environment).toBe("prod");
90
91 // Changing the authorized region replaces the authorization.
92 const replaced = yield* stack.deploy(
93 Effect.gen(function* () {
94 return yield* AggregationAuthorization("TestAuth", {
95 authorizedAccountId: AGGREGATOR_ACCOUNT,
96 authorizedAwsRegion: "us-west-2",
97 tags: { Environment: "prod" },
98 });
99 }),
100 );
101 expect(replaced.authorizedAwsRegion).toBe("us-west-2");
102 expect(replaced.aggregationAuthorizationArn).not.toBe(auth.aggregationAuthorizationArn);
103 yield* assertAuthorizationDeleted(AGGREGATOR_ACCOUNT, "us-east-1");
104
105 yield* stack.destroy();
106 yield* assertAuthorizationDeleted(AGGREGATOR_ACCOUNT, "us-west-2");
107 }),
108 { tags: ["provider:aws", "provider:aws:config", "live"], timeout: 120_000 },
109 );