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.getProjectsLocationsTargets({ 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 "getProjectsLocationsTargets on a missing target 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.getProjectsLocationsTargets({
38 name: `projects/${project}/locations/us-central1/targets/alchemy-missing-target`,
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 Cloud Run target",
53 (stack) =>
54 Effect.gen(function* () {
55 const { project } = yield* GcpEnvironment.current;
56 const runLocation = `projects/${project}/locations/us-central1`;
57
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 return yield* GCP.CloudDeploy.Target("Prod", {
63 run: { location: runLocation },
64 description: "alchemy-test-target",
65 labels: { env: "test" },
66 });
67 }),
68 );
69
70 expect(created.name).toContain("/targets/");
71 expect(created.targetId).toEqual(expect.any(String));
72 expect(created.location).toEqual("us-central1");
73 expect(created.description).toEqual("alchemy-test-target");
74 expect(created.run?.location).toEqual(runLocation);
75 expect(created.requireApproval).toEqual(false);
76 expect(created.labels).toMatchObject({ env: "test" });
77
78 const fetched = yield* clouddeploy.getProjectsLocationsTargets({
79 name: created.name,
80 });
81 expect(fetched.name).toEqual(created.name);
82 expect(fetched.description).toEqual("alchemy-test-target");
83 expect(fetched.labels?.env).toEqual("test");
84 expect(fetched.run?.location).toEqual(runLocation);
85
86 const updated = yield* stack.deploy(
87 Effect.gen(function* () {
88 return yield* GCP.CloudDeploy.Target("Prod", {
89 targetId: created.targetId,
90 run: { location: runLocation },
91 requireApproval: true,
92 description: "alchemy-prod-target",
93 labels: { env: "prod", role: "run" },
94 });
95 }),
96 );
97
98 expect(updated.name).toEqual(created.name);
99 expect(updated.description).toEqual("alchemy-prod-target");
100 expect(updated.requireApproval).toEqual(true);
101 expect(updated.labels).toMatchObject({ env: "prod", role: "run" });
102
103 yield* stack.destroy();
104
105 const gone = yield* waitUntilGone(created.name);
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel),
108 {
109 tags: ["provider:gcp", "provider:gcp:clouddeploy", "live"],
110 timeout: 90_000,
111 },
112);