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

main
ts 99 lines 3.55 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 Provider from "alchemy/Provider";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8
9 const { test } = Test.make({ providers: Cloudflare.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 // Resource Sharing writes are permission-gated and require a *second* account
14 // or organization as recipient. On the standard testing account every write
15 // under /accounts/{id}/shares fails with the typed `Forbidden` error, but the
16 // read path (list shares + list recipients per share) succeeds. The deploy
17 // test is therefore gated behind an env var pointing at a recipient account;
18 // the read-only `list()` assertion always runs.
19 const recipientAccountId = process.env.CLOUDFLARE_TEST_SHARE_RECIPIENT_ACCOUNT_ID;
20
21 test.provider(
22 "list() enumerates share recipients across the account's sent shares",
23 (stack) =>
24 Effect.gen(function* () {
25 yield* stack.destroy();
26
27 // Parent fan-out: list() enumerates the account's sent shares and then
28 // every recipient within each share. On a write-blocked account there
29 // may be zero shares — the result is still a well-typed array.
30 const provider = yield* Provider.findProvider(Cloudflare.ResourceSharing.ShareRecipient);
31 const all = yield* provider.list();
32
33 expect(Array.isArray(all)).toBe(true);
34 for (const r of all) {
35 expect(typeof r.recipientId).toBe("string");
36 expect(typeof r.shareId).toBe("string");
37 expect(typeof r.recipientAccountId).toBe("string");
38 expect(r.associationStatus).not.toEqual("disassociated");
39 }
40
41 yield* stack.destroy();
42 }).pipe(logLevel),
43 {
44 tags: ["provider:cloudflare", "provider:cloudflare:resourcesharing", "live"],
45 timeout: 120_000,
46 },
47 );
48
49 test.provider.skipIf(!recipientAccountId)(
50 "list() includes a freshly deployed ShareRecipient",
51 (stack) =>
52 Effect.gen(function* () {
53 const { accountId } = yield* yield* CloudflareEnvironment;
54 const recipient = recipientAccountId!;
55
56 yield* stack.destroy();
57
58 const policy = yield* stack.deploy(
59 Cloudflare.Gateway.Rule("RecipientListPolicy", {
60 action: "block",
61 traffic: 'dns.fqdn == "recipient-list.alchemy-test.example"',
62 filters: ["dns"],
63 enabled: false,
64 }),
65 );
66
67 const share = yield* stack.deploy(
68 Cloudflare.ResourceSharing.Share("RecipientListShare", {
69 recipients: [],
70 resources: [{ resourceType: "gateway-policy", resourceId: policy.ruleId }],
71 }),
72 );
73
74 const deployed = yield* stack.deploy(
75 Cloudflare.ResourceSharing.ShareRecipient("RecipientListEntry", {
76 shareId: share.shareId,
77 accountId: recipient,
78 }),
79 );
80 expect(deployed.recipientId).toBeTruthy();
81 expect(deployed.accountId).toEqual(accountId);
82
83 const provider = yield* Provider.findProvider(Cloudflare.ResourceSharing.ShareRecipient);
84 const all = yield* provider.list();
85
86 expect(all.some((r) => r.recipientId === deployed.recipientId)).toBe(true);
87
88 yield* stack.destroy();
89 }).pipe(logLevel),
90 {
91 tags: [
92 "provider:cloudflare",
93 "provider:cloudflare:gateway",
94 "provider:cloudflare:resourcesharing",
95 "live",
96 ],
97 timeout: 180_000,
98 },
99 );