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

main
ts 171 lines 6 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 { Action } from "alchemy/Action";
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 /** JSON-safe view of a decoded row so Action output round-trips state. */
14 const describeRow = (row: Record<string, unknown>): Record<string, unknown> => ({
15 ...row,
16 at: row.at instanceof Date ? row.at.toISOString() : row.at,
17 payload: row.payload instanceof Uint8Array ? [...row.payload] : row.payload,
18 });
19
20 test.provider(
21 "ReadTable, WriteTable, and ReadWriteTable clients",
22 (stack) =>
23 Effect.gen(function* () {
24 yield* stack.destroy();
25
26 const out = yield* stack.deploy(
27 Effect.gen(function* () {
28 const dataset = yield* GCP.BigQuery.Dataset("Access", {
29 location: "US-CENTRAL1",
30 forceDestroy: true,
31 });
32 const table = yield* GCP.BigQuery.Table("Scores", {
33 datasetId: dataset.datasetId,
34 schema: [
35 { name: "id", type: "STRING", mode: "REQUIRED" },
36 { name: "score", type: "INTEGER" },
37 { name: "ratio", type: "FLOAT" },
38 { name: "ok", type: "BOOLEAN" },
39 { name: "at", type: "TIMESTAMP" },
40 { name: "payload", type: "BYTES" },
41 { name: "tags", type: "STRING", mode: "REPEATED" },
42 {
43 name: "meta",
44 type: "RECORD",
45 fields: [{ name: "k", type: "STRING" }],
46 },
47 ],
48 });
49 const Probe = Action(
50 "Probe",
51 Effect.gen(function* () {
52 const tableId = yield* table.tableId;
53 const reader = yield* GCP.BigQuery.ReadTable(table);
54 const writer = yield* GCP.BigQuery.WriteTable(table);
55 const both = yield* GCP.BigQuery.ReadWriteTable(table);
56 return Effect.fn(function* () {
57 yield* writer.insert(
58 [
59 {
60 id: "a",
61 score: 5,
62 ratio: 0.5,
63 ok: true,
64 at: new Date("2026-01-02T03:04:05.678Z"),
65 payload: new Uint8Array([0, 1, 255]),
66 tags: ["x", "y"],
67 meta: { k: "v" },
68 },
69 { id: "b", score: 20, tags: [] },
70 ],
71 { insertIds: ["a", "b"] },
72 );
73 yield* both.insert([{ id: "c", score: 30, ok: false }]);
74 const rejected = yield* writer.insert([{ id: "bad", score: "not-a-number" }]).pipe(
75 Effect.map(() => "inserted"),
76 Effect.catchTag("GCP.BigQuery.InsertRowsFailed", (error) =>
77 Effect.succeed(`${error.insertErrors.length}:${error.insertErrors[0]?.index}`),
78 ),
79 );
80
81 // Streaming-buffer rows become readable shortly after insert.
82 const listed = yield* reader.list().pipe(
83 Effect.repeat({
84 schedule: Schedule.spaced("2 seconds"),
85 until: (page) => page.rows.length >= 3,
86 times: 30,
87 }),
88 );
89 const page = yield* both.list({ maxResults: 1 });
90
91 const sql = `SELECT id, score FROM \`${yield* tableId}\` WHERE score >= @min AND id != @skip ORDER BY id`;
92 const queried = yield* reader.query(sql, { min: 10, skip: "none" }).pipe(
93 Effect.repeat({
94 schedule: Schedule.spaced("2 seconds"),
95 until: (rows) => rows.length >= 2,
96 times: 30,
97 }),
98 );
99 const typed = yield* both.query(
100 "SELECT @n AS n, @f AS f, @b AS b, @s AS s, CURRENT_TIMESTAMP() IS NOT NULL AS now",
101 { n: 7, f: 1.5, b: true, s: "str" },
102 );
103
104 return {
105 rejected,
106 rows: listed.rows
107 .map(describeRow)
108 .sort((l, r) => String(l.id).localeCompare(String(r.id))),
109 page: page.rows.length,
110 hasNextPage: page.nextPageToken !== undefined,
111 queried,
112 typed,
113 };
114 });
115 }).pipe(
116 Effect.provide(GCP.BigQuery.ReadTableHttp),
117 Effect.provide(GCP.BigQuery.WriteTableHttp),
118 Effect.provide(GCP.BigQuery.ReadWriteTableHttp),
119 ),
120 );
121 return { probe: yield* Probe({}) };
122 }),
123 );
124
125 expect(out.probe).toEqual({
126 rejected: "1:0",
127 rows: [
128 {
129 id: "a",
130 score: 5,
131 ratio: 0.5,
132 ok: true,
133 at: "2026-01-02T03:04:05.678Z",
134 payload: [0, 1, 255],
135 tags: ["x", "y"],
136 meta: { k: "v" },
137 },
138 {
139 id: "b",
140 score: 20,
141 ratio: null,
142 ok: null,
143 at: null,
144 payload: null,
145 tags: [],
146 meta: null,
147 },
148 {
149 id: "c",
150 score: 30,
151 ratio: null,
152 ok: false,
153 at: null,
154 payload: null,
155 tags: [],
156 meta: null,
157 },
158 ],
159 page: 1,
160 hasNextPage: true,
161 queried: [
162 { id: "b", score: 20 },
163 { id: "c", score: 30 },
164 ],
165 typed: [{ n: 7, f: 1.5, b: true, s: "str", now: true }],
166 });
167
168 yield* stack.destroy();
169 }).pipe(logLevel),
170 { tags: ["provider:gcp", "provider:gcp:bigquery", "live"], timeout: 240_000 },
171 );