markdown.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { expect, test, describe } from "bun:test"
  2. import { ConfigMarkdown } from "../../src/config/markdown"
  3. describe("ConfigMarkdown: normal template", () => {
  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 directories 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 = ConfigMarkdown.files(template)
  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 = ConfigMarkdown.files(backtickTest)
  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 = ConfigMarkdown.files(emailTest)
  66. expect(emailMatches.length).toBe(0)
  67. })
  68. })
  69. describe("ConfigMarkdown: frontmatter parsing", async () => {
  70. const parsed = await ConfigMarkdown.parse(import.meta.dir + "/fixtures/frontmatter.md")
  71. test("should parse without throwing", () => {
  72. expect(parsed).toBeDefined()
  73. expect(parsed.data).toBeDefined()
  74. expect(parsed.content).toBeDefined()
  75. })
  76. test("should extract description field", () => {
  77. expect(parsed.data.description).toBe("This is a description wrapped in quotes")
  78. })
  79. test("should extract occupation field with colon in value", () => {
  80. expect(parsed.data.occupation).toBe("This man has the following occupation: Software Engineer\n")
  81. })
  82. test("should extract title field with single quotes", () => {
  83. expect(parsed.data.title).toBe("Hello World")
  84. })
  85. test("should extract name field with embedded quotes", () => {
  86. expect(parsed.data.name).toBe('John "Doe"')
  87. })
  88. test("should extract family field with embedded single quotes", () => {
  89. expect(parsed.data.family).toBe("He has no 'family'")
  90. })
  91. test("should extract multiline summary field", () => {
  92. expect(parsed.data.summary).toBe("This is a summary\n")
  93. })
  94. test("should not include commented fields in data", () => {
  95. expect(parsed.data.field).toBeUndefined()
  96. })
  97. test("should extract URL with port", () => {
  98. expect(parsed.data.url).toBe("https://example.com:8080/path?query=value\n")
  99. })
  100. test("should extract time with colons", () => {
  101. expect(parsed.data.time).toBe("The time is 12:30:00 PM\n")
  102. })
  103. test("should extract value with multiple colons", () => {
  104. expect(parsed.data.nested).toBe("First: Second: Third: Fourth\n")
  105. })
  106. test("should preserve already double-quoted values with colons", () => {
  107. expect(parsed.data.quoted_colon).toBe("Already quoted: no change needed")
  108. })
  109. test("should preserve already single-quoted values with colons", () => {
  110. expect(parsed.data.single_quoted_colon).toBe("Single quoted: also fine")
  111. })
  112. test("should extract value with quotes and colons mixed", () => {
  113. expect(parsed.data.mixed).toBe('He said "hello: world" and then left\n')
  114. })
  115. test("should handle empty values", () => {
  116. expect(parsed.data.empty).toBeNull()
  117. })
  118. test("should handle dollar sign replacement patterns literally", () => {
  119. expect(parsed.data.dollar).toBe("Use $' and $& for special patterns")
  120. })
  121. test("should not parse fake yaml from content", () => {
  122. expect(parsed.data.fake_field).toBeUndefined()
  123. expect(parsed.data.another).toBeUndefined()
  124. })
  125. test("should extract content after frontmatter without modification", () => {
  126. expect(parsed.content).toContain("Content that should not be parsed:")
  127. expect(parsed.content).toContain("fake_field: this is not yaml")
  128. expect(parsed.content).toContain("url: https://should-not-be-parsed.com:3000")
  129. })
  130. })
  131. describe("ConfigMarkdown: frontmatter parsing w/ empty frontmatter", async () => {
  132. const result = await ConfigMarkdown.parse(import.meta.dir + "/fixtures/empty-frontmatter.md")
  133. test("should parse without throwing", () => {
  134. expect(result).toBeDefined()
  135. expect(result.data).toEqual({})
  136. expect(result.content.trim()).toBe("Content")
  137. })
  138. })
  139. describe("ConfigMarkdown: frontmatter parsing w/ no frontmatter", async () => {
  140. const result = await ConfigMarkdown.parse(import.meta.dir + "/fixtures/no-frontmatter.md")
  141. test("should parse without throwing", () => {
  142. expect(result).toBeDefined()
  143. expect(result.data).toEqual({})
  144. expect(result.content.trim()).toBe("Content")
  145. })
  146. })