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

main
ts 207 lines 7.94 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as organizations from "@distilled.cloud/cloudflare/organizations";
5 import * as Provider from "alchemy/Provider";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { testName } from "../../names.ts";
10
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Organizations are entitlement-gated (closed rollout for tenant /
16 // enterprise customers). On the standard testing account every
17 // `/organizations` call fails with the typed `Forbidden` error
18 // (HTTP 403, Cloudflare code 10000 "Authentication error"). The full
19 // lifecycle test below is gated behind an explicit opt-in env flag for
20 // entitled accounts; the probe test always runs and pins the typed tag.
21 const entitled = !!process.env.CLOUDFLARE_TEST_ORGANIZATIONS;
22
23 // Probe once per suite: either the account can list organizations
24 // (entitled) or the call fails with the typed `Forbidden` tag.
25 const probeEntitlement = organizations.listOrganizations({ pageSize: 1 }).pipe(
26 Effect.as(true),
27 Effect.catchTag("Forbidden", () => Effect.succeed(false)),
28 );
29
30 // Deterministic names — the same on every run (never Date.now()/random).
31 const ORG_NAME_CRUD = testName("alchemy-org-crud");
32 const ORG_NAME_CRUD_RENAMED = testName("alchemy-org-crud-renamed");
33
34 const PROFILE = {
35 businessName: "Alchemy Test Co",
36 businessEmail: "test@alchemy.run",
37 businessPhone: "+1-555-0100",
38 businessAddress: "1 Test Way, Springfield",
39 externalMetadata: "alchemy:test",
40 } as const;
41
42 test.provider(
43 "unentitled accounts surface the typed Forbidden error",
44 (stack) =>
45 Effect.gen(function* () {
46 yield* stack.destroy();
47
48 const canList = yield* probeEntitlement;
49 if (canList) {
50 // Entitled account — nothing to assert here; the lifecycle test
51 // covers the real behavior.
52 yield* Effect.logInfo("account is organizations-entitled; probe test is a no-op");
53 return;
54 }
55
56 // The typed tag — not UnknownCloudflareError, not a status check.
57 const error = yield* organizations.listOrganizations({ pageSize: 1 }).pipe(Effect.flip);
58 expect(error._tag).toEqual("Forbidden");
59
60 const createError = yield* organizations
61 .createOrganization({ name: ORG_NAME_CRUD })
62 .pipe(Effect.flip);
63 expect(createError._tag).toEqual("Forbidden");
64
65 yield* stack.destroy();
66 }).pipe(logLevel),
67 { tags: ["provider:cloudflare", "provider:cloudflare:organization", "live"] },
68 );
69
70 // Canonical `list()` test — ungated probe. `list()` enumerates every
71 // organization reachable by the credentials. On an unentitled account the
72 // `/organizations` collection rejects with the typed `Forbidden` tag, which
73 // the provider's `list()` deliberately tolerates (returning `[]`) so that
74 // account-wide enumeration / `nuke` never blows up on a non-tenant account;
75 // the raw-op probe test above already pins the typed tag. On an entitled
76 // account it returns a well-typed `Attributes[]`.
77 test.provider(
78 "list either enumerates organizations or tolerates the unentitled account",
79 (stack) =>
80 Effect.gen(function* () {
81 yield* stack.destroy();
82
83 const provider = yield* Provider.findProvider(Cloudflare.Organization.Organization);
84
85 const canList = yield* probeEntitlement;
86 if (canList) {
87 const all = yield* provider.list();
88 // Well-typed Attributes array; entitled accounts may have zero or
89 // more organizations, so only assert the shape.
90 expect(Array.isArray(all)).toBe(true);
91 for (const org of all) {
92 expect(typeof org.organizationId).toBe("string");
93 expect(typeof org.name).toBe("string");
94 }
95 } else {
96 // Unentitled: list() swallows the typed Forbidden and reports an
97 // empty collection (nuke-safe), rather than propagating.
98 const all = yield* provider.list();
99 expect(all).toEqual([]);
100 }
101
102 yield* stack.destroy();
103 }).pipe(logLevel),
104 { tags: ["provider:cloudflare", "provider:cloudflare:organization", "live"] },
105 );
106
107 // Gated live enumeration: on an entitled account, a deployed organization
108 // must appear in the exhaustively-paginated `list()` result.
109 test.provider.skipIf(!entitled)(
110 "list enumerates the deployed organization",
111 (stack) =>
112 Effect.gen(function* () {
113 yield* stack.destroy();
114
115 const deployed = yield* stack.deploy(
116 Effect.gen(function* () {
117 return yield* Cloudflare.Organization.Organization("ListOrg", { name: ORG_NAME_CRUD });
118 }),
119 );
120
121 const provider = yield* Provider.findProvider(Cloudflare.Organization.Organization);
122 const all = yield* provider.list();
123
124 expect(all.some((o) => o.organizationId === deployed.organizationId)).toBe(true);
125
126 yield* stack.destroy();
127 }).pipe(logLevel),
128 { tags: ["provider:cloudflare", "provider:cloudflare:organization", "live"], timeout: 120_000 },
129 );
130
131 // Poll until the organization is gone after destroy. Cloudflare answers
132 // GET for a missing org with the typed `OrganizationNotFound` (404).
133 const expectGone = (organizationId: string) =>
134 organizations.getOrganization({ organizationId }).pipe(
135 Effect.asSome,
136 Effect.catchTag("OrganizationNotFound", () => Effect.succeedNone),
137 Effect.repeat({
138 schedule: Schedule.exponential("500 millis"),
139 until: (org) => org._tag === "None",
140 times: 8,
141 }),
142 Effect.map((org) => expect(org._tag).toEqual("None")),
143 );
144
145 test.provider.skipIf(!entitled)(
146 "create, verify out-of-band, update in place, destroy",
147 (stack) =>
148 Effect.gen(function* () {
149 yield* stack.destroy();
150
151 // Create with a business profile.
152 const v1 = yield* stack.deploy(
153 Effect.gen(function* () {
154 return yield* Cloudflare.Organization.Organization("Org", {
155 name: ORG_NAME_CRUD,
156 profile: PROFILE,
157 });
158 }),
159 );
160
161 expect(v1.organizationId).toBeTruthy();
162 expect(v1.name).toEqual(ORG_NAME_CRUD);
163 expect(v1.createTime).toBeTruthy();
164 expect(v1.profile).toEqual(PROFILE);
165
166 // Out-of-band verification via the distilled API.
167 const live = yield* organizations.getOrganization({ organizationId: v1.organizationId });
168 expect(live.name).toEqual(ORG_NAME_CRUD);
169 expect(live.profile).toEqual(PROFILE);
170
171 // In-place update — rename and change the profile email. Same
172 // organization (no replacement).
173 const v2 = yield* stack.deploy(
174 Effect.gen(function* () {
175 return yield* Cloudflare.Organization.Organization("Org", {
176 name: ORG_NAME_CRUD_RENAMED,
177 profile: { ...PROFILE, businessEmail: "ops@alchemy.run" },
178 });
179 }),
180 );
181
182 expect(v2.organizationId).toEqual(v1.organizationId);
183 expect(v2.createTime).toEqual(v1.createTime);
184 expect(v2.name).toEqual(ORG_NAME_CRUD_RENAMED);
185 expect(v2.profile?.businessEmail).toEqual("ops@alchemy.run");
186
187 const updated = yield* organizations.getOrganization({ organizationId: v2.organizationId });
188 expect(updated.name).toEqual(ORG_NAME_CRUD_RENAMED);
189 expect(updated.profile?.businessEmail).toEqual("ops@alchemy.run");
190
191 // No-op deploy — same desired state, same identity.
192 const v3 = yield* stack.deploy(
193 Effect.gen(function* () {
194 return yield* Cloudflare.Organization.Organization("Org", {
195 name: ORG_NAME_CRUD_RENAMED,
196 profile: { ...PROFILE, businessEmail: "ops@alchemy.run" },
197 });
198 }),
199 );
200 expect(v3.organizationId).toEqual(v1.organizationId);
201
202 yield* stack.destroy();
203
204 yield* expectGone(v1.organizationId);
205 }).pipe(logLevel),
206 { tags: ["provider:cloudflare", "provider:cloudflare:organization", "live"], timeout: 120_000 },
207 );