@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 175 lines 5.9 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 zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9
10 const { test } = Test.make({
11 providers: Cloudflare.providers(),
12 state: Cloudflare.state(),
13 });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // Access custom pages are a plan-gated feature: on the standard testing
18 // account `POST /accounts/{id}/access/custom_pages` fails with HTTP 403 code
19 // 12133 "account does not have permission for custom pages" — surfaced as
20 // the typed `AccessCustomPagesNotEntitled` error. The full lifecycle is
21 // gated behind an entitled account supplied via env; the probe test always
22 // runs and pins the typed tag.
23 const entitled = !!process.env.CLOUDFLARE_TEST_ACCESS_CUSTOM_PAGES;
24
25 const HTML_A = "<html><body><h1>Access denied</h1></body></html>";
26 const HTML_B = "<html><body><h1>Still denied</h1></body></html>";
27
28 test.provider.skipIf(entitled)(
29 "unentitled accounts surface the typed AccessCustomPagesNotEntitled error",
30 (stack) =>
31 Effect.gen(function* () {
32 const { accountId } = yield* yield* CloudflareEnvironment;
33 yield* stack.destroy();
34
35 const error = yield* zeroTrust
36 .createAccessCustomPage({
37 accountId,
38 name: "alchemy-access-custom-page-probe",
39 type: "forbidden",
40 customHtml: HTML_A,
41 })
42 .pipe(Effect.flip);
43 expect(error._tag).toEqual("AccessCustomPagesNotEntitled");
44
45 // Reads on the same account succeed, proving the gate is on
46 // creation, not on the API token.
47 const direct = yield* zeroTrust
48 .getAccessCustomPage({
49 accountId,
50 customPageId: "00000000-0000-0000-0000-000000000000",
51 })
52 .pipe(Effect.catchTag("AccessCustomPageNotFound", () => Effect.succeed(undefined)));
53 expect(direct).toBeUndefined();
54 }).pipe(logLevel),
55 {
56 tags: ["provider:cloudflare", "provider:cloudflare:access", "live"],
57 timeout: 120_000,
58 },
59 );
60
61 test.provider.skipIf(!entitled)(
62 "create, update html in place, replace on type change, destroy",
63 (stack) =>
64 Effect.gen(function* () {
65 const { accountId } = yield* yield* CloudflareEnvironment;
66
67 yield* stack.destroy();
68
69 const page = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* Cloudflare.Access.CustomPage("BlockPage", {
72 type: "forbidden",
73 customHtml: HTML_A,
74 });
75 }),
76 );
77
78 expect(page.customPageId).toBeDefined();
79 expect(page.accountId).toEqual(accountId);
80 expect(page.type).toEqual("forbidden");
81
82 const actual = yield* zeroTrust.getAccessCustomPage({
83 accountId,
84 customPageId: page.customPageId,
85 });
86 expect(actual.uid).toEqual(page.customPageId);
87 expect(actual.customHtml).toEqual(HTML_A);
88
89 // Update — html converges in place (same uid).
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 return yield* Cloudflare.Access.CustomPage("BlockPage", {
93 type: "forbidden",
94 customHtml: HTML_B,
95 });
96 }),
97 );
98 expect(updated.customPageId).toEqual(page.customPageId);
99
100 const afterUpdate = yield* zeroTrust.getAccessCustomPage({
101 accountId,
102 customPageId: page.customPageId,
103 });
104 expect(afterUpdate.customHtml).toEqual(HTML_B);
105
106 // Replace — the type cannot change in place.
107 const replaced = yield* stack.deploy(
108 Effect.gen(function* () {
109 return yield* Cloudflare.Access.CustomPage("BlockPage", {
110 type: "identity_denied",
111 customHtml: HTML_B,
112 });
113 }),
114 );
115 expect(replaced.customPageId).not.toEqual(page.customPageId);
116 expect(replaced.type).toEqual("identity_denied");
117
118 yield* stack.destroy();
119
120 const afterDestroy = yield* zeroTrust
121 .getAccessCustomPage({
122 accountId,
123 customPageId: replaced.customPageId,
124 })
125 .pipe(Effect.catchTag("AccessCustomPageNotFound", () => Effect.succeed(undefined)));
126 expect(afterDestroy?.uid ?? undefined).toBeUndefined();
127 }).pipe(logLevel),
128 {
129 tags: ["provider:cloudflare", "provider:cloudflare:access", "live"],
130 timeout: 120_000,
131 },
132 );
133
134 // The custom-pages list endpoint is available on every account (it returns
135 // `[]` when there are no records), so the ungated case always exercises
136 // `list()` and asserts it hydrates the `read` shape. The entitled path
137 // additionally deploys a page and asserts its presence.
138 test.provider(
139 "list enumerates access custom pages at the account scope",
140 (stack) =>
141 Effect.gen(function* () {
142 yield* stack.destroy();
143
144 const provider = yield* Provider.findProvider(Cloudflare.Access.CustomPage);
145
146 if (!entitled) {
147 const all = yield* provider.list();
148 expect(Array.isArray(all)).toBe(true);
149 for (const p of all) {
150 expect(typeof p.customPageId).toBe("string");
151 expect(typeof p.accountId).toBe("string");
152 expect(typeof p.name).toBe("string");
153 }
154 return;
155 }
156
157 const deployed = yield* stack.deploy(
158 Effect.gen(function* () {
159 return yield* Cloudflare.Access.CustomPage("ListPage", {
160 type: "forbidden",
161 customHtml: HTML_A,
162 });
163 }),
164 );
165
166 const all = yield* provider.list();
167 expect(all.some((p) => p.customPageId === deployed.customPageId)).toBe(true);
168
169 yield* stack.destroy();
170 }).pipe(logLevel),
171 {
172 tags: ["provider:cloudflare", "provider:cloudflare:access", "live"],
173 timeout: 120_000,
174 },
175 );