@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 156 lines 5.77 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 Effect from "effect/Effect";
7 import * as Redacted from "effect/Redacted";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import { apiToken, r2Credentials } 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 getSink = (accountId: string, sinkId: string) => pipelines.getSink({ accountId, sinkId });
17
18 const expectSinkGone = (accountId: string, sinkId: string) =>
19 getSink(accountId, sinkId).pipe(
20 Effect.flatMap(() => Effect.fail({ _tag: "SinkNotDeleted" } as const)),
21 Effect.catchTag("SinkNotFound", () => Effect.void),
22 Effect.retry({
23 while: (e) => e._tag === "SinkNotDeleted",
24 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
25 }),
26 );
27
28 const tags = [
29 "provider:cloudflare",
30 "provider:cloudflare:pipelines",
31 "provider:cloudflare:r2",
32 "live",
33 ];
34
35 const rotated = {
36 accessKeyId: Redacted.make("rotated-access-key-id"),
37 secretAccessKey: Redacted.make("rotated-secret-access-key"),
38 };
39
40 test.provider(
41 "r2 sink: credential rotation and equivalent defaults never replace",
42 (stack) =>
43 Effect.gen(function* () {
44 const { accountId } = yield* yield* CloudflareEnvironment;
45 const credentials = yield* r2Credentials;
46 yield* stack.destroy();
47
48 const sink = (opts: {
49 credentials: typeof credentials;
50 explicitDefaults?: boolean;
51 path?: string;
52 }) =>
53 Effect.gen(function* () {
54 const bucket = yield* Cloudflare.R2.Bucket("SinkBucket", { forceDestroy: true });
55 return yield* Cloudflare.Basin.Sink("Sink", {
56 type: "r2",
57 format: opts.explicitDefaults ? { type: "json" } : undefined,
58 config: {
59 bucket: bucket.bucketName,
60 credentials: opts.credentials,
61 path: opts.path,
62 rollingPolicy: opts.explicitDefaults ? { intervalSeconds: 300 } : undefined,
63 },
64 });
65 });
66
67 const initial = yield* stack.deploy(sink({ credentials }));
68
69 // Rotated credentials are write-only and cannot be updated: no replace.
70 const rotatePlan = yield* stack.plan(sink({ credentials: rotated }));
71 expect(rotatePlan.resources.Sink!.action).toEqual("noop");
72
73 // Spelling out the defaults is equivalent.
74 const defaultsPlan = yield* stack.plan(sink({ credentials, explicitDefaults: true }));
75 expect(defaultsPlan.resources.Sink!.action).toEqual("noop");
76
77 // A destination change still replaces.
78 const pathPlan = yield* stack.plan(sink({ credentials, path: "moved" }));
79 expect(pathPlan.resources.Sink!.action).toEqual("replace");
80
81 const after = yield* stack.deploy(sink({ credentials: rotated }));
82 expect(after.sinkId).toEqual(initial.sinkId);
83
84 yield* stack.destroy();
85 yield* expectSinkGone(accountId, initial.sinkId);
86 }).pipe(logLevel),
87 { tags, timeout: 240_000 },
88 );
89
90 test.provider(
91 "catalog sink: config form ⇄ catalog form and r2_data_catalog ⇄ basin_catalog are no-ops",
92 (stack) =>
93 Effect.gen(function* () {
94 const { accountId } = yield* yield* CloudflareEnvironment;
95 const token = yield* apiToken;
96 yield* stack.destroy();
97
98 const lakehouse = Effect.gen(function* () {
99 const bucket = yield* Cloudflare.R2.Bucket("LakeBucket", { forceDestroy: true });
100 const catalog = yield* Cloudflare.Basin.Catalog("Catalog", {
101 bucket,
102 });
103 return { bucket, catalog };
104 });
105
106 // The catalog's attributes are all stable, so the old bucket-name
107 // spelling does not wait for it — enable it in a first deploy.
108 yield* stack.deploy(lakehouse);
109
110 const initial = yield* stack.deploy(
111 Effect.gen(function* () {
112 // The old spelling.
113 const { catalog } = yield* lakehouse;
114 return yield* Cloudflare.Pipelines.Sink("Table", {
115 type: "r2_data_catalog",
116 config: {
117 bucket: catalog.bucketName,
118 namespace: "web",
119 tableName: "page_views",
120 token,
121 },
122 });
123 }),
124 );
125 expect(initial.type).toEqual("r2_data_catalog");
126 expect(initial.tableName).toEqual("page_views");
127 const live = yield* getSink(accountId, initial.sinkId);
128 expect(live.config?.bucket).toEqual(initial.bucket);
129
130 const catalogForm = (sinkToken: Redacted.Redacted<string>) =>
131 Effect.gen(function* () {
132 const { catalog } = yield* lakehouse;
133 return yield* Cloudflare.Basin.Sink("Table", {
134 type: "basin_catalog",
135 catalog,
136 table: { namespace: "web", name: "page_views" },
137 token: sinkToken,
138 });
139 });
140
141 const plan = yield* stack.plan(catalogForm(token));
142 expect(plan.resources.Table!.action).toEqual("noop");
143
144 // A rotated token never replaces (the table already exists, so a
145 // catalog sink could not be recreated on it anyway).
146 const rotatePlan = yield* stack.plan(catalogForm(Redacted.make("rotated-token")));
147 expect(rotatePlan.resources.Table!.action).toEqual("noop");
148
149 const switched = yield* stack.deploy(catalogForm(token));
150 expect(switched.sinkId).toEqual(initial.sinkId);
151
152 yield* stack.destroy();
153 yield* expectSinkGone(accountId, initial.sinkId);
154 }).pipe(logLevel),
155 { tags, timeout: 240_000 },
156 );