Map #20 execution child, built per decisions #68/#69/#70: - ReadingPresenter: block-aware renderer (ADR-0010 stance) — blank-line split, one block per chunk; lists (bullets + ordered + nesting + auto-renumber via per-level counters — one rendering path), headings by level (h1 = slot size, h2 = 0.77x, ### = body+1, #### floors at bold body), HR via the editing syntaxOnly predicate, fenced code, blockquotes (QuoteTextBlock hairline), tables (NSTextTable 1px border, delimiter-row alignment), images (hover payload), links styled + retained. Footnotes stay fallback. Source ranges ride every block (click→edit map). - ReadingSurface: read-only selectable NSTextView canvas (#68: a presentation of the same TextKit2 document). Tap with no selection after mouseDown returns = edit intent at the clicked block (the tracking-loop fix); ⌘-click opens links; scroll tracking feeds the flip-back caret; image hover NSPopover loads off-main. - ProseTypography (#70): three role slots — Body / Headings / Code — each {face, size}, UserDefaults six values, member-based font resolution (family+weight lookup misresolves Concourse Book→Thin), silent system fallback; both prose surfaces read them; editing presenter's headings + dimmed syntax rewired through the slots. - PostEditorView: Read⇄Edit toolbar button (far left) + View menu "Toggle Reading Mode" + ⌘R; reading state transient per open; drafts included (draft mirror); flip-back caret hint applied once per token. - SettingsPaneView: Typography section — three slot rows (family picker incl. current face not in the installed list + size field). - kitchen-sink.md (+png): corpus piece exercising constructs absent from the eight posts, per #69. Verified: 94 unit tests (renderer classification, renumbering, source ranges, fallbacks) + UI smoke; PERF-3k renders 128 blocks in ~17 ms; live-app probe — Read renders kitchen-sink markers-hidden, renumbered, byte-stable file after full round-trip; click-to-edit flips at the clicked paragraph; typography ladder pixel-verified (20/15 ink px).
219 lines
7.0 KiB
Swift
219 lines
7.0 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 NSTextTable blocks with header bold.
|
|
@Test
|
|
func `table renders`() {
|
|
let src = "| A | B |\n| --- | --- |\n| one | two |"
|
|
let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil)
|
|
#expect(doc.bounds.count == 1)
|
|
#expect(doc.content.string.contains("one"))
|
|
#expect(doc.content.string.contains("two"))
|
|
let hasTableBlock = doc.content.enumerateAttribute(
|
|
.paragraphStyle, in: NSRange(location: 0, length: doc.content.length),
|
|
) { value, _, stop in
|
|
if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is NSTextTableBlock }) {
|
|
stop.pointee = true
|
|
}
|
|
}
|
|
_ = hasTableBlock // enumerate returns Void; assertion below is the check
|
|
// direct check: at least one NSTextTableBlock anywhere
|
|
var found = false
|
|
doc.content.enumerateAttribute(
|
|
.paragraphStyle, in: NSRange(location: 0, length: doc.content.length),
|
|
) { value, _, _ in
|
|
if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is NSTextTableBlock }) {
|
|
found = true
|
|
}
|
|
}
|
|
#expect(found)
|
|
}
|
|
|
|
/// 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, hairline block attached.
|
|
@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"))
|
|
var foundQuoteBlock = false
|
|
doc.content.enumerateAttribute(
|
|
.paragraphStyle, in: NSRange(location: 0, length: doc.content.length),
|
|
) { value, _, _ in
|
|
if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is QuoteTextBlock }) {
|
|
foundQuoteBlock = true
|
|
}
|
|
}
|
|
#expect(foundQuoteBlock)
|
|
}
|
|
|
|
/// 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 = ""
|
|
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 = ""
|
|
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: hairline block, 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)
|
|
var foundRule = false
|
|
doc.content.enumerateAttribute(
|
|
.paragraphStyle, in: NSRange(location: 0, length: doc.content.length),
|
|
) { value, _, _ in
|
|
if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is RuleTextBlock }) {
|
|
foundRule = true
|
|
}
|
|
}
|
|
#expect(foundRule)
|
|
}
|
|
|
|
/// 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 |
|
|
|
|

|
|
|
|
[link](https://x.de)
|
|
|
|
* * *
|
|
"""
|
|
let doc = ReadingPresenter.document(for: sink, resolvingImagesIn: nil)
|
|
#expect(doc.bounds.count >= 9)
|
|
}
|
|
}
|