markdown.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // kilocode_change - new file
  2. import { describe, expect, it } from "bun:test"
  3. import { formatMarkdownTables } from "../../../src/cli/cmd/tui/util/markdown"
  4. describe("formatMarkdownTables", () => {
  5. it("formats a simple table with fixed-width columns", () => {
  6. const input = `| Name | Age |
  7. | --- | --- |
  8. | Alice | 30 |
  9. | Bob | 25 |`
  10. const result = formatMarkdownTables(input)
  11. // Each column should be padded to consistent width
  12. const lines = result.split("\n")
  13. expect(lines).toHaveLength(4)
  14. // Check that all pipes are aligned
  15. const pipePositions = lines.map((line) => {
  16. const positions: number[] = []
  17. for (let i = 0; i < line.length; i++) {
  18. if (line[i] === "|") positions.push(i)
  19. }
  20. return positions
  21. })
  22. // All rows should have same pipe positions
  23. for (let i = 1; i < pipePositions.length; i++) {
  24. expect(pipePositions[i]).toEqual(pipePositions[0])
  25. }
  26. })
  27. it("handles tables with varying cell widths", () => {
  28. const input = `| Short | Very Long Header |
  29. | --- | --- |
  30. | A | B |
  31. | Much Longer Cell | X |`
  32. const result = formatMarkdownTables(input)
  33. const lines = result.split("\n")
  34. // Column widths should be normalized
  35. expect(lines[0]).toContain("Short")
  36. expect(lines[0]).toContain("Very Long Header")
  37. expect(lines[3]).toContain("Much Longer Cell")
  38. })
  39. it("preserves column alignment", () => {
  40. const input = `| Left | Center | Right |
  41. | :--- | :---: | ---: |
  42. | 1 | 2 | 3 |`
  43. const result = formatMarkdownTables(input)
  44. const lines = result.split("\n")
  45. // Check that alignment markers are preserved
  46. expect(lines[1]).toContain(":--")
  47. expect(lines[1]).toMatch(/:.*:/) // center has colons on both sides
  48. expect(lines[1]).toMatch(/-+:/) // right-aligned ends with colon
  49. })
  50. it("handles content without tables unchanged", () => {
  51. const input = `# Hello World
  52. This is a paragraph.
  53. - List item 1
  54. - List item 2`
  55. const result = formatMarkdownTables(input)
  56. expect(result).toBe(input)
  57. })
  58. it("handles multiple tables in content", () => {
  59. const input = `# First Table
  60. | A | B |
  61. | --- | --- |
  62. | 1 | 2 |
  63. Some text between tables.
  64. | Column One | Column Two |
  65. | --- | --- |
  66. | Value | Another Value |`
  67. const result = formatMarkdownTables(input)
  68. // Both tables should be formatted
  69. expect(result).toContain("| A ")
  70. expect(result).toContain("| Column One ")
  71. })
  72. it("handles tables with empty cells", () => {
  73. const input = `| Header 1 | Header 2 |
  74. | --- | --- |
  75. | Value | |
  76. | | Value |`
  77. const result = formatMarkdownTables(input)
  78. const lines = result.split("\n")
  79. // Should still format correctly with empty cells
  80. expect(lines).toHaveLength(4)
  81. // Pipes should still be aligned
  82. const firstPipe = lines[0].indexOf("|")
  83. for (const line of lines) {
  84. expect(line[firstPipe]).toBe("|")
  85. }
  86. })
  87. it("handles tables with extra columns in some rows", () => {
  88. const input = `| A | B |
  89. | --- | --- |
  90. | 1 | 2 | 3 |`
  91. const result = formatMarkdownTables(input)
  92. const lines = result.split("\n")
  93. // Should handle the extra column
  94. expect(lines[2]).toContain("3")
  95. })
  96. it("ignores malformed tables", () => {
  97. const input = `| Not a table
  98. Just some text with pipes |`
  99. const result = formatMarkdownTables(input)
  100. expect(result).toBe(input)
  101. })
  102. it("handles tables without leading/trailing pipes", () => {
  103. // Standard markdown tables require pipes
  104. const input = `Name | Age
  105. --- | ---
  106. Alice | 30`
  107. const result = formatMarkdownTables(input)
  108. // Without proper pipe syntax, this shouldn't be formatted as a table
  109. expect(result).toBe(input)
  110. })
  111. it("formats table embedded in markdown content", () => {
  112. const input = `Here is some information:
  113. | Feature | Status |
  114. | --- | --- |
  115. | Tables | Supported |
  116. | Lists | Supported |
  117. And some more text after the table.`
  118. const result = formatMarkdownTables(input)
  119. // The table portion should be formatted
  120. expect(result).toContain("| Feature")
  121. expect(result).toContain("| Tables")
  122. // Non-table content should be preserved
  123. expect(result).toContain("Here is some information:")
  124. expect(result).toContain("And some more text after the table.")
  125. })
  126. })