|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +@testable import OpenClawChatUI |
| 4 | + |
| 5 | +@Suite("ChatMarkdownRenderer") |
| 6 | +struct ChatMarkdownRendererTests { |
| 7 | + // Regression for issue #98028: assistant responses on iOS were rendered |
| 8 | + // as a single line because Foundation's CommonMark parser collapses solo |
| 9 | + // newlines into spaces. The renderer now promotes intra-paragraph |
| 10 | + // newlines into CommonMark hard breaks before parsing. |
| 11 | + |
| 12 | + @Test func `preserves single newlines between assistant lines`() { |
| 13 | + let parsed = ChatMarkdownRenderer.parsedMarkdown("hello\nworld") |
| 14 | + let characters = String(parsed.characters) |
| 15 | + #expect(characters.contains("\n")) |
| 16 | + #expect(characters == "hello\nworld") |
| 17 | + } |
| 18 | + |
| 19 | + @Test func `preserves multiple solo newlines across lines`() { |
| 20 | + let parsed = ChatMarkdownRenderer.parsedMarkdown("Line 1\nLine 2\nLine 3") |
| 21 | + #expect(String(parsed.characters) == "Line 1\nLine 2\nLine 3") |
| 22 | + } |
| 23 | + |
| 24 | + @Test func `injects hard breaks for intra-paragraph newlines`() { |
| 25 | + let prepared = ChatMarkdownRenderer.preserveSoftLineBreaks(in: "hello\nworld") |
| 26 | + #expect(prepared == "hello \nworld") |
| 27 | + } |
| 28 | + |
| 29 | + @Test func `keeps blank line paragraph separators`() { |
| 30 | + let prepared = ChatMarkdownRenderer.preserveSoftLineBreaks(in: "Para1\n\nPara2") |
| 31 | + #expect(prepared == "Para1\n\nPara2") |
| 32 | + } |
| 33 | + |
| 34 | + @Test func `leaves fenced code blocks untouched`() { |
| 35 | + let input = "before\n```\nlet x = 1\nlet y = 2\n```\nafter" |
| 36 | + let prepared = ChatMarkdownRenderer.preserveSoftLineBreaks(in: input) |
| 37 | + // `before` and `after` flank the fence and pick up hard breaks because |
| 38 | + // each is adjacent to a non-blank line, but lines inside the fence |
| 39 | + // stay verbatim so code keeps its layout. |
| 40 | + #expect(prepared == "before \n```\nlet x = 1\nlet y = 2\n```\nafter") |
| 41 | + } |
| 42 | + |
| 43 | + @Test func `does not double-break already hard-broken lines`() { |
| 44 | + let input = "Already hard \nbreak" |
| 45 | + let prepared = ChatMarkdownRenderer.preserveSoftLineBreaks(in: input) |
| 46 | + #expect(prepared == "Already hard \nbreak") |
| 47 | + } |
| 48 | + |
| 49 | + @Test func `leaves backslash hard breaks alone`() { |
| 50 | + let input = "Above\\\nBelow" |
| 51 | + let prepared = ChatMarkdownRenderer.preserveSoftLineBreaks(in: input) |
| 52 | + #expect(prepared == "Above\\\nBelow") |
| 53 | + } |
| 54 | + |
| 55 | + @Test func `passes through single-line and empty inputs`() { |
| 56 | + #expect(ChatMarkdownRenderer.preserveSoftLineBreaks(in: "") == "") |
| 57 | + #expect(ChatMarkdownRenderer.preserveSoftLineBreaks(in: "solo") == "solo") |
| 58 | + } |
| 59 | +
|
| 60 | + @Test func `keeps unordered list semantics intact`() { |
| 61 | + let prepared = ChatMarkdownRenderer.preserveSoftLineBreaks(in: "- one\n- two") |
| 62 | + // Trailing spaces on a list-item content line do not disturb the |
| 63 | + // CommonMark list parser; the second line still begins a list item. |
| 64 | + let parsed = ChatMarkdownRenderer.parsedMarkdown("- one\n- two") |
| 65 | + var sawListItem = false |
| 66 | + for run in parsed.runs { |
| 67 | + if let intent = run.presentationIntent, "\(intent)".contains("listItem") { |
| 68 | + sawListItem = true |
| 69 | + break |
| 70 | + } |
| 71 | + } |
| 72 | + #expect(sawListItem) |
| 73 | + #expect(prepared == "- one \n- two") |
| 74 | + } |
| 75 | +
|
| 76 | + @Test func `keeps header followed by paragraph as header`() { |
| 77 | + let parsed = ChatMarkdownRenderer.parsedMarkdown("# Heading\nBody") |
| 78 | + var sawHeader = false |
| 79 | + for run in parsed.runs { |
| 80 | + if let intent = run.presentationIntent, "\(intent)".contains("header") { |
| 81 | + sawHeader = true |
| 82 | + break |
| 83 | + } |
| 84 | + } |
| 85 | + #expect(sawHeader) |
| 86 | + } |
| 87 | +} |
0 commit comments