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

main
ts 148 lines 5.43 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 apphub from "@distilled.cloud/gcp/apphub_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { isServiceEnabled } from "../ServiceState.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // App Hub is not enabled in the test project; live calls fail with
16 // ServiceDisabled:
17 // "App Hub API has not been used in project <project>
18 // before or it is disabled. Enable it by visiting
19 // https://console.developers.google.com/apis/api/apphub.googleapis.com/overview?project=<project>"
20 const runLifecycle = !process.env.FAST && process.env.GCP_TEST_APPHUB === "1";
21 const location = "us-central1";
22
23 const waitUntilGone = (name: string) =>
24 apphub.getProjectsLocationsApplications({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("1 second"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34 test.provider.skipIf(runLifecycle)(
35 "createProjectsLocationsApplications fails with ServiceDisabled when App Hub is disabled",
36 (stack) =>
37 Effect.gen(function* () {
38 // Only meaningful while the API is disabled: enabled, the create would
39 // provision a real resource.
40 if (yield* isServiceEnabled("apphub.googleapis.com")) return;
41 const { project } = yield* GcpEnvironment.current;
42 yield* stack.destroy();
43
44 const error = yield* Effect.flip(
45 apphub.createProjectsLocationsApplications({
46 parent: `projects/${project}/locations/${location}`,
47 applicationId: "alchemy-apphub-probe",
48 body: {
49 displayName: "probe",
50 scope: { type: "REGIONAL" },
51 },
52 }),
53 );
54 expect(error._tag).toEqual("ServiceDisabled");
55
56 yield* stack.destroy();
57 }).pipe(logLevel),
58 { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 },
59 );
60
61 test.provider.skipIf(runLifecycle)(
62 "getProjectsLocationsApplications on a missing application fails with a typed tag",
63 (stack) =>
64 Effect.gen(function* () {
65 const { project } = yield* GcpEnvironment.current;
66 yield* stack.destroy();
67
68 const error = yield* Effect.flip(
69 apphub.getProjectsLocationsApplications({
70 name: `projects/${project}/locations/${location}/applications/alchemy-missing-app`,
71 }),
72 );
73 expect(["ServiceDisabled", "NotFound"]).toContain(error._tag);
74
75 yield* stack.destroy();
76 }).pipe(logLevel),
77 { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 },
78 );
79
80 test.provider.skipIf(!runLifecycle)(
81 "create, update, and delete an App Hub application",
82 (stack) =>
83 Effect.gen(function* () {
84 const { project } = yield* GcpEnvironment.current;
85 yield* stack.destroy();
86
87 const created = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* GCP.AppHub.Application("Checkout", {
90 location,
91 displayName: "checkout",
92 description: "payments",
93 scope: { type: "REGIONAL" },
94 attributes: {
95 criticality: { type: "MEDIUM" },
96 environment: { type: "DEVELOPMENT" },
97 },
98 });
99 }),
100 );
101
102 expect(created.applicationId).toEqual(expect.any(String));
103 expect(created.name).toEqual(
104 `projects/${project}/locations/${location}/applications/${created.applicationId}`,
105 );
106 expect(created.location).toEqual(location);
107 expect(created.displayName).toEqual("checkout");
108 expect(created.description).toEqual("payments");
109 expect(created.scope?.type).toEqual("REGIONAL");
110 expect(created.attributes?.criticality?.type).toEqual("MEDIUM");
111
112 const fetched = yield* apphub.getProjectsLocationsApplications({
113 name: created.name,
114 });
115 expect(fetched.name).toEqual(created.name);
116 expect(fetched.displayName).toEqual("checkout");
117 expect(fetched.description).toContain("alchemy-id=");
118 expect(fetched.description).toContain("payments");
119 expect(fetched.scope?.type).toEqual("REGIONAL");
120
121 const updated = yield* stack.deploy(
122 Effect.gen(function* () {
123 return yield* GCP.AppHub.Application("Checkout", {
124 applicationId: created.applicationId,
125 location,
126 displayName: "checkout-v2",
127 description: "payments v2",
128 scope: { type: "REGIONAL" },
129 attributes: {
130 criticality: { type: "HIGH" },
131 environment: { type: "TEST" },
132 },
133 });
134 }),
135 );
136
137 expect(updated.name).toEqual(created.name);
138 expect(updated.displayName).toEqual("checkout-v2");
139 expect(updated.description).toEqual("payments v2");
140 expect(updated.attributes?.criticality?.type).toEqual("HIGH");
141 expect(updated.attributes?.environment?.type).toEqual("TEST");
142
143 yield* stack.destroy();
144 const gone = yield* waitUntilGone(created.name);
145 expect(gone).toEqual("gone");
146 }).pipe(logLevel),
147 { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 },
148 );