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

main
ts 100 lines 3.79 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as apikeys from "@distilled.cloud/gcp/apikeys_v2";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const waitUntilGone = (name: string) =>
14 apikeys.getProjectsLocationsKeys({ name }).pipe(
15 Effect.map((key) => ((key.deleteTime ?? "") !== "" ? ("gone" as const) : ("found" as const))),
16 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
17 Effect.repeat({
18 schedule: Schedule.spaced("1 second"),
19 until: (status) => status === "gone",
20 times: 10,
21 }),
22 );
23
24 test.provider(
25 "create, update, and delete an API key",
26 (stack) =>
27 Effect.gen(function* () {
28 yield* stack.destroy();
29
30 const created = yield* stack.deploy(
31 Effect.gen(function* () {
32 return yield* GCP.ApiKeys.Key("Maps", {
33 displayName: "alchemy test maps",
34 annotations: { env: "test" },
35 });
36 }),
37 );
38
39 expect(created.name).toContain("/locations/global/keys/");
40 expect(created.keyId).toEqual(expect.any(String));
41 expect(created.location).toEqual("global");
42 expect(created.displayName).toEqual("alchemy test maps");
43 expect(created.annotations).toMatchObject({ env: "test" });
44 expect(created.keyString).toEqual(expect.any(String));
45 expect((created.keyString ?? "").length).toBeGreaterThan(0);
46
47 const fetched = yield* apikeys.getProjectsLocationsKeys({
48 name: created.name,
49 });
50 expect(fetched.name).toEqual(created.name);
51 expect(fetched.displayName).toEqual("alchemy test maps");
52 expect(fetched.annotations?.env).toEqual("test");
53 expect(fetched.annotations?.["alchemy-id"]).toEqual(expect.any(String));
54 expect(fetched.deleteTime).toBeFalsy();
55
56 const keyString = yield* apikeys.getKeyStringProjectsLocationsKeys({
57 name: created.name,
58 });
59 expect(keyString.keyString).toEqual(created.keyString);
60
61 const updated = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.ApiKeys.Key("Maps", {
64 keyId: created.keyId,
65 displayName: "alchemy test maps v2",
66 annotations: { env: "prod", role: "maps" },
67 restrictions: {
68 apiTargets: [{ service: "geocoding-backend.googleapis.com" }],
69 },
70 });
71 }),
72 );
73
74 expect(updated.name).toEqual(created.name);
75 expect(updated.keyId).toEqual(created.keyId);
76 expect(updated.displayName).toEqual("alchemy test maps v2");
77 expect(updated.annotations).toMatchObject({ env: "prod", role: "maps" });
78 expect(updated.restrictions?.apiTargets?.[0]?.service).toEqual(
79 "geocoding-backend.googleapis.com",
80 );
81 expect(updated.keyString).toEqual(created.keyString);
82
83 const fetchedUpdate = yield* apikeys.getProjectsLocationsKeys({
84 name: created.name,
85 });
86 expect(fetchedUpdate.displayName).toEqual("alchemy test maps v2");
87 expect(fetchedUpdate.annotations?.env).toEqual("prod");
88 expect(fetchedUpdate.annotations?.role).toEqual("maps");
89 expect(fetchedUpdate.annotations?.["alchemy-id"]).toEqual(expect.any(String));
90 expect(fetchedUpdate.restrictions?.apiTargets?.[0]?.service).toEqual(
91 "geocoding-backend.googleapis.com",
92 );
93
94 yield* stack.destroy();
95
96 const gone = yield* waitUntilGone(created.name);
97 expect(gone).toEqual("gone");
98 }).pipe(logLevel),
99 { tags: ["provider:gcp", "provider:gcp:apikeys", "live"], timeout: 90_000 },
100 );