@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 92 lines 3.54 KB
Raw
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 Output from "alchemy/Output";
7 import * as Effect from "effect/Effect";
8 import * as Exit from "effect/Exit";
9 import { MinimumLogLevel } from "effect/References";
10 import { apiToken, PageView } 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 getPipeline = (accountId: string, pipelineId: string) =>
17 pipelines.getV1Pipeline({ accountId, pipelineId });
18
19 /**
20 * The whole Basin topology in one deploy: a typed stream, a catalog sink
21 * that takes the Catalog resource (so it is created after the catalog),
22 * and the SQL pipeline between them.
23 */
24 const lakehouse = (sql: (stream: string, sink: string) => string) =>
25 Effect.gen(function* () {
26 const token = yield* apiToken;
27 const bucket = yield* Cloudflare.R2.Bucket("LakeBucket", { forceDestroy: true });
28 const catalog = yield* Cloudflare.Basin.Catalog("Catalog", {
29 bucket,
30 });
31 const stream = yield* Cloudflare.Basin.Stream("PageViews", { schema: PageView });
32 const sink = yield* Cloudflare.Basin.Sink("PageViewTable", {
33 type: "basin_catalog",
34 catalog,
35 table: { namespace: "web", name: "page_views" },
36 token,
37 });
38 const pipeline = yield* Cloudflare.Basin.Pipeline("Etl", {
39 sql: Output.all(stream.name, sink.name).pipe(
40 Output.map(([streamName, sinkName]) => sql(streamName, sinkName)),
41 ),
42 });
43 return { stream, sink, pipeline };
44 });
45
46 const passthrough = (stream: string, sink: string) => `INSERT INTO ${sink} SELECT * FROM ${stream}`;
47
48 test.provider(
49 "SQL is validated during plan; whitespace-only changes are a no-op",
50 (stack) =>
51 Effect.gen(function* () {
52 const { accountId } = yield* yield* CloudflareEnvironment;
53 yield* stack.destroy();
54
55 const deployed = yield* stack.deploy(lakehouse(passthrough));
56 expect(deployed.sink.type).toEqual("basin_catalog");
57 expect(deployed.pipeline.pipelineId).toBeTruthy();
58
59 // Reformatted SQL is the same statement.
60 const reformatted = yield* stack.plan(
61 lakehouse((stream, sink) => ` INSERT INTO ${sink}\n SELECT *\n FROM ${stream}; `),
62 );
63 expect(reformatted.resources.Etl!.action).toEqual("noop");
64
65 // Invalid SQL fails the plan — before the running pipeline is deleted.
66 const invalid = yield* stack
67 .plan(lakehouse((stream, sink) => `INSERT INTO ${sink} SELEC * FROM ${stream}`))
68 .pipe(Effect.exit);
69 expect(Exit.isFailure(invalid)).toBe(true);
70 expect(String(Exit.isFailure(invalid) ? invalid.cause : "")).toContain("invalid SQL");
71
72 // A valid change is a replacement.
73 const filtered = yield* stack.plan(
74 lakehouse((stream, sink) => `INSERT INTO ${sink} SELECT * FROM ${stream} WHERE url != ''`),
75 );
76 expect(filtered.resources.Etl!.action).toEqual("replace");
77
78 const live = yield* getPipeline(accountId, deployed.pipeline.pipelineId);
79 expect(live.id).toEqual(deployed.pipeline.pipelineId);
80
81 yield* stack.destroy();
82 }).pipe(logLevel),
83 {
84 tags: [
85 "provider:cloudflare",
86 "provider:cloudflare:pipelines",
87 "provider:cloudflare:r2",
88 "live",
89 ],
90 timeout: 240_000,
91 },
92 );