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, 04:45 UTC
| 1 | import * as Planetscale from "@alchemy.run/planetscale"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as ps from "@distilled.cloud/planetscale"; |
| 5 | import { adopt } from "alchemy/AdoptPolicy"; |
| 6 | import * as Provider from "alchemy/Provider"; |
| 7 | import * as RemovalPolicy from "alchemy/RemovalPolicy"; |
| 8 | import { Data, Schedule } from "effect"; |
| 9 | import * as Cause from "effect/Cause"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Exit from "effect/Exit"; |
| 12 | import { MinimumLogLevel } from "effect/References"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: Planetscale.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | const fixturesDir = `${import.meta.dirname}/fixtures`; |
| 19 | |
| 20 | describe |
| 21 | .skipIf(!process.env.PLANETSCALE_TEST) |
| 22 | .concurrent( |
| 23 | "PostgresDatabase", |
| 24 | { tags: ["provider:planetscale", "provider:planetscale:postgres", "live"] }, |
| 25 | () => { |
| 26 | // Read-only: exercises the real org-wide enumeration code path (with the |
| 27 | // per-database default-branch hydration) without provisioning anything. |
| 28 | test.provider("list enumerates databases (read-only)", () => |
| 29 | Effect.gen(function* () { |
| 30 | const provider = yield* Provider.findProvider(Planetscale.PostgresDatabase); |
| 31 | const all = yield* provider.list(); |
| 32 | |
| 33 | expect(Array.isArray(all)).toBe(true); |
| 34 | for (const db of all) { |
| 35 | expect(db).toMatchObject({ |
| 36 | id: expect.any(String), |
| 37 | name: expect.any(String), |
| 38 | organization: expect.any(String), |
| 39 | region: { slug: expect.any(String) }, |
| 40 | arch: expect.stringMatching(/^(x86|arm)$/), |
| 41 | }); |
| 42 | } |
| 43 | }).pipe(logLevel), |
| 44 | ); |
| 45 | |
| 46 | // Deploy-and-find coverage, opt-in only (slow provisioning). |
| 47 | test.provider.skipIf(!process.env.PLANETSCALE_DEPLOY_TEST)( |
| 48 | "list finds a freshly deployed database", |
| 49 | (stack) => |
| 50 | Effect.gen(function* () { |
| 51 | yield* stack.destroy(); |
| 52 | |
| 53 | const { database } = yield* stack.deploy( |
| 54 | Effect.gen(function* () { |
| 55 | const database = yield* Planetscale.PostgresDatabase("ListDatabase", { |
| 56 | name: "alchemy-pg-db-list", |
| 57 | clusterSize: "PS_10", |
| 58 | }); |
| 59 | return { database }; |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | const provider = yield* Provider.findProvider(Planetscale.PostgresDatabase); |
| 64 | const all = yield* provider.list(); |
| 65 | |
| 66 | expect( |
| 67 | all.some( |
| 68 | (db) => db.organization === database.organization && db.name === database.name, |
| 69 | ), |
| 70 | ).toBe(true); |
| 71 | |
| 72 | yield* stack.destroy(); |
| 73 | }).pipe(logLevel), |
| 74 | 5_000_000, |
| 75 | ); |
| 76 | |
| 77 | test.provider( |
| 78 | "create database with minimal settings", |
| 79 | (stack) => |
| 80 | Effect.gen(function* () { |
| 81 | yield* stack.destroy(); |
| 82 | |
| 83 | const { database } = yield* stack.deploy( |
| 84 | Effect.gen(function* () { |
| 85 | const database = yield* Planetscale.PostgresDatabase("PostgresDatabaseBasic", { |
| 86 | clusterSize: "PS_10", |
| 87 | }); |
| 88 | |
| 89 | return { database }; |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | expect(database).toMatchObject({ |
| 94 | id: expect.any(String), |
| 95 | name: expect.any(String), |
| 96 | organization: expect.any(String), |
| 97 | state: expect.any(String), |
| 98 | defaultBranch: "main", |
| 99 | plan: expect.any(String), |
| 100 | createdAt: expect.any(String), |
| 101 | updatedAt: expect.any(String), |
| 102 | htmlUrl: expect.any(String), |
| 103 | region: { slug: expect.any(String) }, |
| 104 | clusterSize: "PS_10_AWS_X86", |
| 105 | }); |
| 106 | |
| 107 | const branch = yield* Planetscale.waitForBranchReady( |
| 108 | database.organization, |
| 109 | database.name, |
| 110 | "main", |
| 111 | ); |
| 112 | |
| 113 | expect(branch.cluster_name).toEqual("PS_10_AWS_X86"); |
| 114 | |
| 115 | yield* stack.destroy(); |
| 116 | }).pipe(logLevel), |
| 117 | 5_000_000, |
| 118 | ); |
| 119 | |
| 120 | test.provider( |
| 121 | "create database with minimal settings and arm arch", |
| 122 | (stack) => |
| 123 | Effect.gen(function* () { |
| 124 | yield* stack.destroy(); |
| 125 | |
| 126 | const { database } = yield* stack.deploy( |
| 127 | Effect.gen(function* () { |
| 128 | const database = yield* Planetscale.PostgresDatabase("PostgresDatabaseBasicArm", { |
| 129 | clusterSize: "PS_10", |
| 130 | arch: "arm", |
| 131 | }); |
| 132 | |
| 133 | return { database }; |
| 134 | }), |
| 135 | ); |
| 136 | |
| 137 | expect(database).toMatchObject({ |
| 138 | id: expect.any(String), |
| 139 | name: expect.any(String), |
| 140 | organization: expect.any(String), |
| 141 | state: expect.any(String), |
| 142 | defaultBranch: "main", |
| 143 | plan: expect.any(String), |
| 144 | createdAt: expect.any(String), |
| 145 | updatedAt: expect.any(String), |
| 146 | htmlUrl: expect.any(String), |
| 147 | region: { slug: expect.any(String) }, |
| 148 | arch: "arm", |
| 149 | clusterSize: "PS_10_AWS_ARM", |
| 150 | }); |
| 151 | |
| 152 | const branch = yield* Planetscale.waitForBranchReady( |
| 153 | database.organization, |
| 154 | database.name, |
| 155 | "main", |
| 156 | ); |
| 157 | |
| 158 | expect(branch.cluster_name).toEqual("PS_10_AWS_ARM"); |
| 159 | |
| 160 | yield* stack.destroy(); |
| 161 | }).pipe(logLevel), |
| 162 | 5_000_000, |
| 163 | ); |
| 164 | |
| 165 | test.provider( |
| 166 | "create, update, and delete database", |
| 167 | (stack) => |
| 168 | Effect.gen(function* () { |
| 169 | const name = `alchemy-test-postgresql-crud`; |
| 170 | |
| 171 | yield* stack.destroy(); |
| 172 | |
| 173 | const { database } = yield* stack.deploy( |
| 174 | Effect.gen(function* () { |
| 175 | const database = yield* Planetscale.PostgresDatabase("PostgresDatabaseCRUD", { |
| 176 | name, |
| 177 | region: { slug: "us-east" }, |
| 178 | clusterSize: "PS_10", |
| 179 | defaultBranch: "main", |
| 180 | requireApprovalForDeploy: false, |
| 181 | restrictBranchRegion: true, |
| 182 | productionBranchWebConsole: true, |
| 183 | }); |
| 184 | |
| 185 | return { database }; |
| 186 | }), |
| 187 | ); |
| 188 | |
| 189 | expect(database).toMatchObject({ |
| 190 | id: expect.any(String), |
| 191 | name, |
| 192 | organization: expect.any(String), |
| 193 | state: expect.any(String), |
| 194 | plan: expect.any(String), |
| 195 | createdAt: expect.any(String), |
| 196 | updatedAt: expect.any(String), |
| 197 | htmlUrl: expect.any(String), |
| 198 | region: { slug: expect.any(String) }, |
| 199 | clusterSize: "PS_10_AWS_X86", |
| 200 | defaultBranch: "main", |
| 201 | requireApprovalForDeploy: false, |
| 202 | restrictBranchRegion: true, |
| 203 | productionBranchWebConsole: true, |
| 204 | }); |
| 205 | |
| 206 | const { updatedDatabase } = yield* stack.deploy( |
| 207 | Effect.gen(function* () { |
| 208 | const updatedDatabase = yield* Planetscale.PostgresDatabase( |
| 209 | "PostgresDatabaseCRUD", |
| 210 | { |
| 211 | name, |
| 212 | clusterSize: "PS_20", |
| 213 | requireApprovalForDeploy: true, |
| 214 | restrictBranchRegion: false, |
| 215 | productionBranchWebConsole: false, |
| 216 | defaultBranch: "main", |
| 217 | }, |
| 218 | ); |
| 219 | |
| 220 | return { updatedDatabase }; |
| 221 | }), |
| 222 | ); |
| 223 | |
| 224 | expect(updatedDatabase).toMatchObject({ |
| 225 | name, |
| 226 | requireApprovalForDeploy: true, |
| 227 | restrictBranchRegion: false, |
| 228 | productionBranchWebConsole: false, |
| 229 | defaultBranch: "main", |
| 230 | }); |
| 231 | |
| 232 | const branch = yield* Planetscale.waitForBranchReady( |
| 233 | database.organization, |
| 234 | database.name, |
| 235 | "main", |
| 236 | ); |
| 237 | |
| 238 | expect(branch.cluster_name).toEqual("PS_20_AWS_X86"); |
| 239 | |
| 240 | yield* stack.destroy(); |
| 241 | |
| 242 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 243 | }).pipe(logLevel), |
| 244 | 5_000_000, |
| 245 | ); |
| 246 | |
| 247 | test.provider( |
| 248 | "creates non-main default branch if specified", |
| 249 | (stack) => |
| 250 | Effect.gen(function* () { |
| 251 | const name = `alchemy-test-postgresql-custom-branch`; |
| 252 | const defaultBranch = "custom"; |
| 253 | |
| 254 | yield* stack.destroy(); |
| 255 | |
| 256 | const { database } = yield* stack.deploy( |
| 257 | Effect.gen(function* () { |
| 258 | const database = yield* Planetscale.PostgresDatabase( |
| 259 | "PostgresDatabaseCustomBranch", |
| 260 | { name, clusterSize: "PS_10", defaultBranch }, |
| 261 | ); |
| 262 | |
| 263 | return { database }; |
| 264 | }), |
| 265 | ); |
| 266 | |
| 267 | expect(database).toMatchObject({ name, defaultBranch }); |
| 268 | |
| 269 | const branch = yield* Planetscale.waitForBranchReady( |
| 270 | database.organization, |
| 271 | database.name, |
| 272 | defaultBranch, |
| 273 | ); |
| 274 | |
| 275 | expect(branch.name).toEqual(defaultBranch); |
| 276 | expect(branch.parent_branch).toEqual("main"); |
| 277 | expect(branch.cluster_name).toEqual("PS_10_AWS_X86"); |
| 278 | |
| 279 | yield* stack.destroy(); |
| 280 | |
| 281 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 282 | }).pipe(logLevel), |
| 283 | 5_000_000, // must wait on multiple resizes and branch creation |
| 284 | ); |
| 285 | |
| 286 | test.provider( |
| 287 | "applies migrations and import files", |
| 288 | (stack) => |
| 289 | Effect.gen(function* () { |
| 290 | yield* stack.destroy(); |
| 291 | |
| 292 | const importFile = `${fixturesDir}/seed.sql`; |
| 293 | const { database } = yield* stack.deploy( |
| 294 | Effect.gen(function* () { |
| 295 | const database = yield* Planetscale.PostgresDatabase("PostgresDatabaseMigrations", { |
| 296 | clusterSize: "PS_10", |
| 297 | migrations: `${fixturesDir}/migrations`, |
| 298 | importFiles: [importFile], |
| 299 | }); |
| 300 | |
| 301 | return { database }; |
| 302 | }), |
| 303 | ); |
| 304 | |
| 305 | expect(database.migrationsTable).toEqual("__alchemy_migrations"); |
| 306 | expect(database.migrationsHashes["0001_create_widgets.sql"]).toEqual( |
| 307 | expect.any(String), |
| 308 | ); |
| 309 | expect(database.importHashes[importFile]).toEqual(expect.any(String)); |
| 310 | |
| 311 | yield* stack.destroy(); |
| 312 | |
| 313 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 314 | }).pipe(logLevel), |
| 315 | 5_000_000, |
| 316 | ); |
| 317 | |
| 318 | test.provider( |
| 319 | "adopt with wrong kind should throw", |
| 320 | (stack) => |
| 321 | Effect.gen(function* () { |
| 322 | const name = `alchemy-test-postgresql-kind`; |
| 323 | |
| 324 | yield* stack.destroy(); |
| 325 | |
| 326 | const { database } = yield* stack.deploy( |
| 327 | Effect.gen(function* () { |
| 328 | const database = yield* Planetscale.MySQLDatabase("MySQLBaselineWrongKind", { |
| 329 | name, |
| 330 | region: { slug: "us-east" }, |
| 331 | clusterSize: "PS_10", |
| 332 | }); |
| 333 | |
| 334 | return { database }; |
| 335 | }), |
| 336 | ); |
| 337 | |
| 338 | yield* Planetscale.waitForDatabaseReady(database.organization, name); |
| 339 | |
| 340 | const exit = yield* Effect.exit( |
| 341 | stack |
| 342 | .deploy( |
| 343 | Effect.gen(function* () { |
| 344 | const database = yield* Planetscale.PostgresDatabase("PostgresWrongKind", { |
| 345 | name, |
| 346 | clusterSize: "PS_10", |
| 347 | }); |
| 348 | |
| 349 | return { database }; |
| 350 | }), |
| 351 | ) |
| 352 | .pipe(adopt(true)), |
| 353 | ); |
| 354 | |
| 355 | expect(Exit.isFailure(exit)).toBe(true); |
| 356 | |
| 357 | if (Exit.isFailure(exit)) { |
| 358 | const pretty = Cause.pretty(exit.cause); |
| 359 | expect(pretty).toContain("mysql"); |
| 360 | expect(pretty).toContain("MySQLDatabase"); |
| 361 | } |
| 362 | |
| 363 | yield* stack.destroy(); |
| 364 | }).pipe(logLevel), |
| 365 | { timeout: 5_000_000, tags: ["provider:planetscale:mysql"] }, |
| 366 | ); |
| 367 | |
| 368 | test.provider( |
| 369 | "adopt with wrong arch should trigger replace", |
| 370 | (stack) => |
| 371 | Effect.gen(function* () { |
| 372 | yield* stack.destroy(); |
| 373 | |
| 374 | // Create a database with arm |
| 375 | const { database } = yield* stack.deploy( |
| 376 | Effect.gen(function* () { |
| 377 | const database = yield* Planetscale.PostgresDatabase("PostgresDatabaseWrongArch", { |
| 378 | arch: "arm", |
| 379 | clusterSize: "PS_10", |
| 380 | }); |
| 381 | |
| 382 | return { database }; |
| 383 | }), |
| 384 | ); |
| 385 | |
| 386 | expect(database.arch).toEqual("arm"); |
| 387 | |
| 388 | // Now try to adopt it with a different arch — should replace |
| 389 | const { newDatabase } = yield* stack.deploy( |
| 390 | Effect.gen(function* () { |
| 391 | const newDatabase = yield* Planetscale.PostgresDatabase( |
| 392 | "PostgresDatabaseWrongArch", |
| 393 | { arch: "x86", clusterSize: "PS_10" }, |
| 394 | ); |
| 395 | return { newDatabase }; |
| 396 | }), |
| 397 | ); |
| 398 | |
| 399 | expect(newDatabase.id).not.toBe(database.id); |
| 400 | |
| 401 | yield* stack.destroy(); |
| 402 | |
| 403 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 404 | yield* waitForDatabaseToBeDeleted(newDatabase.name, newDatabase.organization); |
| 405 | }).pipe(logLevel), |
| 406 | 5_000_000, |
| 407 | ); |
| 408 | |
| 409 | test.provider( |
| 410 | "database with RemovalPolicy.retain(true) should not be deleted via API", |
| 411 | (stack) => |
| 412 | Effect.gen(function* () { |
| 413 | yield* stack.destroy(); |
| 414 | |
| 415 | const { database } = yield* stack.deploy( |
| 416 | Effect.gen(function* () { |
| 417 | const database = yield* Planetscale.PostgresDatabase( |
| 418 | "PostgresDatabaseRetainRemoval", |
| 419 | { region: { slug: "us-east" }, clusterSize: "PS_10" }, |
| 420 | ).pipe(RemovalPolicy.retain(true)); |
| 421 | |
| 422 | return { database }; |
| 423 | }), |
| 424 | ); |
| 425 | |
| 426 | // Verify database exists |
| 427 | yield* Planetscale.waitForDatabaseReady(database.organization, database.name); |
| 428 | |
| 429 | // When we call destroy, the database should NOT be deleted via API |
| 430 | yield* stack.destroy(); |
| 431 | |
| 432 | yield* Planetscale.waitForDatabaseReady(database.organization, database.name); |
| 433 | |
| 434 | // Verify database still exists (was not deleted via API) |
| 435 | const live = yield* ps.getDatabase({ |
| 436 | organization: database.organization, |
| 437 | database: database.name, |
| 438 | }); |
| 439 | |
| 440 | // Database should still exist |
| 441 | expect(live.name).toEqual(database.name); |
| 442 | expect(live.state).toEqual("ready"); |
| 443 | expect(live.kind).toEqual("postgresql"); |
| 444 | |
| 445 | // Clean up manually for the test |
| 446 | yield* ps |
| 447 | .deleteDatabase({ organization: database.organization, database: database.name }) |
| 448 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 449 | }).pipe(logLevel), |
| 450 | 5_000_000, |
| 451 | ); |
| 452 | |
| 453 | test.provider( |
| 454 | "deletionProtection blocks destroy until it is turned off", |
| 455 | (stack) => |
| 456 | Effect.gen(function* () { |
| 457 | const name = `${stack.stage.toLowerCase().replace(/[^a-z0-9-]/g, "-")}-pg-protect`; |
| 458 | const { organization } = yield* yield* Planetscale.Credentials; |
| 459 | |
| 460 | const program = (deletionProtection: boolean, region?: string) => |
| 461 | Effect.gen(function* () { |
| 462 | const database = yield* Planetscale.PostgresDatabase( |
| 463 | "PostgresDatabaseDeletionProtection", |
| 464 | { |
| 465 | name, |
| 466 | clusterSize: "PS_5", |
| 467 | deletionProtection, |
| 468 | ...(region ? { region: { slug: region } } : {}), |
| 469 | }, |
| 470 | ); |
| 471 | return { database }; |
| 472 | }); |
| 473 | const deploy = (deletionProtection: boolean) => |
| 474 | stack.deploy(program(deletionProtection)); |
| 475 | |
| 476 | yield* Effect.gen(function* () { |
| 477 | // Inside the cleanup scope: state left by an interrupted run |
| 478 | // can still point at a protected database, which makes this |
| 479 | // destroy fail. |
| 480 | yield* stack.destroy(); |
| 481 | |
| 482 | const { database } = yield* deploy(true); |
| 483 | expect(database.deletionProtection).toBe(true); |
| 484 | |
| 485 | const live = yield* ps.getDatabase({ |
| 486 | organization, |
| 487 | database: name, |
| 488 | }); |
| 489 | expect(live.deletion_protected).toBe(true); |
| 490 | |
| 491 | // Create-first replacement under the same explicit name would |
| 492 | // converge onto the old database and then delete it, so the |
| 493 | // plan must refuse a region change that keeps the name. |
| 494 | const replacement = yield* Effect.exit( |
| 495 | stack.plan( |
| 496 | program(true, database.region.slug === "eu-west" ? "us-east" : "eu-west"), |
| 497 | ), |
| 498 | ); |
| 499 | expect(Exit.isFailure(replacement)).toBe(true); |
| 500 | if (Exit.isFailure(replacement)) { |
| 501 | expect(Cause.pretty(replacement.cause)).toContain("requires a new database"); |
| 502 | } |
| 503 | |
| 504 | const exit = yield* Effect.exit(stack.destroy()); |
| 505 | expect(Exit.isFailure(exit)).toBe(true); |
| 506 | if (Exit.isFailure(exit)) { |
| 507 | expect(Cause.pretty(exit.cause)).toContain("deletionProtection: false"); |
| 508 | } |
| 509 | |
| 510 | const stillThere = yield* ps.getDatabase({ |
| 511 | organization, |
| 512 | database: name, |
| 513 | }); |
| 514 | expect(stillThere.id).toEqual(database.id); |
| 515 | |
| 516 | const { database: unprotected } = yield* deploy(false); |
| 517 | expect(unprotected.deletionProtection).toBe(false); |
| 518 | |
| 519 | yield* stack.destroy(); |
| 520 | |
| 521 | yield* waitForDatabaseToBeDeleted(name, organization); |
| 522 | }).pipe( |
| 523 | // Registered before the first destroy and deploy so a failed |
| 524 | // run never leaves a protected database behind; a failed |
| 525 | // cleanup fails the test. |
| 526 | Effect.ensuring(deleteTestDatabase(organization, name).pipe(Effect.orDie)), |
| 527 | ); |
| 528 | }).pipe(logLevel), |
| 529 | 5_000_000, |
| 530 | ); |
| 531 | }, |
| 532 | ); |
| 533 | |
| 534 | const waitForDatabaseToBeDeleted = Effect.fn(function* (database: string, organization: string) { |
| 535 | yield* ps |
| 536 | .getDatabase({ |
| 537 | organization, |
| 538 | database, |
| 539 | }) |
| 540 | .pipe( |
| 541 | Effect.flatMap(() => Effect.fail(new DatabaseStillExists())), |
| 542 | Effect.retry({ |
| 543 | while: (e): e is DatabaseStillExists => e instanceof DatabaseStillExists, |
| 544 | // Bounded so a database that never disappears fails the wait |
| 545 | // (after about 90 seconds) instead of hanging test cleanup. |
| 546 | schedule: Schedule.spaced("2 seconds"), |
| 547 | times: 45, |
| 548 | }), |
| 549 | Effect.catchTag("NotFound", () => Effect.void), |
| 550 | ); |
| 551 | }); |
| 552 | |
| 553 | class DatabaseStillExists extends Data.TaggedError("DatabaseStillExists") {} |
| 554 | |
| 555 | /** |
| 556 | * Turns deletion protection off and deletes a test database by name, then |
| 557 | * waits until it is gone. A database that does not exist is a no-op. |
| 558 | */ |
| 559 | const deleteTestDatabase = Effect.fn(function* (organization: string, database: string) { |
| 560 | const live = yield* ps |
| 561 | .getDatabase({ organization, database }) |
| 562 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 563 | if (!live) return; |
| 564 | if (live.deletion_protected) { |
| 565 | yield* ps.updateDatabaseSettings({ |
| 566 | organization, |
| 567 | database, |
| 568 | deletion_protected: false, |
| 569 | }); |
| 570 | } |
| 571 | yield* ps |
| 572 | .deleteDatabase({ organization, database }) |
| 573 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 574 | yield* waitForDatabaseToBeDeleted(database, organization); |
| 575 | }); |