Skip to content

Commit cc7e5f3

Browse files
edburnsCopilot
andauthored
Add Node.js low-level tool-definition E2E test [3/6] (#1725)
* Add Node.js low-level tool-definition E2E test Related to issue #1682 but does not fix #1682. Align low_level_tool_definition coverage with PR #1721 snapshot behavior by only defining tools exercised by the shared snapshot. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix Node.js PR formatting and scope - Apply Prettier formatting to tools.e2e.test.ts so Node ubuntu format check passes. - Drop session lifecycle carryover from this PR by restoring Node session lifecycle files to upstream/main content, keeping this PR focused on low-level tool-definition coverage. Related to issue #1682 but does not fix #1682. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0763c09 commit cc7e5f3

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

nodejs/test/e2e/tools.e2e.test.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { writeFile } from "fs/promises";
66
import { join } from "path";
77
import { assert, describe, expect, it } from "vitest";
88
import { z } from "zod";
9-
import { defineTool, approveAll } from "../../src/index.js";
9+
import { defineTool, approveAll, ToolSet } from "../../src/index.js";
1010
import type { PermissionRequest } from "../../src/index.js";
1111
import { createSdkTestContext } from "./harness/sdkTestContext";
1212

@@ -45,6 +45,54 @@ describe("Custom tools", async () => {
4545
expect(assistantMessage?.data.content).toContain("HELLO");
4646
});
4747

48+
it("low_level_tool_definition", async () => {
49+
let currentPhase = "";
50+
const session = await client.createSession({
51+
onPermissionRequest: approveAll,
52+
availableTools: new ToolSet().addCustom("*").addBuiltIn("web_fetch"),
53+
tools: [
54+
defineTool("set_current_phase", {
55+
description: "Sets the current phase of the agent",
56+
parameters: z.object({
57+
phase: z.enum(["searching", "analyzing", "done"]),
58+
}),
59+
handler: ({ phase }) => {
60+
currentPhase = phase;
61+
return `Phase set to ${phase}`;
62+
},
63+
}),
64+
defineTool("search_items", {
65+
description: "Search for items by keyword",
66+
parameters: z.object({
67+
keyword: z.string(),
68+
}),
69+
handler: (_args, invocation) => {
70+
const args = invocation.arguments as Record<string, unknown>;
71+
if (args.keyword !== "copilot") {
72+
throw new Error(
73+
`Expected keyword to be 'copilot', got: ${String(args.keyword)}`
74+
);
75+
}
76+
return "Found: item_alpha, item_beta";
77+
},
78+
}),
79+
],
80+
});
81+
82+
const assistantMessage = await session.sendAndWait({
83+
prompt: "First, set the current phase to 'analyzing'. Then search for items with keyword 'copilot'. Report the phase and search results.",
84+
});
85+
86+
const content = assistantMessage?.data.content ?? "";
87+
expect(content.length).toBeGreaterThan(0);
88+
expect(content.toLowerCase()).toContain("analyzing");
89+
expect(
90+
content.toLowerCase().includes("item_alpha") ||
91+
content.toLowerCase().includes("item_beta")
92+
).toBe(true);
93+
expect(currentPhase).toBe("analyzing");
94+
});
95+
4896
it("handles tool calling errors", async () => {
4997
const session = await client.createSession({
5098
onPermissionRequest: approveAll,

0 commit comments

Comments
 (0)