1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import { quotaTolerant } from "./parent.ts";
4import * as Test from "@/Test/Alchemy";
5import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1";
6import { expect } from "alchemy-test";
7import * as Effect from "effect/Effect";
8import { MinimumLogLevel } from "effect/References";
9import * as Schedule from "effect/Schedule";
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;
19
20const waitUntilGone = (name: string) =>
21 discoveryengine.getProjectsLocationsCollectionsEnginesControls({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31test.provider(
32 "getProjectsLocationsCollectionsEnginesControls on a missing control fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 discoveryengine.getProjectsLocationsCollectionsEnginesControls({
41 name: `projects/${project}/locations/global/collections/default_collection/engines/alchemy-missing/controls/alchemy-missing`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel, quotaTolerant),
48 {
49 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
50 timeout: 90_000,
51 },
52);
53
54test.provider.skipIf(!runLifecycle)(
55 "create, update, and delete an engine control",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
63 "Docs",
64 {
65 location: "global",
66 displayName: "control-docs",
67 },
68 );
69 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
70 "Search",
71 {
72 location: "global",
73 dataStoreIds: [store.dataStoreId],
74 displayName: "control engine",
75 },
76 );
77 const control = yield* GCP.DiscoveryEngine.CollectionsEnginesControl(
78 "Synonyms",
79 {
80 engine: engine.name,
81 displayName: "hello-hi",
82 synonymsAction: { synonyms: ["hello", "hi"] },
83 },
84 );
85 return { store, engine, control };
86 }),
87 );
88
89 expect(created.control.name).toContain("/controls/");
90 expect(created.control.engine).toEqual(created.engine.name);
91 expect(created.control.displayName).toEqual("hello-hi");
92
93 const fetched =
94 yield* discoveryengine.getProjectsLocationsCollectionsEnginesControls({
95 name: created.control.name,
96 });
97 expect(fetched.name).toEqual(created.control.name);
98 expect(fetched.synonymsAction?.synonyms).toEqual(
99 expect.arrayContaining(["hello", "hi"]),
100 );
101
102 const updated = yield* stack.deploy(
103 Effect.gen(function* () {
104 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
105 "Docs",
106 {
107 dataStoreId: created.store.dataStoreId,
108 location: "global",
109 displayName: "control-docs",
110 },
111 );
112 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
113 "Search",
114 {
115 engineId: created.engine.engineId,
116 location: "global",
117 dataStoreIds: [store.dataStoreId],
118 displayName: "control engine",
119 },
120 );
121 const control = yield* GCP.DiscoveryEngine.CollectionsEnginesControl(
122 "Synonyms",
123 {
124 engine: engine.name,
125 controlId: created.control.controlId,
126 displayName: "greetings",
127 synonymsAction: { synonyms: ["hello", "hi", "hey"] },
128 },
129 );
130 return { store, engine, control };
131 }),
132 );
133
134 expect(updated.control.name).toEqual(created.control.name);
135 expect(updated.control.displayName).toEqual("greetings");
136
137 yield* stack.destroy();
138
139 const gone = yield* waitUntilGone(created.control.name);
140 expect(gone).toEqual("gone");
141 }).pipe(logLevel, quotaTolerant),
142 {
143 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
144 timeout: 120_000,
145 },
146);