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 pipelines from "@distilled.cloud/cloudflare/pipelines"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Exit from "effect/Exit"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import { PageView, pageViewFields } from "./fixtures/pipelines-shared.ts"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 13 | |
| 14 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 15 | |
| 16 | const getStream = (accountId: string, streamId: string) => |
| 17 | pipelines.getStream({ accountId, streamId }); |
| 18 | |
| 19 | const tags = ["provider:cloudflare", "provider:cloudflare:pipelines", "live"]; |
| 20 | |
| 21 | test.provider( |
| 22 | "http defaults to disabled and is applied in place to a public stream", |
| 23 | (stack) => |
| 24 | Effect.gen(function* () { |
| 25 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 26 | yield* stack.destroy(); |
| 27 | |
| 28 | // The pre-upgrade shape: a public, unauthenticated endpoint. |
| 29 | const open = yield* stack.deploy( |
| 30 | Cloudflare.Basin.Stream("Stream", { http: { enabled: true } }), |
| 31 | ); |
| 32 | expect(open.httpEnabled).toBe(true); |
| 33 | expect(open.httpAuthentication).toBe(false); |
| 34 | |
| 35 | // Dropping `http` closes the endpoint — an in-place update. |
| 36 | const plan = yield* stack.plan(Cloudflare.Basin.Stream("Stream", {})); |
| 37 | expect(plan.resources.Stream!.action).toEqual("update"); |
| 38 | |
| 39 | const closed = yield* stack.deploy(Cloudflare.Basin.Stream("Stream", {})); |
| 40 | expect(closed.streamId).toEqual(open.streamId); |
| 41 | expect(closed.httpEnabled).toBe(false); |
| 42 | const live = yield* getStream(accountId, closed.streamId); |
| 43 | expect(live.http.enabled).toBe(false); |
| 44 | |
| 45 | // Converged — re-planning the same props is a no-op. |
| 46 | const again = yield* stack.plan(Cloudflare.Basin.Stream("Stream", {})); |
| 47 | expect(again.resources.Stream!.action).toEqual("noop"); |
| 48 | |
| 49 | // `http: true` re-enables the endpoint with authentication. |
| 50 | const authed = yield* stack.deploy(Cloudflare.Basin.Stream("Stream", { http: true })); |
| 51 | expect(authed.streamId).toEqual(open.streamId); |
| 52 | const liveAuthed = yield* getStream(accountId, authed.streamId); |
| 53 | expect(liveAuthed.http.enabled).toBe(true); |
| 54 | expect(liveAuthed.http.authentication).toBe(true); |
| 55 | |
| 56 | yield* stack.destroy(); |
| 57 | }).pipe(logLevel), |
| 58 | { tags, timeout: 180_000 }, |
| 59 | ); |
| 60 | |
| 61 | test.provider( |
| 62 | "list/struct schema; field list ⇄ Effect Schema is a no-op", |
| 63 | (stack) => |
| 64 | Effect.gen(function* () { |
| 65 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 66 | yield* stack.destroy(); |
| 67 | |
| 68 | const initial = yield* stack.deploy( |
| 69 | Cloudflare.Basin.Stream("Typed", { schema: { fields: pageViewFields } }), |
| 70 | ); |
| 71 | const live = yield* getStream(accountId, initial.streamId); |
| 72 | expect(live.schema?.fields?.map((f) => [f.name, f.type])).toEqual([ |
| 73 | ["url", "string"], |
| 74 | ["at", "timestamp"], |
| 75 | ["tags", "list"], |
| 76 | ["user", "struct"], |
| 77 | ]); |
| 78 | |
| 79 | // The equivalent Effect Schema derives the same field list. |
| 80 | const plan = yield* stack.plan(Cloudflare.Basin.Stream("Typed", { schema: PageView })); |
| 81 | expect(plan.resources.Typed!.action).toEqual("noop"); |
| 82 | |
| 83 | const switched = yield* stack.deploy(Cloudflare.Basin.Stream("Typed", { schema: PageView })); |
| 84 | expect(switched.streamId).toEqual(initial.streamId); |
| 85 | |
| 86 | // A real schema change still replaces. |
| 87 | const changed = yield* stack.plan( |
| 88 | Cloudflare.Basin.Stream("Typed", { |
| 89 | schema: Schema.Struct({ url: Schema.String }), |
| 90 | }), |
| 91 | ); |
| 92 | expect(changed.resources.Typed!.action).toEqual("replace"); |
| 93 | |
| 94 | yield* stack.destroy(); |
| 95 | }).pipe(logLevel), |
| 96 | { tags, timeout: 180_000 }, |
| 97 | ); |
| 98 | |
| 99 | test.provider( |
| 100 | "an Effect Schema with no stream representation fails before planning", |
| 101 | (stack) => |
| 102 | Effect.gen(function* () { |
| 103 | const exit = yield* stack |
| 104 | .plan( |
| 105 | Cloudflare.Basin.Stream("Bad", { |
| 106 | schema: Schema.Struct({ value: Schema.Union([Schema.String, Schema.Number]) }), |
| 107 | }), |
| 108 | ) |
| 109 | .pipe(Effect.exit); |
| 110 | expect(Exit.isFailure(exit)).toBe(true); |
| 111 | expect(String(Exit.isFailure(exit) ? exit.cause : "")).toContain( |
| 112 | "field 'value' is a union of different types", |
| 113 | ); |
| 114 | }).pipe(logLevel), |
| 115 | { |
| 116 | tags: ["unit", "provider:cloudflare", "provider:cloudflare:pipelines", "local"], |
| 117 | timeout: 60_000, |
| 118 | }, |
| 119 | ); |