The best way to control Claude Code, Codex and more from any computer or phone

main
ts 131 lines 4.44 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as sqladmin from "@distilled.cloud/gcp/sqladmin_v1";
6 import type { StackServices } from "alchemy/Stack";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import { MinimumLogLevel } from "effect/References";
10 import * as Schedule from "effect/Schedule";
11
12 const { test } = Test.make({
13 providers: GCP.providers() as Layer.Layer<GCP.ProviderRequirements, never, StackServices>,
14 });
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 // Runs against an existing Cloud SQL instance (creating one takes well over
19 // 5 minutes); set GCP_SQL_INSTANCE to its name.
20 const sqlInstance = process.env.GCP_SQL_INSTANCE || process.env.GCP_TEST_SQL_INSTANCE;
21 const runLifecycle = !!sqlInstance && !process.env.FAST;
22
23 const waitUntilGone = (name: string) =>
24 sqladmin.getBackupBackups({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 // Cloud SQL answers a deleted backup uid with this 403.
28 Effect.catchTag("SqlBackupAccessDenied", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("2 seconds"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36 test.provider(
37 "getBackupBackups on a missing backup fails with SqlBackupAccessDenied",
38 (stack) =>
39 Effect.gen(function* () {
40 const { project } = yield* GcpEnvironment.current;
41 yield* stack.destroy();
42
43 const error = yield* Effect.flip(
44 sqladmin.getBackupBackups({
45 name: `projects/${project}/backups/00000000-0000-0000-0000-000000000000`,
46 }),
47 );
48 // Cloud SQL hides unknown backups behind 403 rather than 404.
49 expect(error._tag).toBe("SqlBackupAccessDenied");
50
51 yield* stack.destroy();
52 }).pipe(logLevel),
53 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
54 );
55
56 test.provider(
57 "lists sql backups",
58 (stack) =>
59 Effect.gen(function* () {
60 const { project } = yield* GcpEnvironment.current;
61 yield* stack.destroy();
62
63 const page = yield* sqladmin.listBackupsBackups({
64 parent: `projects/${project}`,
65 pageSize: 10,
66 });
67 expect((page.backups ?? []).map((backup) => backup.name)).not.toContain(
68 `projects/${project}/backups/00000000-0000-0000-0000-000000000000`,
69 );
70
71 yield* stack.destroy();
72 }).pipe(logLevel),
73 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 90_000 },
74 );
75
76 test.provider.skipIf(!runLifecycle)(
77 "create, update, and delete a sql backup",
78 (stack) =>
79 Effect.gen(function* () {
80 const { project } = yield* GcpEnvironment.current;
81 yield* stack.destroy();
82
83 const instance = sqlInstance!;
84
85 const created = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.SQL.Backup("Nightly", {
88 instance,
89 description: "alchemy-on-demand",
90 });
91 }),
92 );
93
94 expect(created.backupId).toEqual(expect.any(String));
95 expect(created.backupId.length).toBeGreaterThan(0);
96 expect(created.instance).toEqual(instance);
97 expect(created.project).toEqual(project);
98 expect(created.name).toEqual(`projects/${project}/backups/${created.backupId}`);
99 expect(created.description).toEqual("alchemy-on-demand");
100
101 const fetched = yield* sqladmin.getBackupBackups({
102 name: created.name,
103 });
104 expect(fetched.name).toEqual(created.name);
105 expect(fetched.instance).toEqual(instance);
106 expect(fetched.description).toContain("alchemy-on-demand");
107
108 const updated = yield* stack.deploy(
109 Effect.gen(function* () {
110 return yield* GCP.SQL.Backup("Nightly", {
111 instance,
112 description: "alchemy-on-demand",
113 });
114 }),
115 );
116
117 expect(updated.backupId).toEqual(created.backupId);
118 expect(updated.name).toEqual(created.name);
119
120 const refetched = yield* sqladmin.getBackupBackups({
121 name: updated.name,
122 });
123 expect(refetched.name).toEqual(updated.name);
124
125 yield* stack.destroy();
126
127 const gone = yield* waitUntilGone(created.name);
128 expect(gone).toEqual("gone");
129 }).pipe(logLevel),
130 { tags: ["provider:gcp", "provider:gcp:sql", "live"], timeout: 120_000 },
131 );