fileRegex.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { describe, expect, test, beforeAll, mock } from "bun:test"
  2. describe("processFileReferences", () => {
  3. let result: any
  4. beforeAll(async () => {
  5. mock.module("os", () => ({ default: { homedir: () => "/home/fake-user" } }))
  6. const { processFileReferences } = await import("../../src/session/file-reference")
  7. const template = `This is a @valid/path/to/a/file and it should also match at
  8. the beginning of a line:
  9. @another-valid/path/to/a/file
  10. but this is not:
  11. - Adds a "Co-authored-by:" footer which clarifies which AI agent
  12. helped create this commit, using an appropriate \`noreply@...\`
  13. or \`[email protected]\` email address.
  14. We also need to deal with files followed by @commas, ones
  15. with @file-extensions.md, even @multiple.extensions.bak,
  16. hidden directorys like @.config/ or files like @.bashrc
  17. and ones at the end of a sentence like @foo.md.
  18. Also shouldn't forget @/absolute/paths.txt with and @/without/extensions,
  19. as well as @~/home-files and @~/paths/under/home.txt.
  20. If the reference is \`@quoted/in/backticks\` then it shouldn't match at all.`
  21. result = processFileReferences(template, "/base")
  22. })
  23. test("should extract exactly 12 file references", () => {
  24. expect(result.length).toBe(12)
  25. })
  26. test("all files should have correct type and mime", () => {
  27. result.forEach((file: any) => {
  28. expect(file.type).toBe("file")
  29. expect(file.mime).toBe("text/plain")
  30. })
  31. })
  32. test("should extract valid/path/to/a/file", () => {
  33. expect(result[0].filename).toBe("valid/path/to/a/file")
  34. expect(result[0].url).toBe("file:///base/valid/path/to/a/file")
  35. })
  36. test("should extract another-valid/path/to/a/file", () => {
  37. expect(result[1].filename).toBe("another-valid/path/to/a/file")
  38. expect(result[1].url).toBe("file:///base/another-valid/path/to/a/file")
  39. })
  40. test("should extract paths ignoring comma after", () => {
  41. expect(result[2].filename).toBe("commas")
  42. expect(result[2].url).toBe("file:///base/commas")
  43. })
  44. test("should extract a path with a file extension and comma after", () => {
  45. expect(result[3].filename).toBe("file-extensions.md")
  46. expect(result[3].url).toBe("file:///base/file-extensions.md")
  47. })
  48. test("should extract a path with multiple dots and comma after", () => {
  49. expect(result[4].filename).toBe("multiple.extensions.bak")
  50. expect(result[4].url).toBe("file:///base/multiple.extensions.bak")
  51. })
  52. test("should extract hidden directory", () => {
  53. expect(result[5].filename).toBe(".config/")
  54. expect(result[5].url).toBe("file:///base/.config")
  55. })
  56. test("should extract hidden file", () => {
  57. expect(result[6].filename).toBe(".bashrc")
  58. expect(result[6].url).toBe("file:///base/.bashrc")
  59. })
  60. test("should extract a file ignoring period at end of sentence", () => {
  61. expect(result[7].filename).toBe("foo.md")
  62. expect(result[7].url).toBe("file:///base/foo.md")
  63. })
  64. test("should extract an absolute path with an extension", () => {
  65. expect(result[8].filename).toBe("/absolute/paths.txt")
  66. expect(result[8].url).toBe("file:///absolute/paths.txt")
  67. })
  68. test("should extract an absolute path without an extension", () => {
  69. expect(result[9].filename).toBe("/without/extensions")
  70. expect(result[9].url).toBe("file:///without/extensions")
  71. })
  72. test("should extract an absolute path in home directory", () => {
  73. expect(result[10].filename).toBe("~/home-files")
  74. expect(result[10].url).toBe("file:///home/fake-user/home-files")
  75. })
  76. test("should extract an absolute path under home directory", () => {
  77. expect(result[11].filename).toBe("~/paths/under/home.txt")
  78. expect(result[11].url).toBe("file:///home/fake-user/paths/under/home.txt")
  79. })
  80. })