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

main
ts 154 lines 5.47 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { ResourceShare } from "@alchemy.run/aws/RAM";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as ram from "@distilled.cloud/aws/ram";
6 import * as Provider from "alchemy/Provider";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: AWS.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // A well-known AWS documentation placeholder account ID; not assigned to any
17 // real account, so sharing to it is inert but exercises the PRINCIPAL path.
18 const EXTERNAL_PRINCIPAL = "123456789012";
19
20 class ShareStillLive extends Data.TaggedError("ShareStillLive") {}
21 class PrincipalStillAssociated extends Data.TaggedError("PrincipalStillAssociated") {}
22
23 const readShare = Effect.fn(function* (arn: string) {
24 const shares = yield* ram
25 .getResourceShares({ resourceOwner: "SELF", resourceShareArns: [arn] })
26 .pipe(
27 Effect.map((r) => r.resourceShares ?? []),
28 Effect.catchTag("UnknownResourceException", () => Effect.succeed([])),
29 );
30 return shares.find(
31 (s) => s.resourceShareArn === arn && s.status !== "DELETING" && s.status !== "DELETED",
32 );
33 });
34
35 const readPrincipals = Effect.fn(function* (arn: string) {
36 const associations = yield* ram
37 .getResourceShareAssociations({
38 resourceShareArns: [arn],
39 associationType: "PRINCIPAL",
40 })
41 .pipe(
42 Effect.map((r) => r.resourceShareAssociations ?? []),
43 Effect.catchTag("UnknownResourceException", () => Effect.succeed([])),
44 );
45 return associations
46 .filter((a) => a.status === "ASSOCIATED" || a.status === "ASSOCIATING")
47 .map((a) => a.associatedEntity);
48 });
49
50 const assertDeleted = Effect.fn(function* (arn: string) {
51 yield* readShare(arn).pipe(
52 Effect.flatMap((s) => (s === undefined ? Effect.void : Effect.fail(new ShareStillLive()))),
53 Effect.retry({
54 while: (e) => e instanceof ShareStillLive,
55 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(15)]),
56 }),
57 );
58 });
59
60 const shareStack = (props: { principals?: string[]; tags?: Record<string, string> }) =>
61 Effect.gen(function* () {
62 const share = yield* ResourceShare("TestShare", {
63 allowExternalPrincipals: true,
64 principals: props.principals,
65 tags: props.tags,
66 });
67 return { share };
68 });
69
70 test.provider(
71 "create, associate principal, update tags, disassociate, delete",
72 (stack) =>
73 Effect.gen(function* () {
74 yield* stack.destroy();
75
76 // 1. Create with an external principal + tags.
77 const { share } = yield* stack.deploy(
78 shareStack({
79 principals: [EXTERNAL_PRINCIPAL],
80 tags: { team: "platform" },
81 }),
82 );
83
84 expect(share.resourceShareArn).toMatch(/^arn:aws:ram:/);
85 expect(share.allowExternalPrincipals).toBe(true);
86 const arn = share.resourceShareArn;
87
88 // 2. Out-of-band: the share is ACTIVE with our tags.
89 const live = yield* readShare(arn);
90 expect(live?.resourceShareArn).toEqual(arn);
91 expect(live?.allowExternalPrincipals).toBe(true);
92 expect(live?.tags?.some((t) => t.key === "team" && t.value === "platform")).toBe(true);
93 expect(live?.tags?.some((t) => t.key === "alchemy::id")).toBe(true);
94
95 // 3. Out-of-band: the PRINCIPAL association exists (may still be
96 // ASSOCIATING immediately after create).
97 yield* Effect.gen(function* () {
98 const principals = yield* readPrincipals(arn);
99 expect(principals).toContain(EXTERNAL_PRINCIPAL);
100 }).pipe(
101 Effect.retry({
102 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(10)]),
103 }),
104 );
105
106 // 4. Update in place: drop the principal, add a tag. ARN is stable.
107 const { share: updated } = yield* stack.deploy(
108 shareStack({
109 principals: [],
110 tags: { team: "platform", env: "prod" },
111 }),
112 );
113 expect(updated.resourceShareArn).toEqual(arn);
114
115 // 5. Out-of-band: new tag present, principal disassociated.
116 const live2 = yield* readShare(arn);
117 expect(live2?.tags?.some((t) => t.key === "env" && t.value === "prod")).toBe(true);
118
119 yield* readPrincipals(arn).pipe(
120 Effect.flatMap((principals) =>
121 principals.includes(EXTERNAL_PRINCIPAL)
122 ? Effect.fail(new PrincipalStillAssociated())
123 : Effect.void,
124 ),
125 Effect.retry({
126 while: (e) => e instanceof PrincipalStillAssociated,
127 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(12)]),
128 }),
129 );
130
131 // 6. Delete and confirm gone.
132 yield* stack.destroy();
133 yield* assertDeleted(arn);
134 }).pipe(logLevel),
135 { tags: ["provider:aws", "provider:aws:ram", "live"], timeout: 240_000 },
136 );
137
138 test.provider(
139 "list enumerates the deployed resource share",
140 (stack) =>
141 Effect.gen(function* () {
142 yield* stack.destroy();
143
144 const { share } = yield* stack.deploy(shareStack({}));
145
146 const provider = yield* Provider.findProvider(ResourceShare);
147 const all = yield* provider.list();
148 expect(all.some((x) => x.resourceShareArn === share.resourceShareArn)).toBe(true);
149
150 yield* stack.destroy();
151 yield* assertDeleted(share.resourceShareArn);
152 }).pipe(logLevel),
153 { tags: ["provider:aws", "provider:aws:ram", "live"], timeout: 240_000 },
154 );