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.getProjectsLocationsCatalogsServingConfigs({ 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 "getProjectsLocationsCatalogsServingConfigs on a missing serving config 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.getProjectsLocationsCatalogsServingConfigs({
43 name: `projects/${project}/locations/global/catalogs/default_catalog/servingConfigs/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 serving config",
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.CatalogsServingConfig("Preview", {
64 displayName: "preview search",
65 });
66 }),
67 );
68
69 expect(created.name).toContain("/servingConfigs/");
70 expect(created.solutionTypes).toEqual(
71 expect.arrayContaining(["SOLUTION_TYPE_SEARCH"]),
72 );
73
74 const fetched = yield* retail.getProjectsLocationsCatalogsServingConfigs({
75 name: created.name,
76 });
77 expect(fetched.name).toEqual(created.name);
78 expect(fetched.displayName).toEqual(created.displayName);
79
80 const updated = yield* stack.deploy(
81 Effect.gen(function* () {
82 return yield* GCP.Retail.CatalogsServingConfig("Preview", {
83 servingConfigId: created.servingConfigId,
84 catalog: created.catalog,
85 displayName: "preview search updated",
86 });
87 }),
88 );
89
90 expect(updated.name).toEqual(created.name);
91 expect(updated.displayName).toEqual("preview search updated");
92
93 yield* stack.destroy();
94
95 const gone = yield* waitUntilGone(created.name);
96 expect(gone).toEqual("gone");
97 }).pipe(logLevel),
98 { tags: ["provider:gcp", "provider:gcp:retail", "live"], timeout: 90_000 },
99);