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.getProjectsLocationsCollectionsEnginesSessions({ name }).pipe(
22 Effect.as("found" as const),
23 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
24 Effect.repeat({
25 schedule: Schedule.spaced("2 seconds"),
26 until: (status) => status === "gone",
27 times: 10,
28 }),
29 );
30
31test.provider(
32 "getProjectsLocationsCollectionsEnginesSessions on a missing session fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 discoveryengine.getProjectsLocationsCollectionsEnginesSessions({
41 name: `projects/${project}/locations/global/collections/default_collection/engines/alchemy-missing/sessions/alchemy-missing`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel, quotaTolerant),
48 {
49 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
50 timeout: 90_000,
51 },
52);
53
54test.provider.skipIf(!runLifecycle)(
55 "create, update, and delete an engine session",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const store = yield* GCP.DiscoveryEngine.CollectionsDataStore(
63 "Docs",
64 {
65 location: "global",
66 displayName: "session-docs",
67 },
68 );
69 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
70 "Search",
71 {
72 location: "global",
73 dataStoreIds: [store.dataStoreId],
74 displayName: "session engine",
75 },
76 );
77 const session = yield* GCP.DiscoveryEngine.CollectionsEnginesSession(
78 "Chat",
79 {
80 engine: engine.name,
81 displayName: "support",
82 },
83 );
84 return { store, engine, session };
85 }),
86 );
87
88 expect(created.session.name).toContain("/sessions/");
89 expect(created.session.engine).toEqual(created.engine.name);
90 expect(created.session.displayName).toEqual("support");
91
92 const fetched =
93 yield* discoveryengine.getProjectsLocationsCollectionsEnginesSessions({
94 name: created.session.name,
95 });
96 expect(fetched.name).toEqual(created.session.name);
97 expect(fetched.displayName).toEqual("support");
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 displayName: "session-docs",
107 },
108 );
109 const engine = yield* GCP.DiscoveryEngine.CollectionsEngine(
110 "Search",
111 {
112 engineId: created.engine.engineId,
113 location: "global",
114 dataStoreIds: [store.dataStoreId],
115 displayName: "session engine",
116 },
117 );
118 const session = yield* GCP.DiscoveryEngine.CollectionsEnginesSession(
119 "Chat",
120 {
121 engine: engine.name,
122 sessionId: created.session.sessionId,
123 displayName: "support-prod",
124 isPinned: true,
125 },
126 );
127 return { store, engine, session };
128 }),
129 );
130
131 expect(updated.session.name).toEqual(created.session.name);
132 expect(updated.session.displayName).toEqual("support-prod");
133 expect(updated.session.isPinned).toEqual(true);
134
135 yield* stack.destroy();
136
137 const gone = yield* waitUntilGone(created.session.name);
138 expect(gone).toEqual("gone");
139 }).pipe(logLevel, quotaTolerant),
140 {
141 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
142 timeout: 120_000,
143 },
144);