The best way to control Claude Code, Codex and more from any computer or phone

main
ts 131 lines 4.29 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { GcpEnvironment } from "@alchemy.run/gcp/Environment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { ensureDataStore, quotaTolerant } from "./parent.ts";
10
11 const { test } = Test.make({ providers: GCP.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 // Conversations need the Large Language Model add-on (BadRequest "This
16 // feature is only available when Large Language Model add-on is enabled.").
17 // Set GCP_TEST_DISCOVERYENGINE_LLM=1 on a project with the add-on.
18 const runLifecycle = !process.env.FAST && !!process.env.GCP_TEST_DISCOVERYENGINE_LLM;
19 const parentId = "alchds3conv";
20
21 const waitUntilGone = (name: string) =>
22 discoveryengine.getProjectsLocationsDataStoresConversations({ 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
32 test.provider(
33 "getProjectsLocationsDataStoresConversations on a missing conversation 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.getProjectsLocationsDataStoresConversations({
42 name: `projects/${project}/locations/global/dataStores/alchemy-missing/conversations/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
55 test.provider.skipIf(!!process.env.FAST || runLifecycle)(
56 "createProjectsLocationsDataStoresConversations without the LLM add-on is rejected with a typed tag",
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 });
66 const error = yield* Effect.flip(
67 discoveryengine.createProjectsLocationsDataStoresConversations({
68 parent: parent.name ?? "",
69 body: { userPseudoId: "alchemy-probe" },
70 }),
71 );
72 expect(error._tag).toEqual("LlmAddOnRequired");
73
74 yield* stack.destroy();
75 }).pipe(logLevel, quotaTolerant),
76 {
77 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
78 timeout: 120_000,
79 },
80 );
81
82 test.provider.skipIf(!runLifecycle)(
83 "create, update, and delete a data store conversation",
84 (stack) =>
85 Effect.gen(function* () {
86 const { project } = yield* GcpEnvironment.current;
87
88 yield* stack.destroy();
89
90 const parent = yield* ensureDataStore(project, parentId, {
91 contentConfig: "NO_CONTENT",
92 });
93
94 const created = yield* stack.deploy(
95 Effect.gen(function* () {
96 return yield* GCP.DiscoveryEngine.DataStoresConversation("Support", {
97 dataStore: parent.name ?? "",
98 });
99 }),
100 );
101
102 expect(created.name).toContain("/conversations/");
103 expect(created.dataStore).toEqual(parent.name);
104
105 const fetched = yield* discoveryengine.getProjectsLocationsDataStoresConversations({
106 name: created.name,
107 });
108 expect(fetched.name).toEqual(created.name);
109
110 const updated = yield* stack.deploy(
111 Effect.gen(function* () {
112 return yield* GCP.DiscoveryEngine.DataStoresConversation("Support", {
113 dataStore: parent.name ?? "",
114 state: "COMPLETED",
115 });
116 }),
117 );
118
119 expect(updated.name).toEqual(created.name);
120 expect(updated.state).toEqual("COMPLETED");
121
122 yield* stack.destroy();
123
124 const gone = yield* waitUntilGone(created.name);
125 expect(gone).toEqual("gone");
126 }).pipe(logLevel, quotaTolerant),
127 {
128 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
129 timeout: 120_000,
130 },
131 );