@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 136 lines 5.52 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 resourceSharing from "@distilled.cloud/cloudflare/resource-sharing";
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
11 const { test } = Test.make({ providers: Cloudflare.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Resource Sharing writes are permission-gated: on the standard testing
16 // account every POST/PUT/DELETE under /accounts/{id}/shares fails with the
17 // typed `Forbidden` error (HTTP 403, Cloudflare code 10000). Reads
18 // (list/get) succeed. Sharing also requires a *second* account/organization
19 // as recipient. The deploy half of the list test is gated behind an explicit
20 // opt-in env var pointing at a recipient account; the read-only list
21 // assertion always runs.
22 const recipientAccountId = process.env.CLOUDFLARE_TEST_SHARE_RECIPIENT_ACCOUNT_ID;
23
24 const getShare = (accountId: string, shareId: string) =>
25 resourceSharing.getResourceSharing({ accountId, shareId }).pipe(
26 Effect.retry({
27 while: (e) => e._tag === "Forbidden",
28 schedule: Schedule.exponential("500 millis"),
29 times: 8,
30 }),
31 );
32
33 // Poll until the share is gone after destroy — deletion is asynchronous
34 // (`active → deleting → deleted`); a fully purged share answers with the
35 // typed `ShareNotFound` (Cloudflare error code 1004).
36 const expectGone = (accountId: string, shareId: string) =>
37 getShare(accountId, shareId).pipe(
38 Effect.flatMap((share) =>
39 share.status === "deleting" || share.status === "deleted"
40 ? Effect.void
41 : Effect.fail({ _tag: "ShareNotDeleted" } as const),
42 ),
43 Effect.catchTag("ShareNotFound", () => Effect.void),
44 Effect.retry({
45 while: (e) => e._tag === "ShareNotDeleted",
46 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
47 }),
48 );
49
50 test.provider(
51 "list enumerates share resources across the account's sent shares (typed Attributes), and includes a deployed entry when entitled",
52 (stack) =>
53 Effect.gen(function* () {
54 const { accountId } = yield* yield* CloudflareEnvironment;
55
56 yield* stack.destroy();
57
58 const provider = yield* Provider.findProvider(Cloudflare.ResourceSharing.ShareResource);
59
60 // Reads (list) succeed without the Resource Sharing Edit permission, so
61 // the read-only assertion always runs: list() fans out over every sent
62 // share and returns the exhaustively-paginated resource entries in the
63 // read Attributes shape. On a non-entitled account with no sent shares
64 // this is a well-typed `[]`.
65 const before = yield* provider.list();
66 expect(Array.isArray(before)).toBe(true);
67 for (const entry of before) {
68 expect(typeof entry.shareResourceId).toBe("string");
69 expect(entry.accountId).toEqual(accountId);
70 expect(typeof entry.shareId).toBe("string");
71 expect(typeof entry.resourceId).toBe("string");
72 // Deleted / deleting entries are filtered out — never surfaced.
73 expect(entry.status).not.toEqual("deleted");
74 expect(entry.status).not.toEqual("deleting");
75 }
76
77 // Writes are permission-gated (and need a second recipient account), so
78 // only assert presence of a freshly-deployed entry when an entitled
79 // recipient account is provided. On the standard testing token the
80 // deploy would fail with the typed `Forbidden` (HTTP 403, code 10000).
81 if (recipientAccountId) {
82 const policyA = yield* stack.deploy(
83 Cloudflare.Gateway.Rule("ListEntryPolicyA", {
84 action: "block",
85 traffic: 'dns.fqdn == "list-entry-a.alchemy-test.example"',
86 filters: ["dns"],
87 enabled: false,
88 }),
89 );
90 const policyB = yield* stack.deploy(
91 Cloudflare.Gateway.Rule("ListEntryPolicyB", {
92 action: "block",
93 traffic: 'dns.fqdn == "list-entry-b.alchemy-test.example"',
94 filters: ["dns"],
95 enabled: false,
96 }),
97 );
98
99 const share = yield* stack.deploy(
100 Cloudflare.ResourceSharing.Share("ListEntryShare", {
101 recipients: [{ accountId: recipientAccountId }],
102 resources: [{ resourceType: "gateway-policy", resourceId: policyA.ruleId }],
103 }),
104 );
105
106 const entry = yield* stack.deploy(
107 Cloudflare.ResourceSharing.ShareResource("ListEntry", {
108 shareId: share.shareId,
109 resourceType: "gateway-policy",
110 resourceId: policyB.ruleId,
111 }),
112 );
113
114 const all = yield* provider.list();
115 expect(all.some((e) => e.shareResourceId === entry.shareResourceId)).toBe(true);
116 // The deployed entry surfaces in the read Attributes shape.
117 const found = all.find((e) => e.shareResourceId === entry.shareResourceId)!;
118 expect(found.shareId).toEqual(share.shareId);
119 expect(found.resourceId).toEqual(policyB.ruleId);
120
121 yield* stack.destroy();
122 yield* expectGone(accountId, share.shareId);
123 }
124
125 yield* stack.destroy();
126 }).pipe(logLevel),
127 {
128 tags: [
129 "provider:cloudflare",
130 "provider:cloudflare:gateway",
131 "provider:cloudflare:resourcesharing",
132 "live",
133 ],
134 timeout: 180_000,
135 },
136 );