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 = "alchds3schx";
20
21const waitUntilGone = (name: string) =>
22 discoveryengine.getProjectsLocationsDataStoresSchemas({ 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 "getProjectsLocationsDataStoresSchemas on a missing schema 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.getProjectsLocationsDataStoresSchemas({
42 name: `projects/${project}/locations/global/dataStores/alchemy-missing/schemas/alchemy-missing`,
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 schema",
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 skipDefaultSchemaCreation: true,
66 });
67
68 const created = yield* stack.deploy(
69 Effect.gen(function* () {
70 return yield* GCP.DiscoveryEngine.DataStoresSchema("Catalog", {
71 dataStore: parent.name ?? "",
72 schemaId: "alchschema",
73 jsonSchema: JSON.stringify({
74 $schema: "https://json-schema.org/draft/2020-12/schema",
75 type: "object",
76 properties: {
77 title: { type: "string" },
78 sku: { type: "string" },
79 },
80 }),
81 });
82 }),
83 );
84
85 expect(created.name).toContain("/schemas/");
86 expect(created.jsonSchema).toContain("sku");
87
88 const fetched =
89 yield* discoveryengine.getProjectsLocationsDataStoresSchemas({
90 name: created.name,
91 });
92 expect(fetched.name).toEqual(created.name);
93
94 const updated = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.DiscoveryEngine.DataStoresSchema("Catalog", {
97 dataStore: parent.name ?? "",
98 schemaId: "alchschema",
99 jsonSchema: JSON.stringify({
100 $schema: "https://json-schema.org/draft/2020-12/schema",
101 type: "object",
102 properties: {
103 title: { type: "string" },
104 sku: { type: "string" },
105 },
106 }),
107 });
108 }),
109 );
110
111 expect(updated.name).toEqual(created.name);
112 expect(updated.jsonSchema).toContain("sku");
113
114 yield* stack.destroy();
115
116 const gone = yield* waitUntilGone(created.name);
117 expect(gone).toEqual("gone");
118 }).pipe(logLevel, quotaTolerant),
119 {
120 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
121 timeout: 120_000,
122 },
123);