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

main
ts 154 lines 6.04 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 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 { testName } from "../../names.ts";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // Creating accounts is tenant/partner-entitled. The standard testing
17 // account is NOT entitled, so by default only the read paths and the typed
18 // entitlement-rejection path run. Set CLOUDFLARE_TENANT_TEST=1 under tenant
19 // credentials to exercise the full CRUD lifecycle on a real subaccount.
20 const tenantEntitled = !!process.env.CLOUDFLARE_TENANT_TEST;
21
22 // A syntactically valid account id that cannot exist.
23 const missingAccountId = "00000000000000000000000000000000";
24
25 test.provider(
26 "read path: getAccount observes the testing account",
27 (stack) =>
28 Effect.gen(function* () {
29 const { accountId } = yield* yield* CloudflareEnvironment;
30
31 yield* stack.destroy();
32
33 const observed = yield* accounts.getAccount({ accountId });
34 expect(observed.id).toEqual(accountId);
35 expect(observed.name).toBeTruthy();
36 expect(["standard", "enterprise"]).toContain(observed.type);
37
38 yield* stack.destroy();
39 }).pipe(logLevel),
40 { tags: ["provider:cloudflare", "provider:cloudflare:account", "live"] },
41 );
42
43 test.provider(
44 "inaccessible account surfaces a typed tag",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48
49 // With an account-scoped token, an account id outside the token's
50 // scope is rejected with the typed `Unauthorized` tag before routing.
51 // (A tenant-owned account that has been deleted surfaces as
52 // `InvalidRoute`, code 7003 — the tag the provider's `read` and
53 // `delete` treat as "gone".)
54 const error = yield* accounts.getAccount({ accountId: missingAccountId }).pipe(Effect.flip);
55 expect(error._tag).toEqual("Unauthorized");
56
57 yield* stack.destroy();
58 }).pipe(logLevel),
59 { tags: ["provider:cloudflare", "provider:cloudflare:account", "live"] },
60 );
61
62 // Account creation is tenant-gated, so list() is verified read-only: a
63 // standard token can LIST every account it can access, and the token's own
64 // testing account must appear in the exhaustively-paginated result with the
65 // exact `Attributes` shape `read` produces.
66 test.provider(
67 "list enumerates accessible accounts (read-only)",
68 (stack) =>
69 Effect.gen(function* () {
70 const { accountId } = yield* yield* CloudflareEnvironment;
71
72 yield* stack.destroy();
73
74 const provider = yield* Provider.findProvider(Cloudflare.Account.Account);
75 const all = yield* provider.list();
76
77 expect(all.length).toBeGreaterThan(0);
78
79 const self = all.find((a) => a.accountId === accountId);
80 expect(self).toBeDefined();
81 expect(self?.name).toBeTruthy();
82 expect(["standard", "enterprise"]).toContain(self?.type);
83 expect(typeof self?.enforceTwofactor).toEqual("boolean");
84
85 yield* stack.destroy();
86 }).pipe(logLevel),
87 { tags: ["provider:cloudflare", "provider:cloudflare:account", "live"] },
88 );
89
90 test.provider.skipIf(tenantEntitled)(
91 "createAccount is entitlement-gated: typed AccountCreationForbidden",
92 (stack) =>
93 Effect.gen(function* () {
94 yield* stack.destroy();
95
96 // The standard testing account lacks the tenant entitlement, so
97 // account creation fails with the typed tag (code 1002). Never
98 // touches the real testing account.
99 const error = yield* accounts
100 .createAccount({ name: "alchemy-test-entitlement-probe" })
101 .pipe(Effect.flip);
102 expect(error._tag).toEqual("AccountCreationForbidden");
103
104 yield* stack.destroy();
105 }).pipe(logLevel),
106 { tags: ["provider:cloudflare", "provider:cloudflare:account", "live"] },
107 );
108
109 // Full lifecycle — only under tenant credentials (CLOUDFLARE_TENANT_TEST=1).
110 // Creates a brand-new subaccount; the real testing account is never mutated.
111 test.provider.skipIf(!tenantEntitled)(
112 "create subaccount, update name and settings in place, delete",
113 (stack) =>
114 Effect.gen(function* () {
115 yield* stack.destroy();
116
117 const account = yield* stack.deploy(
118 Cloudflare.Account.Account("TestSubaccount", { name: "alchemy-test-subaccount" }),
119 );
120
121 expect(account.accountId).toBeTruthy();
122 expect(account.name).toEqual("alchemy-test-subaccount");
123 expect(account.type).toEqual("standard");
124
125 // Out-of-band verify.
126 const live = yield* accounts.getAccount({ accountId: account.accountId });
127 expect(live.name).toEqual("alchemy-test-subaccount");
128
129 // Rename + settings update happen in place (same account id).
130 const updated = yield* stack.deploy(
131 Cloudflare.Account.Account("TestSubaccount", {
132 name: testName("alchemy-test-subaccount-renamed"),
133 enforceTwofactor: true,
134 }),
135 );
136 expect(updated.accountId).toEqual(account.accountId);
137 expect(updated.name).toEqual(testName("alchemy-test-subaccount-renamed"));
138 expect(updated.enforceTwofactor).toEqual(true);
139
140 yield* stack.destroy();
141
142 // Deletion is queued on Cloudflare's side; the account becomes
143 // unreadable (`InvalidRoute`) once it leaves the tenant.
144 yield* accounts.getAccount({ accountId: account.accountId }).pipe(
145 Effect.flatMap(() => Effect.fail({ _tag: "AccountNotDeleted" } as const)),
146 Effect.catchTag("InvalidRoute", () => Effect.void),
147 Effect.retry({
148 while: (e) => e._tag === "AccountNotDeleted",
149 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
150 }),
151 );
152 }).pipe(logLevel),
153 { tags: ["provider:cloudflare", "provider:cloudflare:account", "live"] },
154 );