| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import { CloudflareEnvironment } from "@/Cloudflare/CloudflareEnvironment"; |
| 3 | import * as Test from "@/Test/Alchemy"; |
| 4 | import * as aisearch from "@distilled.cloud/cloudflare/aisearch"; |
| 5 | import { expect } from "alchemy-test"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import AiSearchCrawlTargetWorker from "./fixtures/crawl-target-worker.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService( |
| 15 | MinimumLogLevel, |
| 16 | process.env.DEBUG ? "Debug" : "Info", |
| 17 | ); |
| 18 | |
| 19 | // Type-level coverage: the `AiSearch` construct result *is* an |
| 20 | // `AiSearchInstance`, so it can be passed anywhere one is expected. These |
| 21 | // assertions fail to compile the moment the construct stops being |
| 22 | // substitutable for an instance. The two binding styles are also exercised at |
| 23 | // runtime by the `bindings-stack` fixtures (Effect Worker `bind(...)` + |
| 24 | // async Worker `env`). |
| 25 | declare const _search: Cloudflare.AI.Search; |
| 26 | // 1. Assignable to an `AiSearchInstance` (and thus to |
| 27 | // `Cloudflare.AI.Search.Search(...)`, which is how an Effect Worker attaches |
| 28 | // the `ai_search` binding). These |
| 29 | // assertions live inside a never-invoked closure so the type checks compile |
| 30 | // without executing the `declare`d binding (which has no runtime value). |
| 31 | void (() => { |
| 32 | const _asInstance: Cloudflare.AI.Search = _search; |
| 33 | void _asInstance; |
| 34 | void Cloudflare.AI.QuerySearch(_search); |
| 35 | }); |
| 36 | // 2. As a Worker `env` binding, `InferEnv` resolves it to the same runtime |
| 37 | // handle (`AiSearchInstance`) it would for a plain `AiSearchInstance`. |
| 38 | type _EnvSearch = Cloudflare.InferEnv<{ |
| 39 | SEARCH: Cloudflare.AI.Search; |
| 40 | }>["SEARCH"]; |
| 41 | type _EnvInstance = Cloudflare.InferEnv<{ |
| 42 | SEARCH: Cloudflare.AI.Search; |
| 43 | }>["SEARCH"]; |
| 44 | const _envSame: _EnvSearch extends _EnvInstance ? true : never = true; |
| 45 | void _envSame; |
| 46 | |
| 47 | const getInstance = (accountId: string, id: string, namespace = "default") => |
| 48 | aisearch.readNamespaceInstance({ accountId, name: namespace, id }).pipe( |
| 49 | Effect.retry({ |
| 50 | while: (e) => e._tag === "Forbidden", |
| 51 | schedule: Schedule.exponential("500 millis"), |
| 52 | times: 8, |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | const expectGone = (accountId: string, id: string, namespace = "default") => |
| 57 | getInstance(accountId, id, namespace).pipe( |
| 58 | Effect.flatMap(() => Effect.fail({ _tag: "InstanceNotDeleted" } as const)), |
| 59 | Effect.catchTag( |
| 60 | ["AiSearchInstanceNotFound", "NamespaceNotFound"], |
| 61 | () => Effect.void, |
| 62 | ), |
| 63 | Effect.retry({ |
| 64 | while: (e) => e._tag === "InstanceNotDeleted", |
| 65 | schedule: Schedule.max([ |
| 66 | Schedule.exponential("500 millis"), |
| 67 | Schedule.recurs(10), |
| 68 | ]), |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | // The `AiSearch` construct composes an R2 bucket, a managed service token |
| 73 | // (AccountApiToken + AiSearchToken children), and the instance — a single |
| 74 | // `yield*` wires the whole pipeline together. |
| 75 | const program = () => |
| 76 | Effect.gen(function* () { |
| 77 | const bucket = yield* Cloudflare.R2.Bucket("AiSearchSource", { |
| 78 | forceDestroy: true, |
| 79 | }); |
| 80 | const search = yield* Cloudflare.AI.Search("Search", { |
| 81 | source: bucket, |
| 82 | }); |
| 83 | return { bucket, search, serviceToken: search.serviceToken }; |
| 84 | }); |
| 85 | |
| 86 | test.provider( |
| 87 | "construct auto-creates a managed token and wires it into the instance", |
| 88 | (stack) => |
| 89 | Effect.gen(function* () { |
| 90 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 91 | |
| 92 | yield* stack.destroy(); |
| 93 | |
| 94 | const deployed = yield* stack.deploy(program()); |
| 95 | |
| 96 | const { search, serviceToken } = deployed; |
| 97 | expect(search.instanceId).toBeTruthy(); |
| 98 | // The managed service token was minted as a child and wired in. |
| 99 | expect(serviceToken).toBeDefined(); |
| 100 | expect(search.tokenId).toEqual(serviceToken!.id); |
| 101 | |
| 102 | // Cloudflare's read projection hides the token association |
| 103 | // (`tokenId` comes back `null`), so verify the instance exists rather |
| 104 | // than re-asserting the token id off the read path. |
| 105 | const live = yield* getInstance(accountId, search.instanceId); |
| 106 | expect(live.id).toEqual(search.instanceId); |
| 107 | |
| 108 | yield* stack.destroy(); |
| 109 | |
| 110 | yield* expectGone(accountId, search.instanceId); |
| 111 | }).pipe(logLevel), |
| 112 | { |
| 113 | tags: [ |
| 114 | "provider:cloudflare", |
| 115 | "provider:cloudflare:ai", |
| 116 | "provider:cloudflare:r2", |
| 117 | "live", |
| 118 | ], |
| 119 | timeout: 300_000, |
| 120 | }, |
| 121 | ); |
| 122 | |
| 123 | // A web-crawler source crawls a seed URL and needs no service token, so the |
| 124 | // construct must NOT mint an AccountApiToken / AiSearchToken — `token` comes |
| 125 | // back undefined. Cloudflare only crawls a domain the account owns, so the |
| 126 | // crawl is seeded at a Worker we deploy (its `workers.dev` URL is owned by |
| 127 | // the account); the fixture serves a sitemap so `parseType: "discover"` |
| 128 | // always finds content. |
| 129 | const crawlerProgram = () => |
| 130 | Effect.gen(function* () { |
| 131 | const target = yield* AiSearchCrawlTargetWorker; |
| 132 | // Exercise the flattened source group end-to-end: `parse` (parseType + |
| 133 | // parse options) must translate into the distilled |
| 134 | // `sourceParams.webCrawler.{parseType,parseOptions}`. |
| 135 | const search = yield* Cloudflare.AI.Search("Search", { |
| 136 | source: target.url.as<string>(), |
| 137 | // Cloudflare renamed the `crawl` parse type to `discover` and removed |
| 138 | // crawl options from the API. The fixture serves a sitemap, so |
| 139 | // discovery always finds content. |
| 140 | parse: { type: "discover", useBrowserRendering: false }, |
| 141 | }); |
| 142 | return { target, search, serviceToken: search.serviceToken }; |
| 143 | }); |
| 144 | |
| 145 | test.provider( |
| 146 | "web-crawler source skips token minting", |
| 147 | (stack) => |
| 148 | Effect.gen(function* () { |
| 149 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 150 | |
| 151 | yield* stack.destroy(); |
| 152 | |
| 153 | // Deploy the crawl target first and wait until its `workers.dev` URL is |
| 154 | // actually serving. AI Search fetches the seed synchronously when it |
| 155 | // validates a web-crawler at create time; against a URL that isn't live |
| 156 | // yet it finds no content and rejects with `missing_sitemap`. |
| 157 | const warmed = yield* stack.deploy( |
| 158 | Effect.gen(function* () { |
| 159 | return yield* AiSearchCrawlTargetWorker; |
| 160 | }), |
| 161 | ); |
| 162 | const client = yield* HttpClient.HttpClient; |
| 163 | const targetUrl = warmed.url; |
| 164 | expect(targetUrl).toBeTypeOf("string"); |
| 165 | yield* client.get(targetUrl as string).pipe( |
| 166 | Effect.flatMap((res) => |
| 167 | res.status === 200 |
| 168 | ? Effect.succeed(res) |
| 169 | : Effect.fail(new Error(`crawl target not ready: ${res.status}`)), |
| 170 | ), |
| 171 | Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 20 }), |
| 172 | ); |
| 173 | |
| 174 | const deployed = yield* stack.deploy(crawlerProgram()); |
| 175 | |
| 176 | const { search, serviceToken } = deployed; |
| 177 | // No service token is minted for a web crawler. |
| 178 | expect(serviceToken).toBeUndefined(); |
| 179 | expect(search.type).toEqual("web-crawler"); |
| 180 | |
| 181 | const live = yield* getInstance(accountId, search.instanceId); |
| 182 | expect(live.type).toEqual("web-crawler"); |
| 183 | |
| 184 | yield* stack.destroy(); |
| 185 | |
| 186 | yield* expectGone(accountId, search.instanceId); |
| 187 | }).pipe(logLevel), |
| 188 | { |
| 189 | tags: [ |
| 190 | "provider:cloudflare", |
| 191 | "provider:cloudflare:ai", |
| 192 | "provider:cloudflare:worker", |
| 193 | "live", |
| 194 | ], |
| 195 | timeout: 300_000, |
| 196 | }, |
| 197 | ); |