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

main
ts 147 lines 5.37 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, datasetId: string, routineId: string) =>
14 bigquery
15 .getRoutines({
16 projectId,
17 datasetId,
18 routineId,
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, update, and delete a routine",
32 (stack) =>
33 Effect.gen(function* () {
34 yield* stack.destroy();
35
36 const created = yield* stack.deploy(
37 Effect.gen(function* () {
38 const dataset = yield* GCP.BigQuery.Dataset("Analytics", {
39 location: "US-CENTRAL1",
40 forceDestroy: true,
41 });
42 return yield* GCP.BigQuery.Routine("Triple", {
43 datasetId: dataset.datasetId,
44 routineType: "SCALAR_FUNCTION",
45 language: "SQL",
46 definitionBody: "x * 3",
47 arguments: [{ name: "x", dataType: { typeKind: "INT64" } }],
48 returnType: { typeKind: "INT64" },
49 description: "multiply by three",
50 });
51 }),
52 );
53
54 expect(created.routineId).toEqual(expect.any(String));
55 expect(created.routineId).toMatch(/^[a-zA-Z0-9_]+$/);
56 expect(created.datasetId).toEqual(expect.any(String));
57 expect(created.project).toEqual(expect.any(String));
58 expect(created.routineType).toEqual("SCALAR_FUNCTION");
59 expect(created.language).toEqual("SQL");
60 expect(created.definitionBody).toEqual("x * 3");
61 expect(created.description).toEqual("multiply by three");
62 expect(created.returnType).toMatchObject({ typeKind: "INT64" });
63 expect(created.arguments).toEqual(
64 expect.arrayContaining([
65 expect.objectContaining({
66 name: "x",
67 dataType: expect.objectContaining({ typeKind: "INT64" }),
68 }),
69 ]),
70 );
71
72 const fetched = yield* bigquery.getRoutines({
73 projectId: created.project,
74 datasetId: created.datasetId,
75 routineId: created.routineId,
76 });
77 expect(fetched.routineReference?.routineId).toEqual(created.routineId);
78 expect(fetched.routineType).toEqual("SCALAR_FUNCTION");
79 expect(fetched.language).toEqual("SQL");
80 expect(fetched.definitionBody).toEqual("x * 3");
81 expect(fetched.description).toContain("alchemy-id=");
82 expect(fetched.description).toContain("multiply by three");
83 expect(fetched.returnType?.typeKind).toEqual("INT64");
84
85 const queried = yield* bigquery.queryJobs({
86 projectId: created.project,
87 body: {
88 query: `SELECT \`${created.project}.${created.datasetId}.${created.routineId}\`(2) AS n`,
89 useLegacySql: false,
90 location: "US-CENTRAL1",
91 },
92 });
93 expect(queried.jobComplete).toEqual(true);
94 expect(queried.rows?.[0]?.f?.[0]?.v).toEqual("6");
95
96 const updated = yield* stack.deploy(
97 Effect.gen(function* () {
98 const dataset = yield* GCP.BigQuery.Dataset("Analytics", {
99 datasetId: created.datasetId,
100 location: "US-CENTRAL1",
101 forceDestroy: true,
102 });
103 return yield* GCP.BigQuery.Routine("Triple", {
104 datasetId: dataset.datasetId,
105 routineId: created.routineId,
106 routineType: "SCALAR_FUNCTION",
107 language: "SQL",
108 definitionBody: "x * 4",
109 arguments: [{ name: "x", dataType: { typeKind: "INT64" } }],
110 returnType: { typeKind: "INT64" },
111 description: "multiply by four",
112 });
113 }),
114 );
115
116 expect(updated.name).toEqual(created.name);
117 expect(updated.routineId).toEqual(created.routineId);
118 expect(updated.definitionBody).toEqual("x * 4");
119 expect(updated.description).toEqual("multiply by four");
120
121 const fetchedUpdate = yield* bigquery.getRoutines({
122 projectId: created.project,
123 datasetId: created.datasetId,
124 routineId: created.routineId,
125 });
126 expect(fetchedUpdate.definitionBody).toEqual("x * 4");
127 expect(fetchedUpdate.description).toContain("alchemy-id=");
128 expect(fetchedUpdate.description).toContain("multiply by four");
129
130 const queriedUpdate = yield* bigquery.queryJobs({
131 projectId: created.project,
132 body: {
133 query: `SELECT \`${created.project}.${created.datasetId}.${created.routineId}\`(2) AS n`,
134 useLegacySql: false,
135 location: "US-CENTRAL1",
136 },
137 });
138 expect(queriedUpdate.jobComplete).toEqual(true);
139 expect(queriedUpdate.rows?.[0]?.f?.[0]?.v).toEqual("8");
140
141 yield* stack.destroy();
142
143 const gone = yield* waitUntilGone(created.project, created.datasetId, created.routineId);
144 expect(gone).toEqual("gone");
145 }).pipe(logLevel),
146 { tags: ["provider:gcp", "provider:gcp:bigquery", "live"], timeout: 90_000 },
147 );