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

main
ts 121 lines 4.27 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 * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 // Needs a provisioned Apigee organization on the testing project (paid, or
15 // ~1h eval provisioning); without one calls fail with ApigeeResourceNotFound (403 "Permission
16 // denied on resource \"organizations/{project}\" (or it may not exist)").
17 // Set GCP_TEST_APIGEE_ORG=1 when the org exists.
18 const runLifecycle = !!process.env.GCP_TEST_APIGEE_ORG;
19
20 const waitUntilGone = (parent: string, fileType: string, fileId: string) =>
21 apigee
22 .getOrganizationsEnvironmentsResourcefiles({
23 parent,
24 type: fileType,
25 name: fileId,
26 })
27 .pipe(
28 Effect.as("found" as const),
29 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
30 Effect.catchTag("ApigeeResourceNotFound", () => Effect.succeed("gone" as const)),
31 Effect.repeat({
32 schedule: Schedule.spaced("2 seconds"),
33 until: (status) => status === "gone",
34 times: 10,
35 }),
36 );
37
38 test.provider(
39 "getOrganizationsEnvironmentsResourcefiles on a missing file fails with ApigeeResourceNotFound",
40 (stack) =>
41 Effect.gen(function* () {
42 const { project } = yield* GcpEnvironment.current;
43 const org = `organizations/${project}`;
44
45 yield* stack.destroy();
46
47 const error = yield* Effect.flip(
48 apigee.getOrganizationsEnvironmentsResourcefiles({
49 parent: `${org}/environments/alchemy-missing`,
50 type: "js",
51 name: "alchemy-missing",
52 }),
53 );
54 expect(error._tag).toEqual("ApigeeResourceNotFound");
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
59 );
60
61 test.provider.skipIf(!runLifecycle)(
62 "create, update, and delete an environment resource file",
63 (stack) =>
64 Effect.gen(function* () {
65 yield* stack.destroy();
66
67 const created = yield* stack.deploy(
68 Effect.gen(function* () {
69 const environment = yield* GCP.Apigee.Environment("Runtime", {
70 displayName: "runtime",
71 });
72 const file = yield* GCP.Apigee.EnvironmentsResourcefile("Helper", {
73 environment: environment.environmentId,
74 fileType: "js",
75 content: "function helper() { return 1; }",
76 });
77 return { environment, file };
78 }),
79 );
80
81 expect(created.file.fileId).toEqual(expect.any(String));
82 expect(created.file.fileType).toEqual("js");
83 expect(created.file.environmentId).toEqual(created.environment.environmentId);
84
85 const fetched = yield* apigee.getOrganizationsEnvironmentsResourcefiles({
86 parent: created.file.parent,
87 type: created.file.fileType,
88 name: created.file.fileId,
89 });
90 expect(fetched.data !== undefined || fetched.contentType !== undefined).toEqual(true);
91
92 const updated = yield* stack.deploy(
93 Effect.gen(function* () {
94 const environment = yield* GCP.Apigee.Environment("Runtime", {
95 environmentId: created.environment.environmentId,
96 displayName: "runtime",
97 });
98 const file = yield* GCP.Apigee.EnvironmentsResourcefile("Helper", {
99 environment: environment.environmentId,
100 fileType: "js",
101 fileId: created.file.fileId,
102 content: "function helper() { return 2; }",
103 });
104 return { environment, file };
105 }),
106 );
107
108 expect(updated.file.fileId).toEqual(created.file.fileId);
109 expect(updated.file.parent).toEqual(created.file.parent);
110
111 yield* stack.destroy();
112
113 const gone = yield* waitUntilGone(
114 created.file.parent,
115 created.file.fileType,
116 created.file.fileId,
117 );
118 expect(gone).toEqual("gone");
119 }).pipe(logLevel),
120 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
121 );