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 .getProjectsLocationsCollectionsDataStoresSessions({ 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 "getProjectsLocationsCollectionsDataStoresSessions on a missing session 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.getProjectsLocationsCollectionsDataStoresSessions({
43 name: `projects/${project}/locations/global/collections/default_collection/dataStores/alchemy-missing/sessions/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 session",
58 (stack) =>
59 Effect.gen(function* () {
60 yield* stack.destroy();
61
62 const created = yield* stack.deploy(
63 Effect.gen(function* () {
64 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
65 "Docs",
66 {
67 location: "global",
68 displayName: "docs",
69 disableCmek: true,
70 },
71 );
72 const session =
73 yield* GCP.DiscoveryEngine.CollectionsDataStoresSession("Chat", {
74 dataStore: store.name,
75 displayName: "chat",
76 isPinned: true,
77 });
78 return { store, session };
79 }),
80 );
81
82 expect(created.session.name).toContain("/sessions/");
83 expect(created.session.displayName).toEqual("chat");
84 expect(created.session.isPinned).toEqual(true);
85
86 const fetched =
87 yield* discoveryengine.getProjectsLocationsCollectionsDataStoresSessions(
88 { name: created.session.name },
89 );
90 expect(fetched.name).toEqual(created.session.name);
91 expect(fetched.displayName).toEqual("chat");
92
93 const updated = yield* stack.deploy(
94 Effect.gen(function* () {
95 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
96 "Docs",
97 {
98 dataStoreId: created.store.dataStoreId,
99 location: "global",
100 collection: created.store.collection,
101 displayName: "docs",
102 disableCmek: true,
103 },
104 );
105 const session =
106 yield* GCP.DiscoveryEngine.CollectionsDataStoresSession("Chat", {
107 dataStore: store.name,
108 sessionId: created.session.sessionId,
109 displayName: "prod",
110 isPinned: false,
111 });
112 return { store, session };
113 }),
114 );
115
116 expect(updated.session.name).toEqual(created.session.name);
117 expect(updated.session.displayName).toEqual("prod");
118 expect(updated.session.isPinned).toEqual(false);
119
120 yield* stack.destroy();
121
122 const gone = yield* waitUntilGone(created.session.name);
123 expect(gone).toEqual("gone");
124 }).pipe(logLevel, quotaTolerant),
125 {
126 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
127 timeout: 120_000,
128 },
129);