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

main
ts 133 lines 4.54 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 looker from "@distilled.cloud/gcp/looker_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const location = "us-central1";
15 const namesOf = (project: string) => {
16 const instanceParent = `projects/${project}/locations/${location}`;
17 const missingInstance = `${instanceParent}/instances/alchemy-missing-looker`;
18 const missingName = `${missingInstance}/backups/alchemy-missing-backup`;
19 return { instanceParent, missingInstance, missingName };
20 };
21
22 // Backups need an ACTIVE Looker instance, which bills hourly. Set
23 // GCP_TEST_LOOKER_INSTANCE to its full resource name to run the lifecycle.
24 const lookerInstance = process.env.GCP_TEST_LOOKER_INSTANCE?.trim();
25
26 const waitUntilGone = (name: string) =>
27 looker.getProjectsLocationsInstancesBackups({ name }).pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.repeat({
31 schedule: Schedule.spaced("2 seconds"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37 test.provider(
38 "getProjectsLocationsInstancesBackups on a missing backup fails with a typed tag",
39 (stack) =>
40 Effect.gen(function* () {
41 const { project } = yield* GcpEnvironment.current;
42 const { missingInstance, missingName } = namesOf(project);
43 yield* stack.destroy();
44
45 const error = yield* Effect.flip(
46 looker.getProjectsLocationsInstancesBackups({ name: missingName }),
47 );
48 expect(error._tag).toEqual("NotFound");
49
50 const listError = yield* Effect.flip(
51 looker.listProjectsLocationsInstancesBackups({
52 parent: missingInstance,
53 pageSize: 10,
54 }),
55 );
56 expect(listError._tag).toEqual("NotFound");
57
58 yield* stack.destroy();
59 }).pipe(logLevel),
60 { tags: ["provider:gcp", "provider:gcp:looker", "live"], timeout: 90_000 },
61 );
62
63 test.provider(
64 "create against a missing Looker instance is rejected with a typed tag",
65 (stack) =>
66 Effect.gen(function* () {
67 const { project } = yield* GcpEnvironment.current;
68 const { missingInstance } = namesOf(project);
69 yield* stack.destroy();
70
71 const error = yield* Effect.flip(
72 stack.deploy(
73 Effect.gen(function* () {
74 return yield* GCP.Looker.InstancesBackup("Nightly", {
75 instance: missingInstance,
76 });
77 }),
78 ),
79 );
80 // "parent resource not found for .../instances/alchemy-missing-looker"
81 expect(error._tag).toEqual("NotFound");
82
83 yield* stack.destroy();
84 }).pipe(logLevel),
85 { tags: ["provider:gcp", "provider:gcp:looker", "live"], timeout: 90_000 },
86 );
87
88 test.provider.skipIf(lookerInstance === undefined || !!process.env.FAST)(
89 "create, refresh, and delete a Looker instance backup",
90 (stack) =>
91 Effect.gen(function* () {
92 const parent = lookerInstance!;
93 yield* stack.destroy();
94
95 const created = yield* stack.deploy(
96 Effect.gen(function* () {
97 return yield* GCP.Looker.InstancesBackup("Nightly", {
98 instance: parent,
99 });
100 }),
101 );
102
103 expect(created.name).toContain("/backups/");
104 expect(created.instance).toEqual(parent);
105 expect(created.location).toEqual(location);
106 expect(created.backupId).toEqual(expect.any(String));
107 expect(created.state).toEqual("ACTIVE");
108
109 const fetched = yield* looker.getProjectsLocationsInstancesBackups({
110 name: created.name,
111 });
112 expect(fetched.name).toEqual(created.name);
113 expect(fetched.state).toEqual("ACTIVE");
114
115 const refreshed = yield* stack.deploy(
116 Effect.gen(function* () {
117 return yield* GCP.Looker.InstancesBackup("Nightly", {
118 instance: parent,
119 backupId: created.backupId,
120 });
121 }),
122 );
123
124 expect(refreshed.name).toEqual(created.name);
125 expect(refreshed.backupId).toEqual(created.backupId);
126 expect(refreshed.state).toEqual("ACTIVE");
127
128 yield* stack.destroy();
129 const gone = yield* waitUntilGone(created.name);
130 expect(gone).toEqual("gone");
131 }).pipe(logLevel),
132 { tags: ["provider:gcp", "provider:gcp:looker", "live"], timeout: 120_000 },
133 );