1import { GcpEnvironment } from "@/GCP/Environment";
2import * as GCP from "@/GCP";
3import * as Test from "@/Test/Alchemy";
4import * as ces from "@distilled.cloud/gcp/ces_v1";
5import { expect } from "alchemy-test";
6import * as Effect from "effect/Effect";
7import { MinimumLogLevel } from "effect/References";
8import * as Schedule from "effect/Schedule";
9
10const { test } = Test.make({ providers: GCP.providers() });
11
12const logLevel = Effect.provideService(
13 MinimumLogLevel,
14 process.env.DEBUG ? "Debug" : "Info",
15);
16
17const waitUntilGone = (name: string) =>
18 ces.getProjectsLocationsAppsTools({ name }).pipe(
19 Effect.as("found" as const),
20 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
21 Effect.repeat({
22 schedule: Schedule.spaced("1 second"),
23 until: (status) => status === "gone",
24 times: 10,
25 }),
26 );
27
28test.provider(
29 "getProjectsLocationsAppsTools on a missing tool fails with a typed tag",
30 (stack) =>
31 Effect.gen(function* () {
32 const { project } = yield* GcpEnvironment.current;
33
34 yield* stack.destroy();
35
36 const error = yield* Effect.flip(
37 ces.getProjectsLocationsAppsTools({
38 name: `projects/${project}/locations/us/apps/missing/tools/missing`,
39 }),
40 );
41 expect(error._tag).toEqual("NotFound");
42
43 yield* stack.destroy();
44 }).pipe(logLevel),
45 { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 },
46);
47
48test.provider(
49 "create, update, and delete a tool",
50 (stack) =>
51 Effect.gen(function* () {
52 yield* stack.destroy();
53
54 const created = yield* stack.deploy(
55 Effect.gen(function* () {
56 const app = yield* GCP.CES.App("Support", {
57 location: "us",
58 displayName: "support-tool",
59 });
60 const tool = yield* GCP.CES.AppsTool("Lookup", {
61 app: app.name,
62 clientFunction: {
63 name: "lookup_order",
64 description: "Look up an order.",
65 parameters: {
66 type: "OBJECT",
67 properties: { orderId: { type: "STRING" } },
68 },
69 },
70 });
71 return { app, tool };
72 }),
73 );
74
75 expect(created.tool.name).toContain("/tools/");
76 expect(created.tool.app).toEqual(created.app.name);
77 expect(created.tool.clientFunction?.name).toEqual("lookup_order");
78 expect(created.tool.clientFunction?.description).toEqual(
79 "Look up an order.",
80 );
81
82 const fetched = yield* ces.getProjectsLocationsAppsTools({
83 name: created.tool.name,
84 });
85 expect(fetched.name).toEqual(created.tool.name);
86 // The description tells the model when to call the tool: no marker.
87 expect(fetched.clientFunction?.description).toEqual("Look up an order.");
88
89 const updated = yield* stack.deploy(
90 Effect.gen(function* () {
91 const app = yield* GCP.CES.App("Support", {
92 appId: created.app.appId,
93 location: created.app.location,
94 displayName: "support-tool",
95 });
96 const tool = yield* GCP.CES.AppsTool("Lookup", {
97 app: app.name,
98 toolId: created.tool.toolId,
99 clientFunction: {
100 name: "lookup_order",
101 description: "Look up an order by id.",
102 parameters: {
103 type: "OBJECT",
104 properties: {
105 orderId: { type: "STRING" },
106 includeHistory: { type: "BOOLEAN" },
107 },
108 },
109 },
110 });
111 return { app, tool };
112 }),
113 );
114
115 expect(updated.tool.name).toEqual(created.tool.name);
116 expect(updated.tool.clientFunction?.description).toEqual(
117 "Look up an order by id.",
118 );
119
120 yield* stack.destroy();
121 const gone = yield* waitUntilGone(created.tool.name);
122 expect(gone).toEqual("gone");
123 }).pipe(logLevel),
124 { tags: ["provider:gcp", "provider:gcp:ces", "live"], timeout: 300_000 },
125);