The best way to control Claude Code, Codex and more from any computer or phone

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