1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as saasservicemgmt from "@distilled.cloud/gcp/saasservicemgmt_v1";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import {
7 location,
8 logLevel,
9 currentProject,
10 runLifecycle,
11 waitUntilGone,
12} from "./common.ts";
13
14const { test } = Test.make({ providers: GCP.providers() });
15
16test.provider(
17 "getProjectsLocationsUnitKinds on a missing unit kind fails with a typed tag",
18 (stack) =>
19 Effect.gen(function* () {
20 const project = yield* currentProject;
21 yield* stack.destroy();
22
23 const error = yield* Effect.flip(
24 saasservicemgmt.getProjectsLocationsUnitKinds({
25 name: `projects/${project}/locations/${location}/unitKinds/alchemy-missing-uk`,
26 }),
27 );
28 expect(error._tag).toEqual("NotFound");
29
30 yield* stack.destroy();
31 }).pipe(logLevel),
32 {
33 tags: ["provider:gcp", "provider:gcp:saasservicemgmt", "live"],
34 timeout: 90_000,
35 },
36);
37
38test.provider.skipIf(!runLifecycle)(
39 "create, update, and delete a unit kind",
40 (stack) =>
41 Effect.gen(function* () {
42 yield* stack.destroy();
43
44 const created = yield* stack.deploy(
45 Effect.gen(function* () {
46 const product = yield* GCP.SaasServiceManagement.Saa("Inventory", {
47 location,
48 locations: [{ name: location }],
49 });
50 const kind = yield* GCP.SaasServiceManagement.UnitKind("Store", {
51 location,
52 saas: product.name,
53 labels: { env: "test" },
54 });
55 return { product, kind };
56 }),
57 );
58
59 expect(created.kind.name).toContain("/unitKinds/");
60 expect(created.kind.saasId).toEqual(created.product.saasId);
61 expect(created.kind.labels).toMatchObject({ env: "test" });
62
63 const fetched = yield* saasservicemgmt.getProjectsLocationsUnitKinds({
64 name: created.kind.name,
65 });
66 expect(fetched.name).toEqual(created.kind.name);
67 expect(fetched.labels?.env).toEqual("test");
68 expect(
69 Object.keys(fetched.labels ?? {}).some((key) =>
70 key.startsWith("alchemy-"),
71 ),
72 ).toEqual(true);
73
74 const updated = yield* stack.deploy(
75 Effect.gen(function* () {
76 const product = yield* GCP.SaasServiceManagement.Saa("Inventory", {
77 saasId: created.product.saasId,
78 location,
79 locations: [{ name: location }],
80 });
81 const kind = yield* GCP.SaasServiceManagement.UnitKind("Store", {
82 unitKindId: created.kind.unitKindId,
83 location,
84 saas: product.name,
85 labels: { env: "prod", role: "kind" },
86 });
87 return { product, kind };
88 }),
89 );
90
91 expect(updated.kind.name).toEqual(created.kind.name);
92 expect(updated.kind.labels).toMatchObject({ env: "prod", role: "kind" });
93
94 yield* stack.destroy();
95 const gone = yield* waitUntilGone(
96 saasservicemgmt.getProjectsLocationsUnitKinds({
97 name: created.kind.name,
98 }),
99 );
100 expect(gone).toEqual("gone");
101 }).pipe(logLevel),
102 {
103 tags: ["provider:gcp", "provider:gcp:saasservicemgmt", "live"],
104 timeout: 90_000,
105 },
106);