@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 231 lines 9.16 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 r2 from "@distilled.cloud/cloudflare/r2";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import * as Redacted from "effect/Redacted";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 // Enabling Sippy requires real external-cloud credentials: an AWS S3 (or
17 // GCS) source bucket with read credentials, plus an R2 API token with
18 // write access for the destination. The testing account ships neither, so
19 // the full lifecycle test below is gated behind env-supplied credentials.
20 // Without them, `putBucketSippy` fails with the typed
21 // `InvalidUpstreamCredentials` (code 10063) — asserted in the ungated test.
22 const sippyCreds =
23 !!process.env.TEST_SIPPY_AWS_BUCKET &&
24 !!process.env.TEST_SIPPY_AWS_REGION &&
25 !!process.env.TEST_SIPPY_AWS_ACCESS_KEY_ID &&
26 !!process.env.TEST_SIPPY_AWS_SECRET_ACCESS_KEY &&
27 !!process.env.TEST_SIPPY_R2_ACCESS_KEY_ID &&
28 !!process.env.TEST_SIPPY_R2_SECRET_ACCESS_KEY;
29
30 // The scoped API token the test harness mints propagates eventually-
31 // consistently across Cloudflare's edge — ride out 403 blips (`Forbidden`,
32 // declared in the distilled error union) on out-of-band verification calls.
33 const getSippy = (accountId: string, bucketName: string) =>
34 r2.getBucketSippy({ accountId, bucketName }).pipe(
35 Effect.retry({
36 while: (e) => e._tag === "Forbidden",
37 schedule: Schedule.exponential("500 millis"),
38 times: 8,
39 }),
40 );
41
42 const program = (opts: { sippy: boolean }) =>
43 Effect.gen(function* () {
44 const bucket = yield* Cloudflare.R2.Bucket("SippyBucket", {
45 forceDestroy: true,
46 });
47 const sippy = opts.sippy
48 ? yield* Cloudflare.R2.BucketSippy("Sippy", {
49 bucketName: bucket.bucketName,
50 source: {
51 provider: "aws",
52 bucket: process.env.TEST_SIPPY_AWS_BUCKET!,
53 region: process.env.TEST_SIPPY_AWS_REGION!,
54 accessKeyId: Redacted.make(process.env.TEST_SIPPY_AWS_ACCESS_KEY_ID!),
55 secretAccessKey: Redacted.make(process.env.TEST_SIPPY_AWS_SECRET_ACCESS_KEY!),
56 },
57 destination: {
58 accessKeyId: Redacted.make(process.env.TEST_SIPPY_R2_ACCESS_KEY_ID!),
59 secretAccessKey: Redacted.make(process.env.TEST_SIPPY_R2_SECRET_ACCESS_KEY!),
60 },
61 })
62 : undefined;
63 return { bucket, sippy };
64 });
65
66 // Ungated — exercises everything Sippy exposes without external creds:
67 // the disabled baseline read, idempotent disable, and the typed failure
68 // tags the provider's lifecycle operations rely on.
69 test.provider(
70 "reads the disabled baseline and surfaces typed errors without external creds",
71 (stack) =>
72 Effect.gen(function* () {
73 const { accountId } = yield* yield* CloudflareEnvironment;
74
75 yield* stack.destroy();
76
77 const { bucket } = yield* stack.deploy(program({ sippy: false }));
78
79 // A bucket with Sippy never configured reads back `enabled: false` —
80 // the provider's `read` maps this to "resource absent".
81 const baseline = yield* getSippy(accountId, bucket.bucketName);
82 expect(baseline.enabled).toEqual(false);
83
84 // Disabling Sippy when it was never enabled succeeds — the
85 // provider's `delete` is idempotent by construction.
86 const disabled = yield* r2.deleteBucketSippy({
87 accountId,
88 bucketName: bucket.bucketName,
89 });
90 expect(disabled.enabled).toEqual(false);
91
92 // Enabling with bogus upstream credentials fails with the typed
93 // `InvalidUpstreamCredentials` tag (code 10063) — the error a
94 // failed reconcile propagates.
95 const putError = yield* r2
96 .putBucketSippy({
97 accountId,
98 bucketName: bucket.bucketName,
99 source: {
100 provider: "aws",
101 bucket: "alchemy-nonexistent-source",
102 region: "us-east-1",
103 accessKeyId: "AKIAFAKEFAKEFAKEFAKE",
104 secretAccessKey: "fakefakefakefakefakefakefakefakefakefake",
105 },
106 destination: {
107 provider: "r2",
108 accessKeyId: "deadbeefdeadbeefdeadbeefdeadbeef",
109 secretAccessKey: "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
110 },
111 })
112 .pipe(Effect.flip);
113 expect(putError._tag).toEqual("InvalidUpstreamCredentials");
114
115 yield* stack.destroy();
116
117 // Once the bucket is gone, the Sippy endpoints report the typed
118 // `NoSuchBucket` (code 10006) — what `read` maps to undefined and
119 // `delete` swallows.
120 const goneError = yield* getSippy(accountId, bucket.bucketName).pipe(Effect.flip);
121 expect(goneError._tag).toEqual("NoSuchBucket");
122
123 // Destroy again — engine-level delete must be idempotent.
124 yield* stack.destroy();
125 }).pipe(logLevel),
126 {
127 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
128 timeout: 120_000,
129 },
130 );
131
132 // list() — parent fan-out singleton. Ungated path: without external creds
133 // no bucket has Sippy enabled, so the enumeration is a well-typed array that
134 // must NOT contain the freshly-deployed (Sippy-disabled) bucket. This still
135 // exercises the bucket fan-out + per-item typed skip end-to-end.
136 test.provider(
137 "list enumerates buckets that have Sippy enabled",
138 (stack) =>
139 Effect.gen(function* () {
140 yield* stack.destroy();
141
142 const { bucket } = yield* stack.deploy(program({ sippy: false }));
143
144 const provider = yield* Provider.findProvider(Cloudflare.R2.BucketSippy);
145 const all = yield* provider.list();
146
147 expect(Array.isArray(all)).toBe(true);
148 // Sippy was never enabled on this bucket, so it must be absent and
149 // every returned item must report `enabled: true` (matching `read`).
150 expect(all.some((s) => s.bucketName === bucket.bucketName)).toBe(false);
151 expect(all.every((s) => s.enabled === true)).toBe(true);
152
153 yield* stack.destroy();
154 }).pipe(logLevel),
155 {
156 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
157 timeout: 120_000,
158 },
159 );
160
161 // Gated list() — with real creds the enabled bucket must appear in the
162 // account-wide enumeration.
163 test.provider.skipIf(!sippyCreds)(
164 "list includes a bucket with Sippy enabled",
165 (stack) =>
166 Effect.gen(function* () {
167 yield* stack.destroy();
168
169 const created = yield* stack.deploy(program({ sippy: true }));
170
171 const provider = yield* Provider.findProvider(Cloudflare.R2.BucketSippy);
172 const all = yield* provider.list();
173
174 expect(all.some((s) => s.bucketName === created.bucket.bucketName && s.enabled)).toBe(true);
175
176 yield* stack.destroy();
177 }).pipe(logLevel),
178 {
179 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
180 timeout: 300_000,
181 },
182 );
183
184 // Full lifecycle — requires env-supplied AWS source + R2 destination
185 // credentials (see `sippyCreds` above for the exact variables).
186 test.provider.skipIf(!sippyCreds)(
187 "enable, no-op redeploy, disable on destroy",
188 (stack) =>
189 Effect.gen(function* () {
190 const { accountId } = yield* yield* CloudflareEnvironment;
191
192 yield* stack.destroy();
193
194 const created = yield* stack.deploy(program({ sippy: true }));
195 expect(created.sippy).toBeDefined();
196 expect(created.sippy!.enabled).toEqual(true);
197 expect(created.sippy!.accountId).toEqual(accountId);
198 expect(created.sippy!.bucketName).toEqual(created.bucket.bucketName);
199 expect(created.sippy!.jurisdiction).toEqual("default");
200 expect(created.sippy!.source.provider).toEqual("aws");
201 expect(created.sippy!.source.bucket).toEqual(process.env.TEST_SIPPY_AWS_BUCKET);
202 expect(created.sippy!.destination.provider).toEqual("r2");
203
204 // Out-of-band — Sippy is live on the bucket.
205 const live = yield* getSippy(accountId, created.bucket.bucketName);
206 expect(live.enabled).toEqual(true);
207 expect(live.source?.bucket).toEqual(process.env.TEST_SIPPY_AWS_BUCKET);
208
209 // Redeploying identical props re-PUTs the same configuration — a
210 // converging no-op (the PUT is a full upsert).
211 const noop = yield* stack.deploy(program({ sippy: true }));
212 expect(noop.sippy!.enabled).toEqual(true);
213 expect(noop.sippy!.bucketName).toEqual(created.bucket.bucketName);
214
215 // Removing the resource disables Sippy but keeps the bucket (and
216 // any already-migrated objects).
217 const removed = yield* stack.deploy(program({ sippy: false }));
218 expect(removed.bucket.bucketName).toEqual(created.bucket.bucketName);
219 const disabled = yield* getSippy(accountId, removed.bucket.bucketName);
220 expect(disabled.enabled).toEqual(false);
221
222 yield* stack.destroy();
223
224 const goneError = yield* getSippy(accountId, created.bucket.bucketName).pipe(Effect.flip);
225 expect(goneError._tag).toEqual("NoSuchBucket");
226 }).pipe(logLevel),
227 {
228 tags: ["provider:cloudflare", "provider:cloudflare:r2", "live"],
229 timeout: 300_000,
230 },
231 );