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

main
ts 143 lines 5 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 observability from "@distilled.cloud/gcp/observability_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 waitUntilGone = (name: string) =>
15 observability.getProjectsLocationsBucketsDatasetsLinks({ name }).pipe(
16 Effect.as("found" as const),
17 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
18 Effect.repeat({
19 schedule: Schedule.spaced("2 seconds"),
20 until: (status) => status === "gone",
21 times: 10,
22 }),
23 );
24
25 // The `_Trace` bucket and its `Spans` dataset only exist once the project
26 // stores trace data in Observability; until then create fails with
27 // BadRequest ("Unable to create link; parent bucket of … is not found").
28 // Set GCP_TEST_OBSERVABILITY_DATASET to an existing dataset name.
29 const spansDataset = process.env.GCP_TEST_OBSERVABILITY_DATASET;
30
31 test.provider(
32 "getProjectsLocationsBucketsDatasetsLinks on a missing link fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36 const defaultDataset = `projects/${project}/locations/us-central1/buckets/_Trace/datasets/Spans`;
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 observability.getProjectsLocationsBucketsDatasetsLinks({
41 name: `${defaultDataset}/links/alchemy-missing-link`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel),
48 {
49 tags: ["provider:gcp", "provider:gcp:observability", "live"],
50 timeout: 90_000,
51 },
52 );
53
54 test.provider.skipIf(!!spansDataset)(
55 "createProjectsLocationsBucketsDatasetsLinks without trace storage fails with BadRequest",
56 (stack) =>
57 Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59 yield* stack.destroy();
60
61 const error = yield* Effect.flip(
62 observability.createProjectsLocationsBucketsDatasetsLinks({
63 parent: `projects/${project}/locations/us-central1/buckets/_Trace/datasets/Spans`,
64 linkId: "alchemy_probe_link",
65 body: {},
66 }),
67 );
68 expect(error._tag).toEqual("BadRequest");
69 expect(error.message).toContain("parent bucket");
70
71 yield* stack.destroy();
72 }).pipe(logLevel),
73 {
74 tags: ["provider:gcp", "provider:gcp:observability", "live"],
75 timeout: 90_000,
76 },
77 );
78
79 test.provider.skipIf(!spansDataset)(
80 "create, update, and delete an observability dataset link",
81 (stack) =>
82 Effect.gen(function* () {
83 const { project } = yield* GcpEnvironment.current;
84 yield* stack.destroy();
85
86 const dataset = spansDataset!;
87
88 const created = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* GCP.Observability.BucketsDatasetsLink("Analytics", {
91 dataset,
92 description: "bigquery analytics",
93 displayName: "Trace analytics",
94 });
95 }),
96 );
97
98 expect(created.linkId).toEqual(expect.any(String));
99 expect(created.linkId).toMatch(/^[a-z][a-z0-9_]*$/);
100 expect(created.dataset).toEqual(dataset);
101 expect(created.project).toEqual(project);
102 expect(created.name).toEqual(`${dataset}/links/${created.linkId}`);
103 expect(created.description).toEqual("bigquery analytics");
104 expect(created.displayName).toEqual("Trace analytics");
105
106 const fetched = yield* observability.getProjectsLocationsBucketsDatasetsLinks({
107 name: created.name,
108 });
109 expect(fetched.name).toEqual(created.name);
110 expect(fetched.description).toContain("alchemy-id=");
111 expect(fetched.description).toContain("bigquery analytics");
112
113 const updated = yield* stack.deploy(
114 Effect.gen(function* () {
115 return yield* GCP.Observability.BucketsDatasetsLink("Analytics", {
116 dataset,
117 linkId: created.linkId,
118 description: "updated analytics",
119 displayName: "Updated traces",
120 });
121 }),
122 );
123
124 expect(updated.name).toEqual(created.name);
125 expect(updated.description).toEqual("updated analytics");
126 expect(updated.displayName).toEqual("Updated traces");
127
128 const fetchedUpdate = yield* observability.getProjectsLocationsBucketsDatasetsLinks({
129 name: created.name,
130 });
131 expect(fetchedUpdate.description).toContain("updated analytics");
132 expect(fetchedUpdate.displayName).toEqual("Updated traces");
133
134 yield* stack.destroy();
135
136 const gone = yield* waitUntilGone(created.name);
137 expect(gone).toEqual("gone");
138 }).pipe(logLevel),
139 {
140 tags: ["provider:gcp", "provider:gcp:observability", "live"],
141 timeout: 120_000,
142 },
143 );