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.getProjectsLocationsApis({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35test.provider(
36 "getProjectsLocationsApis on a missing API 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.getProjectsLocationsApis({
44 name: `projects/${project}/locations/${location}/apis/alchemy-missing-api`,
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 API",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 return yield* GCP.ApiHub.Api("Pets", {
63 location,
64 displayName: "pets",
65 description: "pet store",
66 });
67 }),
68 );
69
70 expect(created.name).toContain("/apis/");
71 expect(created.apiId).toEqual(expect.any(String));
72 expect(created.location).toEqual(location);
73 expect(created.displayName).toEqual("pets");
74 expect(created.description).toEqual("pet store");
75
76 const fetched = yield* apihub.getProjectsLocationsApis({
77 name: created.name,
78 });
79 expect(fetched.name).toEqual(created.name);
80 expect(fetched.displayName).toEqual("pets");
81 expect(fetched.description).toContain("alchemy-id=");
82 expect(fetched.description).toContain("pet store");
83
84 const updated = yield* stack.deploy(
85 Effect.gen(function* () {
86 return yield* GCP.ApiHub.Api("Pets", {
87 apiId: created.apiId,
88 location,
89 displayName: "pets-v2",
90 description: "pet store v2",
91 documentation: { externalUri: "https://example.com/pets" },
92 });
93 }),
94 );
95
96 expect(updated.name).toEqual(created.name);
97 expect(updated.displayName).toEqual("pets-v2");
98 expect(updated.description).toEqual("pet store v2");
99 expect(updated.documentation?.externalUri).toEqual(
100 "https://example.com/pets",
101 );
102
103 yield* stack.destroy();
104
105 const gone = yield* waitUntilGone(created.name);
106 expect(gone).toEqual("gone");
107 }).pipe(logLevel),
108 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
109);