The best way to control Claude Code, Codex and more from any computer or phone
| 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 | // The dedicated bookmarks API is deprecated: Cloudflare no longer allows |
| 18 | // creating NEW bookmark records through it. `POST |
| 19 | // /accounts/{id}/access/bookmarks/{uuid}` with a fresh UUID fails with HTTP |
| 20 | // 404 code 11021 "access.api.error.unknown_application" — surfaced as the |
| 21 | // typed `AccessBookmarkNotFound` error. The resource exists to manage |
| 22 | // pre-existing legacy records; the lifecycle test is gated behind an |
| 23 | // account that still has legacy bookmarks enabled, and the probe test |
| 24 | // always runs and pins the typed tag. |
| 25 | const entitled = !!process.env.CLOUDFLARE_TEST_ACCESS_BOOKMARKS; |
| 26 | |
| 27 | test.provider.skipIf(entitled)( |
| 28 | "legacy bookmark creation surfaces the typed AccessBookmarkNotFound error", |
| 29 | (stack) => |
| 30 | Effect.gen(function* () { |
| 31 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 32 | yield* stack.destroy(); |
| 33 | |
| 34 | const error = yield* zeroTrust |
| 35 | .createAccessBookmark({ |
| 36 | accountId, |
| 37 | bookmarkId: "aaaaaaaa-bbbb-4ccc-8ddd-eeeeffff0001", |
| 38 | name: "alchemy-access-bookmark-probe", |
| 39 | domain: "wiki.alchemy-test-2.us", |
| 40 | }) |
| 41 | .pipe(Effect.flip); |
| 42 | expect(error._tag).toEqual("AccessBookmarkNotFound"); |
| 43 | |
| 44 | // The list endpoint still works — the legacy API is read-only for |
| 45 | // accounts without pre-existing bookmark records. |
| 46 | const bookmarks = yield* zeroTrust.listAccessBookmarks({ accountId }); |
| 47 | expect(Array.isArray(bookmarks.result)).toBe(true); |
| 48 | }).pipe(logLevel), |
| 49 | { |
| 50 | tags: ["provider:cloudflare", "provider:cloudflare:access", "live"], |
| 51 | timeout: 120_000, |
| 52 | }, |
| 53 | ); |
| 54 | |
| 55 | test.provider.skipIf(!entitled)( |
| 56 | "create, update in place, and destroy bookmark", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 60 | |
| 61 | yield* stack.destroy(); |
| 62 | |
| 63 | const bookmark = yield* stack.deploy( |
| 64 | Effect.gen(function* () { |
| 65 | return yield* Cloudflare.Access.Bookmark("Wiki", { |
| 66 | domain: "wiki.alchemy-test-2.us", |
| 67 | }); |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | expect(bookmark.bookmarkId).toBeDefined(); |
| 72 | expect(bookmark.accountId).toEqual(accountId); |
| 73 | expect(bookmark.domain).toEqual("wiki.alchemy-test-2.us"); |
| 74 | expect(bookmark.appLauncherVisible).toBe(true); |
| 75 | |
| 76 | const actual = yield* zeroTrust.getAccessBookmark({ |
| 77 | accountId, |
| 78 | bookmarkId: bookmark.bookmarkId, |
| 79 | }); |
| 80 | expect(actual.id).toEqual(bookmark.bookmarkId); |
| 81 | |
| 82 | // Update — domain and visibility converge in place (same id). |
| 83 | const updated = yield* stack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | return yield* Cloudflare.Access.Bookmark("Wiki", { |
| 86 | domain: "docs.alchemy-test-2.us", |
| 87 | appLauncherVisible: false, |
| 88 | }); |
| 89 | }), |
| 90 | ); |
| 91 | expect(updated.bookmarkId).toEqual(bookmark.bookmarkId); |
| 92 | expect(updated.domain).toEqual("docs.alchemy-test-2.us"); |
| 93 | expect(updated.appLauncherVisible).toBe(false); |
| 94 | |
| 95 | yield* stack.destroy(); |
| 96 | |
| 97 | const afterDestroy = yield* zeroTrust |
| 98 | .getAccessBookmark({ |
| 99 | accountId, |
| 100 | bookmarkId: bookmark.bookmarkId, |
| 101 | }) |
| 102 | .pipe(Effect.catchTag("AccessBookmarkNotFound", () => Effect.succeed(undefined))); |
| 103 | expect(afterDestroy?.id ?? undefined).toBeUndefined(); |
| 104 | }).pipe(logLevel), |
| 105 | { |
| 106 | tags: ["provider:cloudflare", "provider:cloudflare:access", "live"], |
| 107 | timeout: 120_000, |
| 108 | }, |
| 109 | ); |
| 110 | |
| 111 | // The legacy bookmarks list endpoint is read-only but available on every |
| 112 | // account (it returns `[]` when there are no records), so the ungated case |
| 113 | // always exercises `list()` and asserts it hydrates the `read` shape. The |
| 114 | // entitled path additionally deploys a bookmark and asserts its presence. |
| 115 | test.provider( |
| 116 | "list enumerates access bookmarks at the account scope", |
| 117 | (stack) => |
| 118 | Effect.gen(function* () { |
| 119 | yield* stack.destroy(); |
| 120 | |
| 121 | const provider = yield* Provider.findProvider(Cloudflare.Access.Bookmark); |
| 122 | |
| 123 | if (!entitled) { |
| 124 | const all = yield* provider.list(); |
| 125 | expect(Array.isArray(all)).toBe(true); |
| 126 | for (const b of all) { |
| 127 | expect(typeof b.bookmarkId).toBe("string"); |
| 128 | expect(typeof b.accountId).toBe("string"); |
| 129 | } |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | const deployed = yield* stack.deploy( |
| 134 | Effect.gen(function* () { |
| 135 | return yield* Cloudflare.Access.Bookmark("ListWiki", { |
| 136 | domain: "wiki.alchemy-test-2.us", |
| 137 | }); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | const all = yield* provider.list(); |
| 142 | expect(all.some((b) => b.bookmarkId === deployed.bookmarkId)).toBe(true); |
| 143 | |
| 144 | yield* stack.destroy(); |
| 145 | }).pipe(logLevel), |
| 146 | { |
| 147 | tags: ["provider:cloudflare", "provider:cloudflare:access", "live"], |
| 148 | timeout: 120_000, |
| 149 | }, |
| 150 | ); |