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 type { CloudflareResolvedCredentials } from "@alchemy.run/cloudflare/Auth/AuthConfig"; |
| 2 | import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment"; |
| 3 | import { type Database, DatabaseProvider } from "@alchemy.run/cloudflare/D1/Database"; |
| 4 | import { type Connection, ConnectionProvider } from "@alchemy.run/cloudflare/Hyperdrive/Connection"; |
| 5 | import { type Namespace, NamespaceProvider } from "@alchemy.run/cloudflare/KV/Namespace"; |
| 6 | import { LocalRuntimeState } from "@alchemy.run/cloudflare/LocalRuntime"; |
| 7 | import { type Queue, QueueProvider } from "@alchemy.run/cloudflare/Queues/Queue"; |
| 8 | import { type Bucket, BucketProvider } from "@alchemy.run/cloudflare/R2/Bucket"; |
| 9 | import { type Index, IndexProvider } from "@alchemy.run/cloudflare/Vectorize/VectorizeIndex"; |
| 10 | import { describe, expect, it } from "@alchemy.run/test"; |
| 11 | import { apiTokenCredentials, Credentials } from "@distilled.cloud/cloudflare/Credentials"; |
| 12 | import { NodeServices } from "@effect/platform-node"; |
| 13 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 14 | import { ArtifactStore, createArtifactStore } from "alchemy/Artifacts"; |
| 15 | import { Provider } from "alchemy/Provider"; |
| 16 | import { ResourceContext } from "alchemy/ResourceContext"; |
| 17 | import { Stack, type StackSpec } from "alchemy/Stack"; |
| 18 | import { Stage } from "alchemy/Stage"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 21 | import * as Layer from "effect/Layer"; |
| 22 | import * as MutableHashMap from "effect/MutableHashMap"; |
| 23 | import * as Redacted from "effect/Redacted"; |
| 24 | import { resourceContext } from "../Util/ResourceContext.ts"; |
| 25 | |
| 26 | // Regression tests for the "engine-owned names" invariant: a provider's |
| 27 | // `diff` must never order a replace (or rename) because the physical-name |
| 28 | // generator's output for a logical id has drifted from the deployed name. |
| 29 | // This is exactly what happens when the naming algorithm evolves (e.g. the |
| 30 | // truncation fix) — deployed resources keep their old-style names, while a |
| 31 | // fresh recompute yields a new-style name. Before the guard, that mismatch |
| 32 | // deleted the deployed resource (databases included) via replace. |
| 33 | // |
| 34 | // Live suites can never catch this class of bug: a fresh deploy generates |
| 35 | // names with the current algorithm, so `output` always matches the |
| 36 | // recompute. These tests simulate drift directly by handing the real |
| 37 | // provider `diff` a persisted output whose name no generator run today |
| 38 | // could produce. |
| 39 | |
| 40 | const TEST_ACCOUNT = "test-account-id"; |
| 41 | |
| 42 | // A name no current-generator run can produce for the ids used below. |
| 43 | const DRIFTED = "old-algorithm-name-a1b2c3"; |
| 44 | |
| 45 | const stack: Omit<StackSpec, "output"> = { |
| 46 | name: "my-stack", |
| 47 | stage: "dev", |
| 48 | resources: {}, |
| 49 | bindings: {}, |
| 50 | actions: {}, |
| 51 | }; |
| 52 | |
| 53 | const credentials: CloudflareResolvedCredentials = { |
| 54 | type: "apiToken", |
| 55 | apiToken: Redacted.make("test-token"), |
| 56 | accountId: TEST_ACCOUNT, |
| 57 | source: { type: "stored" }, |
| 58 | }; |
| 59 | |
| 60 | const env = Layer.mergeAll( |
| 61 | Layer.succeed(CloudflareEnvironment, Effect.succeed(credentials)), |
| 62 | Layer.succeed(Stack, stack), |
| 63 | Layer.succeed(Stage, stack.stage), |
| 64 | Layer.succeed(ResourceContext, resourceContext("0123456789abcdef0123456789abcdef")), |
| 65 | Layer.succeed(AlchemyContext, { dotAlchemy: "/tmp/.alchemy-test", dev: false, adopt: false }), |
| 66 | // The remaining layers only satisfy the provider layers' type-level |
| 67 | // requirements (reconcile/read need clients); diff never touches them. |
| 68 | Layer.succeed( |
| 69 | Credentials, |
| 70 | Effect.succeed(apiTokenCredentials({ apiToken: Redacted.make("test-token") })), |
| 71 | ), |
| 72 | Layer.sync(ArtifactStore, createArtifactStore), |
| 73 | Layer.succeed( |
| 74 | LocalRuntimeState, |
| 75 | LocalRuntimeState.of({ |
| 76 | queues: MutableHashMap.empty(), |
| 77 | queueConsumers: MutableHashMap.empty(), |
| 78 | workerRestarts: MutableHashMap.empty(), |
| 79 | }), |
| 80 | ), |
| 81 | NodeServices.layer, |
| 82 | FetchHttpClient.layer, |
| 83 | ); |
| 84 | |
| 85 | const diffInput = <Olds, News, Output>(id: string, olds: Olds, news: News, output: Output) => ({ |
| 86 | id, |
| 87 | fqn: id, |
| 88 | instanceId: "0123456789abcdef0123456789abcdef", |
| 89 | olds: olds as never, |
| 90 | news: news as never, |
| 91 | output: output as never, |
| 92 | oldBindings: [] as never, |
| 93 | newBindings: [] as never, |
| 94 | }); |
| 95 | |
| 96 | describe( |
| 97 | "engine-owned names: generator drift never replaces", |
| 98 | { tags: ["unit", "provider:cloudflare", "local"] }, |
| 99 | () => { |
| 100 | it.effect( |
| 101 | "D1 Database: drifted auto-generated name does not replace", |
| 102 | () => |
| 103 | Effect.gen(function* () { |
| 104 | const provider = yield* Provider<Database>("Cloudflare.D1Database"); |
| 105 | const result = yield* provider.diff!( |
| 106 | diffInput( |
| 107 | "Db", |
| 108 | {}, |
| 109 | {}, |
| 110 | { |
| 111 | databaseId: "11111111-2222-3333-4444-555555555555", |
| 112 | databaseName: DRIFTED, |
| 113 | accountId: TEST_ACCOUNT, |
| 114 | }, |
| 115 | ), |
| 116 | ); |
| 117 | expect(result?.action).not.toBe("replace"); |
| 118 | }).pipe(Effect.provide(DatabaseProvider()), Effect.provide(env)), |
| 119 | { tags: ["provider:cloudflare:d1"] }, |
| 120 | ); |
| 121 | |
| 122 | it.effect( |
| 123 | "D1 Database: explicit user rename still replaces", |
| 124 | () => |
| 125 | Effect.gen(function* () { |
| 126 | const provider = yield* Provider<Database>("Cloudflare.D1Database"); |
| 127 | const result = yield* provider.diff!( |
| 128 | diffInput( |
| 129 | "Db", |
| 130 | {}, |
| 131 | { name: "explicit-new-name" }, |
| 132 | { |
| 133 | databaseId: "11111111-2222-3333-4444-555555555555", |
| 134 | databaseName: DRIFTED, |
| 135 | accountId: TEST_ACCOUNT, |
| 136 | }, |
| 137 | ), |
| 138 | ); |
| 139 | expect(result?.action).toBe("replace"); |
| 140 | }).pipe(Effect.provide(DatabaseProvider()), Effect.provide(env)), |
| 141 | { tags: ["provider:cloudflare:d1"] }, |
| 142 | ); |
| 143 | |
| 144 | it.effect( |
| 145 | "D1 Database: explicit name equal to deployed name does not replace", |
| 146 | () => |
| 147 | Effect.gen(function* () { |
| 148 | const provider = yield* Provider<Database>("Cloudflare.D1Database"); |
| 149 | const result = yield* provider.diff!( |
| 150 | diffInput( |
| 151 | "Db", |
| 152 | { name: DRIFTED }, |
| 153 | { name: DRIFTED }, |
| 154 | { |
| 155 | databaseId: "11111111-2222-3333-4444-555555555555", |
| 156 | databaseName: DRIFTED, |
| 157 | accountId: TEST_ACCOUNT, |
| 158 | }, |
| 159 | ), |
| 160 | ); |
| 161 | expect(result?.action).not.toBe("replace"); |
| 162 | }).pipe(Effect.provide(DatabaseProvider()), Effect.provide(env)), |
| 163 | { tags: ["provider:cloudflare:d1"] }, |
| 164 | ); |
| 165 | |
| 166 | it.effect( |
| 167 | "R2 Bucket: drifted auto-generated name does not replace", |
| 168 | () => |
| 169 | Effect.gen(function* () { |
| 170 | const provider = yield* Provider<Bucket>("Cloudflare.R2.Bucket"); |
| 171 | const result = yield* provider.diff!( |
| 172 | diffInput("Files", {}, {}, { bucketName: DRIFTED, accountId: TEST_ACCOUNT }), |
| 173 | ); |
| 174 | expect(result?.action).not.toBe("replace"); |
| 175 | }).pipe(Effect.provide(BucketProvider()), Effect.provide(env)), |
| 176 | { tags: ["provider:cloudflare:r2"] }, |
| 177 | ); |
| 178 | |
| 179 | it.effect( |
| 180 | "R2 Bucket: explicit user rename still replaces", |
| 181 | () => |
| 182 | Effect.gen(function* () { |
| 183 | const provider = yield* Provider<Bucket>("Cloudflare.R2.Bucket"); |
| 184 | const result = yield* provider.diff!( |
| 185 | diffInput( |
| 186 | "Files", |
| 187 | {}, |
| 188 | { name: "explicit-new-bucket" }, |
| 189 | { bucketName: DRIFTED, accountId: TEST_ACCOUNT }, |
| 190 | ), |
| 191 | ); |
| 192 | expect(result?.action).toBe("replace"); |
| 193 | }).pipe(Effect.provide(BucketProvider()), Effect.provide(env)), |
| 194 | { tags: ["provider:cloudflare:r2"] }, |
| 195 | ); |
| 196 | |
| 197 | it.effect( |
| 198 | "Queue: drifted auto-generated name does not replace", |
| 199 | () => |
| 200 | Effect.gen(function* () { |
| 201 | const provider = yield* Provider<Queue>("Cloudflare.Queues.Queue"); |
| 202 | const result = yield* provider.diff!( |
| 203 | diffInput( |
| 204 | "Jobs", |
| 205 | {}, |
| 206 | {}, |
| 207 | { |
| 208 | queueId: "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4", |
| 209 | queueName: DRIFTED, |
| 210 | accountId: TEST_ACCOUNT, |
| 211 | }, |
| 212 | ), |
| 213 | ); |
| 214 | expect(result?.action).not.toBe("replace"); |
| 215 | }).pipe(Effect.provide(QueueProvider()), Effect.provide(env)), |
| 216 | { tags: ["provider:cloudflare:queue"] }, |
| 217 | ); |
| 218 | |
| 219 | it.effect( |
| 220 | "Vectorize Index: drifted auto-generated name does not replace", |
| 221 | () => |
| 222 | Effect.gen(function* () { |
| 223 | const provider = yield* Provider<Index>("Cloudflare.VectorizeIndex"); |
| 224 | const result = yield* provider.diff!( |
| 225 | diffInput("Vectors", {}, {}, { indexName: DRIFTED, accountId: TEST_ACCOUNT }), |
| 226 | ); |
| 227 | expect(result?.action).not.toBe("replace"); |
| 228 | }).pipe(Effect.provide(IndexProvider()), Effect.provide(env)), |
| 229 | { tags: ["provider:cloudflare:vectorize"] }, |
| 230 | ); |
| 231 | |
| 232 | it.effect( |
| 233 | "KV Namespace: drifted auto-generated title does not rename", |
| 234 | () => |
| 235 | Effect.gen(function* () { |
| 236 | const provider = yield* Provider<Namespace>("Cloudflare.KV.Namespace"); |
| 237 | const result = yield* provider.diff!( |
| 238 | diffInput( |
| 239 | "Cache", |
| 240 | {}, |
| 241 | {}, |
| 242 | { |
| 243 | namespaceId: "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4", |
| 244 | title: DRIFTED, |
| 245 | accountId: TEST_ACCOUNT, |
| 246 | }, |
| 247 | ), |
| 248 | ); |
| 249 | expect(result).toBeUndefined(); |
| 250 | }).pipe(Effect.provide(NamespaceProvider()), Effect.provide(env)), |
| 251 | { tags: ["provider:cloudflare:kv"] }, |
| 252 | ); |
| 253 | |
| 254 | it.effect( |
| 255 | "Hyperdrive Connection: drifted auto-generated name does not replace", |
| 256 | () => |
| 257 | Effect.gen(function* () { |
| 258 | const provider = yield* Provider<Connection>("Cloudflare.Hyperdrive"); |
| 259 | const result = yield* provider.diff!( |
| 260 | diffInput( |
| 261 | "Pg", |
| 262 | {}, |
| 263 | {}, |
| 264 | { |
| 265 | hyperdriveId: "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4", |
| 266 | name: DRIFTED, |
| 267 | accountId: TEST_ACCOUNT, |
| 268 | }, |
| 269 | ), |
| 270 | ); |
| 271 | expect(result?.action).not.toBe("replace"); |
| 272 | }).pipe(Effect.provide(ConnectionProvider()), Effect.provide(env)), |
| 273 | { tags: ["provider:cloudflare:hyperdrive"] }, |
| 274 | ); |
| 275 | }, |
| 276 | ); |