1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as retail from "@distilled.cloud/gcp/retail_v2";
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
17// Retail only serves projects that accepted the Retail data use terms (a
18// one-time console step); until then every call fails with
19// RetailDataUseTermsNotAccepted. Set GCP_TEST_RETAIL_TERMS=1 once accepted.
20const runLifecycle = !!process.env.GCP_TEST_RETAIL_TERMS;
21
22const waitUntilGone = (name: string) =>
23 retail.getProjectsLocationsCatalogsControls({ name }).pipe(
24 Effect.as("found" as const),
25 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
26 Effect.repeat({
27 schedule: Schedule.spaced("2 seconds"),
28 until: (status) => status === "gone",
29 times: 10,
30 }),
31 );
32
33test.provider(
34 "getProjectsLocationsCatalogsControls on a missing control fails with a typed tag",
35 (stack) =>
36 Effect.gen(function* () {
37 const { project } = yield* GcpEnvironment.current;
38
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 retail.getProjectsLocationsCatalogsControls({
43 name: `projects/${project}/locations/global/catalogs/default_catalog/controls/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual(
47 runLifecycle ? "NotFound" : "RetailDataUseTermsNotAccepted",
48 );
49
50 yield* stack.destroy();
51 }).pipe(logLevel),
52 { tags: ["provider:gcp", "provider:gcp:retail", "live"], timeout: 90_000 },
53);
54
55test.provider.skipIf(!runLifecycle)(
56 "create, update, and delete a catalog control",
57 (stack) =>
58 Effect.gen(function* () {
59 yield* stack.destroy();
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.Retail.CatalogsControl("Greetings", {
64 displayName: "greetings",
65 synonyms: ["hello", "hi"],
66 });
67 }),
68 );
69
70 expect(created.name).toContain("/controls/");
71 expect(created.synonyms).toEqual(expect.arrayContaining(["hello", "hi"]));
72
73 const fetched = yield* retail.getProjectsLocationsCatalogsControls({
74 name: created.name,
75 });
76 expect(fetched.name).toEqual(created.name);
77 expect(fetched.displayName).toEqual(created.displayName);
78
79 const updated = yield* stack.deploy(
80 Effect.gen(function* () {
81 return yield* GCP.Retail.CatalogsControl("Greetings", {
82 controlId: created.controlId,
83 catalog: created.catalog,
84 displayName: "greetings-updated",
85 synonyms: ["hello", "hi", "hey"],
86 });
87 }),
88 );
89
90 expect(updated.name).toEqual(created.name);
91 expect(updated.synonyms).toEqual(
92 expect.arrayContaining(["hello", "hi", "hey"]),
93 );
94
95 yield* stack.destroy();
96
97 const gone = yield* waitUntilGone(created.name);
98 expect(gone).toEqual("gone");
99 }).pipe(logLevel),
100 { tags: ["provider:gcp", "provider:gcp:retail", "live"], timeout: 90_000 },
101);