The best way to control Claude Code, Codex and more from any computer or phone
| 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 { 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 | // createAssistants is rejected on a plain search engine with BadRequest "Only |
| 16 | // default_assistant is allowed." even for assistantId default_assistant; it |
| 17 | // needs a Gemini Enterprise (Agentspace) engine. Set |
| 18 | // GCP_TEST_DISCOVERYENGINE_ASSISTANT=1 on a project with one. |
| 19 | const runLifecycle = !process.env.FAST && !!process.env.GCP_TEST_DISCOVERYENGINE_ASSISTANT; |
| 20 | |
| 21 | const waitUntilGone = (name: string) => |
| 22 | discoveryengine.getProjectsLocationsCollectionsEnginesAssistants({ 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 | "getProjectsLocationsCollectionsEnginesAssistants on a missing assistant 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.getProjectsLocationsCollectionsEnginesAssistants({ |
| 42 | name: `projects/${project}/locations/global/collections/default_collection/engines/alchemy-missing/assistants/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(!runLifecycle)( |
| 56 | "create, update, and delete an engine assistant", |
| 57 | (stack) => |
| 58 | Effect.gen(function* () { |
| 59 | yield* stack.destroy(); |
| 60 | |
| 61 | const created = yield* stack.deploy( |
| 62 | Effect.gen(function* () { |
| 63 | const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Docs", { |
| 64 | location: "global", |
| 65 | displayName: "assistant-docs", |
| 66 | }); |
| 67 | const engine = yield* GCP.DiscoveryEngine.CollectionsEngine("Search", { |
| 68 | location: "global", |
| 69 | dataStoreIds: [store.dataStoreId], |
| 70 | displayName: "assistant engine", |
| 71 | }); |
| 72 | const assistant = yield* GCP.DiscoveryEngine.CollectionsEnginesAssistant("Help", { |
| 73 | engine: engine.name, |
| 74 | // Engines accept only their default assistant. |
| 75 | assistantId: "default_assistant", |
| 76 | displayName: "docs helper", |
| 77 | description: "answers", |
| 78 | }); |
| 79 | return { store, engine, assistant }; |
| 80 | }), |
| 81 | ); |
| 82 | |
| 83 | expect(created.assistant.name).toContain("/assistants/"); |
| 84 | expect(created.assistant.engine).toEqual(created.engine.name); |
| 85 | expect(created.assistant.displayName).toEqual("docs helper"); |
| 86 | expect(created.assistant.description).toEqual("answers"); |
| 87 | |
| 88 | const fetched = yield* discoveryengine.getProjectsLocationsCollectionsEnginesAssistants({ |
| 89 | name: created.assistant.name, |
| 90 | }); |
| 91 | expect(fetched.name).toEqual(created.assistant.name); |
| 92 | expect(fetched.description).toEqual("answers"); |
| 93 | |
| 94 | const updated = yield* stack.deploy( |
| 95 | Effect.gen(function* () { |
| 96 | const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Docs", { |
| 97 | dataStoreId: created.store.dataStoreId, |
| 98 | location: "global", |
| 99 | displayName: "assistant-docs", |
| 100 | }); |
| 101 | const engine = yield* GCP.DiscoveryEngine.CollectionsEngine("Search", { |
| 102 | engineId: created.engine.engineId, |
| 103 | location: "global", |
| 104 | dataStoreIds: [store.dataStoreId], |
| 105 | displayName: "assistant engine", |
| 106 | }); |
| 107 | const assistant = yield* GCP.DiscoveryEngine.CollectionsEnginesAssistant("Help", { |
| 108 | engine: engine.name, |
| 109 | assistantId: created.assistant.assistantId, |
| 110 | displayName: "docs helper prod", |
| 111 | description: "answers prod", |
| 112 | }); |
| 113 | return { store, engine, assistant }; |
| 114 | }), |
| 115 | ); |
| 116 | |
| 117 | expect(updated.assistant.name).toEqual(created.assistant.name); |
| 118 | expect(updated.assistant.displayName).toEqual("docs helper prod"); |
| 119 | expect(updated.assistant.description).toEqual("answers prod"); |
| 120 | |
| 121 | yield* stack.destroy(); |
| 122 | |
| 123 | const gone = yield* waitUntilGone(created.assistant.name); |
| 124 | expect(gone).toEqual("gone"); |
| 125 | }).pipe(logLevel, quotaTolerant), |
| 126 | { |
| 127 | tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"], |
| 128 | timeout: 120_000, |
| 129 | }, |
| 130 | ); |