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
18// Identity mapping stores need a workforce identity provider configured for
19// the project (BadRequest "IdP must be configured before creating an Identity
20// Mapping Store"). Set GCP_TEST_DISCOVERYENGINE_IDP=1 once one is configured.
21const runLifecycle = !!process.env.GCP_TEST_DISCOVERYENGINE_IDP;
22
23const waitUntilGone = (name: string) =>
24 discoveryengine.getProjectsLocationsIdentityMappingStores({ name }).pipe(
25 Effect.as("found" as const),
26 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
27 Effect.repeat({
28 schedule: Schedule.spaced("2 seconds"),
29 until: (status) => status === "gone",
30 times: 10,
31 }),
32 );
33
34test.provider(
35 "getProjectsLocationsIdentityMappingStores on a missing store fails with a typed tag",
36 (stack) =>
37 Effect.gen(function* () {
38 const { project } = yield* GcpEnvironment.current;
39
40 yield* stack.destroy();
41
42 const error = yield* Effect.flip(
43 discoveryengine.getProjectsLocationsIdentityMappingStores({
44 name: `projects/${project}/locations/global/identityMappingStores/alchemy-ims-missing`,
45 }),
46 );
47 expect(error._tag).toEqual("NotFound");
48
49 yield* stack.destroy();
50 }).pipe(logLevel, quotaTolerant),
51 {
52 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
53 timeout: 90_000,
54 },
55);
56
57test.provider(
58 "createProjectsLocationsIdentityMappingStores without an IdP is rejected with a typed tag",
59 (stack) =>
60 Effect.gen(function* () {
61 const { project } = yield* GcpEnvironment.current;
62
63 yield* stack.destroy();
64
65 const error = yield* Effect.flip(
66 discoveryengine.createProjectsLocationsIdentityMappingStores({
67 parent: `projects/${project}/locations/global`,
68 identityMappingStoreId: "alchemyimismissing",
69 disableCmek: true,
70 body: {
71 name: `projects/${project}/locations/global/identityMappingStores/alchemyimismissing`,
72 },
73 }),
74 );
75 expect(error._tag).toEqual("IdentityProviderNotConfigured");
76
77 yield* stack.destroy();
78 }).pipe(logLevel, quotaTolerant),
79 {
80 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
81 timeout: 90_000,
82 },
83);
84
85test.provider.skipIf(!runLifecycle)(
86 "create and delete an identity mapping store",
87 (stack) =>
88 Effect.gen(function* () {
89 yield* stack.destroy();
90
91 const created = yield* stack.deploy(
92 Effect.gen(function* () {
93 return yield* GCP.DiscoveryEngine.IdentityMappingStore("Users", {
94 location: "global",
95 disableCmek: true,
96 });
97 }),
98 );
99
100 expect(created.name).toContain("/identityMappingStores/");
101 expect(created.identityMappingStoreId.startsWith("alch")).toEqual(true);
102 expect(created.location).toEqual("global");
103
104 const fetched =
105 yield* discoveryengine.getProjectsLocationsIdentityMappingStores({
106 name: created.name,
107 });
108 expect(fetched.name).toEqual(created.name);
109
110 yield* stack.destroy();
111
112 const gone = yield* waitUntilGone(created.name);
113 expect(gone).toEqual("gone");
114 }).pipe(logLevel, quotaTolerant),
115 {
116 tags: ["provider:gcp", "provider:gcp:discoveryengine", "live"],
117 timeout: 120_000,
118 },
119);