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 intel from "@distilled.cloud/cloudflare/intel"; |
| 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 | // Custom indicator feeds require the account to be approved as a |
| 16 | // Cloudforce One feed provider. On the standard testing account |
| 17 | // `POST /accounts/{id}/intel/indicator-feeds` fails with HTTP 403 |
| 18 | // "Account does not have permission to create a feed" — surfaced as the |
| 19 | // typed `IndicatorFeedsNotEntitled` error. The full lifecycle tests below |
| 20 | // are gated behind an entitled account supplied via env; the probe test |
| 21 | // always runs and pins the typed tags. |
| 22 | // |
| 23 | // NOTE: Cloudflare exposes NO delete endpoint for indicator feeds — the |
| 24 | // provider adopts same-named feeds across runs (deterministic names), so |
| 25 | // the entitled lifecycle re-uses one feed instead of leaking. |
| 26 | const entitled = !!process.env.CLOUDFLARE_TEST_INTEL_FEEDS; |
| 27 | |
| 28 | // The scoped API token the test harness mints propagates eventually- |
| 29 | // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`, |
| 30 | // declared in the distilled error union) on out-of-band verification calls. |
| 31 | // `IndicatorFeedNotFound` also matches 403 (Cloudflare answers 403 for |
| 32 | // missing feeds), so only retry the cross-cutting Forbidden tag. |
| 33 | const getFeed = (accountId: string, feedId: number) => |
| 34 | intel.getIndicatorFeed({ accountId, feedId }).pipe( |
| 35 | Effect.retry({ |
| 36 | while: (e) => e._tag === "Forbidden", |
| 37 | schedule: Schedule.exponential("500 millis"), |
| 38 | times: 8, |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | // Deterministic name — feeds cannot be deleted, so every run of this suite |
| 43 | // adopts the same feed instead of leaking a new one. |
| 44 | const FEED_NAME = "alchemy-intel-test-feed"; |
| 45 | |
| 46 | test.provider.skipIf(entitled)( |
| 47 | "unentitled accounts surface the typed IndicatorFeedsNotEntitled error", |
| 48 | (stack) => |
| 49 | Effect.gen(function* () { |
| 50 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 51 | |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | // The testing account is not a feed provider — the distilled create |
| 55 | // call must fail with the typed entitlement tag. Reads on the same |
| 56 | // account succeed (and show no feeds), proving the gate is on |
| 57 | // creation, not on the API token. |
| 58 | const error = yield* intel |
| 59 | .createIndicatorFeed({ |
| 60 | accountId, |
| 61 | name: "alchemy-intel-entitlement-probe", |
| 62 | description: "entitlement probe", |
| 63 | }) |
| 64 | .pipe(Effect.flip); |
| 65 | expect(error._tag).toEqual("IndicatorFeedsNotEntitled"); |
| 66 | |
| 67 | const list = yield* intel.listIndicatorFeeds({ accountId }).pipe( |
| 68 | Effect.retry({ |
| 69 | while: (e) => e._tag === "Forbidden", |
| 70 | schedule: Schedule.exponential("500 millis"), |
| 71 | times: 8, |
| 72 | }), |
| 73 | ); |
| 74 | expect(list.result).toEqual([]); |
| 75 | |
| 76 | // A feed that does not exist surfaces as the typed |
| 77 | // `IndicatorFeedNotFound` (Cloudflare misuses HTTP 403 with a |
| 78 | // "does not exist" message for missing feeds). |
| 79 | const notFound = yield* intel |
| 80 | .getIndicatorFeed({ accountId, feedId: 99999999 }) |
| 81 | .pipe(Effect.flip); |
| 82 | expect(notFound._tag).toEqual("IndicatorFeedNotFound"); |
| 83 | |
| 84 | yield* stack.destroy(); |
| 85 | }).pipe(logLevel), |
| 86 | { |
| 87 | tags: ["provider:cloudflare", "provider:cloudflare:intel", "live"], |
| 88 | timeout: 120_000, |
| 89 | }, |
| 90 | ); |
| 91 | |
| 92 | // Canonical `list()` test (account collection). Enumeration is NOT gated by |
| 93 | // the feed-provider entitlement (only creation is), so this runs on the |
| 94 | // standard testing account and returns a well-typed (typically empty) array. |
| 95 | // On an unentitled account the account simply owns no feeds. |
| 96 | test.provider( |
| 97 | "list returns a well-typed array of indicator feeds", |
| 98 | (stack) => |
| 99 | Effect.gen(function* () { |
| 100 | yield* stack.destroy(); |
| 101 | |
| 102 | const provider = yield* Provider.findProvider(Cloudflare.Intel.IndicatorFeed); |
| 103 | const all = yield* provider.list(); |
| 104 | |
| 105 | expect(Array.isArray(all)).toBe(true); |
| 106 | // Every element is the exact `read` Attributes shape. |
| 107 | for (const feed of all) { |
| 108 | expect(typeof feed.feedId).toEqual("number"); |
| 109 | expect(feed.accountId).toBeTruthy(); |
| 110 | } |
| 111 | |
| 112 | yield* stack.destroy(); |
| 113 | }).pipe(logLevel), |
| 114 | { |
| 115 | tags: ["provider:cloudflare", "provider:cloudflare:intel", "live"], |
| 116 | timeout: 120_000, |
| 117 | }, |
| 118 | ); |
| 119 | |
| 120 | // Requires a Cloudforce One feed-provider account to create the feed under |
| 121 | // test; unlock with CLOUDFLARE_TEST_INTEL_FEEDS=1. Asserts the deployed feed |
| 122 | // is present in the exhaustively-paginated list(). |
| 123 | test.provider.skipIf(!entitled)( |
| 124 | "list enumerates the deployed indicator feed", |
| 125 | (stack) => |
| 126 | Effect.gen(function* () { |
| 127 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 128 | |
| 129 | yield* stack.destroy(); |
| 130 | |
| 131 | const feed = yield* stack.deploy( |
| 132 | Cloudflare.Intel.IndicatorFeed("ListFeed", { |
| 133 | name: FEED_NAME, |
| 134 | description: "alchemy intel list feed", |
| 135 | }), |
| 136 | ); |
| 137 | |
| 138 | const provider = yield* Provider.findProvider(Cloudflare.Intel.IndicatorFeed); |
| 139 | const all = yield* provider.list(); |
| 140 | |
| 141 | const found = all.find((f) => f.feedId === feed.feedId); |
| 142 | expect(found).toBeDefined(); |
| 143 | expect(found?.accountId).toEqual(accountId); |
| 144 | expect(found?.name).toEqual(FEED_NAME); |
| 145 | |
| 146 | yield* stack.destroy(); |
| 147 | }).pipe(logLevel), |
| 148 | { |
| 149 | tags: ["provider:cloudflare", "provider:cloudflare:intel", "live"], |
| 150 | timeout: 120_000, |
| 151 | }, |
| 152 | ); |
| 153 | |
| 154 | // Requires a Cloudforce One feed-provider account — unentitled accounts fail with |
| 155 | // the typed IndicatorFeedsNotEntitled (HTTP 403). Unlock with CLOUDFLARE_TEST_INTEL_FEEDS=1. |
| 156 | test.provider.skipIf(!entitled)( |
| 157 | "create (or adopt), verify out-of-band, update in place, destroy", |
| 158 | (stack) => |
| 159 | Effect.gen(function* () { |
| 160 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 161 | |
| 162 | yield* stack.destroy(); |
| 163 | |
| 164 | const feed = yield* stack.deploy( |
| 165 | Cloudflare.Intel.IndicatorFeed("TestFeed", { |
| 166 | name: FEED_NAME, |
| 167 | description: "alchemy intel lifecycle feed", |
| 168 | }), |
| 169 | ); |
| 170 | |
| 171 | expect(feed.feedId).toBeGreaterThan(0); |
| 172 | expect(feed.accountId).toEqual(accountId); |
| 173 | expect(feed.name).toEqual(FEED_NAME); |
| 174 | // Documented defaults. |
| 175 | expect(feed.isAttributable).toEqual(false); |
| 176 | expect(feed.isDownloadable).toEqual(false); |
| 177 | expect(feed.isPublic).toEqual(false); |
| 178 | |
| 179 | const live = yield* getFeed(accountId, feed.feedId); |
| 180 | expect(live.name).toEqual(FEED_NAME); |
| 181 | |
| 182 | // In-place update: description + visibility flags, same id. |
| 183 | const updated = yield* stack.deploy( |
| 184 | Cloudflare.Intel.IndicatorFeed("TestFeed", { |
| 185 | name: FEED_NAME, |
| 186 | description: "alchemy intel lifecycle feed v2", |
| 187 | isAttributable: true, |
| 188 | isDownloadable: true, |
| 189 | }), |
| 190 | ); |
| 191 | expect(updated.feedId).toEqual(feed.feedId); |
| 192 | expect(updated.description).toEqual("alchemy intel lifecycle feed v2"); |
| 193 | expect(updated.isAttributable).toEqual(true); |
| 194 | expect(updated.isDownloadable).toEqual(true); |
| 195 | |
| 196 | const liveUpdated = yield* getFeed(accountId, feed.feedId); |
| 197 | expect(liveUpdated.description).toEqual("alchemy intel lifecycle feed v2"); |
| 198 | expect(liveUpdated.isAttributable).toEqual(true); |
| 199 | |
| 200 | // No-op redeploy keeps the same feed. |
| 201 | const noop = yield* stack.deploy( |
| 202 | Cloudflare.Intel.IndicatorFeed("TestFeed", { |
| 203 | name: FEED_NAME, |
| 204 | description: "alchemy intel lifecycle feed v2", |
| 205 | isAttributable: true, |
| 206 | isDownloadable: true, |
| 207 | }), |
| 208 | ); |
| 209 | expect(noop.feedId).toEqual(feed.feedId); |
| 210 | |
| 211 | // Destroy is a documented no-op (no delete API) — the feed must |
| 212 | // still exist afterwards, and a fresh deploy adopts it back under |
| 213 | // the same id instead of creating a duplicate. |
| 214 | yield* stack.destroy(); |
| 215 | const orphan = yield* getFeed(accountId, feed.feedId); |
| 216 | expect(orphan.id).toEqual(feed.feedId); |
| 217 | |
| 218 | const adopted = yield* stack.deploy( |
| 219 | Cloudflare.Intel.IndicatorFeed("TestFeed", { |
| 220 | name: FEED_NAME, |
| 221 | description: "alchemy intel lifecycle feed v2", |
| 222 | isAttributable: true, |
| 223 | isDownloadable: true, |
| 224 | }), |
| 225 | ); |
| 226 | expect(adopted.feedId).toEqual(feed.feedId); |
| 227 | |
| 228 | yield* stack.destroy(); |
| 229 | }).pipe(logLevel), |
| 230 | { |
| 231 | tags: ["provider:cloudflare", "provider:cloudflare:intel", "live"], |
| 232 | timeout: 120_000, |
| 233 | }, |
| 234 | ); |
| 235 | |
| 236 | // Requires a Cloudflare feed-provider account (typed IndicatorFeedsNotEntitled otherwise); |
| 237 | // unlock with CLOUDFLARE_TEST_INTEL_FEEDS=1 (+ CLOUDFLARE_TEST_INTEL_CONSUMER_ACCOUNT for the grant step). |
| 238 | test.provider.skipIf(!entitled)( |
| 239 | "uploads a STIX2 snapshot and grants/revokes a consumer permission", |
| 240 | (stack) => |
| 241 | Effect.gen(function* () { |
| 242 | const consumer = process.env.CLOUDFLARE_TEST_INTEL_CONSUMER_ACCOUNT; |
| 243 | |
| 244 | yield* stack.destroy(); |
| 245 | |
| 246 | const snapshot = JSON.stringify({ |
| 247 | type: "bundle", |
| 248 | id: "bundle--3fbf63f2-7e2d-4d5a-9b4b-7f4a5d2f9c01", |
| 249 | objects: [ |
| 250 | { |
| 251 | type: "indicator", |
| 252 | spec_version: "2.1", |
| 253 | id: "indicator--a932fcc6-e032-476c-826f-cb970a5a1ade", |
| 254 | created: "2024-01-01T00:00:00.000Z", |
| 255 | modified: "2024-01-01T00:00:00.000Z", |
| 256 | pattern: "[domain-name:value = 'malicious.example.com']", |
| 257 | pattern_type: "stix", |
| 258 | valid_from: "2024-01-01T00:00:00Z", |
| 259 | }, |
| 260 | ], |
| 261 | }); |
| 262 | |
| 263 | const feed = yield* stack.deploy( |
| 264 | Cloudflare.Intel.IndicatorFeed("SnapshotFeed", { |
| 265 | name: FEED_NAME, |
| 266 | description: "alchemy intel snapshot feed", |
| 267 | snapshot, |
| 268 | }), |
| 269 | ); |
| 270 | expect(feed.snapshotHash).toBeTruthy(); |
| 271 | |
| 272 | // Re-deploying the identical snapshot is a no-op (same hash). |
| 273 | const noop = yield* stack.deploy( |
| 274 | Cloudflare.Intel.IndicatorFeed("SnapshotFeed", { |
| 275 | name: FEED_NAME, |
| 276 | description: "alchemy intel snapshot feed", |
| 277 | snapshot, |
| 278 | }), |
| 279 | ); |
| 280 | expect(noop.feedId).toEqual(feed.feedId); |
| 281 | expect(noop.snapshotHash).toEqual(feed.snapshotHash); |
| 282 | |
| 283 | if (consumer) { |
| 284 | const grant = yield* stack.deploy( |
| 285 | Cloudflare.Intel.IndicatorFeedPermission("ConsumerGrant", { |
| 286 | feedId: feed.feedId, |
| 287 | accountTag: consumer, |
| 288 | }), |
| 289 | ); |
| 290 | expect(grant.feedId).toEqual(feed.feedId); |
| 291 | expect(grant.accountTag).toEqual(consumer); |
| 292 | } |
| 293 | |
| 294 | yield* stack.destroy(); |
| 295 | }).pipe(logLevel), |
| 296 | { |
| 297 | tags: ["provider:cloudflare", "provider:cloudflare:intel", "live"], |
| 298 | timeout: 120_000, |
| 299 | }, |
| 300 | ); |