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 = "alchds3svc";
20
21const waitUntilGone = (name: string) =>
22 discoveryengine.getProjectsLocationsDataStoresServingConfigs({ 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 "getProjectsLocationsDataStoresServingConfigs on a missing config 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.getProjectsLocationsDataStoresServingConfigs({
42 name: `projects/${project}/locations/global/dataStores/alchemy-missing/servingConfigs/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 serving config",
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.DataStoresServingConfig("Search", {
71 dataStore: parent.name ?? "",
72 displayName: "web-search",
73 solutionType: "SOLUTION_TYPE_SEARCH",
74 });
75 }),
76 );
77
78 expect(created.name).toContain("/servingConfigs/");
79 expect(created.displayName).toEqual("web-search");
80
81 const fetched =
82 yield* discoveryengine.getProjectsLocationsDataStoresServingConfigs({
83 name: created.name,
84 });
85 expect(fetched.name).toEqual(created.name);
86
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* GCP.DiscoveryEngine.DataStoresServingConfig("Search", {
90 dataStore: parent.name ?? "",
91 servingConfigId: created.servingConfigId,
92 displayName: "web-search-v2",
93 solutionType: "SOLUTION_TYPE_SEARCH",
94 });
95 }),
96 );
97
98 expect(updated.name).toEqual(created.name);
99 expect(updated.displayName).toEqual("web-search-v2");
100
101 yield* stack.destroy();
102
103 const gone = yield* waitUntilGone(created.name);
104 expect(gone).toEqual("gone");
105 }).pipe(logLevel, quotaTolerant),
106 {
107 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
108 timeout: 120_000,
109 },
110);