@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

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