1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as apihub from "@distilled.cloud/gcp/apihub_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// API Hub needs a host project registration first; without it instance
18// create fails with BadRequest ("Unable to find host project registration
19// for this project."). Set GCP_TEST_APIHUB_HOST_PROJECT=1 on a registered
20// project that has no hub yet (one instance per project).
21const runLifecycle = !!process.env.GCP_TEST_APIHUB_HOST_PROJECT;
22const location = "us-central1";
23
24const waitUntilGone = (name: string) =>
25 apihub.getProjectsLocationsApiHubInstances({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("2 seconds"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35test.provider(
36 "getProjectsLocationsApiHubInstances on a missing instance fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40 yield* stack.destroy();
41
42 const error = yield* Effect.flip(
43 apihub.getProjectsLocationsApiHubInstances({
44 name: `projects/${project}/locations/${location}/apiHubInstances/alchemy-missing-hub`,
45 }),
46 );
47 expect(error._tag).toEqual("NotFound");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
52);
53
54test.provider.skipIf(runLifecycle)(
55 "createProjectsLocationsApiHubInstances without a host project registration fails with BadRequest",
56 (stack) =>
57 Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59 yield* stack.destroy();
60
61 const error = yield* Effect.flip(
62 apihub.createProjectsLocationsApiHubInstances({
63 parent: `projects/${project}/locations/${location}`,
64 apiHubInstanceId: "alchemy-apihub-probe",
65 body: { config: { vertexLocation: "us" } },
66 }),
67 );
68 expect(error._tag).toEqual("BadRequest");
69 expect(error.message).toContain("host project registration");
70
71 yield* stack.destroy();
72 }).pipe(logLevel),
73 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
74);
75
76test.provider.skipIf(!runLifecycle)(
77 "create, update, and delete an API Hub instance",
78 (stack) =>
79 Effect.gen(function* () {
80 yield* stack.destroy();
81
82 const created = yield* stack.deploy(
83 Effect.gen(function* () {
84 return yield* GCP.ApiHub.ApiHubInstance("Hub", {
85 location,
86 description: "alchemy test hub",
87 labels: { env: "test" },
88 config: { disableSearch: false, vertexLocation: "us" },
89 });
90 }),
91 );
92
93 expect(created.name).toContain("/apiHubInstances/");
94 expect(created.apiHubInstanceId).toEqual(expect.any(String));
95 expect(created.location).toEqual(location);
96 expect(created.description).toEqual("alchemy test hub");
97 expect(created.labels).toMatchObject({ env: "test" });
98
99 const fetched = yield* apihub.getProjectsLocationsApiHubInstances({
100 name: created.name,
101 });
102 expect(fetched.name).toEqual(created.name);
103 expect(fetched.labels?.env).toEqual("test");
104 expect(
105 Object.keys(fetched.labels ?? {}).some((key) =>
106 key.startsWith("alchemy-"),
107 ),
108 ).toEqual(true);
109
110 const updated = yield* stack.deploy(
111 Effect.gen(function* () {
112 return yield* GCP.ApiHub.ApiHubInstance("Hub", {
113 apiHubInstanceId: created.apiHubInstanceId,
114 location,
115 description: "alchemy test hub v2",
116 labels: { env: "prod" },
117 config: {
118 disableSearch: true,
119 vertexLocation: "us",
120 },
121 });
122 }),
123 );
124
125 expect(updated.name).toEqual(created.name);
126 expect(updated.disableSearch).toEqual(true);
127 expect(updated.vertexLocation).toEqual("us");
128
129 yield* stack.destroy();
130
131 const gone = yield* waitUntilGone(created.name);
132 expect(gone).toEqual("gone");
133 }).pipe(logLevel),
134 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 120_000 },
135);