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

main
ts 127 lines 4.4 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 apihub from "@distilled.cloud/gcp/apihub_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 API Hub instance in us-central1 (one per project,
15 // behind a host project registration); without one writes fail with
16 // BadRequest ("Invalid resource state … API Hub instance …"). Set
17 // GCP_TEST_APIHUB_INSTANCE=1 when the hub exists.
18 const runLifecycle = !!process.env.GCP_TEST_APIHUB_INSTANCE;
19 const location = "us-central1";
20
21 const waitUntilGone = (name: string) =>
22 apihub.getProjectsLocationsAttributes({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("1 second"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32 test.provider.skipIf(!runLifecycle)(
33 "getProjectsLocationsAttributes on a missing attribute fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 apihub.getProjectsLocationsAttributes({
41 name: `projects/${project}/locations/${location}/attributes/alchemy-missing-attr`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel),
48 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
49 );
50
51 test.provider.skipIf(runLifecycle)(
52 "getProjectsLocationsAttributes fails with ApiHubNotProvisioned without an API hub instance",
53 (stack) =>
54 Effect.gen(function* () {
55 const { project } = yield* GcpEnvironment.current;
56 yield* stack.destroy();
57
58 const error = yield* Effect.flip(
59 apihub.getProjectsLocationsAttributes({
60 name: `projects/${project}/locations/${location}/attributes/alchemy-missing-attr`,
61 }),
62 );
63 expect(error._tag).toEqual("ApiHubNotProvisioned");
64
65 yield* stack.destroy();
66 }).pipe(logLevel),
67 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
68 );
69
70 test.provider.skipIf(!runLifecycle)(
71 "create, update, and delete an API Hub attribute",
72 (stack) =>
73 Effect.gen(function* () {
74 yield* stack.destroy();
75
76 const created = yield* stack.deploy(
77 Effect.gen(function* () {
78 return yield* GCP.ApiHub.Attribute("OwnerTeam", {
79 location,
80 displayName: "owner-team",
81 description: "owning team",
82 dataType: "STRING",
83 scope: "API",
84 });
85 }),
86 );
87
88 expect(created.name).toContain("/attributes/");
89 expect(created.attributeId).toEqual(expect.any(String));
90 expect(created.displayName).toEqual("owner-team");
91 expect(created.description).toEqual("owning team");
92 expect(created.dataType).toEqual("STRING");
93 expect(created.scope).toEqual("API");
94
95 const fetched = yield* apihub.getProjectsLocationsAttributes({
96 name: created.name,
97 });
98 expect(fetched.name).toEqual(created.name);
99 expect(fetched.description).toContain("alchemy-id=");
100 expect(fetched.description).toContain("owning team");
101
102 const updated = yield* stack.deploy(
103 Effect.gen(function* () {
104 return yield* GCP.ApiHub.Attribute("OwnerTeam", {
105 attributeId: created.attributeId,
106 location,
107 displayName: "owner-team-v2",
108 description: "owning team v2",
109 dataType: "STRING",
110 scope: "API",
111 cardinality: 2,
112 });
113 }),
114 );
115
116 expect(updated.name).toEqual(created.name);
117 expect(updated.displayName).toEqual("owner-team-v2");
118 expect(updated.description).toEqual("owning team v2");
119 expect(updated.cardinality).toEqual(2);
120
121 yield* stack.destroy();
122
123 const gone = yield* waitUntilGone(created.name);
124 expect(gone).toEqual("gone");
125 }).pipe(logLevel),
126 { tags: ["provider:gcp", "provider:gcp:apihub", "live"], timeout: 90_000 },
127 );