@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, 04:45 UTC

main
ts 585 lines 19.7 KB
Raw
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 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 "MySQLBranch",
21 { tags: ["provider:planetscale", "provider:planetscale:mysql", "live"] },
22 () => {
23 test.provider(
24 "adopts existing branch when adopt is true",
25 (stack) =>
26 Effect.gen(function* () {
27 const dbName = "alchemy-branch-adopt-true";
28 const branchName = "adopted";
29
30 yield* stack.destroy();
31
32 const { database } = yield* stack.deploy(
33 Effect.gen(function* () {
34 const database = yield* Planetscale.MySQLDatabase("Database", {
35 name: dbName,
36 region: { slug: "us-east" },
37 clusterSize: "PS_10",
38 });
39
40 return { database };
41 }),
42 );
43
44 yield* Planetscale.waitForBranchReady(database.organization, dbName, "main");
45 yield* deleteBranchIfExists(database.organization, dbName, branchName);
46
47 yield* ps.createBranch({
48 organization: database.organization,
49 database: dbName,
50 name: branchName,
51 parent_branch: "main",
52 });
53 yield* Planetscale.waitForBranchReady(database.organization, dbName, branchName);
54
55 const { branch } = yield* stack
56 .deploy(
57 Effect.gen(function* () {
58 const database = yield* Planetscale.MySQLDatabase("Database", {
59 name: dbName,
60 region: { slug: "us-east" },
61 clusterSize: "PS_10",
62 });
63 const branch = yield* Planetscale.MySQLBranch("AdoptedBranch", {
64 name: branchName,
65 database,
66 parentBranch: "main",
67 isProduction: false,
68 });
69
70 return { database, branch };
71 }),
72 )
73 .pipe(adopt(true));
74
75 expect(branch).toMatchObject({
76 name: branchName,
77 organization: database.organization,
78 database: dbName,
79 parentBranch: "main",
80 production: false,
81 createdAt: expect.any(String),
82 updatedAt: expect.any(String),
83 htmlUrl: expect.any(String),
84 region: { slug: expect.any(String) },
85 });
86
87 yield* stack.destroy();
88 yield* waitForDatabaseToBeDeleted(dbName, database.organization);
89 }).pipe(logLevel),
90 5_000_000,
91 );
92
93 test.provider(
94 "errors on existing branch when adopt is false",
95 (stack) =>
96 Effect.gen(function* () {
97 const dbName = "alchemy-branch-adopt-false";
98 const branchName = "existing";
99
100 yield* stack.destroy();
101
102 const { database } = yield* stack.deploy(
103 Effect.gen(function* () {
104 const database = yield* Planetscale.MySQLDatabase("Database", {
105 name: dbName,
106 region: { slug: "us-east" },
107 clusterSize: "PS_10",
108 });
109
110 return { database };
111 }),
112 );
113
114 yield* Planetscale.waitForBranchReady(database.organization, dbName, "main");
115 yield* deleteBranchIfExists(database.organization, dbName, branchName);
116
117 yield* ps.createBranch({
118 organization: database.organization,
119 database: dbName,
120 name: branchName,
121 parent_branch: "main",
122 });
123 yield* Planetscale.waitForBranchReady(database.organization, dbName, branchName);
124
125 const exit = yield* stack
126 .deploy(
127 Effect.gen(function* () {
128 const database = yield* Planetscale.MySQLDatabase("Database", {
129 name: dbName,
130 region: { slug: "us-east" },
131 clusterSize: "PS_10",
132 });
133 const branch = yield* Planetscale.MySQLBranch("ExistingBranch", {
134 name: branchName,
135 database,
136 parentBranch: "main",
137 isProduction: false,
138 });
139
140 return { database, branch };
141 }),
142 )
143 .pipe(Effect.exit);
144
145 yield* deleteBranchIfExists(database.organization, dbName, branchName);
146 yield* stack.destroy();
147 yield* waitForDatabaseToBeDeleted(dbName, database.organization);
148
149 expect(Exit.isFailure(exit)).toBe(true);
150 if (Exit.isFailure(exit)) {
151 const pretty = Cause.pretty(exit.cause);
152 expect(pretty).toContain("Cannot adopt resource");
153 expect(pretty).toContain("Planetscale.MySQLBranch");
154 }
155 }).pipe(logLevel),
156 5_000_000,
157 );
158
159 test.provider(
160 "can create branch with backup",
161 (stack) =>
162 Effect.gen(function* () {
163 const dbName = "alchemy-branch-backup";
164 const branchName = "restored";
165
166 yield* stack.destroy();
167
168 const { database } = yield* stack.deploy(
169 Effect.gen(function* () {
170 const database = yield* Planetscale.MySQLDatabase("Database", {
171 name: dbName,
172 region: { slug: "us-east" },
173 clusterSize: "PS_10",
174 });
175
176 return { database };
177 }),
178 );
179
180 yield* Planetscale.waitForBranchReady(database.organization, dbName, "main");
181 yield* deleteBranchIfExists(database.organization, dbName, branchName);
182
183 const backup = yield* ps.createBackup({
184 organization: database.organization,
185 database: dbName,
186 branch: "main",
187 name: "alchemy-branch-backup-source",
188 retention_unit: "hour",
189 retention_value: 1,
190 });
191 yield* waitForBackupSuccess(database.organization, dbName, "main", backup.id);
192
193 const { branch } = yield* stack.deploy(
194 Effect.gen(function* () {
195 const database = yield* Planetscale.MySQLDatabase("Database", {
196 name: dbName,
197 region: { slug: "us-east" },
198 clusterSize: "PS_10",
199 });
200 const branch = yield* Planetscale.MySQLBranch("RestoredBranch", {
201 name: branchName,
202 database,
203 parentBranch: "main",
204 isProduction: true,
205 backupId: backup.id,
206 clusterSize: "PS_10",
207 });
208
209 return { database, branch };
210 }),
211 );
212
213 expect(branch).toMatchObject({
214 name: branchName,
215 database: dbName,
216 parentBranch: "main",
217 production: true,
218 });
219
220 const live = yield* Planetscale.waitForBranchReady(
221 database.organization,
222 dbName,
223 branchName,
224 );
225 expect(live.name).toEqual(branchName);
226 expect(live.production).toBe(true);
227 expect(live.cluster_name).toEqual("PS_10");
228
229 yield* stack.deploy(
230 Effect.gen(function* () {
231 const database = yield* Planetscale.MySQLDatabase("Database", {
232 name: dbName,
233 region: { slug: "us-east" },
234 clusterSize: "PS_10",
235 });
236 const branch = yield* Planetscale.MySQLBranch("RestoredBranch", {
237 name: branchName,
238 database,
239 parentBranch: "main",
240 isProduction: false,
241 });
242
243 return { database, branch };
244 }),
245 );
246
247 yield* stack.destroy();
248 yield* waitForDatabaseToBeDeleted(dbName, database.organization);
249 }).pipe(logLevel),
250 5_000_000,
251 );
252
253 test.provider(
254 "can enable and disable safe migrations",
255 (stack) =>
256 Effect.gen(function* () {
257 yield* stack.destroy();
258
259 const { database, branch } = yield* stack.deploy(
260 Effect.gen(function* () {
261 const database = yield* Planetscale.MySQLDatabase("Database", {
262 clusterSize: "PS_10",
263 });
264 const branch = yield* Planetscale.MySQLBranch("Branch", {
265 database,
266 parentBranch: "main",
267 isProduction: true,
268 clusterSize: "PS_10",
269 safeMigrations: true,
270 });
271
272 return { database, branch };
273 }),
274 );
275
276 const enabled = yield* ps.getBranch({
277 organization: database.organization,
278 database: database.name,
279 branch: branch.name,
280 });
281 expect(enabled.production).toBe(true);
282 expect(enabled.safe_migrations).toBe(true);
283
284 const { updatedBranch } = yield* stack.deploy(
285 Effect.gen(function* () {
286 const database = yield* Planetscale.MySQLDatabase("Database", {
287 clusterSize: "PS_10",
288 });
289 const updatedBranch = yield* Planetscale.MySQLBranch("Branch", {
290 database,
291 parentBranch: "main",
292 isProduction: true,
293 clusterSize: "PS_10",
294 safeMigrations: false,
295 });
296
297 return { database, updatedBranch };
298 }),
299 );
300
301 expect(updatedBranch.name).toEqual(branch.name);
302
303 const disabled = yield* ps.getBranch({
304 organization: database.organization,
305 database: database.name,
306 branch: branch.name,
307 });
308 expect(disabled.safe_migrations).toBe(false);
309
310 yield* stack.deploy(
311 Effect.gen(function* () {
312 const database = yield* Planetscale.MySQLDatabase("Database", {
313 clusterSize: "PS_10",
314 });
315 const branch = yield* Planetscale.MySQLBranch("Branch", {
316 database,
317 parentBranch: "main",
318 isProduction: false,
319 });
320
321 return { database, branch };
322 }),
323 );
324
325 yield* stack.destroy();
326 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
327 }).pipe(logLevel),
328 5_000_000,
329 );
330
331 test.provider(
332 "can update cluster size",
333 (stack) =>
334 Effect.gen(function* () {
335 yield* stack.destroy();
336
337 const { database, branch } = yield* stack.deploy(
338 Effect.gen(function* () {
339 const database = yield* Planetscale.MySQLDatabase("Database", {
340 clusterSize: "PS_10",
341 });
342 const branch = yield* Planetscale.MySQLBranch("Branch", {
343 database,
344 parentBranch: "main",
345 isProduction: true,
346 clusterSize: "PS_10",
347 });
348
349 return { database, branch };
350 }),
351 );
352
353 const initial = yield* Planetscale.waitForBranchReady(
354 database.organization,
355 database.name,
356 branch.name,
357 );
358 expect(initial.cluster_name).toEqual("PS_10");
359
360 const { resizedBranch } = yield* stack.deploy(
361 Effect.gen(function* () {
362 const database = yield* Planetscale.MySQLDatabase("Database", {
363 clusterSize: "PS_10",
364 });
365 const resizedBranch = yield* Planetscale.MySQLBranch("Branch", {
366 database,
367 parentBranch: "main",
368 isProduction: true,
369 clusterSize: "PS_20",
370 });
371
372 return { database, resizedBranch };
373 }),
374 );
375
376 expect(resizedBranch.name).toEqual(branch.name);
377
378 const resized = yield* Planetscale.waitForBranchReady(
379 database.organization,
380 database.name,
381 branch.name,
382 );
383 expect(resized.cluster_name).toEqual("PS_20");
384
385 yield* stack.deploy(
386 Effect.gen(function* () {
387 const database = yield* Planetscale.MySQLDatabase("Database", {
388 clusterSize: "PS_10",
389 });
390 const branch = yield* Planetscale.MySQLBranch("Branch", {
391 database,
392 parentBranch: "main",
393 isProduction: false,
394 });
395
396 return { database, branch };
397 }),
398 );
399
400 yield* stack.destroy();
401 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
402 }).pipe(logLevel),
403 5_000_000,
404 );
405
406 test.provider(
407 "can create branch with Branch object as parent",
408 (stack) =>
409 Effect.gen(function* () {
410 yield* stack.destroy();
411
412 const { database, parent, child } = yield* stack.deploy(
413 Effect.gen(function* () {
414 const database = yield* Planetscale.MySQLDatabase("Database", {
415 clusterSize: "PS_10",
416 });
417 const parent = yield* Planetscale.MySQLBranch("ParentBranch", {
418 database,
419 parentBranch: "main",
420 isProduction: false,
421 });
422 const child = yield* Planetscale.MySQLBranch("ChildBranch", {
423 database,
424 parentBranch: parent,
425 isProduction: false,
426 });
427
428 return { database, parent, child };
429 }),
430 );
431
432 expect(child).toMatchObject({
433 database: database.name,
434 parentBranch: parent.name,
435 production: false,
436 });
437
438 const live = yield* Planetscale.waitForBranchReady(
439 database.organization,
440 database.name,
441 child.name,
442 );
443 expect(live.parent_branch).toEqual(parent.name);
444
445 yield* stack.destroy();
446 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
447 }).pipe(logLevel),
448 5_000_000,
449 );
450
451 test.provider(
452 "branch with RemovalPolicy.retain(true) should not be deleted via API",
453 (stack) =>
454 Effect.gen(function* () {
455 const dbName = "alchemy-branch-retain";
456 const branchName = "retained";
457
458 yield* stack.destroy();
459
460 const { database } = yield* stack.deploy(
461 Effect.gen(function* () {
462 const database = yield* Planetscale.MySQLDatabase("Database", {
463 name: dbName,
464 region: { slug: "us-east" },
465 clusterSize: "PS_10",
466 }).pipe(RemovalPolicy.retain(true));
467
468 return { database };
469 }),
470 );
471
472 yield* Planetscale.waitForBranchReady(database.organization, dbName, "main");
473 yield* deleteBranchIfExists(database.organization, dbName, branchName);
474
475 const { branch } = yield* stack.deploy(
476 Effect.gen(function* () {
477 const database = yield* Planetscale.MySQLDatabase("Database", {
478 name: dbName,
479 region: { slug: "us-east" },
480 clusterSize: "PS_10",
481 }).pipe(RemovalPolicy.retain(true));
482 const branch = yield* Planetscale.MySQLBranch("RetainedBranch", {
483 name: branchName,
484 database,
485 parentBranch: "main",
486 isProduction: false,
487 }).pipe(RemovalPolicy.retain(true));
488
489 return { database, branch };
490 }),
491 );
492
493 expect(branch.name).toEqual(branchName);
494
495 yield* stack.destroy();
496
497 const live = yield* Planetscale.waitForBranchReady(
498 database.organization,
499 dbName,
500 branchName,
501 );
502 expect(live.name).toEqual(branchName);
503
504 yield* deleteBranchIfExists(database.organization, dbName, branchName);
505 yield* ps
506 .deleteDatabase({ organization: database.organization, database: dbName })
507 .pipe(Effect.catchTag("NotFound", () => Effect.void));
508 yield* waitForDatabaseToBeDeleted(dbName, database.organization);
509 }).pipe(logLevel),
510 5_000_000,
511 );
512 },
513 );
514
515 const deleteBranchIfExists = Effect.fn(function* (
516 organization: string,
517 database: string,
518 branch: string,
519 ) {
520 yield* ps
521 .deleteBranch({ organization, database, branch })
522 .pipe(Effect.catchTag("NotFound", () => Effect.void));
523 yield* waitForBranchToBeDeleted(organization, database, branch);
524 });
525
526 const waitForBranchToBeDeleted = Effect.fn(function* (
527 organization: string,
528 database: string,
529 branch: string,
530 ) {
531 yield* ps.getBranch({ organization, database, branch }).pipe(
532 Effect.flatMap(() => Effect.fail(new BranchStillExists())),
533 Effect.retry({
534 while: (e): e is BranchStillExists => e instanceof BranchStillExists,
535 schedule: Schedule.exponential(100),
536 }),
537 Effect.catchTag("NotFound", () => Effect.void),
538 );
539 });
540
541 const waitForDatabaseToBeDeleted = Effect.fn(function* (database: string, organization: string) {
542 yield* ps.getDatabase({ organization, database }).pipe(
543 Effect.flatMap(() => Effect.fail(new DatabaseStillExists())),
544 Effect.retry({
545 while: (e): e is DatabaseStillExists => e instanceof DatabaseStillExists,
546 schedule: Schedule.exponential(100),
547 }),
548 Effect.catchTag("NotFound", () => Effect.void),
549 );
550 });
551
552 const waitForBackupSuccess = Effect.fn(function* (
553 organization: string,
554 database: string,
555 branch: string,
556 id: string,
557 ) {
558 return yield* ps.getBackup({ organization, database, branch, id }).pipe(
559 Effect.flatMap((backup) => {
560 switch (backup.state) {
561 case "success":
562 return Effect.succeed(backup);
563 case "failed":
564 case "canceled":
565 case "ignored":
566 return Effect.fail(new BackupNotReady({ retryable: false, state: backup.state }));
567 default:
568 return Effect.fail(new BackupNotReady({ retryable: true, state: backup.state }));
569 }
570 }),
571 Effect.retry({
572 while: (e): e is BackupNotReady => e instanceof BackupNotReady && e.retryable,
573 schedule: Schedule.max([Schedule.exponential(1_000), Schedule.recurs(120)]),
574 }),
575 );
576 });
577
578 class BranchStillExists extends Data.TaggedError("BranchStillExists") {}
579
580 class DatabaseStillExists extends Data.TaggedError("DatabaseStillExists") {}
581
582 class BackupNotReady extends Data.TaggedError("BackupNotReady")<{
583 retryable: boolean;
584 state: string;
585 }> {}