1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as migrationcenter from "@distilled.cloud/gcp/migrationcenter_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { GcpEnvironment } from "@/GCP/Environment";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 migrationcenter.getProjectsLocationsReportConfigs({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsReportConfigs on a missing config fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 migrationcenter.getProjectsLocationsReportConfigs({
37 name: `projects/${project}/locations/us-central1/reportConfigs/alchemy-missing-config`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel),
44 {
45 tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"],
46 timeout: 90_000,
47 },
48);
49
50test.provider(
51 "create and delete a migration center report config",
52 (stack) =>
53 Effect.gen(function* () {
54 yield* stack.destroy();
55
56 const created = yield* stack.deploy(
57 Effect.gen(function* () {
58 const group = yield* GCP.MigrationCenter.Group("Workloads", {
59 location: "us-central1",
60 displayName: "tco-group",
61 });
62 const prefs = yield* GCP.MigrationCenter.PreferenceSet("Prod", {
63 location: "us-central1",
64 displayName: "tco-prefs",
65 });
66 const config = yield* GCP.MigrationCenter.ReportConfig("Tco", {
67 location: "us-central1",
68 displayName: "tco",
69 description: "quarterly tco",
70 groupPreferencesetAssignments: [
71 { group: group.name, preferenceSet: prefs.name },
72 ],
73 });
74 return { group, prefs, config };
75 }),
76 );
77
78 expect(created.config.reportConfigId).toEqual(expect.any(String));
79 expect(created.config.displayName).toEqual("tco");
80 expect(created.config.description).toEqual("quarterly tco");
81 expect(created.config.groupPreferencesetAssignments).toHaveLength(1);
82 expect(created.config.groupPreferencesetAssignments[0]?.group).toEqual(
83 created.group.name,
84 );
85 expect(
86 created.config.groupPreferencesetAssignments[0]?.preferenceSet,
87 ).toEqual(created.prefs.name);
88
89 const fetched = yield* migrationcenter.getProjectsLocationsReportConfigs({
90 name: created.config.name,
91 });
92 expect(fetched.name).toEqual(created.config.name);
93 expect(fetched.description).toContain("alchemy-id=");
94 expect(fetched.description).toContain("quarterly tco");
95
96 const updated = yield* stack.deploy(
97 Effect.gen(function* () {
98 const group = yield* GCP.MigrationCenter.Group("Workloads", {
99 groupId: created.group.groupId,
100 location: "us-central1",
101 displayName: "tco-group",
102 });
103 const prefs = yield* GCP.MigrationCenter.PreferenceSet("Prod", {
104 preferenceSetId: created.prefs.preferenceSetId,
105 location: "us-central1",
106 displayName: "tco-prefs",
107 });
108 const config = yield* GCP.MigrationCenter.ReportConfig("Tco", {
109 reportConfigId: created.config.reportConfigId,
110 location: "us-central1",
111 displayName: "tco-v2",
112 description: "quarterly tco v2",
113 groupPreferencesetAssignments: [
114 { group: group.name, preferenceSet: prefs.name },
115 ],
116 });
117 return { group, prefs, config };
118 }),
119 );
120
121 expect(updated.config.reportConfigId).toEqual(
122 created.config.reportConfigId,
123 );
124 expect(updated.config.displayName).toEqual("tco-v2");
125 expect(updated.config.description).toEqual("quarterly tco v2");
126
127 yield* stack.destroy();
128 const gone = yield* waitUntilGone(updated.config.name);
129 expect(gone).toEqual("gone");
130 }).pipe(logLevel),
131 {
132 tags: ["provider:gcp", "provider:gcp:migrationcenter", "live"],
133 timeout: 120_000,
134 },
135);