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

main
ts 132 lines 4.54 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 appengine from "@distilled.cloud/gcp/appengine_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { CERT_A, CERT_B, KEY_A, KEY_B } from "./fixtures/cert.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Needs an App Engine application in the project, which is permanent once
16 // created (it can never be deleted). The testing project has none and the
17 // App Engine Admin API is off (ServiceDisabled: "App Engine Admin API has not been
18 // used in project ... or it is disabled."). Set GCP_TEST_APPENGINE_APP=1 on a
19 // project with an app to run the lifecycle.
20 const runLifecycle = !!process.env.GCP_TEST_APPENGINE_APP;
21
22 const waitUntilGone = (appsId: string, certificateId: string) =>
23 appengine
24 .getAppsAuthorizedCertificates({
25 appsId,
26 authorizedCertificatesId: certificateId,
27 })
28 .pipe(
29 Effect.as("found" as const),
30 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
31 Effect.repeat({
32 schedule: Schedule.spaced("1 second"),
33 until: (status) => status === "gone",
34 times: 10,
35 }),
36 );
37
38 test.provider(
39 "getAppsAuthorizedCertificates on a missing certificate fails with a typed tag",
40 (stack) =>
41 Effect.gen(function* () {
42 const { project } = yield* GcpEnvironment.current;
43 yield* stack.destroy();
44
45 const error = yield* Effect.flip(
46 appengine.getAppsAuthorizedCertificates({
47 appsId: project,
48 authorizedCertificatesId: "missing",
49 }),
50 );
51 expect(["ServiceDisabled", "NotFound"]).toContain(error._tag);
52
53 yield* stack.destroy();
54 }).pipe(logLevel),
55 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
56 );
57
58 test.provider.skipIf(runLifecycle)(
59 "createAppsAuthorizedCertificates without an App Engine app fails with a typed tag",
60 (stack) =>
61 Effect.gen(function* () {
62 const { project } = yield* GcpEnvironment.current;
63 yield* stack.destroy();
64
65 const error = yield* Effect.flip(
66 appengine.createAppsAuthorizedCertificates({
67 appsId: project,
68 body: {
69 displayName: "Alchemy Appengine Probe",
70 certificateRawData: {
71 publicCertificate: CERT_A,
72 privateKey: KEY_A,
73 },
74 },
75 }),
76 );
77 expect(["ServiceDisabled", "NotFound"]).toContain(error._tag);
78
79 yield* stack.destroy();
80 }).pipe(logLevel),
81 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
82 );
83
84 test.provider.skipIf(!runLifecycle)(
85 "create, update, and delete an authorized certificate",
86 (stack) =>
87 Effect.gen(function* () {
88 const { project } = yield* GcpEnvironment.current;
89 yield* stack.destroy();
90
91 const created = yield* stack.deploy(
92 Effect.gen(function* () {
93 return yield* GCP.AppEngine.AppsAuthorizedCertificate("FrontendTls", {
94 displayName: "frontend",
95 publicCertificate: CERT_A,
96 privateKey: KEY_A,
97 });
98 }),
99 );
100
101 expect(created.certificateId.length).toBeGreaterThan(0);
102 expect(created.displayName).toEqual("frontend");
103 expect(created.appsId).toEqual(project);
104
105 const fetched = yield* appengine.getAppsAuthorizedCertificates({
106 appsId: created.appsId,
107 authorizedCertificatesId: created.certificateId,
108 });
109 expect(fetched.id).toEqual(created.certificateId);
110 expect(fetched.displayName).toContain("[alchemy ");
111
112 const updated = yield* stack.deploy(
113 Effect.gen(function* () {
114 return yield* GCP.AppEngine.AppsAuthorizedCertificate("FrontendTls", {
115 certificateId: created.certificateId,
116 displayName: "frontend-prod",
117 publicCertificate: CERT_B,
118 privateKey: KEY_B,
119 });
120 }),
121 );
122
123 expect(updated.certificateId).toEqual(created.certificateId);
124 expect(updated.displayName).toEqual("frontend-prod");
125
126 yield* stack.destroy();
127
128 const gone = yield* waitUntilGone(created.appsId, created.certificateId);
129 expect(gone).toEqual("gone");
130 }).pipe(logLevel),
131 { tags: ["provider:gcp", "provider:gcp:appengine", "live"], timeout: 90_000 },
132 );