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 .getProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites({
23 name,
24 })
25 .pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("2 seconds"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35test.provider(
36 "getProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites on a missing target site fails with a typed tag",
37 (stack) =>
38 Effect.gen(function* () {
39 const { project } = yield* GcpEnvironment.current;
40
41 yield* stack.destroy();
42
43 const error = yield* Effect.flip(
44 discoveryengine.getProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites(
45 {
46 name: `projects/${project}/locations/global/collections/default_collection/dataStores/alchemy-missing/siteSearchEngine/targetSites/alchemy-missing`,
47 },
48 ),
49 );
50 expect(error._tag).toEqual("NotFound");
51
52 yield* stack.destroy();
53 }).pipe(logLevel, quotaTolerant),
54 {
55 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
56 timeout: 90_000,
57 },
58);
59
60test.provider.skipIf(!runLifecycle)(
61 "create, update, and delete a collection data store target site",
62 (stack) =>
63 Effect.gen(function* () {
64 yield* stack.destroy();
65
66 const created = yield* stack.deploy(
67 Effect.gen(function* () {
68 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Web", {
69 location: "global",
70 displayName: "docs",
71 contentConfig: "PUBLIC_WEBSITE",
72 disableCmek: true,
73 });
74 const site =
75 yield* GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSite(
76 "Docs",
77 {
78 dataStore: store.name,
79 providedUriPattern: "www.example.com/docs/",
80 type: "INCLUDE",
81 },
82 );
83 return { store, site };
84 }),
85 );
86
87 expect(created.site.name).toContain("/targetSites/");
88 expect(created.site.type).toEqual("INCLUDE");
89
90 const fetched =
91 yield* discoveryengine.getProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites(
92 { name: created.site.name },
93 );
94 expect(fetched.name).toEqual(created.site.name);
95
96 const updated = yield* stack.deploy(
97 Effect.gen(function* () {
98 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Web", {
99 dataStoreId: created.store.dataStoreId,
100 location: "global",
101 collection: created.store.collection,
102 displayName: "docs",
103 contentConfig: "PUBLIC_WEBSITE",
104 disableCmek: true,
105 });
106 const site =
107 yield* GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSite(
108 "Docs",
109 {
110 dataStore: store.name,
111 providedUriPattern: "www.example.com/docs/",
112 type: "EXCLUDE",
113 },
114 );
115 return { store, site };
116 }),
117 );
118
119 expect(updated.site.name).toEqual(created.site.name);
120
121 yield* stack.destroy();
122
123 const gone = yield* waitUntilGone(created.site.name);
124 expect(gone).toEqual("gone");
125 }).pipe(logLevel, quotaTolerant),
126 {
127 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
128 timeout: 120_000,
129 },
130);