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.getProjectsLocationsPluginsInstances({ 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 "getProjectsLocationsPluginsInstances 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.getProjectsLocationsPluginsInstances({
44 name: `projects/${project}/locations/${location}/plugins/alchemy-missing-plugin/instances/alchemy-missing-instance`,
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 "create, update, and delete an API Hub plugin instance",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const plugin = yield* GCP.ApiHub.Plugin("OnRamp", {
63 location,
64 displayName: "on-ramp",
65 description: "plugin for instance test",
66 });
67 const instance = yield* GCP.ApiHub.PluginsInstance("Collector", {
68 plugin: plugin.name,
69 displayName: "orders collector",
70 actions: [{ actionId: "sync-metadata" }],
71 });
72 return { plugin, instance };
73 }),
74 );
75
76 expect(created.instance.name).toContain("/instances/");
77 expect(created.instance.plugin).toEqual(created.plugin.name);
78 expect(created.instance.displayName).toEqual("orders collector");
79 expect(created.instance.location).toEqual(location);
80
81 const fetched = yield* apihub.getProjectsLocationsPluginsInstances({
82 name: created.instance.name,
83 });
84 expect(fetched.name).toEqual(created.instance.name);
85 expect(fetched.displayName).toContain("alchemy-id=");
86 expect(fetched.displayName).toContain("orders collector");
87
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 const plugin = yield* GCP.ApiHub.Plugin("OnRamp", {
91 pluginId: created.plugin.pluginId,
92 location,
93 displayName: "on-ramp",
94 description: "plugin for instance test",
95 });
96 const instance = yield* GCP.ApiHub.PluginsInstance("Collector", {
97 plugin: plugin.name,
98 pluginInstanceId: created.instance.pluginInstanceId,
99 displayName: "orders collector (updated)",
100 actions: [{ actionId: "sync-metadata" }],
101 });
102 return { plugin, instance };
103 }),
104 );
105
106 expect(updated.instance.name).toEqual(created.instance.name);
107 expect(updated.instance.displayName).toEqual(
108 "orders collector (updated)",
109 );
110
111 yield* stack.destroy();
112 const gone = yield* waitUntilGone(created.instance.name);
113 expect(gone).toEqual("gone");
114 }).pipe(logLevel),
115 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 120_000 },
116);