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.getProjectsLocationsCollectionsEngines({ 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 "getProjectsLocationsCollectionsEngines on a missing engine 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.getProjectsLocationsCollectionsEngines({
41 name: `projects/${project}/locations/global/collections/default_collection/engines/alchemy-eng-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 a collection engine",
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: "engine-docs",
67 },
68 );
69 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
70 "Search",
71 {
72 location: "global",
73 dataStoreIds: [store.dataStoreId],
74 displayName: "docs search",
75 solutionType: "SOLUTION_TYPE_SEARCH",
76 industryVertical: "GENERIC",
77 },
78 );
79 return { store, engine };
80 }),
81 );
82
83 expect(created.engine.name).toContain("/engines/");
84 expect(created.engine.displayName).toEqual("docs search");
85 expect(created.engine.solutionType).toEqual("SOLUTION_TYPE_SEARCH");
86 expect(created.engine.dataStoreIds).toContain(created.store.dataStoreId);
87
88 const fetched =
89 yield* discoveryengine.getProjectsLocationsCollectionsEngines({
90 name: created.engine.name,
91 });
92 expect(fetched.name).toEqual(created.engine.name);
93 expect(fetched.displayName).toEqual("docs search");
94
95 const updated = yield* stack.deploy(
96 Effect.gen(function* () {
97 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
98 "Docs",
99 {
100 dataStoreId: created.store.dataStoreId,
101 location: "global",
102 displayName: "engine-docs",
103 },
104 );
105 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
106 "Search",
107 {
108 engineId: created.engine.engineId,
109 location: "global",
110 dataStoreIds: [store.dataStoreId],
111 displayName: "docs search prod",
112 solutionType: "SOLUTION_TYPE_SEARCH",
113 industryVertical: "GENERIC",
114 },
115 );
116 return { store, engine };
117 }),
118 );
119
120 expect(updated.engine.name).toEqual(created.engine.name);
121 expect(updated.engine.displayName).toEqual("docs search prod");
122
123 yield* stack.destroy();
124
125 const gone = yield* waitUntilGone(created.engine.name);
126 expect(gone).toEqual("gone");
127 }).pipe(logLevel, quotaTolerant),
128 {
129 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
130 timeout: 120_000,
131 },
132);