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

main
ts 115 lines 4.28 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Domain } from "@alchemy.run/aws/DataZone";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as datazone from "@distilled.cloud/aws/datazone";
6 import * as iam from "@distilled.cloud/aws/iam";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: AWS.providers() });
12
13 // A deleted domain is reported by GetDomain as AccessDeniedException, not
14 // ResourceNotFoundException — DataZone checks domain-scoped auth before
15 // existence. Both mean "absent".
16 const findDomain = (identifier: string) =>
17 datazone.getDomain({ identifier }).pipe(
18 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined)),
19 Effect.catchTag("AccessDeniedException", () => Effect.succeed(undefined)),
20 );
21
22 class StillExists extends Data.TaggedError("StillExists")<{
23 readonly what: string;
24 }> {}
25
26 // Domain deletion is asynchronous (typically ~1 minute) — poll until gone.
27 const assertDomainGone = (identifier: string) =>
28 findDomain(identifier).pipe(
29 Effect.flatMap((d) =>
30 d === undefined || d.status === "DELETED" || d.status === "DELETING"
31 ? Effect.void
32 : Effect.fail(new StillExists({ what: `domain ${identifier}` })),
33 ),
34 Effect.retry({
35 while: (e) => e._tag === "StillExists",
36 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]),
37 }),
38 );
39
40 test.provider(
41 "create domain with managed role, update description, delete",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 // 1. Deploy a domain (auto-created execution role).
47 const result = yield* stack.deploy(
48 Effect.gen(function* () {
49 const domain = yield* Domain("TestDomain", {
50 description: "alchemy datazone domain test",
51 tags: { Environment: "test" },
52 });
53 return {
54 domainId: domain.domainId,
55 domainArn: domain.domainArn,
56 name: domain.name,
57 status: domain.status,
58 portalUrl: domain.portalUrl,
59 roleName: domain.roleName,
60 domainExecutionRole: domain.domainExecutionRole,
61 };
62 }),
63 );
64
65 expect(result.domainId).toMatch(/^dzd/);
66 expect(result.domainArn).toContain(":domain/");
67 expect(result.status).toBe("AVAILABLE");
68
69 // out-of-band: domain is AVAILABLE and tags are branded.
70 const created = yield* findDomain(result.domainId);
71 expect(created).toBeDefined();
72 expect(created!.status).toBe("AVAILABLE");
73 expect(created!.name).toBe(result.name);
74 expect(created!.tags?.Environment).toBe("test");
75 expect(created!.tags?.["alchemy::id"]).toBe("TestDomain");
76
77 // out-of-band: the managed execution role exists.
78 expect(result.roleName).toBeDefined();
79 const role = yield* iam
80 .getRole({ RoleName: result.roleName! })
81 .pipe(Effect.map((r) => r.Role));
82 expect(role.Arn).toBe(result.domainExecutionRole);
83 expect(created!.domainExecutionRole).toBe(result.domainExecutionRole);
84
85 // 2. Update the description — converges via updateDomain, same domain.
86 const updated = yield* stack.deploy(
87 Effect.gen(function* () {
88 const domain = yield* Domain("TestDomain", {
89 description: "alchemy datazone domain test (updated)",
90 tags: { Environment: "test", Update: "yes" },
91 });
92 return { domainId: domain.domainId };
93 }),
94 );
95 expect(updated.domainId).toBe(result.domainId);
96
97 const observed = yield* findDomain(result.domainId);
98 expect(observed!.description).toBe("alchemy datazone domain test (updated)");
99 expect(observed!.tags?.Update).toBe("yes");
100
101 // 3. Destroy — domain and managed role are removed.
102 yield* stack.destroy();
103 yield* assertDomainGone(result.domainId);
104
105 const roleGone = yield* iam.getRole({ RoleName: result.roleName! }).pipe(
106 Effect.map(() => false),
107 Effect.catchTag("NoSuchEntityException", () => Effect.succeed(true)),
108 );
109 expect(roleGone).toBe(true);
110 }),
111 {
112 tags: ["provider:aws", "provider:aws:datazone", "provider:aws:iam", "live"],
113 timeout: 480_000,
114 },
115 );