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

main
ts 115 lines 4.39 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 = (name: string) =>
21 apigee.getOrganizationsEnvironmentsKeystoresAliases({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.catchTag("ApigeeResourceNotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32 test.provider(
33 "getOrganizationsEnvironmentsKeystoresAliases on a missing alias fails with ApigeeResourceNotFound",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37 const org = `organizations/${project}`;
38
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 apigee.getOrganizationsEnvironmentsKeystoresAliases({
43 name: `${org}/environments/alchemy-missing/keystores/alchemy-missing/aliases/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("ApigeeResourceNotFound");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
51 );
52
53 test.provider.skipIf(!runLifecycle)(
54 "create, update, and delete a keystore alias",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 const environment = yield* GCP.Apigee.Environment("Runtime", {
62 displayName: "runtime",
63 });
64 const keystore = yield* GCP.Apigee.EnvironmentsKeystore("Tls", {
65 environment: environment.environmentId,
66 });
67 const alias = yield* GCP.Apigee.EnvironmentsKeystoresAlias("Server", {
68 environment: environment.environmentId,
69 keystore: keystore.keystoreId,
70 subject: { commonName: "api.example.com" },
71 certValidityInDays: 365,
72 });
73 return { environment, keystore, alias };
74 }),
75 );
76
77 expect(created.alias.aliasId).toEqual(expect.any(String));
78 expect(created.alias.keystoreId).toEqual(created.keystore.keystoreId);
79 expect(created.alias.environmentId).toEqual(created.environment.environmentId);
80
81 const fetched = yield* apigee.getOrganizationsEnvironmentsKeystoresAliases({
82 name: created.alias.name,
83 });
84 expect(fetched.alias).toEqual(created.alias.aliasId);
85
86 const updated = yield* stack.deploy(
87 Effect.gen(function* () {
88 const environment = yield* GCP.Apigee.Environment("Runtime", {
89 environmentId: created.environment.environmentId,
90 displayName: "runtime",
91 });
92 const keystore = yield* GCP.Apigee.EnvironmentsKeystore("Tls", {
93 environment: environment.environmentId,
94 keystoreId: created.keystore.keystoreId,
95 });
96 const alias = yield* GCP.Apigee.EnvironmentsKeystoresAlias("Server", {
97 environment: environment.environmentId,
98 keystore: keystore.keystoreId,
99 aliasId: created.alias.aliasId,
100 subject: { commonName: "api.example.com" },
101 certValidityInDays: 30,
102 });
103 return { environment, keystore, alias };
104 }),
105 );
106
107 expect(updated.alias.name).toEqual(created.alias.name);
108
109 yield* stack.destroy();
110
111 const gone = yield* waitUntilGone(created.alias.name);
112 expect(gone).toEqual("gone");
113 }).pipe(logLevel),
114 { tags: ["provider:gcp", "provider:gcp:apigee", "live"], timeout: 90_000 },
115 );