1import * as GCP from "@/GCP";
2import * as Test from "@/Test/Alchemy";
3import * as dialogflow from "@distilled.cloud/gcp/dialogflow_v3";
4import { expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import { MinimumLogLevel } from "effect/References";
7import * as Schedule from "effect/Schedule";
8import { deleteAgent, ensureAgent, quotaTolerant } from "./parent.ts";
9import { GcpEnvironment } from "@/GCP/Environment";
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 dialogflow.getProjectsLocationsAgentsEnvironmentsExperiments({ 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 "getProjectsLocationsAgentsEnvironmentsExperiments on a missing experiment fails with a typed tag",
33 (stack) =>
34 Effect.gen(function* () {
35 const { project } = yield* GcpEnvironment.current;
36 yield* stack.destroy();
37
38 const error = yield* Effect.flip(
39 dialogflow.getProjectsLocationsAgentsEnvironmentsExperiments({
40 name: `projects/${project}/locations/global/agents/alchemy-missing/environments/alchemy-missing/experiments/alchemy-missing`,
41 }),
42 );
43 expect(error._tag).toEqual("NotFound");
44
45 yield* stack.destroy();
46 }).pipe(logLevel, quotaTolerant),
47 {
48 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
49 timeout: 90_000,
50 },
51);
52
53test.provider.skipIf(!runLifecycle)(
54 "create, update, and delete an experiment",
55 (stack) =>
56 Effect.gen(function* () {
57 const { project } = yield* GcpEnvironment.current;
58 yield* stack.destroy();
59 const agent = yield* ensureAgent(project, "alchemy-df-experiment");
60 const startFlow = agent.startFlow ?? "";
61
62 yield* Effect.gen(function* () {
63 const created = yield* stack.deploy(
64 Effect.gen(function* () {
65 const control = yield* GCP.Dialogflow.AgentsFlowsVersion("V1", {
66 flow: startFlow,
67 displayName: "control",
68 description: "control snapshot",
69 });
70 const treatment = yield* GCP.Dialogflow.AgentsFlowsVersion("V2", {
71 flow: startFlow,
72 displayName: "treatment",
73 description: control.versionId,
74 });
75 const environment = yield* GCP.Dialogflow.AgentsEnvironment(
76 "Prod",
77 {
78 agent: agent.name ?? "",
79 displayName: "prod",
80 versionConfigs: [{ version: control.name }],
81 },
82 );
83 const experiment =
84 yield* GCP.Dialogflow.AgentsEnvironmentsExperiment("Ab", {
85 environment: environment.name,
86 displayName: "checkout-ab",
87 description: "ab test",
88 definition: {
89 versionVariants: {
90 variants: [
91 {
92 version: control.name,
93 trafficAllocation: 0.5,
94 isControlGroup: true,
95 },
96 {
97 version: treatment.name,
98 trafficAllocation: 0.5,
99 },
100 ],
101 },
102 },
103 });
104 return { control, treatment, environment, experiment };
105 }),
106 );
107
108 expect(created.experiment.name).toContain("/experiments/");
109 expect(created.experiment.displayName).toEqual("checkout-ab");
110 expect(created.experiment.description).toEqual("ab test");
111
112 const fetched =
113 yield* dialogflow.getProjectsLocationsAgentsEnvironmentsExperiments({
114 name: created.experiment.name,
115 });
116 expect(fetched.name).toEqual(created.experiment.name);
117 expect(fetched.description).toContain("[alchemy ");
118
119 const updated = yield* stack.deploy(
120 Effect.gen(function* () {
121 const control = yield* GCP.Dialogflow.AgentsFlowsVersion("V1", {
122 flow: startFlow,
123 versionId: created.control.versionId,
124 displayName: "control",
125 description: "control snapshot",
126 });
127 const treatment = yield* GCP.Dialogflow.AgentsFlowsVersion("V2", {
128 flow: startFlow,
129 versionId: created.treatment.versionId,
130 displayName: "treatment",
131 description: control.versionId,
132 });
133 const environment = yield* GCP.Dialogflow.AgentsEnvironment(
134 "Prod",
135 {
136 agent: agent.name ?? "",
137 environmentId: created.environment.environmentId,
138 displayName: "prod",
139 versionConfigs: [{ version: control.name }],
140 },
141 );
142 const experiment =
143 yield* GCP.Dialogflow.AgentsEnvironmentsExperiment("Ab", {
144 environment: environment.name,
145 experimentId: created.experiment.experimentId,
146 displayName: "checkout-ab-v2",
147 description: "ab test v2",
148 definition: {
149 versionVariants: {
150 variants: [
151 {
152 version: control.name,
153 trafficAllocation: 0.5,
154 isControlGroup: true,
155 },
156 {
157 version: treatment.name,
158 trafficAllocation: 0.5,
159 },
160 ],
161 },
162 },
163 });
164 return { control, treatment, environment, experiment };
165 }),
166 );
167
168 expect(updated.experiment.name).toEqual(created.experiment.name);
169 expect(updated.experiment.displayName).toEqual("checkout-ab-v2");
170 expect(updated.experiment.description).toEqual("ab test v2");
171
172 yield* stack.destroy();
173 const gone = yield* waitUntilGone(created.experiment.name);
174 expect(gone).toEqual("gone");
175 }).pipe(Effect.ensuring(deleteAgent(agent.name ?? "")));
176 }).pipe(logLevel, quotaTolerant),
177 {
178 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
179 timeout: 120_000,
180 },
181);