markdown.test.ts 2.8 KB

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