1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9import { ensureDataStore, quotaTolerant } from "./parent.ts";
10
11const { test } = Test.make({ providers: GCP.providers() });
12
13const logLevel = Effect.provideService(
14 MinimumLogLevel,
15 process.env.DEBUG ? "Debug" : "Info",
16);
17
18const runLifecycle = !process.env.FAST;
19const parentId = "alchds3ctl";
20
21const waitUntilGone = (name: string) =>
22 discoveryengine.getProjectsLocationsDataStoresControls({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32test.provider(
33 "getProjectsLocationsDataStoresControls on a missing control fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37
38 yield* stack.destroy();
39
40 const error = yield* Effect.flip(
41 discoveryengine.getProjectsLocationsDataStoresControls({
42 name: `projects/${project}/locations/global/collections/default_collection/dataStores/alchemy-missing/controls/alchemymissing`,
43 }),
44 );
45 expect(error._tag).toEqual("NotFound");
46
47 yield* stack.destroy();
48 }).pipe(logLevel, quotaTolerant),
49 {
50 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
51 timeout: 90_000,
52 },
53);
54
55test.provider.skipIf(!runLifecycle)(
56 "create, update, and delete a data store control",
57 (stack) =>
58 Effect.gen(function* () {
59 const { project } = yield* GcpEnvironment.current;
60
61 yield* stack.destroy();
62
63 const parent = yield* ensureDataStore(project, parentId, {
64 contentConfig: "NO_CONTENT",
65 withEngine: true,
66 });
67
68 const created = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.DiscoveryEngine.DataStoresControl("Synonyms", {
71 dataStore: parent.name ?? "",
72 displayName: "happy-glad",
73 synonymsAction: { synonyms: ["happy", "glad"] },
74 });
75 }),
76 );
77
78 expect(created.name).toContain("/controls/");
79 expect(created.dataStore).toEqual(parent.name);
80 expect(created.displayName).toEqual("happy-glad");
81
82 const fetched =
83 yield* discoveryengine.getProjectsLocationsDataStoresControls({
84 name: created.name,
85 });
86 expect(fetched.name).toEqual(created.name);
87
88 const updated = yield* stack.deploy(
89 Effect.gen(function* () {
90 return yield* GCP.DiscoveryEngine.DataStoresControl("Synonyms", {
91 dataStore: parent.name ?? "",
92 controlId: created.controlId,
93 displayName: "mood",
94 synonymsAction: { synonyms: ["happy", "glad"] },
95 });
96 }),
97 );
98
99 expect(updated.name).toEqual(created.name);
100 expect(updated.displayName).toEqual("mood");
101
102 yield* stack.destroy();
103
104 const gone = yield* waitUntilGone(created.name);
105 expect(gone).toEqual("gone");
106 }).pipe(logLevel, quotaTolerant),
107 {
108 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
109 timeout: 120_000,
110 },
111);