1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as developerconnect from "@distilled.cloud/gcp/developerconnect_v1";
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 developerconnect.getProjectsLocationsInsightsConfigs({ 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 "getProjectsLocationsInsightsConfigs on a missing config fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 developerconnect.getProjectsLocationsInsightsConfigs({
37 name: `projects/${project}/locations/us-central1/insightsConfigs/alchemy-missing-insights`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:developerconnect", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider(
51 "create, update, replace, and delete an insights config",
52 (stack) =>
53 Effect.gen(function* () {
54 const { project } = yield* GcpEnvironment.current;
55 yield* stack.destroy();
56
57 const created = yield* stack.deploy(
58 Effect.gen(function* () {
59 return yield* GCP.DeveloperConnect.InsightsConfig("Sdlc", {
60 location: "us-central1",
61 projects: { projectIds: [project] },
62 labels: { env: "test" },
63 });
64 }),
65 );
66
67 expect(created.insightsConfigId).toEqual(expect.any(String));
68 expect(created.name).toContain("/insightsConfigs/");
69 expect(created.location).toEqual("us-central1");
70 expect(created.labels).toMatchObject({ env: "test" });
71
72 const fetched =
73 yield* developerconnect.getProjectsLocationsInsightsConfigs({
74 name: created.name,
75 });
76 expect(fetched.name).toEqual(created.name);
77 expect(fetched.labels?.env).toEqual("test");
78 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
79
80 const updated = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* GCP.DeveloperConnect.InsightsConfig("Sdlc", {
83 insightsConfigId: created.insightsConfigId,
84 location: "us-central1",
85 projects: { projectIds: [project] },
86 labels: { env: "prod", role: "sdlc" },
87 });
88 }),
89 );
90
91 expect(updated.name).toEqual(created.name);
92 expect(updated.labels).toMatchObject({ env: "prod", role: "sdlc" });
93
94 const fetchedUpdate =
95 yield* developerconnect.getProjectsLocationsInsightsConfigs({
96 name: updated.name,
97 });
98 expect(fetchedUpdate.labels?.env).toEqual("prod");
99 expect(fetchedUpdate.labels?.role).toEqual("sdlc");
100
101 const replaced = yield* stack.deploy(
102 Effect.gen(function* () {
103 return yield* GCP.DeveloperConnect.InsightsConfig("Sdlc", {
104 insightsConfigId: created.insightsConfigId,
105 location: "us-east1",
106 projects: { projectIds: [project] },
107 labels: { env: "test" },
108 });
109 }),
110 );
111
112 expect(replaced.insightsConfigId).toEqual(created.insightsConfigId);
113 expect(replaced.location).toEqual("us-east1");
114 expect(replaced.name).toContain("/locations/us-east1/");
115 expect(replaced.name).not.toEqual(created.name);
116
117 const oldGone = yield* waitUntilGone(created.name);
118 expect(oldGone).toEqual("gone");
119
120 yield* stack.destroy();
121
122 const gone = yield* waitUntilGone(replaced.name);
123 expect(gone).toEqual("gone");
124 }).pipe(logLevel),
125 {
126 tags: ["provider:gcp", "provider:gcp:developerconnect", "live"],
127 timeout: 300_000,
128 },
129);