-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathbuiltin_tools.test.ts
More file actions
101 lines (91 loc) · 4.65 KB
/
builtin_tools.test.ts
File metadata and controls
101 lines (91 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { writeFile, mkdir } from "fs/promises";
import { join } from "path";
import { describe, expect, it } from "vitest";
import { createSdkTestContext } from "./harness/sdkTestContext";
describe("Built-in Tools", async () => {
const { copilotClient: client, workDir } = await createSdkTestContext();
describe("bash", () => {
it("should capture exit code in output", async () => {
const session = await client.createSession();
const msg = await session.sendAndWait({
prompt: "Run 'echo hello && echo world'. Tell me the exact output.",
});
expect(msg?.data.content).toContain("hello");
expect(msg?.data.content).toContain("world");
});
it("should capture stderr output", async () => {
const session = await client.createSession();
const msg = await session.sendAndWait({
prompt: "Run 'echo error_msg >&2; echo ok' and tell me what stderr said. Reply with just the stderr content.",
});
expect(msg?.data.content).toContain("error_msg");
});
});
describe("view", () => {
it("should read file with line range", async () => {
await writeFile(join(workDir, "lines.txt"), "line1\nline2\nline3\nline4\nline5\n");
const session = await client.createSession();
const msg = await session.sendAndWait({
prompt: "Read lines 2 through 4 of the file 'lines.txt' in this directory. Tell me what those lines contain.",
});
expect(msg?.data.content).toContain("line2");
expect(msg?.data.content).toContain("line4");
});
it("should handle nonexistent file gracefully", async () => {
const session = await client.createSession();
const msg = await session.sendAndWait({
prompt: "Try to read the file 'does_not_exist.txt'. If it doesn't exist, say 'FILE_NOT_FOUND'.",
});
expect(msg?.data.content?.toUpperCase()).toMatch(
/NOT.FOUND|NOT.EXIST|NO.SUCH|FILE_NOT_FOUND|DOES.NOT.EXIST|ERROR/i
);
});
});
describe("edit", () => {
it("should edit a file successfully", async () => {
await writeFile(join(workDir, "edit_me.txt"), "Hello World\nGoodbye World\n");
const session = await client.createSession();
const msg = await session.sendAndWait({
prompt: "Edit the file 'edit_me.txt': replace 'Hello World' with 'Hi Universe'. Then read it back and tell me its contents.",
});
expect(msg?.data.content).toContain("Hi Universe");
});
});
describe("create_file", () => {
it("should create a new file", async () => {
const session = await client.createSession();
const msg = await session.sendAndWait({
prompt: "Create a file called 'new_file.txt' with the content 'Created by test'. Then read it back to confirm.",
});
expect(msg?.data.content).toContain("Created by test");
});
});
describe("grep", () => {
it("should search for patterns in files", async () => {
await writeFile(join(workDir, "data.txt"), "apple\nbanana\napricot\ncherry\n");
const session = await client.createSession();
const msg = await session.sendAndWait({
prompt: "Search for lines starting with 'ap' in the file 'data.txt'. Tell me which lines matched.",
});
expect(msg?.data.content).toContain("apple");
expect(msg?.data.content).toContain("apricot");
});
});
describe("glob", () => {
it("should find files by pattern", async () => {
await mkdir(join(workDir, "src"), { recursive: true });
await writeFile(join(workDir, "src", "app.ts"), "export const app = 1;");
await writeFile(join(workDir, "src", "index.ts"), "export const index = 1;");
await writeFile(join(workDir, "README.md"), "# Readme");
const session = await client.createSession();
const msg = await session.sendAndWait({
prompt: "Find all .ts files in this directory (recursively). List the filenames you found.",
});
expect(msg?.data.content).toContain("app.ts");
expect(msg?.data.content).toContain("index.ts");
});
});
});