| 1 | import { GcpEnvironment } from "@/GCP/Environment"; |
| 2 | import * as GCP from "@/GCP"; |
| 3 | import { quotaTolerant } from "./parent.ts"; |
| 4 | import * as Test from "@/Test/Alchemy"; |
| 5 | import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1"; |
| 6 | import { expect } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { MinimumLogLevel } from "effect/References"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: GCP.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService( |
| 14 | MinimumLogLevel, |
| 15 | process.env.DEBUG ? "Debug" : "Info", |
| 16 | ); |
| 17 | |
| 18 | const runLifecycle = !process.env.FAST; |
| 19 | |
| 20 | const waitUntilGone = (name: string) => |
| 21 | discoveryengine |
| 22 | .getProjectsLocationsCollectionsEnginesServingConfigs({ 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 | |
| 33 | test.provider( |
| 34 | "getProjectsLocationsCollectionsEnginesServingConfigs on a missing config 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.getProjectsLocationsCollectionsEnginesServingConfigs({ |
| 43 | name: `projects/${project}/locations/global/collections/default_collection/engines/alchemy-missing/servingConfigs/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 | |
| 56 | test.provider.skipIf(!runLifecycle)( |
| 57 | "create, update, and delete an engine serving config", |
| 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: "serving-docs", |
| 69 | }, |
| 70 | ); |
| 71 | const engine = yield* GCP.DiscoveryEngine.CollectionsEngine( |
| 72 | "Search", |
| 73 | { |
| 74 | location: "global", |
| 75 | dataStoreIds: [store.dataStoreId], |
| 76 | displayName: "serving engine", |
| 77 | }, |
| 78 | ); |
| 79 | const serving = |
| 80 | yield* GCP.DiscoveryEngine.CollectionsEnginesServingConfig( |
| 81 | "Primary", |
| 82 | { |
| 83 | engine: engine.name, |
| 84 | displayName: "primary", |
| 85 | solutionType: "SOLUTION_TYPE_SEARCH", |
| 86 | }, |
| 87 | ); |
| 88 | return { store, engine, serving }; |
| 89 | }), |
| 90 | ); |
| 91 | |
| 92 | expect(created.serving.name).toContain("/servingConfigs/"); |
| 93 | expect(created.serving.engine).toEqual(created.engine.name); |
| 94 | expect(created.serving.displayName).toEqual("primary"); |
| 95 | |
| 96 | const fetched = |
| 97 | yield* discoveryengine.getProjectsLocationsCollectionsEnginesServingConfigs( |
| 98 | { name: created.serving.name }, |
| 99 | ); |
| 100 | expect(fetched.name).toEqual(created.serving.name); |
| 101 | expect(fetched.displayName).toEqual("primary"); |
| 102 | |
| 103 | const updated = yield* stack.deploy( |
| 104 | Effect.gen(function* () { |
| 105 | const store = yield* GCP.DiscoveryEngine.CollectionsDataStore( |
| 106 | "Docs", |
| 107 | { |
| 108 | dataStoreId: created.store.dataStoreId, |
| 109 | location: "global", |
| 110 | displayName: "serving-docs", |
| 111 | }, |
| 112 | ); |
| 113 | const engine = yield* GCP.DiscoveryEngine.CollectionsEngine( |
| 114 | "Search", |
| 115 | { |
| 116 | engineId: created.engine.engineId, |
| 117 | location: "global", |
| 118 | dataStoreIds: [store.dataStoreId], |
| 119 | displayName: "serving engine", |
| 120 | }, |
| 121 | ); |
| 122 | const serving = |
| 123 | yield* GCP.DiscoveryEngine.CollectionsEnginesServingConfig( |
| 124 | "Primary", |
| 125 | { |
| 126 | engine: engine.name, |
| 127 | servingConfigId: created.serving.servingConfigId, |
| 128 | displayName: "primary-prod", |
| 129 | solutionType: "SOLUTION_TYPE_SEARCH", |
| 130 | }, |
| 131 | ); |
| 132 | return { store, engine, serving }; |
| 133 | }), |
| 134 | ); |
| 135 | |
| 136 | expect(updated.serving.name).toEqual(created.serving.name); |
| 137 | expect(updated.serving.displayName).toEqual("primary-prod"); |
| 138 | |
| 139 | yield* stack.destroy(); |
| 140 | |
| 141 | const gone = yield* waitUntilGone(created.serving.name); |
| 142 | expect(gone).toEqual("gone"); |
| 143 | }).pipe(logLevel, quotaTolerant), |
| 144 | { |
| 145 | tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"], |
| 146 | timeout: 120_000, |
| 147 | }, |
| 148 | ); |