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
22 .getProjectsLocationsCollectionsDataStoresServingConfigs({ name })
23 .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 "getProjectsLocationsCollectionsDataStoresServingConfigs 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 discoveryengine.getProjectsLocationsCollectionsDataStoresServingConfigs(
43 {
44 name: `projects/${project}/locations/global/collections/default_collection/dataStores/alchemy-missing/servingConfigs/alchemy-missing`,
45 },
46 ),
47 );
48 expect(error._tag).toEqual("NotFound");
49
50 yield* stack.destroy();
51 }).pipe(logLevel, quotaTolerant),
52 {
53 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
54 timeout: 90_000,
55 },
56);
57
58test.provider.skipIf(!runLifecycle)(
59 "create, update, and delete a collection data store serving config",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const parent = yield* stack.deploy(
65 Effect.gen(function* () {
66 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
67 "Docs",
68 {
69 location: "global",
70 displayName: "docs",
71 disableCmek: true,
72 },
73 );
74 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
75 "Search",
76 {
77 location: "global",
78 dataStoreIds: [store.dataStoreId],
79 displayName: "eng",
80 solutionType: "SOLUTION_TYPE_SEARCH",
81 industryVertical: "GENERIC",
82 },
83 );
84 return { store, engine };
85 }),
86 );
87
88 const created = yield* stack.deploy(
89 Effect.gen(function* () {
90 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
91 "Docs",
92 {
93 dataStoreId: parent.store.dataStoreId,
94 location: "global",
95 collection: parent.store.collection,
96 displayName: "docs",
97 disableCmek: true,
98 },
99 );
100 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
101 "Search",
102 {
103 engineId: parent.engine.engineId,
104 location: "global",
105 dataStoreIds: [store.dataStoreId],
106 displayName: "eng",
107 solutionType: "SOLUTION_TYPE_SEARCH",
108 industryVertical: "GENERIC",
109 },
110 );
111 const serving =
112 yield* GCP.DiscoveryEngine.CollectionsDataStoresServingConfig(
113 "Preview",
114 {
115 dataStore: store.name,
116 displayName: parent.engine.engineId.slice(0, 2),
117 },
118 );
119 return { store, engine, serving };
120 }),
121 );
122
123 expect(created.serving.name).toContain("/servingConfigs/");
124
125 const fetched =
126 yield* discoveryengine.getProjectsLocationsCollectionsDataStoresServingConfigs(
127 { name: created.serving.name },
128 );
129 expect(fetched.name).toEqual(created.serving.name);
130 expect(fetched.displayName).toEqual(created.serving.displayName);
131
132 const updated = yield* stack.deploy(
133 Effect.gen(function* () {
134 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
135 "Docs",
136 {
137 dataStoreId: created.store.dataStoreId,
138 location: "global",
139 collection: created.store.collection,
140 displayName: "docs",
141 disableCmek: true,
142 },
143 );
144 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
145 "Search",
146 {
147 engineId: created.engine.engineId,
148 location: "global",
149 dataStoreIds: [store.dataStoreId],
150 displayName: "eng",
151 solutionType: "SOLUTION_TYPE_SEARCH",
152 industryVertical: "GENERIC",
153 },
154 );
155 const serving =
156 yield* GCP.DiscoveryEngine.CollectionsDataStoresServingConfig(
157 "Preview",
158 {
159 dataStore: store.name,
160 servingConfigId: created.serving.servingConfigId,
161 displayName: created.engine.engineId.slice(0, 4),
162 },
163 );
164 return { store, engine, serving };
165 }),
166 );
167
168 expect(updated.serving.name).toEqual(created.serving.name);
169
170 yield* stack.destroy();
171
172 const gone = yield* waitUntilGone(created.serving.name);
173 expect(gone).toEqual("gone");
174 }).pipe(logLevel, quotaTolerant),
175 {
176 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
177 timeout: 120_000,
178 },
179);