1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as apphub from "@distilled.cloud/gcp/apphub_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
17// App Hub is not enabled in the test project; live calls fail with
18// ServiceDisabled:
19// "App Hub API has not been used in project alchemy-gcp-testing-83661
20// before or it is disabled." Workload registration also needs a
21// discovered workload in the host project.
22const runLifecycle = !process.env.FAST && process.env.GCP_TEST_APPHUB === "1";
23const location = "us-central1";
24
25const waitUntilGone = (name: string) =>
26 apphub.getProjectsLocationsApplicationsWorkloads({ name }).pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("1 second"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36const firstDiscoveredWorkload = GcpEnvironment.current.pipe(
37 Effect.flatMap(({ project }) =>
38 apphub
39 .listProjectsLocationsDiscoveredWorkloads({
40 parent: `projects/${project}/locations/${location}`,
41 pageSize: 10,
42 })
43 .pipe(
44 Effect.map((page) => page.discoveredWorkloads?.[0]?.name),
45 Effect.catchTag("NotFound", () => Effect.succeed(undefined)),
46 ),
47 ),
48);
49
50test.provider.skipIf(runLifecycle)(
51 "getProjectsLocationsApplicationsWorkloads on a missing workload fails with a typed tag",
52 (stack) =>
53 Effect.gen(function* () {
54 const { project } = yield* GcpEnvironment.current;
55 yield* stack.destroy();
56
57 const error = yield* Effect.flip(
58 apphub.getProjectsLocationsApplicationsWorkloads({
59 name: `projects/${project}/locations/${location}/applications/alchemy-missing-app/workloads/alchemy-missing-workload`,
60 }),
61 );
62 expect(error._tag).toEqual("ServiceDisabled");
63
64 yield* stack.destroy();
65 }).pipe(logLevel),
66 { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 },
67);
68
69test.provider.skipIf(!runLifecycle)(
70 "create, update, and delete an App Hub workload",
71 (stack) =>
72 Effect.gen(function* () {
73 yield* stack.destroy();
74
75 const discoveredWorkload = yield* firstDiscoveredWorkload;
76 expect(discoveredWorkload).toEqual(expect.any(String));
77
78 const created = yield* stack.deploy(
79 Effect.gen(function* () {
80 const app = yield* GCP.AppHub.Application("Store", {
81 location,
82 displayName: "store",
83 scope: { type: "REGIONAL" },
84 });
85 const workload = yield* GCP.AppHub.ApplicationsWorkload("Api", {
86 application: app.name,
87 location,
88 discoveredWorkload: discoveredWorkload!,
89 displayName: "api",
90 description: "checkout mig",
91 attributes: { criticality: { type: "MEDIUM" } },
92 });
93 return { app, workload };
94 }),
95 );
96
97 expect(created.workload.name).toContain("/workloads/");
98 expect(created.workload.application).toEqual(created.app.name);
99 expect(created.workload.displayName).toEqual("api");
100 expect(created.workload.description).toEqual("checkout mig");
101 expect(created.workload.discoveredWorkload).toBeDefined();
102
103 const fetched = yield* apphub.getProjectsLocationsApplicationsWorkloads({
104 name: created.workload.name,
105 });
106 expect(fetched.name).toEqual(created.workload.name);
107 expect(fetched.description).toContain("alchemy-id=");
108 expect(fetched.description).toContain("checkout mig");
109
110 const updated = yield* stack.deploy(
111 Effect.gen(function* () {
112 const app = yield* GCP.AppHub.Application("Store", {
113 applicationId: created.app.applicationId,
114 location,
115 displayName: "store",
116 scope: { type: "REGIONAL" },
117 });
118 const workload = yield* GCP.AppHub.ApplicationsWorkload("Api", {
119 workloadId: created.workload.workloadId,
120 application: app.name,
121 location,
122 discoveredWorkload: created.workload.discoveredWorkload!,
123 displayName: "api-v2",
124 description: "checkout mig v2",
125 attributes: { criticality: { type: "HIGH" } },
126 });
127 return { app, workload };
128 }),
129 );
130
131 expect(updated.workload.name).toEqual(created.workload.name);
132 expect(updated.workload.displayName).toEqual("api-v2");
133 expect(updated.workload.description).toEqual("checkout mig v2");
134 expect(updated.workload.attributes?.criticality?.type).toEqual("HIGH");
135
136 yield* stack.destroy();
137 const gone = yield* waitUntilGone(created.workload.name);
138 expect(gone).toEqual("gone");
139 }).pipe(logLevel),
140 { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 },
141);