1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as ces from "@distilled.cloud/gcp/ces_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 ces.getProjectsLocationsAppsDeployments({ 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 "getProjectsLocationsAppsDeployments on a missing deployment 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 ces.getProjectsLocationsAppsDeployments({
38 name: `projects/${project}/locations/us/apps/missing/deployments/missing`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel),
45 { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 },
46);
47
48test.provider(
49 "create, update, and delete a deployment",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const created = yield* stack.deploy(
55 Effect.gen(function* () {
56 const app = yield* GCP.CES.App("Support", {
57 location: "us",
58 displayName: "support-deployment",
59 });
60 const version = yield* GCP.CES.AppsVersion("V1", { app: app.name });
61 const deployment = yield* GCP.CES.AppsDeployment("Prod", {
62 app: app.name,
63 appVersion: version.name,
64 displayName: "prod",
65 channelProfile: { channelType: "API", profileId: "api" },
66 });
67 return { app, deployment };
68 }),
69 );
70
71 expect(created.deployment.name).toContain("/deployments/");
72 expect(created.deployment.app).toEqual(created.app.name);
73 expect(created.deployment.displayName).toEqual("prod");
74 expect(created.deployment.channelProfile).toBeDefined();
75
76 const fetched = yield* ces.getProjectsLocationsAppsDeployments({
77 name: created.deployment.name,
78 });
79 expect(fetched.name).toEqual(created.deployment.name);
80 expect(fetched.displayName).toContain("alchemy-");
81
82 const updated = yield* stack.deploy(
83 Effect.gen(function* () {
84 const app = yield* GCP.CES.App("Support", {
85 appId: created.app.appId,
86 location: created.app.location,
87 displayName: "support-deployment",
88 });
89 const version = yield* GCP.CES.AppsVersion("V1", { app: app.name });
90 const deployment = yield* GCP.CES.AppsDeployment("Prod", {
91 app: app.name,
92 appVersion: version.name,
93 deploymentId: created.deployment.deploymentId,
94 displayName: "prod-api",
95 channelProfile: { channelType: "API", profileId: "api" },
96 });
97 return { app, deployment };
98 }),
99 );
100
101 expect(updated.deployment.name).toEqual(created.deployment.name);
102 expect(updated.deployment.displayName).toEqual("prod-api");
103
104 yield* stack.destroy();
105 const gone = yield* waitUntilGone(created.deployment.name);
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel),
108 { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 },
109);