1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as apim from "@distilled.cloud/gcp/apim_v1alpha";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17// The API Management API is not enabled in the testing project: every call
18// answers 403 SERVICE_DISABLED (typed `ServiceDisabled`). Set GCP_TEST_APIM=1
19// on a project with the API enabled to run the lifecycle.
20const apimEnabled = !!process.env.GCP_TEST_APIM;
21const runLifecycle = !process.env.FAST && apimEnabled;
22const location = "us-central1";
23
24const waitUntilGone = (name: string) =>
25 apim.getProjectsLocationsObservationJobs({ 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 "getProjectsLocationsObservationJobs on a missing job fails with ServiceDisabled while the API is disabled",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40 const parent = `projects/${project}/locations/${location}`;
41
42 yield* stack.destroy();
43
44 const error = yield* Effect.flip(
45 apim.getProjectsLocationsObservationJobs({
46 name: `${parent}/observationJobs/alchemy-missing-job`,
47 }),
48 );
49 expect(error._tag).toEqual(apimEnabled ? "NotFound" : "ServiceDisabled");
50
51 yield* stack.destroy();
52 }).pipe(logLevel),
53 { tags: ["provider:gcp", "provider:gcp:apim", "live"], timeout: 90_000 },
54);
55
56test.provider.skipIf(!runLifecycle)(
57 "create, enable, and delete an API Observation job",
58 (stack) =>
59 Effect.gen(function* () {
60 const { project } = yield* GcpEnvironment.current;
61 const parent = `projects/${project}/locations/${location}`;
62
63 yield* stack.destroy();
64
65 const created = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* GCP.Apim.ObservationJob("Shadow", {
68 location,
69 enabled: false,
70 });
71 }),
72 );
73
74 expect(created.name).toContain("/observationJobs/");
75 expect(created.observationJobId).toEqual(expect.any(String));
76 expect(created.location).toEqual(location);
77 expect(created.enabled).toEqual(false);
78
79 const fetched = yield* apim.getProjectsLocationsObservationJobs({
80 name: created.name,
81 });
82 expect(fetched.name).toEqual(created.name);
83
84 const updated = yield* stack.deploy(
85 Effect.gen(function* () {
86 return yield* GCP.Apim.ObservationJob("Shadow", {
87 observationJobId: created.observationJobId,
88 location,
89 enabled: true,
90 });
91 }),
92 );
93
94 expect(updated.name).toEqual(created.name);
95 expect(updated.enabled).toEqual(true);
96
97 const refetched = yield* apim.getProjectsLocationsObservationJobs({
98 name: created.name,
99 });
100 expect((refetched.state ?? "").toUpperCase()).toEqual("ENABLED");
101
102 yield* stack.destroy();
103 const gone = yield* waitUntilGone(created.name);
104 expect(gone).toEqual("gone");
105 }).pipe(logLevel),
106 { tags: ["provider:gcp", "provider:gcp:apim", "live"], timeout: 120_000 },
107);