1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import { unzipFiles } from "@/Util/zip.ts";
5import * as apigee from "@distilled.cloud/gcp/apigee_v1";
6import { expect } from "alchemy-test";
7import * as Effect from "effect/Effect";
8import { MinimumLogLevel } from "effect/References";
9import * as Schedule from "effect/Schedule";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18// Needs a provisioned Apigee organization on the testing project (paid, or
19// ~1h eval provisioning); without one calls fail with ApigeeResourceNotFound (403 "Permission
20// denied on resource \"organizations/{project}\" (or it may not exist)").
21// Set GCP_TEST_APIGEE_ORG=1 when the org exists.
22const runLifecycle = !!process.env.GCP_TEST_APIGEE_ORG;
23
24const waitUntilGone = (name: string) =>
25 apigee.getOrganizationsSharedflows({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () =>
28 Effect.succeed("gone" as const),
29 ),
30 Effect.repeat({
31 schedule: Schedule.spaced("1 second"),
32 until: (status) => status === "gone",
33 times: 10,
34 }),
35 );
36
37const descriptionFromBundle = (body: apigee.GoogleApiHttpBody) =>
38 Effect.gen(function* () {
39 const data = body.data;
40 if (data === undefined || data.length === 0) return "";
41 const bytes = yield* Effect.sync(() => Buffer.from(data, "base64"));
42 const entries = yield* unzipFiles(bytes);
43 const xmlPath = Object.keys(entries).find((path) =>
44 /^sharedflowbundle\/[^/]+\.xml$/i.test(path),
45 );
46 if (xmlPath === undefined) return "";
47 return yield* Effect.sync(() =>
48 Buffer.from(entries[xmlPath]!).toString("utf8"),
49 );
50 });
51
52test.provider(
53 "getOrganizationsSharedflows on a missing shared flow fails with ApigeeResourceNotFound",
54 (stack) =>
55 Effect.gen(function* () {
56 const { project } = yield* GcpEnvironment.current;
57
58 yield* stack.destroy();
59
60 const error = yield* Effect.flip(
61 apigee.getOrganizationsSharedflows({
62 name: `organizations/${project}/sharedflows/alchemy-apigee-missing-flow`,
63 }),
64 );
65 expect(error._tag).toEqual("ApigeeResourceNotFound");
66
67 yield* stack.destroy();
68 }).pipe(logLevel),
69 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
70);
71
72test.provider.skipIf(!runLifecycle)(
73 "create, update, and delete an Apigee shared flow",
74 (stack) =>
75 Effect.gen(function* () {
76 const { project } = yield* GcpEnvironment.current;
77
78 yield* stack.destroy();
79
80 const created = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* GCP.Apigee.Sharedflow("Traffic", {
83 description: "alchemy test shared flow",
84 });
85 }),
86 );
87
88 expect(created.sharedflowId).toEqual(expect.any(String));
89 expect(created.organization).toEqual(project);
90 expect(created.name).toEqual(
91 `organizations/${project}/sharedflows/${created.sharedflowId}`,
92 );
93 expect(created.description).toEqual("alchemy test shared flow");
94 expect(created.latestRevisionId).toEqual(expect.any(String));
95
96 const fetched = yield* apigee.getOrganizationsSharedflows({
97 name: created.name,
98 });
99 expect(fetched.name).toEqual(created.sharedflowId);
100
101 const revision = yield* apigee.getOrganizationsSharedflowsRevisions({
102 name: `${created.name}/revisions/${created.latestRevisionId}`,
103 format: "bundle",
104 });
105 const createdXml = yield* descriptionFromBundle(revision);
106 expect(createdXml).toContain("alchemy-id=");
107 expect(createdXml).toContain("alchemy test shared flow");
108
109 const updated = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* GCP.Apigee.Sharedflow("Traffic", {
112 sharedflowId: created.sharedflowId,
113 description: "alchemy updated shared flow",
114 });
115 }),
116 );
117
118 expect(updated.sharedflowId).toEqual(created.sharedflowId);
119 expect(updated.description).toEqual("alchemy updated shared flow");
120 expect(updated.latestRevisionId).not.toEqual(created.latestRevisionId);
121
122 const fetchedRevision =
123 yield* apigee.getOrganizationsSharedflowsRevisions({
124 name: `${updated.name}/revisions/${updated.latestRevisionId}`,
125 format: "bundle",
126 });
127 const updatedXml = yield* descriptionFromBundle(fetchedRevision);
128 expect(updatedXml).toContain("alchemy updated shared flow");
129 expect(updatedXml).toContain("alchemy-id=");
130
131 yield* stack.destroy();
132
133 const gone = yield* waitUntilGone(created.name);
134 expect(gone).toEqual("gone");
135 }).pipe(logLevel),
136 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
137);