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

main
ts 160 lines 6.53 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as accounts from "@distilled.cloud/cloudflare/accounts";
6 import * as iam from "@distilled.cloud/cloudflare/iam";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // The membership identity is an account-member id. Look one up from the
18 // account's member roster (the testing account always has at least its
19 // owner as an accepted member); pick deterministically.
20 const findMemberId = (accountId: string) =>
21 accounts.listMembers.items({ accountId, status: "accepted" }).pipe(
22 Stream.runCollect,
23 Effect.flatMap((chunk) => {
24 const ids = Array.from(chunk)
25 .flatMap((m) => (m.id ? [m.id] : []))
26 .sort();
27 return ids.length > 0
28 ? Effect.succeed(ids[0]!)
29 : Effect.die(new Error("no accepted account members found"));
30 }),
31 );
32
33 // The scoped API token the test harness mints propagates eventually-
34 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
35 // declared in the distilled error union) on out-of-band verification calls.
36 const getMembership = (accountId: string, userGroupId: string, memberId: string) =>
37 iam.getUserGroupMember({ accountId, userGroupId, memberId }).pipe(
38 Effect.retry({
39 while: (e) => e._tag === "Forbidden",
40 schedule: Schedule.exponential("500 millis"),
41 times: 8,
42 }),
43 );
44
45 // A removed member answers GET with the typed `UserGroupMemberNotFound`;
46 // once the group itself is destroyed, with `UserGroupNotFound`. Both are
47 // the success condition here.
48 const expectGone = (accountId: string, userGroupId: string, memberId: string) =>
49 getMembership(accountId, userGroupId, memberId).pipe(
50 Effect.asSome,
51 Effect.catchTag(["UserGroupMemberNotFound", "UserGroupNotFound"], () => Effect.succeedNone),
52 Effect.repeat({
53 schedule: Schedule.exponential("500 millis"),
54 until: (m) => m._tag === "None",
55 times: 8,
56 }),
57 Effect.map((m) => expect(m._tag).toEqual("None")),
58 );
59
60 // Both groups stay deployed across both steps; only the membership's target
61 // group flips, so the replacement is isolated to the membership itself.
62 //
63 // Group names are provider-generated (unique per stack instance) rather than
64 // hardcoded constants: each `test.provider` runs in its own scratch stack, so
65 // a shared deterministic name would make the two cases in this file adopt the
66 // SAME physical group (user-group names are unique per account) and stomp on
67 // each other when their lifecycles overlap. Letting the engine name them
68 // keeps every test's groups isolated and self-healing.
69 const program = (opts: { memberId: string; target: "A" | "B" }) =>
70 Effect.gen(function* () {
71 const groupA = yield* Cloudflare.Iam.UserGroup("GroupA", {});
72 const groupB = yield* Cloudflare.Iam.UserGroup("GroupB", {});
73 const membership = yield* Cloudflare.Iam.UserGroupMembership("Membership", {
74 userGroup: opts.target === "A" ? groupA.userGroupId : groupB.userGroupId,
75 memberId: opts.memberId,
76 });
77 return { groupA, groupB, membership };
78 });
79
80 test.provider(
81 "create, verify out-of-band, replace when the user group changes, destroy",
82 (stack) =>
83 Effect.gen(function* () {
84 const { accountId } = yield* yield* CloudflareEnvironment;
85 const memberId = yield* findMemberId(accountId);
86
87 yield* stack.destroy();
88
89 // Create — member joins group A.
90 const v1 = yield* stack.deploy(program({ memberId, target: "A" }));
91
92 expect(v1.membership.userGroupId).toEqual(v1.groupA.userGroupId);
93 expect(v1.membership.memberId).toEqual(memberId);
94 expect(v1.membership.accountId).toEqual(accountId);
95
96 // Out-of-band verification via the distilled API.
97 const live = yield* getMembership(accountId, v1.groupA.userGroupId, memberId);
98 expect(live.id).toEqual(memberId);
99
100 // Changing the target group is an identity change — the membership is
101 // replaced: added to group B, removed from group A.
102 const v2 = yield* stack.deploy(program({ memberId, target: "B" }));
103
104 expect(v2.groupA.userGroupId).toEqual(v1.groupA.userGroupId);
105 expect(v2.groupB.userGroupId).toEqual(v1.groupB.userGroupId);
106 expect(v2.membership.userGroupId).toEqual(v2.groupB.userGroupId);
107 expect(v2.membership.memberId).toEqual(memberId);
108
109 const moved = yield* getMembership(accountId, v2.groupB.userGroupId, memberId);
110 expect(moved.id).toEqual(memberId);
111
112 // The old membership in group A was deleted by the replacement.
113 yield* expectGone(accountId, v1.groupA.userGroupId, memberId);
114
115 yield* stack.destroy();
116
117 // Destroy removed the membership (and the groups themselves).
118 yield* expectGone(accountId, v2.groupB.userGroupId, memberId);
119 }).pipe(logLevel),
120 {
121 tags: ["provider:cloudflare", "provider:cloudflare:account", "provider:cloudflare:iam", "live"],
122 timeout: 120_000,
123 },
124 );
125
126 test.provider(
127 "list enumerates memberships across all user groups in the account",
128 (stack) =>
129 Effect.gen(function* () {
130 const { accountId } = yield* yield* CloudflareEnvironment;
131 const memberId = yield* findMemberId(accountId);
132
133 yield* stack.destroy();
134
135 // Deploy a membership so there is at least one to enumerate.
136 const deployed = yield* stack.deploy(program({ memberId, target: "A" }));
137
138 // Resolve the provider with the typed helper — element type is the
139 // resource's exact Attributes shape (no `any`).
140 const provider = yield* Provider.findProvider(Cloudflare.Iam.UserGroupMembership);
141
142 // Parent fan-out + per-group pagination must surface our deployed
143 // membership somewhere in the exhaustively-collected result.
144 const all = yield* provider.list();
145 expect(
146 all.some(
147 (m) =>
148 m.userGroupId === deployed.membership.userGroupId &&
149 m.memberId === memberId &&
150 m.accountId === accountId,
151 ),
152 ).toBe(true);
153
154 yield* stack.destroy();
155 }).pipe(logLevel),
156 {
157 tags: ["provider:cloudflare", "provider:cloudflare:account", "provider:cloudflare:iam", "live"],
158 timeout: 120_000,
159 },
160 );