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

main
ts 122 lines 4.43 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 logging from "@distilled.cloud/gcp/logging_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 // Organization-scoped: set GOOGLE_ORGANIZATION_ID when the credentials
12 // administer the organization (the testing service account does not).
13 const organizationId = process.env.GOOGLE_ORGANIZATION_ID?.trim().replace(/^organizations\//, "");
14 const organization = `organizations/${organizationId}`;
15
16 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
17
18 const waitUntilGone = (name: string) =>
19 logging.getOrganizationsLocationsSavedQueries({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("1 second"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider.skipIf(!organizationId)(
30 "getOrganizationsLocationsSavedQueries on a missing query fails with NotFound",
31 (stack) =>
32 Effect.gen(function* () {
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 logging.getOrganizationsLocationsSavedQueries({
37 name: `${organization}/locations/global/savedQueries/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
45 );
46
47 test.provider.skipIf(!organizationId)(
48 "create, update, replace, and delete an organization logging saved query",
49 (stack) =>
50 Effect.gen(function* () {
51 yield* stack.destroy();
52
53 const created = yield* stack.deploy(
54 Effect.gen(function* () {
55 return yield* GCP.Logging.OrganizationSavedQuery("Errors", {
56 organization,
57 displayName: "organization errors",
58 loggingQuery: { filter: "severity>=ERROR" },
59 description: "error query",
60 });
61 }),
62 );
63
64 expect(created.savedQueryId).toEqual(expect.any(String));
65 expect(created.location).toEqual("global");
66 expect(created.organization).toEqual(organization);
67 expect(created.name).toEqual(
68 `${organization}/locations/global/savedQueries/${created.savedQueryId}`,
69 );
70 expect(created.displayName).toEqual("organization errors");
71 expect(created.loggingQuery?.filter).toEqual("severity>=ERROR");
72 expect(created.description).toEqual("error query");
73
74 const fetched = yield* logging.getOrganizationsLocationsSavedQueries({
75 name: created.name,
76 });
77 expect(fetched.displayName).toEqual("organization errors");
78 expect(fetched.description).toContain("alchemy-id=");
79
80 const updated = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* GCP.Logging.OrganizationSavedQuery("Errors", {
83 organization,
84 savedQueryId: created.savedQueryId,
85 displayName: "organization warnings",
86 loggingQuery: { filter: "severity>=WARNING" },
87 description: "warning query",
88 });
89 }),
90 );
91
92 expect(updated.name).toEqual(created.name);
93 expect(updated.displayName).toEqual("organization warnings");
94 expect(updated.loggingQuery?.filter).toEqual("severity>=WARNING");
95
96 const last = created.savedQueryId.at(-1) ?? "a";
97 const nextId = `${created.savedQueryId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
98
99 const replaced = yield* stack.deploy(
100 Effect.gen(function* () {
101 return yield* GCP.Logging.OrganizationSavedQuery("Errors", {
102 organization,
103 savedQueryId: nextId,
104 displayName: "replaced query",
105 loggingQuery: { filter: "severity>=ERROR" },
106 description: "replaced query",
107 });
108 }),
109 );
110
111 expect(replaced.savedQueryId).not.toEqual(created.savedQueryId);
112
113 const previousGone = yield* waitUntilGone(created.name);
114 expect(previousGone).toEqual("gone");
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(replaced.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
122 );