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

main
ts 114 lines 3.99 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 osconfig from "@distilled.cloud/gcp/osconfig_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 osconfig.getFoldersLocationsGlobalPolicyOrchestrators({ name }).pipe(
15 Effect.as("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 // The OS Config API is disabled on the testing project (every call fails
25 // with ServiceDisabled), and folder orchestrators need a folder. Set
26 // GCP_TEST_OSCONFIG=1 and GOOGLE_FOLDER_ID on a project with OS Config
27 // enabled.
28 const folderId = process.env.GOOGLE_FOLDER_ID;
29 const runLifecycle = !!process.env.GCP_TEST_OSCONFIG && !!folderId;
30
31 const folderOf = () =>
32 Effect.succeed(
33 folderId === undefined
34 ? ""
35 : folderId.startsWith("folders/")
36 ? folderId
37 : `folders/${folderId}`,
38 );
39
40 test.provider(
41 "getFoldersLocationsGlobalPolicyOrchestrators on a missing orchestrator fails with a typed tag",
42 (stack) =>
43 Effect.gen(function* () {
44 yield* stack.destroy();
45
46 const folder = (yield* folderOf()) || "folders/0";
47 const error = yield* Effect.flip(
48 osconfig.getFoldersLocationsGlobalPolicyOrchestrators({
49 name: `${folder}/locations/global/policyOrchestrators/alchemy-missing-orch`,
50 }),
51 );
52 expect(["ServiceDisabled", "Forbidden"]).toContain(error._tag);
53
54 yield* stack.destroy();
55 }).pipe(logLevel),
56 { tags: ["provider:gcp", "provider:gcp:osconfig", "live"], timeout: 90_000 },
57 );
58
59 test.provider.skipIf(!runLifecycle)(
60 "create, update, and delete a folder policy orchestrator",
61 (stack) =>
62 Effect.gen(function* () {
63 yield* stack.destroy();
64
65 const folder = yield* folderOf();
66
67 const created = yield* stack.deploy(
68 Effect.gen(function* () {
69 return yield* GCP.OSConfig.FoldersLocationsGlobalPolicyOrchestrator("Debian", {
70 folderId: folder,
71 description: "folder validation",
72 labels: { env: "test" },
73 state: "STOPPED",
74 });
75 }),
76 );
77
78 expect(created.policyOrchestratorId).toEqual(expect.any(String));
79 expect(created.folderId).toEqual(folder.replace("folders/", ""));
80 expect(created.parent).toEqual(`${folder}/locations/global`);
81 expect(created.name).toEqual(
82 `${folder}/locations/global/policyOrchestrators/${created.policyOrchestratorId}`,
83 );
84 expect(created.state).toEqual("STOPPED");
85 expect(created.labels).toMatchObject({ env: "test" });
86
87 const fetched = yield* osconfig.getFoldersLocationsGlobalPolicyOrchestrators({
88 name: created.name,
89 });
90 expect(fetched.name).toEqual(created.name);
91 expect(fetched.labels?.env).toEqual("test");
92
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 return yield* GCP.OSConfig.FoldersLocationsGlobalPolicyOrchestrator("Debian", {
96 folderId: folder,
97 policyOrchestratorId: created.policyOrchestratorId,
98 description: "updated folder",
99 labels: { env: "prod" },
100 state: "STOPPED",
101 });
102 }),
103 );
104
105 expect(updated.name).toEqual(created.name);
106 expect(updated.description).toEqual("updated folder");
107 expect(updated.labels).toMatchObject({ env: "prod" });
108
109 yield* stack.destroy();
110 const gone = yield* waitUntilGone(created.name);
111 expect(gone).toEqual("gone");
112 }).pipe(logLevel),
113 { tags: ["provider:gcp", "provider:gcp:osconfig", "live"], timeout: 90_000 },
114 );