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

main
ts 116 lines 3.78 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 const agentDisplayName = "alch-df-int";
17
18 const waitUntilGone = (name: string) =>
19 dialogflow.getProjectsLocationsAgentsIntents({ name }).pipe(
20 Effect.as("found" as const),
21 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
22 Effect.repeat({
23 schedule: Schedule.spaced("2 seconds"),
24 until: (status) => status === "gone",
25 times: 10,
26 }),
27 );
28
29 test.provider(
30 "getProjectsLocationsAgentsIntents on a missing intent fails with a typed tag",
31 (stack) =>
32 Effect.gen(function* () {
33 const { project } = yield* GcpEnvironment.current;
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 dialogflow.getProjectsLocationsAgentsIntents({
38 name: `projects/${project}/locations/global/agents/missing/intents/missing`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel, quotaTolerant),
45 {
46 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
47 timeout: 90_000,
48 },
49 );
50
51 test.provider.skipIf(!runLifecycle)(
52 "create, update, and delete an intent",
53 (stack) =>
54 Effect.gen(function* () {
55 const { project } = yield* GcpEnvironment.current;
56 yield* stack.destroy();
57
58 const agent = yield* ensureAgent(project, agentDisplayName);
59 const agentName = agent.name ?? "";
60
61 const created = yield* stack.deploy(
62 Effect.gen(function* () {
63 return yield* GCP.Dialogflow.AgentsIntent("Hello", {
64 agent: agentName,
65 displayName: "hello",
66 labels: { env: "test" },
67 trainingPhrases: [
68 { parts: [{ text: "hello" }], repeatCount: 1 },
69 { parts: [{ text: "hi there" }], repeatCount: 1 },
70 ],
71 });
72 }),
73 );
74
75 expect(created.name).toContain("/intents/");
76 expect(created.agent).toEqual(agentName);
77 expect(created.displayName).toEqual("hello");
78 expect(created.labels).toMatchObject({ env: "test" });
79 expect(created.trainingPhrases?.length).toBeGreaterThan(0);
80
81 const fetched = yield* dialogflow.getProjectsLocationsAgentsIntents({
82 name: created.name,
83 });
84 expect(fetched.name).toEqual(created.name);
85 expect(fetched.labels?.env).toEqual("test");
86
87 const updated = yield* stack.deploy(
88 Effect.gen(function* () {
89 return yield* GCP.Dialogflow.AgentsIntent("Hello", {
90 agent: agentName,
91 intentId: created.intentId,
92 displayName: "hello",
93 labels: { env: "prod" },
94 trainingPhrases: [
95 { parts: [{ text: "hello" }], repeatCount: 1 },
96 { parts: [{ text: "good morning" }], repeatCount: 1 },
97 ],
98 });
99 }),
100 );
101
102 expect(updated.name).toEqual(created.name);
103 expect(updated.labels).toMatchObject({ env: "prod" });
104
105 yield* stack.destroy();
106
107 const gone = yield* waitUntilGone(created.name);
108 expect(gone).toEqual("gone");
109
110 yield* deleteAgent(agentName);
111 }).pipe(logLevel, quotaTolerant),
112 {
113 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
114 timeout: 120_000,
115 },
116 );