1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as cloudbuild from "@distilled.cloud/gcp/cloudbuild_v2";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8
9const { test } = Test.make({ providers: GCP.providers() });
10
11const logLevel = Effect.provideService(
12 MinimumLogLevel,
13 process.env.DEBUG ? "Debug" : "Info",
14);
15
16const waitUntilGone = (name: string) =>
17 cloudbuild.getProjectsLocationsConnections({ name }).pipe(
18 Effect.as("found" as const),
19 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
20 Effect.repeat({
21 schedule: Schedule.spaced("1 second"),
22 until: (status) => status === "gone",
23 times: 10,
24 }),
25 );
26
27test.provider(
28 "create, update, replace, and delete a connection",
29 (stack) =>
30 Effect.gen(function* () {
31 yield* stack.destroy();
32
33 const created = yield* stack.deploy(
34 Effect.gen(function* () {
35 return yield* GCP.CloudBuild.Connection("Github", {
36 location: "us-central1",
37 annotations: { env: "test" },
38 githubConfig: {},
39 });
40 }),
41 );
42
43 expect(created.connectionId).toEqual(expect.any(String));
44 expect(created.name).toContain("/connections/");
45 expect(created.location).toEqual("us-central1");
46 expect(created.disabled).toEqual(false);
47 expect(created.annotations).toMatchObject({ env: "test" });
48 expect(created.githubConfig).toEqual(expect.any(Object));
49 expect(created.installationState?.stage).toEqual(expect.any(String));
50
51 const fetched = yield* cloudbuild.getProjectsLocationsConnections({
52 name: created.name,
53 });
54 expect(fetched.name).toEqual(created.name);
55 expect(fetched.annotations?.env).toEqual("test");
56 expect(fetched.annotations?.["alchemy-id"]).toEqual(expect.any(String));
57 expect(fetched.disabled ?? false).toEqual(false);
58 expect(fetched.githubConfig).toEqual(expect.any(Object));
59
60 const listed = yield* cloudbuild.listProjectsLocationsConnections({
61 parent: `projects/${created.project}/locations/${created.location}`,
62 });
63 expect(
64 (listed.connections ?? []).some(
65 (connection) => connection.name === created.name,
66 ),
67 ).toEqual(true);
68
69 const updated = yield* stack.deploy(
70 Effect.gen(function* () {
71 return yield* GCP.CloudBuild.Connection("Github", {
72 connectionId: created.connectionId,
73 location: "us-central1",
74 annotations: { env: "prod", role: "scm" },
75 disabled: true,
76 githubConfig: {},
77 });
78 }),
79 );
80
81 expect(updated.name).toEqual(created.name);
82 expect(updated.connectionId).toEqual(created.connectionId);
83 expect(updated.disabled).toEqual(true);
84 expect(updated.annotations).toMatchObject({ env: "prod", role: "scm" });
85
86 const fetchedUpdate = yield* cloudbuild.getProjectsLocationsConnections({
87 name: updated.name,
88 });
89 expect(fetchedUpdate.disabled).toEqual(true);
90 expect(fetchedUpdate.annotations?.env).toEqual("prod");
91 expect(fetchedUpdate.annotations?.role).toEqual("scm");
92
93 const replaced = yield* stack.deploy(
94 Effect.gen(function* () {
95 return yield* GCP.CloudBuild.Connection("Github", {
96 connectionId: created.connectionId,
97 location: "us-east1",
98 annotations: { env: "test" },
99 githubConfig: {},
100 });
101 }),
102 );
103
104 expect(replaced.connectionId).toEqual(created.connectionId);
105 expect(replaced.location).toEqual("us-east1");
106 expect(replaced.name).toContain("/locations/us-east1/");
107 expect(replaced.name).not.toEqual(created.name);
108 expect(replaced.disabled).toEqual(false);
109
110 const oldGone = yield* waitUntilGone(created.name);
111 expect(oldGone).toEqual("gone");
112
113 const fetchedReplace = yield* cloudbuild.getProjectsLocationsConnections({
114 name: replaced.name,
115 });
116 expect(fetchedReplace.name).toEqual(replaced.name);
117 expect(fetchedReplace.disabled ?? false).toEqual(false);
118 expect(fetchedReplace.annotations?.env).toEqual("test");
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:cloudbuild", "live"],
127 timeout: 90_000,
128 },
129);