@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 174 lines 7.61 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 d1 from "@distilled.cloud/cloudflare/d1";
6 import * as kv from "@distilled.cloud/cloudflare/kv";
7 import * as r2 from "@distilled.cloud/cloudflare/r2";
8 import * as Alchemy from "alchemy";
9 import { Stack } from "alchemy/Stack";
10 import { State, type ResourceState } from "alchemy/State";
11 import * as Effect from "effect/Effect";
12 import { MinimumLogLevel } from "effect/References";
13 import { inDev } from "../test.resources.ts";
14
15 const { test } = Test.make({ providers: Cloudflare.providers() });
16
17 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
18
19 /**
20 * The full provider-mode cycle against the real cloud. Mode transitions are
21 * engine-orchestrated REPLACEMENTS (the persisted `providerMode` stamp vs
22 * the resolved mode; the provider diff is not consulted) — the providers
23 * themselves carry no promotion logic:
24 *
25 * 1. dev (all local) — `dev:` rows, zero cloud calls
26 * 2. dev + remote() on KV — hybrid: the namespace switches local → live
27 * WITHIN dev (replacement, real cloud id);
28 * bucket/DB stay local
29 * 3. deploy (all live) — bucket/DB promote (replacements, new real
30 * ids); the already-live namespace keeps its
31 * identity (no churn)
32 * 4. dev, remote() removed — demotion: all three switch live → local;
33 * the real cloud resources are deleted by the
34 * live provider (the stamped mode) and fresh
35 * `dev:` rows replace them
36 * 5. destroy — local rows, nothing left upstream
37 */
38 test.provider(
39 "KV/R2/D1 cycle local → hybrid → live → local with engine-orchestrated replacements",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43 const { accountId } = yield* yield* CloudflareEnvironment;
44
45 const program = (liveKV: boolean) =>
46 Effect.gen(function* () {
47 const namespace = yield* Cloudflare.KV.Namespace("CycleKV").pipe(Alchemy.remote(liveKV));
48 const bucket = yield* Cloudflare.R2.Bucket("CycleBucket", {
49 forceDestroy: true,
50 });
51 const database = yield* Cloudflare.D1.Database("CycleDB");
52 return { namespace, bucket, database };
53 });
54
55 const namespaceExists = (namespaceId: string) =>
56 kv.getNamespace({ accountId, namespaceId }).pipe(
57 Effect.as(true),
58 Effect.catchTag("NamespaceNotFound", () => Effect.succeed(false)),
59 );
60 const bucketExists = (bucketName: string) =>
61 r2.getBucket({ accountId, bucketName }).pipe(
62 Effect.as(true),
63 Effect.catchTag("NoSuchBucket", () => Effect.succeed(false)),
64 );
65 const databaseExists = (databaseId: string) =>
66 d1.getDatabase({ accountId, databaseId }).pipe(
67 Effect.as(true),
68 Effect.catchTag("DatabaseNotFound", () => Effect.succeed(false)),
69 );
70
71 // 1. dev — everything is a local `dev:` row.
72 const dev1 = yield* inDev(stack.deploy(program(false)));
73 expect(dev1.namespace.namespaceId).toMatch(/^dev:/);
74 expect(dev1.bucket.bucketName).toMatch(/^dev:/);
75 expect(dev1.database.databaseId).toMatch(/^dev:/);
76
77 // 2. dev + remote() on the namespace — hybrid. The engine replaces the
78 // local KV row with a real cloud namespace; the others stay local
79 // (identities unchanged — no churn from an unrelated sibling switch).
80 const dev2 = yield* inDev(stack.deploy(program(true)));
81 expect(dev2.namespace.namespaceId).not.toMatch(/^dev:/);
82 expect(yield* namespaceExists(dev2.namespace.namespaceId)).toBe(true);
83 expect(dev2.bucket.bucketName).toBe(dev1.bucket.bucketName);
84 expect(dev2.database.databaseId).toBe(dev1.database.databaseId);
85
86 // 3. plain deploy — bucket/DB promote to real resources via
87 // replacement (new identities); the namespace was ALREADY live, so
88 // its identity is stable across the promotion (same-mode noop).
89 const live = yield* stack.deploy(program(true));
90 expect(live.namespace.namespaceId).toBe(dev2.namespace.namespaceId);
91 expect(live.bucket.bucketName).not.toMatch(/^dev:/);
92 expect(live.database.databaseId).not.toMatch(/^dev:/);
93 expect(yield* bucketExists(live.bucket.bucketName)).toBe(true);
94 expect(yield* databaseExists(live.database.databaseId)).toBe(true);
95
96 // 4. back to dev with remote() removed — demotion. Every resource
97 // switches live → local: fresh `dev:` rows, and the live provider
98 // (the stamped mode that created them) deletes the cloud resources.
99 const dev3 = yield* inDev(stack.deploy(program(false)));
100 expect(dev3.namespace.namespaceId).toMatch(/^dev:/);
101 expect(dev3.bucket.bucketName).toMatch(/^dev:/);
102 expect(dev3.database.databaseId).toMatch(/^dev:/);
103 expect(yield* namespaceExists(live.namespace.namespaceId)).toBe(false);
104 expect(yield* bucketExists(live.bucket.bucketName)).toBe(false);
105 expect(yield* databaseExists(live.database.databaseId)).toBe(false);
106
107 // 5. destroy — local rows only; nothing upstream to clean.
108 yield* stack.destroy();
109 }).pipe(logLevel),
110 {
111 tags: [
112 "provider:cloudflare",
113 "provider:cloudflare:d1",
114 "provider:cloudflare:kv",
115 "provider:cloudflare:r2",
116 "live",
117 ],
118 timeout: 180_000,
119 },
120 );
121
122 /**
123 * Regression: state rows written by alchemy versions before providerMode
124 * stamping (≤ 2.0.0-beta.64) in dev mode have no `providerMode` stamp but
125 * carry `dev:`-marker identities. A plain destroy on a newer version must
126 * infer "local" from the marker and route the delete to the local provider
127 * variant instead of handing the `dev:` id to the live provider — the
128 * Cloudflare API rejects it with `BadRequest: There is a malformed
129 * parameter in the request`, permanently wedging the destroy.
130 */
131 test.provider(
132 "a legacy pre-stamping dev queue row (no providerMode, dev: id) destroys cleanly in live mode",
133 (stack) =>
134 Effect.gen(function* () {
135 yield* stack.destroy();
136 const state = yield* yield* State;
137 const stk = yield* Stack;
138 const { accountId } = yield* yield* CloudflareEnvironment;
139
140 // The exact shape `alchemy dev` on beta.64 persisted for a local
141 // queue: a fabricated `dev:<uuid>` id and no providerMode field.
142 const fqn = "LegacyDevQueue";
143 yield* state.set({
144 stack: stk.name,
145 stage: stk.stage,
146 fqn,
147 value: {
148 kind: "resource",
149 status: "created",
150 fqn,
151 logicalId: fqn,
152 instanceId: "beta64legacyrow0",
153 namespace: undefined,
154 resourceType: "Cloudflare.Queues.Queue",
155 providerVersion: 0,
156 props: {},
157 attr: {
158 queueId: "dev:00000000-0000-4000-8000-000000000000",
159 queueName: "legacy-dev-queue",
160 accountId,
161 },
162 bindings: [],
163 downstream: [],
164 } satisfies ResourceState,
165 });
166
167 // Live-mode destroy: the marker-inferred local variant deletes the
168 // row (a no-op against its empty in-memory registry) — no cloud call.
169 yield* stack.destroy();
170
171 expect(yield* state.get({ stack: stk.name, stage: stk.stage, fqn })).toBeUndefined();
172 }).pipe(logLevel),
173 { tags: ["provider:cloudflare", "live"], timeout: 120_000 },
174 );