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.getProjectsLocationsCatalogsModels({ 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 "getProjectsLocationsCatalogsModels on a missing model 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.getProjectsLocationsCatalogsModels({
43 name: `projects/${project}/locations/global/catalogs/default_catalog/models/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 model",
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.CatalogsModel("Homepage", {
64 displayName: "homepage recs",
65 type: "recommended-for-you",
66 trainingState: "PAUSED",
67 filteringOption: "RECOMMENDATIONS_FILTERING_DISABLED",
68 });
69 }),
70 );
71
72 expect(created.name).toContain("/models/");
73 expect(created.type).toEqual("recommended-for-you");
74
75 const fetched = yield* retail.getProjectsLocationsCatalogsModels({
76 name: created.name,
77 });
78 expect(fetched.name).toEqual(created.name);
79 expect(fetched.displayName).toEqual(created.displayName);
80
81 const updated = yield* stack.deploy(
82 Effect.gen(function* () {
83 return yield* GCP.Retail.CatalogsModel("Homepage", {
84 modelId: created.modelId,
85 catalog: created.catalog,
86 displayName: "homepage recs",
87 type: "recommended-for-you",
88 trainingState: "PAUSED",
89 filteringOption: "RECOMMENDATIONS_FILTERING_ENABLED",
90 });
91 }),
92 );
93
94 expect(updated.name).toEqual(created.name);
95 expect(updated.filteringOption).toEqual(
96 "RECOMMENDATIONS_FILTERING_ENABLED",
97 );
98
99 yield* stack.destroy();
100
101 const gone = yield* waitUntilGone(created.name);
102 expect(gone).toEqual("gone");
103 }).pipe(logLevel),
104 { tags: ["provider:gcp", "provider:gcp:retail", "live"], timeout: 120_000 },
105);