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
| 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 { Action } from "alchemy/Action"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as HttpClient from "effect/http/HttpClient"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as EffectStream from "effect/Stream"; |
| 13 | import PipelinesHttpWorker from "./fixtures/pipelines-http-worker.ts"; |
| 14 | import { PageView, PageViews } from "./fixtures/pipelines-stream.ts"; |
| 15 | import PipelinesWorker from "./fixtures/pipelines-worker.ts"; |
| 16 | |
| 17 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 18 | |
| 19 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 20 | |
| 21 | class WorkerNotReady extends Data.TaggedError("WorkerNotReady")<{ |
| 22 | status: number; |
| 23 | body: string; |
| 24 | }> {} |
| 25 | |
| 26 | // A fresh workers.dev URL takes a few seconds to serve; retry until 200. |
| 27 | const getJson = (url: string) => |
| 28 | HttpClient.get(url).pipe( |
| 29 | Effect.flatMap((res) => |
| 30 | res.status === 200 |
| 31 | ? res.json |
| 32 | : res.text.pipe( |
| 33 | Effect.flatMap((body) => Effect.fail(new WorkerNotReady({ status: res.status, body }))), |
| 34 | ), |
| 35 | ), |
| 36 | Effect.retry({ |
| 37 | while: (e): e is WorkerNotReady => e instanceof WorkerNotReady, |
| 38 | schedule: Schedule.max([ |
| 39 | Schedule.min([Schedule.exponential("500 millis"), Schedule.spaced("5 seconds")]), |
| 40 | Schedule.recurs(20), |
| 41 | ]), |
| 42 | }), |
| 43 | ); |
| 44 | |
| 45 | // A just-created stream's ingest host can take a moment to accept writes. |
| 46 | const ingestRetry = { |
| 47 | while: (e: { _tag: string; reason?: string }) => |
| 48 | e._tag === "StreamSendError" && e.reason === "SendFailed", |
| 49 | schedule: Schedule.exponential("1 second"), |
| 50 | times: 6, |
| 51 | } as const; |
| 52 | |
| 53 | test.provider( |
| 54 | "typed WriteStream and StreamSink: native binding (Worker) and HTTP ingest (Action)", |
| 55 | (stack) => |
| 56 | Effect.gen(function* () { |
| 57 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 58 | yield* stack.destroy(); |
| 59 | |
| 60 | const deployed = yield* stack.deploy( |
| 61 | Effect.gen(function* () { |
| 62 | const stream = yield* PageViews; |
| 63 | const worker = yield* PipelinesWorker; |
| 64 | |
| 65 | // WriteStreamLocal / StreamSinkLocal POST to the stream's HTTP |
| 66 | // ingest endpoint (`http: true`) with the current credentials. |
| 67 | const Seed = Action( |
| 68 | "Seed", |
| 69 | Effect.gen(function* () { |
| 70 | const views = yield* Cloudflare.Basin.WriteStream(stream); |
| 71 | const sink = yield* Cloudflare.Basin.StreamSink(stream); |
| 72 | return Effect.fn(function* () { |
| 73 | yield* views |
| 74 | .send([new PageView({ url: "/seed", at: new Date(), tags: ["seed"] })]) |
| 75 | .pipe(Effect.retry(ingestRetry)); |
| 76 | yield* EffectStream.range(1, 50).pipe( |
| 77 | EffectStream.map( |
| 78 | (i) => new PageView({ url: `/seed/${i}`, at: new Date(), tags: [] }), |
| 79 | ), |
| 80 | EffectStream.run(sink), |
| 81 | Effect.retry(ingestRetry), |
| 82 | ); |
| 83 | const invalid = yield* views |
| 84 | .send([{ url: "/x", at: "nope", tags: [] } as unknown as PageView]) |
| 85 | .pipe( |
| 86 | Effect.as("none"), |
| 87 | Effect.catchTag("StreamSendError", (e) => |
| 88 | Effect.succeed(e.reason ?? "unknown"), |
| 89 | ), |
| 90 | ); |
| 91 | return { invalid }; |
| 92 | }); |
| 93 | }).pipe( |
| 94 | Effect.provide(Cloudflare.Basin.WriteStreamLocal), |
| 95 | Effect.provide(Cloudflare.Basin.StreamSinkLocal), |
| 96 | ), |
| 97 | ); |
| 98 | const seeded = yield* Seed({}); |
| 99 | |
| 100 | return { |
| 101 | streamId: stream.streamId, |
| 102 | url: worker.url.as<string>(), |
| 103 | seeded, |
| 104 | }; |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | expect(deployed.seeded).toEqual({ invalid: "InvalidRecord" }); |
| 109 | |
| 110 | const live = yield* pipelines.getStream({ accountId, streamId: deployed.streamId }); |
| 111 | expect(live.http.enabled).toBe(true); |
| 112 | expect(live.http.authentication).toBe(true); |
| 113 | |
| 114 | expect(yield* getJson(`${deployed.url}/send`)).toEqual({ sent: 2 }); |
| 115 | expect(yield* getJson(`${deployed.url}/sink?count=300`)).toEqual({ sent: 300 }); |
| 116 | expect(yield* getJson(`${deployed.url}/invalid`)).toEqual({ |
| 117 | reason: "InvalidRecord", |
| 118 | index: 0, |
| 119 | }); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | }).pipe(logLevel), |
| 123 | { |
| 124 | tags: [ |
| 125 | "provider:cloudflare", |
| 126 | "provider:cloudflare:pipelines", |
| 127 | "provider:cloudflare:worker", |
| 128 | "live", |
| 129 | ], |
| 130 | timeout: 240_000, |
| 131 | }, |
| 132 | ); |
| 133 | |
| 134 | // WriteStreamHttp/StreamSinkHttp mint a scoped `Pipelines Send` API token. |
| 135 | test.provider( |
| 136 | "WriteStreamHttp and StreamSinkHttp send through a scoped token", |
| 137 | (stack) => |
| 138 | Effect.gen(function* () { |
| 139 | yield* stack.destroy(); |
| 140 | const { url } = yield* stack.deploy( |
| 141 | Effect.gen(function* () { |
| 142 | yield* PageViews; |
| 143 | const worker = yield* PipelinesHttpWorker; |
| 144 | return { url: worker.url.as<string>() }; |
| 145 | }), |
| 146 | ); |
| 147 | expect(yield* getJson(`${url}/send`)).toEqual({ sent: 21 }); |
| 148 | yield* stack.destroy(); |
| 149 | }).pipe(logLevel), |
| 150 | { |
| 151 | tags: [ |
| 152 | "provider:cloudflare", |
| 153 | "provider:cloudflare:pipelines", |
| 154 | "provider:cloudflare:worker", |
| 155 | "live", |
| 156 | ], |
| 157 | timeout: 240_000, |
| 158 | }, |
| 159 | ); |