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

main
ts 138 lines 5.13 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
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 // App Hub is not enabled in the test project; live calls fail with
15 // ServiceDisabled:
16 // "App Hub API has not been used in project <project>
17 // before or it is disabled." Service registration also needs a
18 // discovered service in the host project.
19 const runLifecycle = !process.env.FAST && process.env.GCP_TEST_APPHUB === "1";
20 const location = "us-central1";
21
22 const waitUntilGone = (name: string) =>
23 apphub.getProjectsLocationsApplicationsServices({ name }).pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("1 second"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33 const firstDiscoveredService = GcpEnvironment.current.pipe(
34 Effect.flatMap(({ project }) =>
35 apphub
36 .listProjectsLocationsDiscoveredServices({
37 parent: `projects/${project}/locations/${location}`,
38 pageSize: 10,
39 })
40 .pipe(
41 Effect.map((page) => page.discoveredServices?.[0]?.name),
42 Effect.catchTag("NotFound", () => Effect.succeed(undefined)),
43 ),
44 ),
45 );
46
47 test.provider.skipIf(runLifecycle)(
48 "getProjectsLocationsApplicationsServices on a missing service fails with a typed tag",
49 (stack) =>
50 Effect.gen(function* () {
51 const { project } = yield* GcpEnvironment.current;
52 yield* stack.destroy();
53
54 const error = yield* Effect.flip(
55 apphub.getProjectsLocationsApplicationsServices({
56 name: `projects/${project}/locations/${location}/applications/alchemy-missing-app/services/alchemy-missing-service`,
57 }),
58 );
59 expect(["ServiceDisabled", "NotFound"]).toContain(error._tag);
60
61 yield* stack.destroy();
62 }).pipe(logLevel),
63 { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 },
64 );
65
66 test.provider.skipIf(!runLifecycle)(
67 "create, update, and delete an App Hub service",
68 (stack) =>
69 Effect.gen(function* () {
70 yield* stack.destroy();
71
72 const discoveredService = yield* firstDiscoveredService;
73 expect(discoveredService).toEqual(expect.any(String));
74
75 const created = yield* stack.deploy(
76 Effect.gen(function* () {
77 const app = yield* GCP.AppHub.Application("Catalog", {
78 location,
79 displayName: "catalog",
80 scope: { type: "REGIONAL" },
81 });
82 const service = yield* GCP.AppHub.ApplicationsService("Frontend", {
83 application: app.name,
84 location,
85 discoveredService: discoveredService!,
86 displayName: "frontend",
87 description: "https frontend",
88 attributes: { criticality: { type: "MEDIUM" } },
89 });
90 return { app, service };
91 }),
92 );
93
94 expect(created.service.name).toContain("/services/");
95 expect(created.service.application).toEqual(created.app.name);
96 expect(created.service.displayName).toEqual("frontend");
97 expect(created.service.description).toEqual("https frontend");
98 expect(created.service.discoveredService).toBeDefined();
99
100 const fetched = yield* apphub.getProjectsLocationsApplicationsServices({
101 name: created.service.name,
102 });
103 expect(fetched.name).toEqual(created.service.name);
104 expect(fetched.description).toContain("alchemy-id=");
105 expect(fetched.description).toContain("https frontend");
106
107 const updated = yield* stack.deploy(
108 Effect.gen(function* () {
109 const app = yield* GCP.AppHub.Application("Catalog", {
110 applicationId: created.app.applicationId,
111 location,
112 displayName: "catalog",
113 scope: { type: "REGIONAL" },
114 });
115 const service = yield* GCP.AppHub.ApplicationsService("Frontend", {
116 serviceId: created.service.serviceId,
117 application: app.name,
118 location,
119 discoveredService: created.service.discoveredService!,
120 displayName: "frontend-v2",
121 description: "https frontend v2",
122 attributes: { criticality: { type: "HIGH" } },
123 });
124 return { app, service };
125 }),
126 );
127
128 expect(updated.service.name).toEqual(created.service.name);
129 expect(updated.service.displayName).toEqual("frontend-v2");
130 expect(updated.service.description).toEqual("https frontend v2");
131 expect(updated.service.attributes?.criticality?.type).toEqual("HIGH");
132
133 yield* stack.destroy();
134 const gone = yield* waitUntilGone(created.service.name);
135 expect(gone).toEqual("gone");
136 }).pipe(logLevel),
137 { tags: ["provider:gcp", "provider:gcp:apphub", "live"], timeout: 90_000 },
138 );