@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 672 lines 23.5 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 * as Provider from "alchemy/Provider";
6 import * as RemovalPolicy from "alchemy/RemovalPolicy";
7 import { Redacted } from "effect";
8 import * as Cause from "effect/Cause";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as Exit from "effect/Exit";
12 import { MinimumLogLevel } from "effect/References";
13 import * as Schedule from "effect/Schedule";
14
15 const { test } = Test.make({ providers: Planetscale.providers() });
16
17 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
18
19 describe
20 .skipIf(!process.env.PLANETSCALE_TEST)
21 .concurrent(
22 "PostgresRole",
23 { tags: ["provider:planetscale", "provider:planetscale:postgres", "live"] },
24 () => {
25 // Read-only: PARENT FAN-OUT enumeration (org -> databases -> branches ->
26 // default role) against the live org, without provisioning anything.
27 test.provider("list enumerates default roles (read-only)", () =>
28 Effect.gen(function* () {
29 const provider = yield* Provider.findProvider(Planetscale.PostgresDefaultRole);
30 const all = yield* provider.list();
31
32 expect(Array.isArray(all)).toBe(true);
33 for (const r of all) {
34 expect(r).toMatchObject({
35 id: expect.any(String),
36 name: expect.any(String),
37 organization: expect.any(String),
38 database: expect.any(String),
39 branch: expect.any(String),
40 privateHost: expect.any(String),
41 privateConnectionServiceName: expect.any(String),
42 });
43 }
44 }).pipe(logLevel),
45 );
46
47 // Deploy-and-find coverage, opt-in only (slow provisioning).
48 test.provider.skipIf(!process.env.PLANETSCALE_DEPLOY_TEST)(
49 "list finds a freshly deployed default role",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const { database, role } = yield* stack.deploy(
55 Effect.gen(function* () {
56 const database = yield* Planetscale.PostgresDatabase("ListDb", {
57 name: "alchemy-pg-default-list",
58 clusterSize: "PS_10",
59 arch: "arm",
60 });
61 const role = yield* Planetscale.PostgresDefaultRole("ListRole", {
62 database,
63 forceReset: true,
64 });
65 return { database, role };
66 }),
67 );
68
69 const provider = yield* Provider.findProvider(Planetscale.PostgresDefaultRole);
70 const all = yield* provider.list();
71
72 expect(
73 all.some(
74 (r) =>
75 r.organization === database.organization &&
76 r.database === database.name &&
77 r.branch === role.branch,
78 ),
79 ).toBe(true);
80
81 yield* stack.destroy();
82 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
83 }).pipe(logLevel),
84 5_000_000,
85 );
86
87 // Read-only: PARENT FAN-OUT enumeration (org -> databases -> branches ->
88 // roles, excluding the default role) against the live org, without
89 // provisioning anything.
90 test.provider("list enumerates roles (read-only)", () =>
91 Effect.gen(function* () {
92 const provider = yield* Provider.findProvider(Planetscale.PostgresRole);
93 const all = yield* provider.list();
94
95 expect(Array.isArray(all)).toBe(true);
96 for (const r of all) {
97 expect(r).toMatchObject({
98 id: expect.any(String),
99 name: expect.any(String),
100 organization: expect.any(String),
101 database: expect.any(String),
102 branch: expect.any(String),
103 privateHost: expect.any(String),
104 privateConnectionServiceName: expect.any(String),
105 });
106 }
107 }).pipe(logLevel),
108 );
109
110 // Deploy-and-find coverage, opt-in only (slow provisioning).
111 test.provider.skipIf(!process.env.PLANETSCALE_DEPLOY_TEST)(
112 "list finds a freshly deployed role",
113 (stack) =>
114 Effect.gen(function* () {
115 yield* stack.destroy();
116
117 const { database, role } = yield* stack.deploy(
118 Effect.gen(function* () {
119 const database = yield* Planetscale.PostgresDatabase("ListDb", {
120 name: "alchemy-pg-role-list",
121 clusterSize: "PS_10",
122 arch: "arm",
123 });
124 const role = yield* Planetscale.PostgresRole("ListRole", {
125 database,
126 inheritedRoles: ["pg_read_all_data"],
127 });
128 return { database, role };
129 }),
130 );
131
132 const provider = yield* Provider.findProvider(Planetscale.PostgresRole);
133 const all = yield* provider.list();
134
135 expect(
136 all.some(
137 (r) =>
138 r.organization === database.organization &&
139 r.database === database.name &&
140 r.id === role.id,
141 ),
142 ).toBe(true);
143
144 yield* stack.destroy();
145 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
146 }).pipe(logLevel),
147 5_000_000,
148 );
149
150 test.provider(
151 "default role - create, duplicate fails, forceReset returns new id",
152 (stack) =>
153 Effect.gen(function* () {
154 yield* stack.destroy();
155
156 // First: create default role, expect success
157 const { database, role1 } = yield* stack.deploy(
158 Effect.gen(function* () {
159 const database = yield* Planetscale.PostgresDatabase("Database", {
160 clusterSize: "PS_10",
161 arch: "arm",
162 });
163 const role1 = yield* Planetscale.PostgresDefaultRole("Role1", { database });
164
165 return { role1, database };
166 }),
167 );
168
169 expect(role1).toMatchObject({
170 id: expect.any(String),
171 name: expect.any(String),
172 host: expect.any(String),
173 username: expect.any(String),
174 password: expect.any(Object),
175 databaseName: "postgres",
176 branch: "main",
177 organization: database.organization,
178 privateHost: expect.any(String),
179 privateConnectionServiceName: expect.any(String),
180 });
181
182 const defaultRoleFromApi = yield* ps.getDefaultRole({
183 organization: database.organization,
184 database: database.name,
185 branch: "main",
186 });
187 expect(role1.privateConnectionServiceName).toEqual(
188 defaultRoleFromApi.private_connection_service_name,
189 );
190 expect(role1.privateHost).toEqual(defaultRoleFromApi.private_access_host_url);
191
192 // Second: create again without forceReset — should fail (default already exists)
193 const exit = yield* stack
194 .deploy(
195 Effect.gen(function* () {
196 const database = yield* Planetscale.PostgresDatabase("Database", {
197 clusterSize: "PS_10",
198 arch: "arm",
199 });
200 const role2 = yield* Planetscale.PostgresDefaultRole("Role2", { database });
201
202 return { role2 };
203 }),
204 )
205 .pipe(Effect.exit);
206
207 expect(Exit.isFailure(exit)).toBe(true);
208
209 if (Exit.isFailure(exit)) {
210 expect(Cause.pretty(exit.cause)).toMatch(
211 /Default role already exists.*Use forceReset/,
212 );
213 }
214
215 // Third: create with forceReset — should succeed and return a different role id
216 const { role3 } = yield* stack.deploy(
217 Effect.gen(function* () {
218 const database = yield* Planetscale.PostgresDatabase("Database", {
219 clusterSize: "PS_10",
220 arch: "arm",
221 });
222 const role3 = yield* Planetscale.PostgresDefaultRole("Role3", {
223 database,
224 forceReset: true,
225 });
226
227 return { role3, database };
228 }),
229 );
230
231 expect(role3).toMatchObject({
232 id: expect.any(String),
233 name: expect.any(String),
234 host: expect.any(String),
235 username: expect.any(String),
236 password: expect.any(Object),
237 databaseName: "postgres",
238 branch: "main",
239 organization: database.organization,
240 });
241
242 // the default role ID is the same, but the password is different
243 expect(Redacted.value(role3.password)).not.toEqual(Redacted.value(role1.password));
244
245 yield* stack.destroy();
246 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
247 }).pipe(logLevel),
248 5_000_000,
249 );
250
251 test.provider(
252 "create and delete role",
253 (stack) =>
254 Effect.gen(function* () {
255 yield* stack.destroy();
256
257 // Create a role
258 const { database, role } = yield* stack.deploy(
259 Effect.gen(function* () {
260 const database = yield* Planetscale.PostgresDatabase("Database", {
261 clusterSize: "PS_10",
262 arch: "arm",
263 });
264 const role = yield* Planetscale.PostgresRole("Role", {
265 database,
266 // Empty array means no permissions, which is fine for testing.
267 inheritedRoles: [],
268 });
269
270 return { database, role };
271 }),
272 );
273
274 expect(role).toMatchObject({
275 id: expect.any(String),
276 name: expect.any(String),
277 host: expect.any(String),
278 username: expect.any(String),
279 password: expect.any(Object),
280 privateHost: expect.any(String),
281 privateConnectionServiceName: expect.any(String),
282 });
283
284 const roleFromApi = yield* ps.getRole({
285 id: role.id,
286 database: database.name,
287 organization: database.organization,
288 branch: "main",
289 });
290 expect(role.privateConnectionServiceName).toEqual(
291 roleFromApi.private_connection_service_name,
292 );
293 expect(role.privateHost).toEqual(roleFromApi.private_access_host_url);
294
295 // Update role with different ttl (should trigger replacement)
296 const { updatedRole } = yield* stack.deploy(
297 Effect.gen(function* () {
298 const database = yield* Planetscale.PostgresDatabase("Database", {
299 clusterSize: "PS_10",
300 arch: "arm",
301 });
302 const updatedRole = yield* Planetscale.PostgresRole("Role", {
303 database,
304 ttl: 3600,
305 inheritedRoles: [],
306 });
307
308 return { database, updatedRole };
309 }),
310 );
311
312 expect(role.id).not.toEqual(updatedRole.id);
313 expect(updatedRole.ttl).toEqual(3600);
314
315 const found = yield* ps
316 .getRole({
317 id: role.id,
318 database: database.name,
319 organization: database.organization,
320 branch: "main",
321 })
322 .pipe(
323 Effect.map(() => true),
324 Effect.catchTag("NotFound", () => Effect.succeed(false)),
325 );
326
327 expect(found).toBe(false);
328
329 const updatedRoleFromApi = yield* ps.getRole({
330 id: updatedRole.id,
331 database: database.name,
332 organization: database.organization,
333 branch: "main",
334 });
335
336 expect(updatedRoleFromApi.ttl).toEqual(3600);
337
338 yield* stack.destroy();
339
340 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
341 }).pipe(logLevel),
342 5_000_000,
343 );
344
345 // Regression (#1832): `--force` upgrades the unchanged database's noop
346 // to an update. Its conditional `name` stable must survive, or the
347 // role sees `database.name === undefined`, plans a replacement and
348 // rotates its credentials on every forced deploy.
349 test.provider(
350 "forced redeploy keeps the role (no replacement, credentials unchanged)",
351 (stack) =>
352 Effect.gen(function* () {
353 yield* stack.destroy();
354
355 const program = Effect.gen(function* () {
356 const database = yield* Planetscale.PostgresDatabase("Database", {
357 clusterSize: "PS_10",
358 arch: "arm",
359 });
360 const role = yield* Planetscale.PostgresRole("RoleForce", {
361 database,
362 inheritedRoles: ["pg_read_all_data"],
363 });
364 return { database, role };
365 });
366
367 const { database, role } = yield* stack.deploy(program);
368 const { role: forced } = yield* stack.deploy(program, { force: true });
369
370 expect(forced.id).toEqual(role.id);
371 expect(forced.name).toEqual(role.name);
372 expect(forced.username).toEqual(role.username);
373 expect(Redacted.value(forced.password)).toEqual(Redacted.value(role.password));
374
375 // Out-of-band: the original role still exists and is the only
376 // non-default role on the branch (no replacement was created).
377 const live = yield* ps.getRole({
378 id: role.id,
379 database: database.name,
380 organization: database.organization,
381 branch: "main",
382 });
383 expect(live.id).toEqual(role.id);
384 const roles = yield* ps.listRoles({
385 database: database.name,
386 organization: database.organization,
387 branch: "main",
388 });
389 expect(roles.data.filter((r) => !r.default).map((r) => r.id)).toEqual([role.id]);
390
391 yield* stack.destroy();
392
393 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
394 }).pipe(logLevel),
395 5_000_000,
396 );
397
398 test.provider(
399 "role with replication is created with the attribute and replaced when it changes",
400 (stack) =>
401 Effect.gen(function* () {
402 yield* stack.destroy();
403
404 const { database, role1 } = yield* stack.deploy(
405 Effect.gen(function* () {
406 const database = yield* Planetscale.PostgresDatabase("Database", {
407 clusterSize: "PS_10",
408 arch: "arm",
409 });
410 const role1 = yield* Planetscale.PostgresRole("RoleReplication", {
411 database,
412 inheritedRoles: ["postgres"],
413 withReplication: true,
414 });
415
416 return { database, role1 };
417 }),
418 );
419
420 expect(role1).toMatchObject({
421 id: expect.any(String),
422 inheritedRoles: ["postgres"],
423 withReplication: true,
424 });
425
426 const { role2 } = yield* stack.deploy(
427 Effect.gen(function* () {
428 const database = yield* Planetscale.PostgresDatabase("Database", {
429 clusterSize: "PS_10",
430 arch: "arm",
431 });
432 const role2 = yield* Planetscale.PostgresRole("RoleReplication", {
433 database,
434 inheritedRoles: ["postgres"],
435 });
436
437 return { role2 };
438 }),
439 );
440
441 expect(role2).toMatchObject({ withReplication: false });
442 expect(role2.id).not.toEqual(role1.id);
443
444 yield* stack.destroy();
445
446 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
447 }).pipe(logLevel),
448 5_000_000,
449 );
450
451 test.provider(
452 "role gets replaced when properties change",
453 (stack) =>
454 Effect.gen(function* () {
455 yield* stack.destroy();
456
457 const { database, role1 } = yield* stack.deploy(
458 Effect.gen(function* () {
459 const database = yield* Planetscale.PostgresDatabase("Database", {
460 clusterSize: "PS_10",
461 arch: "arm",
462 });
463 const role1 = yield* Planetscale.PostgresRole("RoleReplace", {
464 database: database,
465 inheritedRoles: ["pg_read_all_data"],
466 ttl: 3600,
467 });
468
469 return { database, role1 };
470 }),
471 );
472
473 expect(role1).toMatchObject({
474 id: expect.any(String),
475 name: expect.any(String),
476 inheritedRoles: ["pg_read_all_data"],
477 });
478
479 const originalId = role1.id;
480 const originalName = role1.name;
481
482 const { role2 } = yield* stack.deploy(
483 Effect.gen(function* () {
484 const database = yield* Planetscale.PostgresDatabase("Database", {
485 clusterSize: "PS_10",
486 arch: "arm",
487 });
488 const role2 = yield* Planetscale.PostgresRole("RoleReplace", {
489 database: database,
490 inheritedRoles: ["postgres"],
491 ttl: 7200,
492 });
493
494 return { role2 };
495 }),
496 );
497
498 expect(role2).toMatchObject({
499 id: expect.any(String),
500 name: expect.any(String),
501 inheritedRoles: ["postgres"],
502 });
503
504 expect(role2.id).not.toEqual(originalId);
505 expect(role2.name).not.toEqual(originalName);
506
507 yield* stack.destroy();
508
509 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
510 }).pipe(logLevel),
511 5_000_000,
512 );
513
514 test.provider(
515 "role with RemovalPolicy.retain(true) should not be deleted via API",
516 (stack) =>
517 Effect.gen(function* () {
518 yield* stack.destroy();
519
520 const { organization } = yield* yield* Planetscale.Credentials;
521
522 const { database, role } = yield* stack.deploy(
523 Effect.gen(function* () {
524 const database = yield* Planetscale.PostgresDatabase("Database", {
525 clusterSize: "PS_10",
526 arch: "arm",
527 }).pipe(RemovalPolicy.retain(true));
528 const role = yield* Planetscale.PostgresRole("RoleRetainRemoval", {
529 database,
530 inheritedRoles: ["postgres"],
531 }).pipe(RemovalPolicy.retain(true));
532
533 return { database, role };
534 }),
535 );
536
537 expect(role).toMatchObject({
538 id: expect.any(String),
539 name: expect.any(String),
540 database: database.name,
541 inheritedRoles: ["postgres"],
542 });
543
544 yield* stack.destroy();
545
546 const liveRole = yield* ps
547 .getRole({ organization, database: database.name, branch: "main", id: role.id })
548 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
549
550 expect(liveRole).toBeDefined();
551 expect(liveRole?.id).toEqual(role.id);
552
553 // deleting the db takes care of deleting the role
554 yield* ps
555 .deleteDatabase({ organization, database: database.name })
556 .pipe(Effect.catchTag("NotFound", () => Effect.void));
557
558 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
559 }).pipe(logLevel),
560 5_000_000,
561 );
562
563 test.provider(
564 "role update: successor is updatable without replacement",
565 (stack) =>
566 Effect.gen(function* () {
567 yield* stack.destroy();
568
569 const { database, role1 } = yield* stack.deploy(
570 Effect.gen(function* () {
571 const database = yield* Planetscale.PostgresDatabase("Database", {
572 clusterSize: "PS_10",
573 arch: "arm",
574 });
575 const role1 = yield* Planetscale.PostgresRole("RoleSuccessor", {
576 database,
577 inheritedRoles: ["postgres"],
578 successor: "postgres",
579 });
580
581 return { database, role1 };
582 }),
583 );
584
585 expect(role1).toMatchObject({
586 id: expect.any(String),
587 name: expect.any(String),
588 successor: "postgres",
589 });
590
591 const originalId = role1.id;
592
593 const { role2 } = yield* stack.deploy(
594 Effect.gen(function* () {
595 const database = yield* Planetscale.PostgresDatabase("Database", {
596 clusterSize: "PS_10",
597 arch: "arm",
598 });
599 const role2 = yield* Planetscale.PostgresRole("RoleSuccessor", {
600 database,
601 inheritedRoles: ["postgres"],
602 successor: "pg_read_all_data",
603 });
604
605 return { role2 };
606 }),
607 );
608
609 expect(role2).toMatchObject({ id: originalId, successor: "pg_read_all_data" });
610
611 expect(role2.id).toEqual(originalId);
612
613 yield* stack.destroy();
614
615 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
616 }).pipe(logLevel),
617 5_000_000,
618 );
619
620 test.provider(
621 "role with custom branch",
622 (stack) =>
623 Effect.gen(function* () {
624 yield* stack.destroy();
625
626 const { database, branch, role } = yield* stack.deploy(
627 Effect.gen(function* () {
628 const database = yield* Planetscale.PostgresDatabase("Database", {
629 clusterSize: "PS_10",
630 arch: "arm",
631 });
632
633 const branch = yield* Planetscale.PostgresBranch("CustomBranch", { database });
634
635 const role = yield* Planetscale.PostgresRole("RoleCustomBranch", {
636 database,
637 branch,
638 inheritedRoles: ["postgres"],
639 });
640
641 return { database, branch, role };
642 }),
643 );
644
645 expect(role).toMatchObject({
646 id: expect.any(String),
647 name: expect.any(String),
648 database: database.name,
649 branch: branch.name,
650 inheritedRoles: ["postgres"],
651 });
652
653 yield* stack.destroy();
654
655 yield* waitForDatabaseToBeDeleted(database.name, database.organization);
656 }).pipe(logLevel),
657 5_000_000,
658 );
659 },
660 );
661 const waitForDatabaseToBeDeleted = Effect.fn(function* (database: string, organization: string) {
662 yield* ps.getDatabase({ organization, database }).pipe(
663 Effect.flatMap(() => Effect.fail(new DatabaseStillExists())),
664 Effect.retry({
665 while: (e): e is DatabaseStillExists => e instanceof DatabaseStillExists,
666 schedule: Schedule.exponential(100),
667 }),
668 Effect.catchTag("NotFound", () => Effect.void),
669 );
670 });
671
672 class DatabaseStillExists extends Data.TaggedError("DatabaseStillExists") {}