Files
museion/MuseionTests/ReadingPresenterTests.swift
T
AlexKucera 4138bf22dd fix(editor): draw quote/HR/table decorations on the canvas (#71 follow-up 2)
NSTextBlock/NSTextTable layout is broken on this TextKit build regardless
of engine: textBlocks zero-width the line rect (quotes wrapped one
character per line), NSTextTable rows stack vertically with no grid
(tables rendered as concatenated text with a stray outline). The TextKit1
bridge attempt failed live too. Stop fighting the API:

- Presenter emits plain content: quotes indent-only, HR a thin invisible
  line, tables mono-space-padded grid text with alignment honored.
- Each Block/Bound carries a Decoration payload (quote indent, rule,
  table column offsets); ReadingCanvasView.draw renders hairlines,
  rules, and table grids from measured layoutManager geometry.
- Tests assert decorations + column alignment instead of textBlocks.

Full suite green; lint clean.
2026-10-05 18:05:16 +02:00

203 lines
6.5 KiB
Swift

//
// ReadingPresenterTests.swift
// MuseionTests
//
// #71 (map #20): renderer coverage — the smallest checks that fail if the
// block classification, auto-renumbering, source-range mapping, or inline
// interpretation break. Kitchen-sink fixture strings inline (no file
// fixtures per the suite's convention).
//
import AppKit
@testable import Museion
import Testing
@MainActor
struct ReadingPresenterTests {
/// Blank-line split + heading fonting + markup hidden.
@Test
func `headings and paragraphs`() {
let doc = ReadingPresenter.document(for: "# Title\n\nBody with _em_.\n\n### Deep", resolvingImagesIn: nil)
#expect(doc.bounds.count == 3)
let flat = doc.content.string
#expect(!flat.contains("#"))
#expect(flat.contains("Title"))
#expect(flat.contains("Body with em."))
}
/// Auto-renumbering: `1. 1. 1.` renders 1, 2, 3; source never rewritten.
@Test
func `ordered list auto renumbers`() {
let src = "1. one\n1. two\n1. three"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 1)
#expect(doc.content.string.contains("1. one"))
#expect(doc.content.string.contains("2. two"))
#expect(doc.content.string.contains("3. three"))
#expect(!doc.content.string.contains("1. two"))
}
/// Nesting: deeper items indent further; mixed bullet/number nesting rides
/// the same path.
@Test
func `list nesting`() {
let src = "- top\n - nested\n- back"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 1)
#expect(doc.content.string.contains("top"))
#expect(doc.content.string.contains("nested"))
}
/// The #68 click→edit map: block source ranges are monotonic and cover
/// every block; a location mid-document maps to its containing block.
@Test
func `source ranges map blocks`() {
let src = "# A\n\nfirst para\n\n- l1\n- l2\n\nlast para"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 4)
let locations = doc.bounds.map(\.sourceRange.location)
#expect(locations == locations.sorted())
// "last para" starts at its own chunk offset
let nsSrc = src as NSString
let expected = nsSrc.range(of: "last para").location
#expect(doc.bounds[3].sourceRange.location == expected)
}
/// Tables render as a mono grid: decoration carries column offsets,
/// header/body rows are padded columns of text.
@Test
func `table renders`() throws {
let src = "| A | B |\n| --- | --- |\n| one | two |"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 1)
let lines = doc.content.string.components(separatedBy: "\n")
#expect(lines.count == 2)
#expect(lines[0].contains("A"))
#expect(lines[1].contains("one"))
// alignment: same column starts across rows
let colA0 = try lines[0].distance(from: lines[0].startIndex, to: #require(lines[0].firstIndex(of: "A")))
let colA1 = try lines[1].distance(from: lines[1].startIndex, to: #require(lines[1].firstIndex(of: "o")))
#expect(colA0 == colA1)
guard case let .table(columns)? = doc.bounds[0].decoration else {
Issue.record("expected table decoration")
return
}
#expect(columns.count == 3) // two columns + closing edge
}
/// Fenced code: mono face, markers hidden, content intact.
@Test
func `fenced code`() {
let src = "```swift\nlet x = 1\n```"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(!doc.content.string.contains("```"))
#expect(doc.content.string.contains("let x = 1"))
let attrs = doc.content.attributes(at: 0, effectiveRange: nil)
#expect((attrs[.font] as? NSFont)?.fontName == ProseTypography.codeFont.fontName)
}
/// Blockquote: markers hidden, quote decoration on the bound.
@Test
func blockquote() {
let src = "> quoted _words_\n>> deeper"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(!doc.content.string.contains(">"))
#expect(doc.content.string.contains("quoted words"))
guard case .quote? = doc.bounds[0].decoration else {
Issue.record("expected quote decoration")
return
}
}
/// Links styled + attribute retained (⌘-click opens).
@Test
func `links retained`() {
let src = "See [the docs](https://example.de) now"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
let range = (doc.content.string as NSString).range(of: "the docs")
#expect(range.location != NSNotFound)
let attrs = doc.content.attributes(at: range.location, effectiveRange: nil)
#expect(attrs[.link] != nil)
}
/// Images: hover payload carried on the bound, syntax stays text.
@Test
func `image hover payload`() {
let src = "![alt text](assets/img.png)"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 1)
#expect(doc.bounds[0].imageURL != nil)
#expect(doc.content.string.contains("alt text"))
}
/// Image URL resolution against the post folder (#71).
@Test
func `image URL resolves against post folder`() {
let src = "![x](assets/img.png)"
let folder = URL(fileURLWithPath: "/tmp/posts/example")
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: folder)
#expect(doc.bounds[0].imageURL?.path == "/tmp/posts/example/assets/img.png")
}
/// Footnotes are the one fallback: source text, no transformation.
@Test
func `footnotes stay fallback`() {
let src = "Text with note[^1]\n\n[^1]: the note"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.content.string.contains("[^1]"))
}
/// HR: rule decoration, matcher shared with editing.
@Test
func `horizontal rule`() {
let src = "above\n\n* * *\n\nbelow"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 3)
guard case .rule? = doc.bounds[1].decoration else {
Issue.record("expected rule decoration")
return
}
}
/// Soft-joined lines: one paragraph, single spaces.
@Test
func `soft joined lines`() {
let src = "line one\nline two"
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
#expect(doc.bounds.count == 1)
#expect(doc.content.string == "line one line two")
}
/// Kitchen-sink smoke: every construct in one document without crashing.
@Test
func `kitchen sink renders`() {
let sink = """
# Sink
- bullet
- another
1. one
1. two
> quote
```txt
code
```
| H |
| --- |
| c |
![i](a.png)
[link](https://x.de)
* * *
"""
let doc = ReadingPresenter.document(for: sink, resolvingImagesIn: nil)
#expect(doc.bounds.count >= 9)
}
}