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.getProjectsLocationsRuntimeProjectAttachments({ 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 "getProjectsLocationsRuntimeProjectAttachments on a missing attachment 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.getProjectsLocationsRuntimeProjectAttachments({
44 name: `projects/${project}/locations/${location}/runtimeProjectAttachments/alchemy-missing-attachment`,
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 and delete an API Hub runtime project attachment",
56 (stack) =>
57 Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.ApiHub.RuntimeProjectAttachment("Runtime", {
64 location,
65 runtimeProject: `projects/${project}`,
66 });
67 }),
68 );
69
70 expect(created.name).toContain("/runtimeProjectAttachments/");
71 expect(created.runtimeProjectAttachmentId).toEqual(project);
72 expect(created.location).toEqual(location);
73 expect(created.runtimeProject).toBeDefined();
74
75 const fetched =
76 yield* apihub.getProjectsLocationsRuntimeProjectAttachments({
77 name: created.name,
78 });
79 expect(fetched.name).toEqual(created.name);
80
81 yield* stack.destroy();
82 const gone = yield* waitUntilGone(created.name);
83 expect(gone).toEqual("gone");
84 }).pipe(logLevel),
85 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
86);