fileRegex.test.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { describe, expect, test } from "bun:test"
  2. import { SessionPrompt } from "../../src/session/prompt"
  3. describe("fileRegex", () => {
  4. const template = `This is a @valid/path/to/a/file and it should also match at
  5. the beginning of a line:
  6. @another-valid/path/to/a/file
  7. but this is not:
  8. - Adds a "Co-authored-by:" footer which clarifies which AI agent
  9. helped create this commit, using an appropriate \`noreply@...\`
  10. or \`[email protected]\` email address.
  11. We also need to deal with files followed by @commas, ones
  12. with @file-extensions.md, even @multiple.extensions.bak,
  13. hidden directorys like @.config/ or files like @.bashrc
  14. and ones at the end of a sentence like @foo.md.
  15. Also shouldn't forget @/absolute/paths.txt with and @/without/extensions,
  16. as well as @~/home-files and @~/paths/under/home.txt.
  17. If the reference is \`@quoted/in/backticks\` then it shouldn't match at all.`
  18. const matches = Array.from(template.matchAll(SessionPrompt.fileRegex))
  19. test("should extract exactly 12 file references", () => {
  20. expect(matches.length).toBe(12)
  21. })
  22. test("should extract valid/path/to/a/file", () => {
  23. expect(matches[0][1]).toBe("valid/path/to/a/file")
  24. })
  25. test("should extract another-valid/path/to/a/file", () => {
  26. expect(matches[1][1]).toBe("another-valid/path/to/a/file")
  27. })
  28. test("should extract paths ignoring comma after", () => {
  29. expect(matches[2][1]).toBe("commas")
  30. })
  31. test("should extract a path with a file extension and comma after", () => {
  32. expect(matches[3][1]).toBe("file-extensions.md")
  33. })
  34. test("should extract a path with multiple dots and comma after", () => {
  35. expect(matches[4][1]).toBe("multiple.extensions.bak")
  36. })
  37. test("should extract hidden directory", () => {
  38. expect(matches[5][1]).toBe(".config/")
  39. })
  40. test("should extract hidden file", () => {
  41. expect(matches[6][1]).toBe(".bashrc")
  42. })
  43. test("should extract a file ignoring period at end of sentence", () => {
  44. expect(matches[7][1]).toBe("foo.md")
  45. })
  46. test("should extract an absolute path with an extension", () => {
  47. expect(matches[8][1]).toBe("/absolute/paths.txt")
  48. })
  49. test("should extract an absolute path without an extension", () => {
  50. expect(matches[9][1]).toBe("/without/extensions")
  51. })
  52. test("should extract an absolute path in home directory", () => {
  53. expect(matches[10][1]).toBe("~/home-files")
  54. })
  55. test("should extract an absolute path under home directory", () => {
  56. expect(matches[11][1]).toBe("~/paths/under/home.txt")
  57. })
  58. test("should not match when preceded by backtick", () => {
  59. const backtickTest = "This `@should/not/match` should be ignored"
  60. const backtickMatches = Array.from(backtickTest.matchAll(SessionPrompt.fileRegex))
  61. expect(backtickMatches.length).toBe(0)
  62. })
  63. test("should not match email addresses", () => {
  64. const emailTest = "Contact [email protected] for help"
  65. const emailMatches = Array.from(emailTest.matchAll(SessionPrompt.fileRegex))
  66. expect(emailMatches.length).toBe(0)
  67. })
  68. })