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 * as Provider from "alchemy/Provider"; |
| 6 | import * as RemovalPolicy from "alchemy/RemovalPolicy"; |
| 7 | import { Data, Schedule } from "effect"; |
| 8 | import * as Cause from "effect/Cause"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Exit from "effect/Exit"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Planetscale.providers() }); |
| 14 | |
| 15 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 16 | |
| 17 | describe |
| 18 | .skipIf(!process.env.PLANETSCALE_TEST) |
| 19 | .concurrent( |
| 20 | "MySQLPassword", |
| 21 | { tags: ["provider:planetscale", "provider:planetscale:mysql", "live"] }, |
| 22 | () => { |
| 23 | // Read-only: PARENT FAN-OUT enumeration (org -> databases -> branches -> |
| 24 | // passwords) against the live org, without provisioning anything. |
| 25 | test.provider("list enumerates passwords (read-only)", () => |
| 26 | Effect.gen(function* () { |
| 27 | const provider = yield* Provider.findProvider(Planetscale.MySQLPassword); |
| 28 | const all = yield* provider.list(); |
| 29 | |
| 30 | expect(Array.isArray(all)).toBe(true); |
| 31 | for (const p of all) { |
| 32 | expect(p).toMatchObject({ |
| 33 | id: expect.any(String), |
| 34 | name: expect.any(String), |
| 35 | organization: expect.any(String), |
| 36 | database: expect.any(String), |
| 37 | branch: expect.any(String), |
| 38 | }); |
| 39 | } |
| 40 | }).pipe(logLevel), |
| 41 | ); |
| 42 | |
| 43 | // Deploy-and-find coverage, opt-in only (slow provisioning). |
| 44 | test.provider.skipIf(!process.env.PLANETSCALE_DEPLOY_TEST)( |
| 45 | "list finds a freshly deployed password", |
| 46 | (stack) => |
| 47 | Effect.gen(function* () { |
| 48 | yield* stack.destroy(); |
| 49 | |
| 50 | const { database, password } = yield* stack.deploy( |
| 51 | Effect.gen(function* () { |
| 52 | const database = yield* Planetscale.MySQLDatabase("ListDb", { |
| 53 | name: "alchemy-mysql-pw-list", |
| 54 | clusterSize: "PS_10", |
| 55 | }); |
| 56 | const password = yield* Planetscale.MySQLPassword("ListPassword", { |
| 57 | database, |
| 58 | role: "reader", |
| 59 | }); |
| 60 | return { database, password }; |
| 61 | }), |
| 62 | ); |
| 63 | |
| 64 | const provider = yield* Provider.findProvider(Planetscale.MySQLPassword); |
| 65 | const all = yield* provider.list(); |
| 66 | |
| 67 | expect( |
| 68 | all.some( |
| 69 | (p) => |
| 70 | p.organization === database.organization && |
| 71 | p.database === database.name && |
| 72 | p.id === password.id, |
| 73 | ), |
| 74 | ).toBe(true); |
| 75 | |
| 76 | yield* stack.destroy(); |
| 77 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 78 | }).pipe(logLevel), |
| 79 | 5_000_000, |
| 80 | ); |
| 81 | |
| 82 | test.provider( |
| 83 | "create, update, and delete password", |
| 84 | (stack) => |
| 85 | Effect.gen(function* () { |
| 86 | yield* stack.destroy(); |
| 87 | |
| 88 | const { database, branch, password } = yield* stack.deploy( |
| 89 | Effect.gen(function* () { |
| 90 | const database = yield* Planetscale.MySQLDatabase("Database", { |
| 91 | clusterSize: "PS_10", |
| 92 | }); |
| 93 | |
| 94 | const branch = yield* Planetscale.MySQLBranch("Branch", { |
| 95 | database, |
| 96 | parentBranch: "main", |
| 97 | isProduction: false, |
| 98 | }); |
| 99 | |
| 100 | const password = yield* Planetscale.MySQLPassword("Password", { |
| 101 | database, |
| 102 | branch, |
| 103 | role: "reader", |
| 104 | }); |
| 105 | |
| 106 | return { database, branch, password }; |
| 107 | }), |
| 108 | ); |
| 109 | |
| 110 | expect(password).toMatchObject({ |
| 111 | id: expect.any(String), |
| 112 | name: expect.any(String), |
| 113 | role: "reader", |
| 114 | host: expect.any(String), |
| 115 | username: expect.any(String), |
| 116 | organization: expect.any(String), |
| 117 | database: database.name, |
| 118 | branch: branch.name, |
| 119 | }); |
| 120 | |
| 121 | // Verify password was created by querying the API directly |
| 122 | const fetched = yield* ps.getPassword({ |
| 123 | organization: database.organization, |
| 124 | database: database.name, |
| 125 | branch: branch.name, |
| 126 | id: password.id, |
| 127 | }); |
| 128 | |
| 129 | expect(fetched.id).toEqual(password.id); |
| 130 | expect(fetched.name).toEqual(password.name); |
| 131 | expect(fetched.role).toEqual("reader"); |
| 132 | |
| 133 | // Update the password (only name and cidrs should trigger update, not replace) |
| 134 | const { updatedPassword } = yield* stack.deploy( |
| 135 | Effect.gen(function* () { |
| 136 | const sameDatabase = yield* Planetscale.MySQLDatabase("Database", { |
| 137 | clusterSize: "PS_10", |
| 138 | }); |
| 139 | |
| 140 | const sameBranch = yield* Planetscale.MySQLBranch("Branch", { |
| 141 | database: sameDatabase, |
| 142 | parentBranch: "main", |
| 143 | isProduction: false, |
| 144 | }); |
| 145 | |
| 146 | const updatedPassword = yield* Planetscale.MySQLPassword("Password", { |
| 147 | name: "test-updated-password-name", |
| 148 | database: sameDatabase.name, |
| 149 | branch: sameBranch.name, |
| 150 | role: "reader", |
| 151 | }); |
| 152 | |
| 153 | return { updatedPassword }; |
| 154 | }), |
| 155 | ); |
| 156 | |
| 157 | expect(updatedPassword.id).toEqual(password.id); |
| 158 | expect(updatedPassword.name).not.toEqual(password.name); |
| 159 | |
| 160 | // Verify password was updated |
| 161 | const fetchedUpdated = yield* ps.getPassword({ |
| 162 | organization: database.organization, |
| 163 | database: database.name, |
| 164 | branch: branch.name, |
| 165 | id: updatedPassword.id, |
| 166 | }); |
| 167 | |
| 168 | expect(fetchedUpdated.id).toEqual(password.id); |
| 169 | expect(fetchedUpdated.name).toEqual(updatedPassword.name); |
| 170 | |
| 171 | yield* stack.destroy(); |
| 172 | |
| 173 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 174 | }).pipe(logLevel), |
| 175 | 5_000_000, |
| 176 | ); |
| 177 | |
| 178 | test.provider( |
| 179 | "password gets replaced when properties other than name and cidrs change", |
| 180 | (stack) => |
| 181 | Effect.gen(function* () { |
| 182 | yield* stack.destroy(); |
| 183 | |
| 184 | const { database, branch, password } = yield* stack.deploy( |
| 185 | Effect.gen(function* () { |
| 186 | const database = yield* Planetscale.MySQLDatabase("Database", { |
| 187 | clusterSize: "PS_10", |
| 188 | }); |
| 189 | |
| 190 | const branch = yield* Planetscale.MySQLBranch("Branch", { |
| 191 | database, |
| 192 | parentBranch: "main", |
| 193 | isProduction: false, |
| 194 | }); |
| 195 | |
| 196 | const password = yield* Planetscale.MySQLPassword("Password", { |
| 197 | database, |
| 198 | branch, |
| 199 | role: "reader", |
| 200 | ttl: 3600, |
| 201 | cidrs: ["0.0.0.0/0"], |
| 202 | }); |
| 203 | return { database, branch, password }; |
| 204 | }), |
| 205 | ); |
| 206 | |
| 207 | const originalId = password.id; |
| 208 | expect(password.role).toEqual("reader"); |
| 209 | expect(password.ttl).toEqual(3600); |
| 210 | |
| 211 | // Change role from reader -> writer (should trigger replace). |
| 212 | const { replacedPassword } = yield* stack.deploy( |
| 213 | Effect.gen(function* () { |
| 214 | const database = yield* Planetscale.MySQLDatabase("Database", { |
| 215 | clusterSize: "PS_10", |
| 216 | }); |
| 217 | |
| 218 | const branch = yield* Planetscale.MySQLBranch("Branch", { |
| 219 | database, |
| 220 | parentBranch: "main", |
| 221 | isProduction: false, |
| 222 | }); |
| 223 | |
| 224 | const replacedPassword = yield* Planetscale.MySQLPassword("Password", { |
| 225 | database, |
| 226 | branch, |
| 227 | role: "writer", |
| 228 | ttl: 3600, |
| 229 | cidrs: ["0.0.0.0/0"], |
| 230 | }); |
| 231 | return { replacedPassword }; |
| 232 | }), |
| 233 | ); |
| 234 | |
| 235 | // New ID due to replacement. |
| 236 | expect(replacedPassword.id).not.toEqual(originalId); |
| 237 | expect(replacedPassword.role).toEqual("writer"); |
| 238 | |
| 239 | // Old password should have been deleted as part of the replace. |
| 240 | const oldExit = yield* ps |
| 241 | .getPassword({ |
| 242 | organization: database.organization, |
| 243 | database: database.name, |
| 244 | branch: branch.name, |
| 245 | id: originalId, |
| 246 | }) |
| 247 | .pipe(Effect.exit); |
| 248 | expect(Exit.isFailure(oldExit)).toBe(true); |
| 249 | if (Exit.isFailure(oldExit)) { |
| 250 | expect(Cause.pretty(oldExit.cause)).toContain("NotFound"); |
| 251 | } |
| 252 | |
| 253 | // New password exists with the new role. |
| 254 | const newFetched = yield* ps.getPassword({ |
| 255 | organization: database.organization, |
| 256 | database: database.name, |
| 257 | branch: branch.name, |
| 258 | id: replacedPassword.id, |
| 259 | }); |
| 260 | expect(newFetched.id).toEqual(replacedPassword.id); |
| 261 | expect(newFetched.role).toEqual("writer"); |
| 262 | |
| 263 | yield* stack.destroy(); |
| 264 | yield* waitForDatabaseToBeDeleted(database.name, database.organization); |
| 265 | }).pipe(logLevel), |
| 266 | 5_000_000, |
| 267 | ); |
| 268 | |
| 269 | test.provider( |
| 270 | "password with RemovalPolicy.retain(true) should not be deleted via API", |
| 271 | (stack) => |
| 272 | Effect.gen(function* () { |
| 273 | const dbName = `alchemy-test-pwd-retain`; |
| 274 | const passwordName = `retain-password`; |
| 275 | |
| 276 | yield* stack.destroy(); |
| 277 | |
| 278 | const { database, password } = yield* stack.deploy( |
| 279 | Effect.gen(function* () { |
| 280 | // Retain the database too — otherwise deleting it would cascade |
| 281 | // to the password and we couldn't observe the retain behavior. |
| 282 | const database = yield* Planetscale.MySQLDatabase("Database", { |
| 283 | name: dbName, |
| 284 | clusterSize: "PS_10", |
| 285 | }).pipe(RemovalPolicy.retain(true)); |
| 286 | const password = yield* Planetscale.MySQLPassword("Password", { |
| 287 | name: passwordName, |
| 288 | database, |
| 289 | role: "reader", |
| 290 | }).pipe(RemovalPolicy.retain(true)); |
| 291 | return { database, password }; |
| 292 | }), |
| 293 | ); |
| 294 | |
| 295 | // Password exists post-deploy. |
| 296 | const fetched = yield* ps.getPassword({ |
| 297 | organization: database.organization, |
| 298 | database: database.name, |
| 299 | branch: "main", |
| 300 | id: password.id, |
| 301 | }); |
| 302 | expect(fetched.id).toEqual(password.id); |
| 303 | |
| 304 | // Destroy the stack — both retained, so neither should be removed. |
| 305 | yield* stack.destroy(); |
| 306 | |
| 307 | const { organization } = yield* yield* Planetscale.Credentials; |
| 308 | |
| 309 | // Database should still exist and be ready. |
| 310 | const liveDb = yield* Planetscale.waitForDatabaseReady(organization, dbName); |
| 311 | expect(liveDb.name).toEqual(dbName); |
| 312 | |
| 313 | // Password should still exist (was not deleted via API). |
| 314 | const stillExists = yield* ps.getPassword({ |
| 315 | organization, |
| 316 | database: dbName, |
| 317 | branch: "main", |
| 318 | id: password.id, |
| 319 | }); |
| 320 | expect(stillExists.id).toEqual(password.id); |
| 321 | expect(stillExists.name).toEqual(password.name); |
| 322 | |
| 323 | // Manual cleanup for the test. |
| 324 | yield* ps |
| 325 | .deletePassword({ organization, database: dbName, branch: "main", id: password.id }) |
| 326 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 327 | |
| 328 | yield* ps |
| 329 | .deleteDatabase({ organization, database: dbName }) |
| 330 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 331 | |
| 332 | yield* waitForDatabaseToBeDeleted(dbName, organization); |
| 333 | }).pipe(logLevel), |
| 334 | 5_000_000, |
| 335 | ); |
| 336 | }, |
| 337 | ); |
| 338 | |
| 339 | const waitForDatabaseToBeDeleted = Effect.fn(function* (database: string, organization: string) { |
| 340 | yield* ps.getDatabase({ organization, database }).pipe( |
| 341 | Effect.flatMap(() => Effect.fail(new DatabaseStillExists())), |
| 342 | Effect.retry({ |
| 343 | while: (e): e is DatabaseStillExists => e instanceof DatabaseStillExists, |
| 344 | schedule: Schedule.exponential(100), |
| 345 | }), |
| 346 | Effect.catchTag("NotFound", () => Effect.void), |
| 347 | ); |
| 348 | }); |
| 349 | |
| 350 | class DatabaseStillExists extends Data.TaggedError("DatabaseStillExists") {} |