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.getFoldersLocationsSavedQueries({ 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 folder 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.FolderSavedQuery("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.parent).toEqual(`projects/${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.getFoldersLocationsSavedQueries({
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.FolderSavedQuery("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.displayName).toEqual("warnings and errors");
79 expect(updated.loggingQuery?.filter).toEqual("severity>=WARNING");
80 expect(updated.description).toEqual("warning query");
81
82 const last = created.savedQueryId.at(-1) ?? "a";
83 const nextId = `${created.savedQueryId.slice(0, -1)}${last === "z" ? "0" : "z"}`;
84
85 const replaced = yield* stack.deploy(
86 Effect.gen(function* () {
87 return yield* GCP.Logging.FolderSavedQuery("Errors", {
88 savedQueryId: nextId,
89 displayName: "replaced query",
90 loggingQuery: { filter: "severity>=WARNING" },
91 description: "replaced query",
92 });
93 }),
94 );
95
96 expect(replaced.savedQueryId).not.toEqual(created.savedQueryId);
97 expect(replaced.displayName).toEqual("replaced query");
98
99 const previousGone = yield* waitUntilGone(created.name);
100 expect(previousGone).toEqual("gone");
101
102 yield* stack.destroy();
103
104 const gone = yield* waitUntilGone(replaced.name);
105 expect(gone).toEqual("gone");
106 }).pipe(logLevel),
107 { tags: ["provider:gcp", "provider:gcp:logging", "live"], timeout: 90_000 },
108);