|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import { GrepTool } from "../../src/tool/grep" |
| 4 | +import { Instance } from "../../src/project/instance" |
| 5 | +import { tmpdir } from "../fixture/fixture" |
| 6 | + |
| 7 | +const ctx = { |
| 8 | + sessionID: "test", |
| 9 | + messageID: "", |
| 10 | + callID: "", |
| 11 | + agent: "build", |
| 12 | + abort: AbortSignal.any([]), |
| 13 | + metadata: () => {}, |
| 14 | +} |
| 15 | + |
| 16 | +const projectRoot = path.join(__dirname, "../..") |
| 17 | + |
| 18 | +describe("tool.grep", () => { |
| 19 | + test("basic search", async () => { |
| 20 | + await Instance.provide({ |
| 21 | + directory: projectRoot, |
| 22 | + fn: async () => { |
| 23 | + const grep = await GrepTool.init() |
| 24 | + const result = await grep.execute( |
| 25 | + { |
| 26 | + pattern: "export", |
| 27 | + path: path.join(projectRoot, "src/tool"), |
| 28 | + include: "*.ts", |
| 29 | + }, |
| 30 | + ctx, |
| 31 | + ) |
| 32 | + expect(result.metadata.matches).toBeGreaterThan(0) |
| 33 | + expect(result.output).toContain("Found") |
| 34 | + }, |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + test("no matches returns correct output", async () => { |
| 39 | + await using tmp = await tmpdir({ |
| 40 | + init: async (dir) => { |
| 41 | + await Bun.write(path.join(dir, "test.txt"), "hello world") |
| 42 | + }, |
| 43 | + }) |
| 44 | + await Instance.provide({ |
| 45 | + directory: tmp.path, |
| 46 | + fn: async () => { |
| 47 | + const grep = await GrepTool.init() |
| 48 | + const result = await grep.execute( |
| 49 | + { |
| 50 | + pattern: "xyznonexistentpatternxyz123", |
| 51 | + path: tmp.path, |
| 52 | + }, |
| 53 | + ctx, |
| 54 | + ) |
| 55 | + expect(result.metadata.matches).toBe(0) |
| 56 | + expect(result.output).toBe("No files found") |
| 57 | + }, |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + test("handles CRLF line endings in output", async () => { |
| 62 | + // This test verifies the regex split handles both \n and \r\n |
| 63 | + await using tmp = await tmpdir({ |
| 64 | + init: async (dir) => { |
| 65 | + // Create a test file with content |
| 66 | + await Bun.write(path.join(dir, "test.txt"), "line1\nline2\nline3") |
| 67 | + }, |
| 68 | + }) |
| 69 | + await Instance.provide({ |
| 70 | + directory: tmp.path, |
| 71 | + fn: async () => { |
| 72 | + const grep = await GrepTool.init() |
| 73 | + const result = await grep.execute( |
| 74 | + { |
| 75 | + pattern: "line", |
| 76 | + path: tmp.path, |
| 77 | + }, |
| 78 | + ctx, |
| 79 | + ) |
| 80 | + expect(result.metadata.matches).toBeGreaterThan(0) |
| 81 | + }, |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +describe("CRLF regex handling", () => { |
| 87 | + test("regex correctly splits Unix line endings", () => { |
| 88 | + const unixOutput = "file1.txt|1|content1\nfile2.txt|2|content2\nfile3.txt|3|content3" |
| 89 | + const lines = unixOutput.trim().split(/\r?\n/) |
| 90 | + expect(lines.length).toBe(3) |
| 91 | + expect(lines[0]).toBe("file1.txt|1|content1") |
| 92 | + expect(lines[2]).toBe("file3.txt|3|content3") |
| 93 | + }) |
| 94 | + |
| 95 | + test("regex correctly splits Windows CRLF line endings", () => { |
| 96 | + const windowsOutput = "file1.txt|1|content1\r\nfile2.txt|2|content2\r\nfile3.txt|3|content3" |
| 97 | + const lines = windowsOutput.trim().split(/\r?\n/) |
| 98 | + expect(lines.length).toBe(3) |
| 99 | + expect(lines[0]).toBe("file1.txt|1|content1") |
| 100 | + expect(lines[2]).toBe("file3.txt|3|content3") |
| 101 | + }) |
| 102 | + |
| 103 | + test("regex handles mixed line endings", () => { |
| 104 | + const mixedOutput = "file1.txt|1|content1\nfile2.txt|2|content2\r\nfile3.txt|3|content3" |
| 105 | + const lines = mixedOutput.trim().split(/\r?\n/) |
| 106 | + expect(lines.length).toBe(3) |
| 107 | + }) |
| 108 | +}) |
0 commit comments