grep.test.ts 3.2 KB

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