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

main
ts 118 lines 4.16 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 // Non-global Dialogflow CX locations need the {region}-dialogflow host, which
17 // distilled does not route to yet; the global location works everywhere.
18 const location = "global";
19 const agentDisplayName = "alch-df-wh";
20
21 const waitUntilGone = (name: string) =>
22 dialogflow.getProjectsLocationsAgentsWebhooks({ name }).pipe(
23 Effect.as("found" as const),
24 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
25 Effect.repeat({
26 schedule: Schedule.spaced("2 seconds"),
27 until: (status) => status === "gone",
28 times: 10,
29 }),
30 );
31
32 test.provider(
33 "getProjectsLocationsAgentsWebhooks on a missing webhook fails with a typed tag",
34 (stack) =>
35 Effect.gen(function* () {
36 const { project } = yield* GcpEnvironment.current;
37 yield* stack.destroy();
38
39 const error = yield* Effect.flip(
40 dialogflow.getProjectsLocationsAgentsWebhooks({
41 name: `projects/${project}/locations/${location}/agents/missing/webhooks/missing`,
42 }),
43 );
44 expect(error._tag).toEqual("NotFound");
45
46 yield* stack.destroy();
47 }).pipe(logLevel, quotaTolerant),
48 {
49 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
50 timeout: 90_000,
51 },
52 );
53
54 test.provider.skipIf(!runLifecycle)(
55 "create, update, and delete a webhook",
56 (stack) =>
57 Effect.gen(function* () {
58 const { project } = yield* GcpEnvironment.current;
59 yield* stack.destroy();
60
61 const agent = yield* ensureAgent(project, agentDisplayName, location);
62 const agentName = agent.name ?? "";
63
64 yield* Effect.gen(function* () {
65 const created = yield* stack.deploy(
66 Effect.gen(function* () {
67 return yield* GCP.Dialogflow.AgentsWebhook("Fulfillment", {
68 agent: agentName,
69 location,
70 displayName: "orders",
71 genericWebService: { uri: "https://example.com/dialogflow" },
72 });
73 }),
74 );
75
76 expect(created.name).toContain("/webhooks/");
77 expect(created.agent).toEqual(agentName);
78 expect(created.location).toEqual(location);
79 expect(created.displayName).toEqual("orders");
80 expect(created.disabled).toEqual(false);
81 expect(created.genericWebService?.uri).toEqual("https://example.com/dialogflow");
82
83 const fetched = yield* dialogflow.getProjectsLocationsAgentsWebhooks({
84 name: created.name,
85 });
86 expect(fetched.name).toEqual(created.name);
87 expect(fetched.displayName).toMatch(/^\[(alchemy|alc) /);
88
89 const updated = yield* stack.deploy(
90 Effect.gen(function* () {
91 return yield* GCP.Dialogflow.AgentsWebhook("Fulfillment", {
92 agent: agentName,
93 location,
94 webhookId: created.webhookId,
95 displayName: "orders-v2",
96 disabled: true,
97 genericWebService: {
98 uri: "https://example.com/dialogflow-v2",
99 },
100 });
101 }),
102 );
103
104 expect(updated.name).toEqual(created.name);
105 expect(updated.displayName).toEqual("orders-v2");
106 expect(updated.disabled).toEqual(true);
107 expect(updated.genericWebService?.uri).toEqual("https://example.com/dialogflow-v2");
108
109 yield* stack.destroy();
110 const gone = yield* waitUntilGone(created.name);
111 expect(gone).toEqual("gone");
112 }).pipe(Effect.ensuring(deleteAgent(agentName)));
113 }).pipe(logLevel, quotaTolerant),
114 {
115 tags: ["provider:gcp", "provider:gcp:dialogflow", "live"],
116 timeout: 120_000,
117 },
118 );