1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as apigee from "@distilled.cloud/gcp/apigee_v1";
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
17const siteId = process.env.GCP_TEST_APIGEE_SITE ?? "";
18const apiProductName = process.env.GCP_TEST_APIGEE_PRODUCT ?? "";
19
20// Needs a provisioned Apigee organization on the testing project (paid, or
21// ~1h eval provisioning); without one calls fail with ApigeeResourceNotFound (403 "Permission
22// denied on resource \"organizations/{project}\" (or it may not exist)").
23// Set GCP_TEST_APIGEE_ORG=1 when the org exists.
24const runLifecycle =
25 !!process.env.GCP_TEST_APIGEE_ORG && !!siteId && !!apiProductName;
26
27const waitUntilGone = (name: string) =>
28 apigee.getOrganizationsSitesApidocs({ name }).pipe(
29 Effect.as("found" as const),
30 Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () =>
31 Effect.succeed("gone" as const),
32 ),
33 Effect.repeat({
34 schedule: Schedule.spaced("1 second"),
35 until: (status) => status === "gone",
36 times: 10,
37 }),
38 );
39
40test.provider(
41 "getOrganizationsSitesApidocs on a missing catalog item fails with ApigeeResourceNotFound",
42 (stack) =>
43 Effect.gen(function* () {
44 const { project } = yield* GcpEnvironment.current;
45
46 yield* stack.destroy();
47
48 const error = yield* Effect.flip(
49 apigee.getOrganizationsSitesApidocs({
50 name: `organizations/${project}/sites/alchemy-missing-site/apidocs/alchemy-missing-doc`,
51 }),
52 );
53 expect(error._tag).toEqual("ApigeeResourceNotFound");
54
55 yield* stack.destroy();
56 }).pipe(logLevel),
57 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
58);
59
60test.provider.skipIf(!runLifecycle)(
61 "create, update, and delete an Apigee catalog item",
62 (stack) =>
63 Effect.gen(function* () {
64 const { project } = yield* GcpEnvironment.current;
65
66 yield* stack.destroy();
67
68 const created = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.Apigee.SitesApidoc("Checkout", {
71 siteId,
72 apiProductName,
73 title: "Checkout API",
74 description: "place orders",
75 });
76 }),
77 );
78
79 expect(created.apiDocId).toEqual(expect.any(String));
80 expect(created.organization).toEqual(project);
81 expect(created.siteId).toEqual(siteId);
82 expect(created.apiProductName).toEqual(apiProductName);
83 expect(created.title).toEqual("Checkout API");
84 expect(created.description).toEqual("place orders");
85 expect(created.published).toEqual(false);
86 expect(created.name).toEqual(
87 `organizations/${project}/sites/${siteId}/apidocs/${created.apiDocId}`,
88 );
89
90 const fetched = yield* apigee.getOrganizationsSitesApidocs({
91 name: created.name,
92 });
93 expect(fetched.data?.id).toEqual(created.apiDocId);
94 expect(fetched.data?.description).toEqual("place orders");
95
96 const updated = yield* stack.deploy(
97 Effect.gen(function* () {
98 return yield* GCP.Apigee.SitesApidoc("Checkout", {
99 siteId,
100 apiProductName,
101 title: "Checkout API v2",
102 description: "place and refund orders",
103 published: true,
104 });
105 }),
106 );
107
108 expect(updated.apiDocId).toEqual(created.apiDocId);
109 expect(updated.title).toEqual("Checkout API v2");
110 expect(updated.description).toEqual("place and refund orders");
111 expect(updated.published).toEqual(true);
112
113 const fetchedUpdate = yield* apigee.getOrganizationsSitesApidocs({
114 name: updated.name,
115 });
116 expect(fetchedUpdate.data?.title).toEqual("Checkout API v2");
117 expect(fetchedUpdate.data?.description).toContain(
118 "place and refund orders",
119 );
120 expect(fetchedUpdate.data?.published).toEqual(true);
121
122 yield* stack.destroy();
123
124 const gone = yield* waitUntilGone(created.name);
125 expect(gone).toEqual("gone");
126 }).pipe(logLevel),
127 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
128);