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

main
ts 122 lines 4.53 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 dialogflow from "@distilled.cloud/gcp/dialogflow_v3";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9 import { deleteAgent, ensureAgent, 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 const runLifecycle = !process.env.FAST;
16
17 const waitUntilGone = (name: string) =>
18 dialogflow.getProjectsLocationsAgentsEnvironments({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("2 seconds"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28 test.provider(
29 "getProjectsLocationsAgentsEnvironments on a missing environment fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33 yield* stack.destroy();
34
35 const error = yield* Effect.flip(
36 dialogflow.getProjectsLocationsAgentsEnvironments({
37 name: `projects/${project}/locations/global/agents/alchemy-missing/environments/alchemy-missing`,
38 }),
39 );
40 expect(error._tag).toEqual("NotFound");
41
42 yield* stack.destroy();
43 }).pipe(logLevel, quotaTolerant),
44 {
45 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
46 timeout: 90_000,
47 },
48 );
49
50 test.provider.skipIf(!runLifecycle)(
51 "create, update, and delete an environment",
52 (stack) =>
53 Effect.gen(function* () {
54 const { project } = yield* GcpEnvironment.current;
55 yield* stack.destroy();
56 const agent = yield* ensureAgent(project, "alchemy-df-environment");
57 const startFlow = agent.startFlow ?? "";
58
59 yield* Effect.gen(function* () {
60 const created = yield* stack.deploy(
61 Effect.gen(function* () {
62 const version = yield* GCP.Dialogflow.AgentsFlowsVersion("V1", {
63 flow: startFlow,
64 displayName: "v1",
65 description: "env snapshot",
66 });
67 const environment = yield* GCP.Dialogflow.AgentsEnvironment("Prod", {
68 agent: agent.name ?? "",
69 displayName: "prod",
70 description: "production",
71 versionConfigs: [{ version: version.name }],
72 });
73 return { version, environment };
74 }),
75 );
76
77 expect(created.environment.name).toContain("/environments/");
78 expect(created.environment.displayName).toEqual("prod");
79 expect(created.environment.description).toEqual("production");
80 expect(created.environment.versionConfigs).toEqual(
81 expect.arrayContaining([expect.objectContaining({ version: created.version.name })]),
82 );
83
84 const fetched = yield* dialogflow.getProjectsLocationsAgentsEnvironments({
85 name: created.environment.name,
86 });
87 expect(fetched.name).toEqual(created.environment.name);
88 expect(fetched.description).toContain("[alchemy ");
89
90 const updated = yield* stack.deploy(
91 Effect.gen(function* () {
92 const version = yield* GCP.Dialogflow.AgentsFlowsVersion("V1", {
93 flow: startFlow,
94 versionId: created.version.versionId,
95 displayName: "v1",
96 description: "env snapshot",
97 });
98 const environment = yield* GCP.Dialogflow.AgentsEnvironment("Prod", {
99 agent: agent.name ?? "",
100 environmentId: created.environment.environmentId,
101 displayName: "staging",
102 description: "staging env",
103 versionConfigs: [{ version: version.name }],
104 });
105 return { version, environment };
106 }),
107 );
108
109 expect(updated.environment.name).toEqual(created.environment.name);
110 expect(updated.environment.displayName).toEqual("staging");
111 expect(updated.environment.description).toEqual("staging env");
112
113 yield* stack.destroy();
114 const gone = yield* waitUntilGone(created.environment.name);
115 expect(gone).toEqual("gone");
116 }).pipe(Effect.ensuring(deleteAgent(agent.name ?? "")));
117 }).pipe(logLevel, quotaTolerant),
118 {
119 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
120 timeout: 120_000,
121 },
122 );