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

main
ts 168 lines 6.47 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 rma from "@distilled.cloud/gcp/rapidmigrationassessment_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 // The Rapid Migration Assessment API is disabled on the testing project
15 // (every call fails with ServiceDisabled). Set GCP_TEST_RMA=1 on a project
16 // with the API enabled.
17 const runLifecycle = !!process.env.GCP_TEST_RMA && !process.env.FAST;
18
19 const waitUntilGone = (name: string) =>
20 rma.getProjectsLocationsCollectors({ name }).pipe(
21 Effect.as("found" as const),
22 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
23 Effect.repeat({
24 schedule: Schedule.spaced("2 seconds"),
25 until: (status) => status === "gone",
26 times: 10,
27 }),
28 );
29
30 // Skipped: every Rapid Migration Assessment request (even listing locations)
31 // answers an HTML 502 after ~40s, so the probe can't complete.
32 test.provider.skip(
33 "getProjectsLocationsCollectors on a missing collector fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37 const parent = `projects/${project}/locations/us-central1`;
38 const missingName = `${parent}/collectors/alchemy-missing-collector`;
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(rma.getProjectsLocationsCollectors({ name: missingName }));
42 expect(error._tag).toEqual("ServiceDisabled");
43
44 yield* stack.destroy();
45 }).pipe(logLevel),
46 {
47 tags: ["provider:gcp", "provider:gcp:rapidmigrationassessment", "live"],
48 timeout: 90_000,
49 },
50 );
51
52 // Skipped: every Rapid Migration Assessment request (even listing locations)
53 // answers an HTML 502 after ~40s, so the probe can't complete.
54 test.provider.skip(
55 "createProjectsLocationsCollectors is rejected with ServiceDisabled when Rapid Migration Assessment is disabled",
56 (stack) =>
57 Effect.gen(function* () {
58 // Only meaningful while the API is disabled: enabled, the create would
59 // provision a real resource.
60 if (yield* isServiceEnabled("rapidmigrationassessment.googleapis.com")) return;
61 const { project } = yield* GcpEnvironment.current;
62 const parent = `projects/${project}/locations/us-central1`;
63 const serviceAccount = `alchemy-testing@${project}.iam.gserviceaccount.com`;
64 yield* stack.destroy();
65
66 const error = yield* Effect.flip(
67 rma.createProjectsLocationsCollectors({
68 parent,
69 collectorId: "alchemyrmaprobe",
70 body: {
71 displayName: "alchemy-probe",
72 collectionDays: 7,
73 expectedAssetCount: "1",
74 serviceAccount,
75 },
76 }),
77 );
78 expect(error._tag).toEqual("ServiceDisabled");
79
80 yield* stack.destroy();
81 }).pipe(logLevel),
82 {
83 tags: ["provider:gcp", "provider:gcp:rapidmigrationassessment", "live"],
84 timeout: 90_000,
85 },
86 );
87
88 test.provider.skipIf(!runLifecycle)(
89 "create, update, and delete a collector",
90 (stack) =>
91 Effect.gen(function* () {
92 const { project } = yield* GcpEnvironment.current;
93 const parent = `projects/${project}/locations/us-central1`;
94 const missingName = `${parent}/collectors/alchemy-missing-collector`;
95 const serviceAccount = `alchemy-testing@${project}.iam.gserviceaccount.com`;
96 yield* stack.destroy();
97
98 const created = yield* stack.deploy(
99 Effect.gen(function* () {
100 return yield* GCP.RapidMigrationAssessment.Collector("OnPrem", {
101 location: "us-central1",
102 displayName: "on-prem collector",
103 description: "inventory appliance",
104 collectionDays: 7,
105 expectedAssetCount: 10,
106 serviceAccount,
107 labels: { env: "test" },
108 });
109 }),
110 );
111
112 expect(created.collectorId).toEqual(expect.any(String));
113 expect(created.name).toEqual(`${parent}/collectors/${created.collectorId}`);
114 expect(created.project).toEqual(project);
115 expect(created.location).toEqual("us-central1");
116 expect(created.displayName).toEqual("on-prem collector");
117 expect(created.description).toEqual("inventory appliance");
118 expect(created.collectionDays).toEqual(7);
119 expect(created.labels).toMatchObject({ env: "test" });
120
121 const fetched = yield* rma.getProjectsLocationsCollectors({
122 name: created.name,
123 });
124 expect(fetched.name).toEqual(created.name);
125 expect(fetched.displayName).toEqual("on-prem collector");
126 expect(fetched.labels?.env).toEqual("test");
127 expect(fetched.labels?.["alchemy-id"]).toEqual(expect.any(String));
128 expect(fetched.collectionDays).toEqual(7);
129
130 const updated = yield* stack.deploy(
131 Effect.gen(function* () {
132 return yield* GCP.RapidMigrationAssessment.Collector("OnPrem", {
133 collectorId: created.collectorId,
134 location: "us-central1",
135 displayName: "on-prem collector v2",
136 description: "inventory appliance v2",
137 collectionDays: 14,
138 expectedAssetCount: 25,
139 serviceAccount,
140 labels: { env: "prod", role: "rma" },
141 });
142 }),
143 );
144
145 expect(updated.name).toEqual(created.name);
146 expect(updated.collectorId).toEqual(created.collectorId);
147 expect(updated.displayName).toEqual("on-prem collector v2");
148 expect(updated.description).toEqual("inventory appliance v2");
149 expect(updated.collectionDays).toEqual(14);
150 expect(updated.labels).toMatchObject({ env: "prod", role: "rma" });
151
152 const fetchedUpdate = yield* rma.getProjectsLocationsCollectors({
153 name: created.name,
154 });
155 expect(fetchedUpdate.displayName).toEqual("on-prem collector v2");
156 expect(fetchedUpdate.collectionDays).toEqual(14);
157 expect(fetchedUpdate.labels?.env).toEqual("prod");
158 expect(fetchedUpdate.labels?.role).toEqual("rma");
159
160 yield* stack.destroy();
161 const gone = yield* waitUntilGone(created.name);
162 expect(gone).toEqual("gone");
163 }).pipe(logLevel),
164 {
165 tags: ["provider:gcp", "provider:gcp:rapidmigrationassessment", "live"],
166 timeout: 120_000,
167 },
168 );