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 .getProjectsLocationsCollectionsDataStoresBranchesDocuments({ 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 "getProjectsLocationsCollectionsDataStoresBranchesDocuments on a missing document 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.getProjectsLocationsCollectionsDataStoresBranchesDocuments(
43 {
44 name: `projects/${project}/locations/global/collections/default_collection/dataStores/alchemy-missing/branches/default_branch/documents/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 document",
60 (stack) =>
61 Effect.gen(function* () {
62 yield* stack.destroy();
63
64 const created = yield* stack.deploy(
65 Effect.gen(function* () {
66 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
67 "Docs",
68 {
69 location: "global",
70 displayName: "docs",
71 contentConfig: "NO_CONTENT",
72 disableCmek: true,
73 },
74 );
75 const document =
76 yield* GCP.DiscoveryEngine.CollectionsDataStoresBranchesDocument(
77 "About",
78 {
79 dataStore: store.name,
80 jsonData: JSON.stringify({
81 title: "About us",
82 uri: "https://example.com/about",
83 }),
84 },
85 );
86 return { store, document };
87 }),
88 );
89
90 expect(created.document.name).toContain("/documents/");
91 expect(created.document.dataStore).toEqual(created.store.name);
92
93 const fetched =
94 yield* discoveryengine.getProjectsLocationsCollectionsDataStoresBranchesDocuments(
95 { name: created.document.name },
96 );
97 expect(fetched.name).toEqual(created.document.name);
98
99 const updated = yield* stack.deploy(
100 Effect.gen(function* () {
101 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
102 "Docs",
103 {
104 dataStoreId: created.store.dataStoreId,
105 location: "global",
106 collection: created.store.collection,
107 displayName: "docs",
108 contentConfig: "NO_CONTENT",
109 disableCmek: true,
110 },
111 );
112 const document =
113 yield* GCP.DiscoveryEngine.CollectionsDataStoresBranchesDocument(
114 "About",
115 {
116 dataStore: store.name,
117 documentId: created.document.documentId,
118 jsonData: JSON.stringify({
119 title: "About the team",
120 uri: "https://example.com/about",
121 }),
122 },
123 );
124 return { store, document };
125 }),
126 );
127
128 expect(updated.document.name).toEqual(created.document.name);
129
130 yield* stack.destroy();
131
132 const gone = yield* waitUntilGone(created.document.name);
133 expect(gone).toEqual("gone");
134 }).pipe(logLevel, quotaTolerant),
135 {
136 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
137 timeout: 120_000,
138 },
139);