@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.52 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 monitoring from "@distilled.cloud/gcp/monitoring_v3";
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 = (name: string) =>
14 monitoring.getProjectsMetricDescriptors({ name }).pipe(
15 Effect.as("found" as const),
16 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
17 Effect.repeat({
18 schedule: Schedule.spaced("1 second"),
19 until: (status) => status === "gone",
20 times: 10,
21 }),
22 );
23
24 test.provider(
25 "create, update, replace, and delete a metric descriptor",
26 (stack) =>
27 Effect.gen(function* () {
28 yield* stack.destroy();
29
30 const created = yield* stack.deploy(
31 Effect.gen(function* () {
32 return yield* GCP.Monitoring.MetricDescriptor("Paid", {
33 displayName: "Invoice paid amount",
34 description: "amount collected per invoice",
35 labels: [{ key: "currency", valueType: "STRING" }],
36 });
37 }),
38 );
39
40 expect(created.name).toContain("/metricDescriptors/");
41 expect(created.type.startsWith("custom.googleapis.com/")).toEqual(true);
42 expect(created.metricKind).toEqual("GAUGE");
43 expect(created.valueType).toEqual("DOUBLE");
44 expect(created.unit).toEqual("1");
45 expect(created.displayName).toEqual("Invoice paid amount");
46 expect(created.description).toEqual("amount collected per invoice");
47 expect(created.labels.some((label) => label.key === "currency")).toEqual(true);
48
49 const fetched = yield* monitoring
50 .getProjectsMetricDescriptors({
51 name: created.name,
52 })
53 .pipe(
54 Effect.retry({
55 times: 10,
56 schedule: Schedule.exponential("200 millis"),
57 while: (error) => error._tag === "NotFound",
58 }),
59 );
60 expect(fetched.type).toEqual(created.type);
61 expect(fetched.metricKind).toEqual("GAUGE");
62 expect(fetched.description).toContain("alchemy-id=");
63 expect(fetched.description).toContain("amount collected per invoice");
64
65 const updated = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* GCP.Monitoring.MetricDescriptor("Paid", {
68 type: created.type,
69 displayName: "Invoice paid (cents)",
70 description: "amount collected per invoice in cents",
71 unit: "1",
72 labels: [
73 { key: "currency", valueType: "STRING" },
74 { key: "source", valueType: "STRING" },
75 ],
76 });
77 }),
78 );
79
80 expect(updated.name).toEqual(created.name);
81 expect(updated.type).toEqual(created.type);
82 expect(updated.displayName).toEqual("Invoice paid (cents)");
83 expect(updated.description).toEqual("amount collected per invoice in cents");
84 expect(updated.labels.map((label) => label.key).sort()).toEqual(
85 ["currency", "source"].sort(),
86 );
87
88 const fetchedUpdate = yield* monitoring
89 .getProjectsMetricDescriptors({
90 name: updated.name,
91 })
92 .pipe(
93 Effect.repeat({
94 schedule: Schedule.spaced("500 millis"),
95 until: (descriptor) =>
96 descriptor.displayName === "Invoice paid (cents)" &&
97 (descriptor.description ?? "").includes("in cents"),
98 times: 10,
99 }),
100 );
101 expect(fetchedUpdate.displayName).toEqual("Invoice paid (cents)");
102 expect(fetchedUpdate.description).toContain("in cents");
103
104 const replaced = yield* stack.deploy(
105 Effect.gen(function* () {
106 return yield* GCP.Monitoring.MetricDescriptor("Paid", {
107 metricKind: "CUMULATIVE",
108 valueType: "INT64",
109 displayName: "Invoice paid count",
110 description: "count of paid invoices",
111 });
112 }),
113 );
114
115 expect(replaced.metricKind).toEqual("CUMULATIVE");
116 expect(replaced.valueType).toEqual("INT64");
117 expect(replaced.description).toEqual("count of paid invoices");
118
119 yield* stack.destroy();
120
121 const gone = yield* waitUntilGone(replaced.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 {
125 tags: ["provider:gcp", "provider:gcp:monitoring", "live"],
126 timeout: 90_000,
127 },
128 );