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

main
ts 136 lines 4.79 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 bigtable from "@distilled.cloud/gcp/bigtableadmin_v2";
6 import * as Output from "alchemy/Output";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Lifecycles provision a Bigtable instance; skipped with --fast.
16 const runLifecycle = !process.env.FAST;
17
18 const waitUntilGone = (name: string) =>
19 bigtable.getProjectsInstancesLogicalViews({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("2 seconds"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getProjectsInstancesLogicalViews on a missing instance fails with NotFound",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34
35 yield* stack.destroy();
36
37 const error = yield* Effect.flip(
38 bigtable.getProjectsInstancesLogicalViews({
39 name: `projects/${project}/instances/alchemybtmissing/logicalViews/missing`,
40 }),
41 );
42 expect(error._tag).toEqual("NotFound");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 90_000 },
47 );
48
49 test.provider.skipIf(!runLifecycle)(
50 "create, update, and delete a bigtable logical view",
51 (stack) =>
52 Effect.gen(function* () {
53 yield* stack.destroy();
54
55 const created = yield* stack.deploy(
56 Effect.gen(function* () {
57 const instance = yield* GCP.Bigtable.Instance("Data", {
58 displayName: "alchemy-test-bt-logical",
59 type: "PRODUCTION",
60 clusters: {
61 cluster: {
62 location: "us-central1-b",
63 serveNodes: 1,
64 defaultStorageType: "HDD",
65 },
66 },
67 });
68 const table = yield* GCP.Bigtable.Table("Users", {
69 instance: instance.name,
70 tableId: "users",
71 columnFamilies: { cf: { gcRule: { maxNumVersions: 1 } } },
72 });
73 const view = yield* GCP.Bigtable.InstancesLogicalView("Active", {
74 instance: instance.name,
75 query: Output.interpolate`SELECT _key FROM \`${table.tableId}\``,
76 });
77 return { instance, table, view };
78 }),
79 );
80
81 expect(created.view.name).toContain("/logicalViews/");
82 expect(created.view.logicalViewId).toEqual(expect.any(String));
83 expect(created.view.instance).toEqual(created.instance.name);
84 expect(created.view.query).toContain("users");
85 expect(created.view.deletionProtection).toEqual(false);
86
87 const fetched = yield* bigtable.getProjectsInstancesLogicalViews({
88 name: created.view.name,
89 });
90 expect(fetched.name).toEqual(created.view.name);
91 expect(fetched.query).toContain("users");
92
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 const instance = yield* GCP.Bigtable.Instance("Data", {
96 instanceId: created.instance.instanceId,
97 displayName: "alchemy-test-bt-logical",
98 type: "PRODUCTION",
99 clusters: {
100 cluster: {
101 location: "us-central1-b",
102 serveNodes: 1,
103 defaultStorageType: "HDD",
104 },
105 },
106 });
107 const table = yield* GCP.Bigtable.Table("Users", {
108 instance: instance.name,
109 tableId: "users",
110 columnFamilies: { cf: { gcRule: { maxNumVersions: 1 } } },
111 });
112 const view = yield* GCP.Bigtable.InstancesLogicalView("Active", {
113 instance: instance.name,
114 logicalViewId: created.view.logicalViewId,
115 query: Output.interpolate`SELECT _key, cf FROM \`${table.tableId}\``,
116 deletionProtection: true,
117 });
118 return { instance, table, view };
119 }),
120 );
121
122 expect(updated.view.name).toEqual(created.view.name);
123 expect(updated.view.deletionProtection).toEqual(true);
124
125 const refetched = yield* bigtable.getProjectsInstancesLogicalViews({
126 name: created.view.name,
127 });
128 expect(refetched.deletionProtection).toEqual(true);
129
130 yield* stack.destroy();
131
132 const gone = yield* waitUntilGone(created.view.name);
133 expect(gone).toEqual("gone");
134 }).pipe(logLevel),
135 { tags: ["provider:gcp", "provider:gcp:bigtable", "live"], timeout: 120_000 },
136 );