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 .getProjectsLocationsCollectionsDataStoresSchemas({ 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 "getProjectsLocationsCollectionsDataStoresSchemas on a missing schema 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.getProjectsLocationsCollectionsDataStoresSchemas({
43 name: `projects/${project}/locations/global/collections/default_collection/dataStores/alchemy-missing/schemas/alchemy-missing`,
44 }),
45 );
46 expect(error._tag).toEqual("NotFound");
47
48 yield* stack.destroy();
49 }).pipe(logLevel, quotaTolerant),
50 {
51 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
52 timeout: 90_000,
53 },
54);
55
56test.provider.skipIf(!runLifecycle)(
57 "create, update, and delete a collection data store schema",
58 (stack) =>
59 Effect.gen(function* () {
60 yield* stack.destroy();
61
62 const parent = yield* stack.deploy(
63 Effect.gen(function* () {
64 return yield* GCP.DiscoveryEngine.CollectionsDataStore("Docs", {
65 location: "global",
66 displayName: "docs",
67 skipDefaultSchemaCreation: true,
68 disableCmek: true,
69 });
70 }),
71 );
72
73 const created = yield* stack.deploy(
74 Effect.gen(function* () {
75 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
76 "Docs",
77 {
78 dataStoreId: parent.dataStoreId,
79 location: "global",
80 collection: parent.collection,
81 displayName: "docs",
82 skipDefaultSchemaCreation: true,
83 disableCmek: true,
84 },
85 );
86 const schema = yield* GCP.DiscoveryEngine.CollectionsDataStoresSchema(
87 "Fields",
88 {
89 dataStore: store.name,
90 schemaId: "fields",
91 jsonSchema: JSON.stringify({
92 $schema: "https://json-schema.org/draft/2020-12/schema",
93 type: "object",
94 properties: { title: { type: "string" } },
95 }),
96 },
97 );
98 return { store, schema };
99 }),
100 );
101
102 expect(created.schema.name).toContain("/schemas/");
103 expect(created.schema.jsonSchema).toContain("title");
104
105 const fetched =
106 yield* discoveryengine.getProjectsLocationsCollectionsDataStoresSchemas(
107 { name: created.schema.name },
108 );
109 expect(fetched.name).toEqual(created.schema.name);
110
111 const updated = yield* stack.deploy(
112 Effect.gen(function* () {
113 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
114 "Docs",
115 {
116 dataStoreId: created.store.dataStoreId,
117 location: "global",
118 collection: created.store.collection,
119 displayName: "docs",
120 disableCmek: true,
121 },
122 );
123 const schema = yield* GCP.DiscoveryEngine.CollectionsDataStoresSchema(
124 "Fields",
125 {
126 dataStore: store.name,
127 schemaId: "fields",
128 jsonSchema: JSON.stringify({
129 $schema: "https://json-schema.org/draft/2020-12/schema",
130 type: "object",
131 properties: {
132 title: { type: "string" },
133 uri: { type: "string" },
134 },
135 }),
136 },
137 );
138 return { store, schema };
139 }),
140 );
141
142 expect(updated.schema.name).toEqual(created.schema.name);
143 expect(updated.schema.jsonSchema).toContain("uri");
144
145 yield* stack.destroy();
146
147 const gone = yield* waitUntilGone(created.schema.name);
148 expect(gone).toEqual("gone");
149 }).pipe(logLevel, quotaTolerant),
150 {
151 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
152 timeout: 120_000,
153 },
154);