1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as clouddeploy from "@distilled.cloud/gcp/clouddeploy_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
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 clouddeploy.getProjectsLocationsDeliveryPipelines({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("2 seconds"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsDeliveryPipelines on a missing pipeline fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 clouddeploy.getProjectsLocationsDeliveryPipelines({
38 name: `projects/${project}/locations/us-central1/deliveryPipelines/alchemy-missing-pipeline`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel),
45 {
46 tags: ["provider:gcp", "provider:gcp:clouddeploy", "live"],
47 timeout: 90_000,
48 },
49);
50
51test.provider(
52 "create, update, and delete a delivery pipeline",
53 (stack) =>
54 Effect.gen(function* () {
55 const { project } = yield* GcpEnvironment.current;
56
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 const target = yield* GCP.CloudDeploy.Target("Staging", {
62 run: {
63 location: `projects/${project}/locations/us-central1`,
64 },
65 labels: { env: "test" },
66 });
67 const pipeline = yield* GCP.CloudDeploy.DeliveryPipeline("App", {
68 serialPipeline: { stages: [{ targetId: target.targetId }] },
69 description: "alchemy-test-pipeline",
70 labels: { env: "test" },
71 });
72 return { target, pipeline };
73 }),
74 );
75
76 expect(created.pipeline.name).toContain("/deliveryPipelines/");
77 expect(created.pipeline.deliveryPipelineId).toEqual(expect.any(String));
78 expect(created.pipeline.location).toEqual("us-central1");
79 expect(created.pipeline.description).toEqual("alchemy-test-pipeline");
80 expect(created.pipeline.serialPipeline?.stages?.[0]?.targetId).toEqual(
81 created.target.targetId,
82 );
83 expect(created.pipeline.labels).toMatchObject({ env: "test" });
84 expect(created.pipeline.suspended).toEqual(false);
85
86 const fetched = yield* clouddeploy.getProjectsLocationsDeliveryPipelines({
87 name: created.pipeline.name,
88 });
89 expect(fetched.name).toEqual(created.pipeline.name);
90 expect(fetched.description).toEqual("alchemy-test-pipeline");
91 expect(fetched.labels?.env).toEqual("test");
92
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 const target = yield* GCP.CloudDeploy.Target("Staging", {
96 targetId: created.target.targetId,
97 run: {
98 location: `projects/${project}/locations/us-central1`,
99 },
100 labels: { env: "test" },
101 });
102 const pipeline = yield* GCP.CloudDeploy.DeliveryPipeline("App", {
103 deliveryPipelineId: created.pipeline.deliveryPipelineId,
104 serialPipeline: { stages: [{ targetId: target.targetId }] },
105 description: "alchemy-prod-pipeline",
106 labels: { env: "prod", role: "deploy" },
107 });
108 return pipeline;
109 }),
110 );
111
112 expect(updated.name).toEqual(created.pipeline.name);
113 expect(updated.description).toEqual("alchemy-prod-pipeline");
114 expect(updated.labels).toMatchObject({ env: "prod", role: "deploy" });
115
116 yield* stack.destroy();
117
118 const gone = yield* waitUntilGone(created.pipeline.name);
119 expect(gone).toEqual("gone");
120 }).pipe(logLevel),
121 {
122 tags: ["provider:gcp", "provider:gcp:clouddeploy", "live"],
123 timeout: 90_000,
124 },
125);