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

main
ts 101 lines 3.69 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as bigquery from "@distilled.cloud/gcp/bigquery_v2";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const waitUntilGone = (projectId: string, jobId: string, location: string) =>
14 bigquery
15 .getJobs({
16 projectId,
17 jobId,
18 location,
19 })
20 .pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("1 second"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30 test.provider(
31 "create, replace, and delete a query job",
32 (stack) =>
33 Effect.gen(function* () {
34 yield* stack.destroy();
35
36 const created = yield* stack.deploy(
37 Effect.gen(function* () {
38 return yield* GCP.BigQuery.Job("Count", {
39 location: "US-CENTRAL1",
40 labels: { env: "test" },
41 query: { query: "SELECT 1 AS n", useLegacySql: false },
42 });
43 }),
44 );
45
46 expect(created.jobId).toEqual(expect.any(String));
47 expect(created.project).toEqual(expect.any(String));
48 expect(created.location.toUpperCase()).toEqual("US-CENTRAL1");
49 expect(created.labels).toMatchObject({ env: "test" });
50 expect(created.jobType).toEqual("QUERY");
51 expect(created.state).toEqual("DONE");
52 expect(created.errorResult).toBeUndefined();
53 expect(created.query).toEqual("SELECT 1 AS n");
54
55 const fetched = yield* bigquery.getJobs({
56 projectId: created.project,
57 jobId: created.jobId,
58 location: created.location,
59 });
60 expect(fetched.jobReference?.jobId).toEqual(created.jobId);
61 expect(fetched.configuration?.labels?.env).toEqual("test");
62 expect(fetched.configuration?.labels?.["alchemy-id"]).toEqual(expect.any(String));
63 expect(fetched.configuration?.query?.query).toEqual("SELECT 1 AS n");
64 expect(fetched.status?.state).toEqual("DONE");
65
66 const updated = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* GCP.BigQuery.Job("Count", {
69 location: "US-CENTRAL1",
70 labels: { env: "prod", role: "query" },
71 query: { query: "SELECT 2 AS n", useLegacySql: false },
72 });
73 }),
74 );
75
76 expect(updated.jobId).toEqual(expect.any(String));
77 expect(updated.jobId).not.toEqual(created.jobId);
78 expect(updated.labels).toMatchObject({ env: "prod", role: "query" });
79 expect(updated.query).toEqual("SELECT 2 AS n");
80 expect(updated.state).toEqual("DONE");
81 expect(updated.errorResult).toBeUndefined();
82
83 const fetchedUpdate = yield* bigquery.getJobs({
84 projectId: updated.project,
85 jobId: updated.jobId,
86 location: updated.location,
87 });
88 expect(fetchedUpdate.configuration?.labels?.env).toEqual("prod");
89 expect(fetchedUpdate.configuration?.labels?.role).toEqual("query");
90 expect(fetchedUpdate.configuration?.query?.query).toEqual("SELECT 2 AS n");
91
92 const oldGone = yield* waitUntilGone(created.project, created.jobId, created.location);
93 expect(oldGone).toEqual("gone");
94
95 yield* stack.destroy();
96
97 const gone = yield* waitUntilGone(updated.project, updated.jobId, updated.location);
98 expect(gone).toEqual("gone");
99 }).pipe(logLevel),
100 { tags: ["provider:gcp", "provider:gcp:bigquery", "live"], timeout: 90_000 },
101 );