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";
23const resourceUri = "organizations/alchemy/environments/test/apis/orders";
24
25const waitUntilGone = (name: string) =>
26 apihub.getProjectsLocationsDeployments({ name }).pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("1 second"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36test.provider(
37 "getProjectsLocationsDeployments on a missing deployment fails with a typed tag",
38 (stack) =>
39 Effect.gen(function* () {
40 const { project } = yield* GcpEnvironment.current;
41 yield* stack.destroy();
42
43 const error = yield* Effect.flip(
44 apihub.getProjectsLocationsDeployments({
45 name: `projects/${project}/locations/${location}/deployments/alchemy-missing-deployment`,
46 }),
47 );
48 expect(error._tag).toEqual("NotFound");
49
50 yield* stack.destroy();
51 }).pipe(logLevel),
52 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
53);
54
55test.provider.skipIf(!runLifecycle)(
56 "create, update, and delete an API Hub deployment",
57 (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.ApiHub.Deployment("Orders", {
64 location,
65 displayName: "orders staging",
66 description: "checkout proxy",
67 resourceUri,
68 endpoints: ["https://orders.example.com"],
69 });
70 }),
71 );
72
73 expect(created.name).toContain("/deployments/");
74 expect(created.deploymentId).toEqual(expect.any(String));
75 expect(created.location).toEqual(location);
76 expect(created.displayName).toEqual("orders staging");
77 expect(created.description).toEqual("checkout proxy");
78 expect(created.resourceUri).toEqual(resourceUri);
79 expect(created.endpoints).toEqual(["https://orders.example.com"]);
80
81 const fetched = yield* apihub.getProjectsLocationsDeployments({
82 name: created.name,
83 });
84 expect(fetched.name).toEqual(created.name);
85 expect(fetched.description).toContain("alchemy-id=");
86 expect(fetched.description).toContain("checkout proxy");
87
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* GCP.ApiHub.Deployment("Orders", {
91 deploymentId: created.deploymentId,
92 location,
93 displayName: "orders staging",
94 description: "checkout proxy (updated)",
95 resourceUri,
96 endpoints: [
97 "https://orders.example.com",
98 "https://orders-alt.example.com",
99 ],
100 });
101 }),
102 );
103
104 expect(updated.name).toEqual(created.name);
105 expect(updated.description).toEqual("checkout proxy (updated)");
106 expect(updated.endpoints).toEqual([
107 "https://orders.example.com",
108 "https://orders-alt.example.com",
109 ]);
110
111 const fetchedUpdate = yield* apihub.getProjectsLocationsDeployments({
112 name: created.name,
113 });
114 expect(fetchedUpdate.description).toContain("checkout proxy (updated)");
115
116 yield* stack.destroy();
117 const gone = yield* waitUntilGone(created.name);
118 expect(gone).toEqual("gone");
119 }).pipe(logLevel),
120 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
121);