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// Needs a provisioned API Hub instance in us-central1 (one per project,
18// behind a host project registration); without one writes fail with
19// BadRequest ("Invalid resource state … API Hub instance …"). Set
20// GCP_TEST_APIHUB_INSTANCE=1 when the hub exists.
21const runLifecycle = !!process.env.GCP_TEST_APIHUB_INSTANCE;
22const location = "us-central1";
23
24const waitUntilGone = (name: string) =>
25 apihub.getProjectsLocationsPlugins({ 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.skipIf(!runLifecycle)(
36 "getProjectsLocationsPlugins on a missing plugin 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.getProjectsLocationsPlugins({
44 name: `projects/${project}/locations/${location}/plugins/alchemy-missing-plugin`,
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 "getProjectsLocationsPlugins fails with ApiHubNotProvisioned without an API hub instance",
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.getProjectsLocationsPlugins({
63 name: `projects/${project}/locations/${location}/plugins/alchemy-missing-plugin`,
64 }),
65 );
66 expect(error._tag).toEqual("ApiHubNotProvisioned");
67
68 yield* stack.destroy();
69 }).pipe(logLevel),
70 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
71);
72
73test.provider.skipIf(!runLifecycle)(
74 "create, disable, and delete an API Hub plugin",
75 (stack) =>
76 Effect.gen(function* () {
77 yield* stack.destroy();
78
79 const created = yield* stack.deploy(
80 Effect.gen(function* () {
81 return yield* GCP.ApiHub.Plugin("OnRamp", {
82 location,
83 displayName: "on-ramp",
84 description: "custom collector",
85 });
86 }),
87 );
88
89 expect(created.name).toContain("/plugins/");
90 expect(created.pluginId).toEqual(expect.any(String));
91 expect(created.location).toEqual(location);
92 expect(created.displayName).toEqual("on-ramp");
93 expect(created.description).toEqual("custom collector");
94 expect(created.pluginCategory).toEqual("API_PRODUCER");
95
96 const fetched = yield* apihub.getProjectsLocationsPlugins({
97 name: created.name,
98 });
99 expect(fetched.name).toEqual(created.name);
100 expect(fetched.description).toContain("alchemy-id=");
101 expect(fetched.description).toContain("custom collector");
102
103 const updated = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GCP.ApiHub.Plugin("OnRamp", {
106 pluginId: created.pluginId,
107 location,
108 displayName: "on-ramp",
109 description: "custom collector",
110 enabled: false,
111 });
112 }),
113 );
114
115 expect(updated.name).toEqual(created.name);
116 expect(
117 updated.state === "DISABLED" || updated.state === undefined,
118 ).toEqual(true);
119
120 yield* stack.destroy();
121 const gone = yield* waitUntilGone(created.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
125);