1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as logging from "@distilled.cloud/gcp/logging_v2";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 logging.getProjectsLocationsSavedQueries({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "create, update, replace, and delete a logging saved query",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33
34 yield* stack.destroy();
35
36 const created = yield* stack.deploy(
37 Effect.gen(function* () {
38 return yield* GCP.Logging.SavedQuery("Errors", {
39 displayName: "application errors",
40 loggingQuery: { filter: "severity>=ERROR" },
41 description: "error query",
42 });
43 }),
44 );
45
46 expect(created.savedQueryId).toEqual(expect.any(String));
47 expect(created.location).toEqual("global");
48 expect(created.project).toEqual(project);
49 expect(created.name).toEqual(
50 `projects/${project}/locations/global/savedQueries/${created.savedQueryId}`,
51 );
52 expect(created.displayName).toEqual("application errors");
53 expect(created.visibility).toEqual("PRIVATE");
54 expect(created.loggingQuery?.filter).toEqual("severity>=ERROR");
55 expect(created.description).toEqual("error query");
56
57 const fetched = yield* logging.getProjectsLocationsSavedQueries({
58 name: created.name,
59 });
60 expect(fetched.name).toEqual(created.name);
61 expect(fetched.displayName).toEqual("application errors");
62 expect(fetched.loggingQuery?.filter).toEqual("severity>=ERROR");
63 expect(fetched.description).toContain("alchemy-id=");
64 expect(fetched.description).toContain("error query");
65
66 const updated = yield* stack.deploy(
67 Effect.gen(function* () {
68 return yield* GCP.Logging.SavedQuery("Errors", {
69 savedQueryId: created.savedQueryId,
70 displayName: "warnings and errors",
71 loggingQuery: { filter: "severity>=WARNING" },
72 description: "warning query",
73 });
74 }),
75 );
76
77 expect(updated.name).toEqual(created.name);
78 expect(updated.savedQueryId).toEqual(created.savedQueryId);
79 expect(updated.displayName).toEqual("warnings and errors");
80 expect(updated.loggingQuery?.filter).toEqual("severity>=WARNING");
81 expect(updated.description).toEqual("warning query");
82 expect(updated.createTime).toEqual(created.createTime);
83
84 const fetchedUpdate = yield* logging.getProjectsLocationsSavedQueries({
85 name: created.name,
86 });
87 expect(fetchedUpdate.displayName).toEqual("warnings and errors");
88 expect(fetchedUpdate.loggingQuery?.filter).toEqual("severity>=WARNING");
89 expect(fetchedUpdate.description).toContain("warning query");
90
91 const last = created.savedQueryId.at(-1) ?? "a";
92 const nextSavedQueryId = `${created.savedQueryId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
93
94 const replaced = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.Logging.SavedQuery("Errors", {
97 savedQueryId: nextSavedQueryId,
98 displayName: "replaced query",
99 loggingQuery: { filter: "severity>=WARNING" },
100 description: "replaced query",
101 });
102 }),
103 );
104
105 expect(replaced.savedQueryId).not.toEqual(created.savedQueryId);
106 expect(replaced.name).toEqual(
107 `projects/${project}/locations/global/savedQueries/${replaced.savedQueryId}`,
108 );
109 expect(replaced.displayName).toEqual("replaced query");
110
111 const fetchedReplacement =
112 yield* logging.getProjectsLocationsSavedQueries({
113 name: replaced.name,
114 });
115 expect(fetchedReplacement.name).toEqual(replaced.name);
116
117 const previousGone = yield* waitUntilGone(created.name);
118 expect(previousGone).toEqual("gone");
119
120 yield* stack.destroy();
121
122 const gone = yield* waitUntilGone(replaced.name);
123 expect(gone).toEqual("gone");
124 }).pipe(logLevel),
125 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
126);