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

main
ts 193 lines 7.1 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 iam from "@distilled.cloud/cloudflare/iam";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import { testName } from "../../names.ts";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // Deterministic names — the same on every run (never Date.now()/random).
18 const UG_NAME = testName("alchemy-iam-ug-crud");
19 const UG_NAME_RENAMED = testName("alchemy-iam-ug-crud-renamed");
20 const RG_NAME = testName("alchemy-iam-ug-crud-rg");
21
22 // Built-in permission groups looked up by their stable catalog names.
23 const PERMISSION_GROUP_NAME = "Page Rules Read";
24 const PERMISSION_GROUP_NAME_2 = "Security Center Read";
25
26 const findPermissionGroupId = (accountId: string, name: string) =>
27 iam.listPermissionGroups.items({ accountId, name }).pipe(
28 Stream.runHead,
29 Effect.flatMap((pg) =>
30 pg._tag === "Some"
31 ? Effect.succeed(pg.value.id)
32 : Effect.die(new Error(`permission group "${name}" not found`)),
33 ),
34 );
35
36 // The scoped API token the test harness mints propagates eventually-
37 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
38 // declared in the distilled error union) on out-of-band verification calls.
39 const getUserGroup = (accountId: string, userGroupId: string) =>
40 iam.getUserGroup({ accountId, userGroupId }).pipe(
41 Effect.retry({
42 while: (e) => e._tag === "Forbidden",
43 schedule: Schedule.exponential("500 millis"),
44 times: 8,
45 }),
46 );
47
48 // A destroyed user group answers GET with the typed `UserGroupNotFound`.
49 const expectGone = (accountId: string, userGroupId: string) =>
50 getUserGroup(accountId, userGroupId).pipe(
51 Effect.asSome,
52 Effect.catchTag("UserGroupNotFound", () => Effect.succeedNone),
53 Effect.repeat({
54 schedule: Schedule.exponential("500 millis"),
55 until: (g) => g._tag === "None",
56 times: 8,
57 }),
58 Effect.map((g) => expect(g._tag).toEqual("None")),
59 );
60
61 // One program deploying the resource group and the user group whose policy
62 // references it. The policy props reference the resource group's output, so
63 // the engine orders the user group after it on deploy (and before it on
64 // destroy).
65 const program = (opts: { name: string; permissionGroupId: string; accountScopeKey: string }) =>
66 Effect.gen(function* () {
67 const rg = yield* Cloudflare.Iam.ResourceGroup("Rg", {
68 name: RG_NAME,
69 scope: { key: opts.accountScopeKey, objects: [{ key: "*" }] },
70 });
71 const group = yield* Cloudflare.Iam.UserGroup("Group", {
72 name: opts.name,
73 policies: [
74 {
75 // Cloudflare rejects `deny` user-group policies ("Policy
76 // validation failed") — only `allow` is exercised here.
77 access: "allow",
78 permissionGroups: [opts.permissionGroupId],
79 resourceGroups: [rg.resourceGroupId],
80 },
81 ],
82 });
83 return { rg, group };
84 });
85
86 test.provider(
87 "create with a policy, verify out-of-band, update policies in place, destroy",
88 (stack) =>
89 Effect.gen(function* () {
90 const { accountId } = yield* yield* CloudflareEnvironment;
91 const accountScopeKey = `com.cloudflare.api.account.${accountId}`;
92 const permissionGroupId = yield* findPermissionGroupId(accountId, PERMISSION_GROUP_NAME);
93 const permissionGroupId2 = yield* findPermissionGroupId(accountId, PERMISSION_GROUP_NAME_2);
94
95 yield* stack.destroy();
96
97 // Create — one allow policy joining the permission group and the
98 // resource group.
99 const v1 = yield* stack.deploy(
100 program({
101 name: UG_NAME,
102 permissionGroupId,
103 accountScopeKey,
104 }),
105 );
106
107 expect(v1.group.userGroupId).toBeTruthy();
108 expect(v1.group.accountId).toEqual(accountId);
109 expect(v1.group.name).toEqual(UG_NAME);
110 expect(v1.group.createdOn).toBeTruthy();
111 expect(v1.group.policies).toHaveLength(1);
112 expect(v1.group.policies[0]!.access).toEqual("allow");
113 expect(v1.group.policies[0]!.permissionGroups).toEqual([permissionGroupId]);
114 expect(v1.group.policies[0]!.resourceGroups).toEqual([v1.rg.resourceGroupId]);
115
116 // Out-of-band verification via the distilled API.
117 const live = yield* getUserGroup(accountId, v1.group.userGroupId);
118 expect(live.name).toEqual(UG_NAME);
119 expect(live.policies ?? []).toHaveLength(1);
120 expect(live.policies?.[0]?.access).toEqual("allow");
121
122 // In-place update — rename and swap the policy's permission group.
123 // Same user group (no replacement).
124 const v2 = yield* stack.deploy(
125 program({
126 name: UG_NAME_RENAMED,
127 permissionGroupId: permissionGroupId2,
128 accountScopeKey,
129 }),
130 );
131
132 expect(v2.group.userGroupId).toEqual(v1.group.userGroupId);
133 expect(v2.group.name).toEqual(UG_NAME_RENAMED);
134 expect(v2.group.policies[0]!.permissionGroups).toEqual([permissionGroupId2]);
135
136 const updated = yield* getUserGroup(accountId, v2.group.userGroupId);
137 expect(updated.name).toEqual(UG_NAME_RENAMED);
138 expect(updated.policies?.[0]?.permissionGroups?.[0]?.id).toEqual(permissionGroupId2);
139
140 // No-op deploy — same desired state, same identity, reconcile
141 // observes the in-sync state and applies nothing.
142 const v3 = yield* stack.deploy(
143 program({
144 name: UG_NAME_RENAMED,
145 permissionGroupId: permissionGroupId2,
146 accountScopeKey,
147 }),
148 );
149 expect(v3.group.userGroupId).toEqual(v1.group.userGroupId);
150
151 yield* stack.destroy();
152
153 yield* expectGone(accountId, v1.group.userGroupId);
154 }).pipe(logLevel),
155 {
156 tags: ["provider:cloudflare", "provider:cloudflare:iam", "live"],
157 timeout: 120_000,
158 },
159 );
160
161 const UG_LIST_NAME = testName("alchemy-iam-ug-list");
162
163 test.provider(
164 "list enumerates the deployed user group",
165 (stack) =>
166 Effect.gen(function* () {
167 yield* stack.destroy();
168
169 const deployed = yield* stack.deploy(
170 Effect.gen(function* () {
171 return yield* Cloudflare.Iam.UserGroup("ListGroup", {
172 name: UG_LIST_NAME,
173 });
174 }),
175 );
176
177 const provider = yield* Provider.findProvider(Cloudflare.Iam.UserGroup);
178 const all = yield* provider.list();
179
180 // Exhaustively-paginated account collection contains the deployed
181 // group, hydrated into the exact `read` Attributes shape.
182 const found = all.find((g) => g.userGroupId === deployed.userGroupId);
183 expect(found).toBeTruthy();
184 expect(found!.name).toEqual(UG_LIST_NAME);
185 expect(found!.accountId).toEqual(deployed.accountId);
186
187 yield* stack.destroy();
188 }).pipe(logLevel),
189 {
190 tags: ["provider:cloudflare", "provider:cloudflare:iam", "live"],
191 timeout: 120_000,
192 },
193 );