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.getProjectsLocationsExternalApis({ 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 "getProjectsLocationsExternalApis 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.getProjectsLocationsExternalApis({
44 name: `projects/${project}/locations/${location}/externalApis/alchemy-missing-external-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 External 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.ExternalApi("Stripe", {
63 location,
64 displayName: "Stripe",
65 description: "payments",
66 endpoints: ["https://api.stripe.com"],
67 paths: ["/v1/charges"],
68 });
69 }),
70 );
71
72 expect(created.name).toContain("/externalApis/");
73 expect(created.externalApiId).toEqual(expect.any(String));
74 expect(created.location).toEqual(location);
75 expect(created.displayName).toEqual("Stripe");
76 expect(created.description).toEqual("payments");
77 expect(created.endpoints).toEqual(["https://api.stripe.com"]);
78 expect(created.paths).toEqual(["/v1/charges"]);
79
80 const fetched = yield* apihub.getProjectsLocationsExternalApis({
81 name: created.name,
82 });
83 expect(fetched.name).toEqual(created.name);
84 expect(fetched.description).toContain("alchemy-id=");
85 expect(fetched.description).toContain("payments");
86
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* GCP.ApiHub.ExternalApi("Stripe", {
90 externalApiId: created.externalApiId,
91 location,
92 displayName: "Stripe",
93 description: "payments (updated)",
94 endpoints: ["https://api.stripe.com"],
95 paths: ["/v1/charges", "/v1/customers"],
96 });
97 }),
98 );
99
100 expect(updated.name).toEqual(created.name);
101 expect(updated.description).toEqual("payments (updated)");
102 expect(updated.paths).toEqual(["/v1/charges", "/v1/customers"]);
103
104 yield* stack.destroy();
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);