feat(reading): swap reading surface to apex HTML pipeline (#76)
ReadingPresenter becomes markdown -> bundled apex -> HTML fragment with data-source-line stamps (chunker sentinels + regex restamp, #74-proven); ReadingSurface becomes one WKWebView reloaded in place (no remount), CSS vars from ProseTypography slots + flow knobs. TextKit reading machinery deleted (ADR-0011: no fallbacks; stale-flow defect dies unfixed). Footnotes now render natively (apex upgrade over #69's fallback). Tests re-targeted at HTML string asserts incl. real-bundled-binary pipeline runs. Closes-nothing: wayfinder #76 on map #20; branch wayfinder/76-apex-html-reading.
This commit is contained in:
1 parent
fffcb0a059
commit
cc04ec236e
4 files changed
+505
-965
No files matched your search
@@ -2,201 +2,172 @@
|
||||
// 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).
|
||||
// #76 (map #20, ADR-0011): apex HTML pipeline coverage — the smallest
|
||||
// checks that fail if the source map (chunker/stamper/blocks), the apex
|
||||
// invocation, or the page assembly break. Fixture strings inline (suite
|
||||
// convention); the pipeline tests run the real bundled apex binary.
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import Foundation
|
||||
@testable import Museion
|
||||
import Testing
|
||||
|
||||
@MainActor
|
||||
struct ReadingPresenterTests {
|
||||
/// Blank-line split + heading fonting + markup hidden.
|
||||
// MARK: Source map
|
||||
|
||||
/// Chunker: sentinels before chunks, blank structure preserved (spike
|
||||
/// self-check fixture).
|
||||
@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."))
|
||||
func chunkerStampsSentinels() {
|
||||
let md = "# T\n\nA\n\nB\n1. x\n\n\nC"
|
||||
#expect(ReadingPresenter.stampSource(md) == "<!--S:1-->\n# T\n\n<!--S:3-->\nA\n\n<!--S:5-->\nB\n1. x\n\n<!--S:9-->\nC")
|
||||
}
|
||||
|
||||
/// Auto-renumbering: `1. 1. 1.` renders 1, 2, 3; source never rewritten.
|
||||
/// Stamper: sentinel → attribute on the following element; multi-digit.
|
||||
@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"))
|
||||
func stamperRewritesAttributes() {
|
||||
let html = "<h1>T</h1>\n<!--S:1-->\n<p>A</p>\n<!--S:12-->\n<ul>\n<li>B</li>\n</ul>"
|
||||
#expect(ReadingPresenter.stampHTML(html) == "<h1>T</h1>\n<p data-source-line=\"1\">A</p>\n<ul data-source-line=\"12\">\n<li>B</li>\n</ul>")
|
||||
}
|
||||
|
||||
/// Nesting: deeper items indent further; mixed bullet/number nesting rides
|
||||
/// the same path.
|
||||
/// The #68 click→edit map: blocks are monotonic, lines are 1-based, and
|
||||
/// ranges point at the exact chunk offsets in the source.
|
||||
@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`() {
|
||||
func blocksMapChunks() {
|
||||
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 blocks = ReadingPresenter.blocks(for: src)
|
||||
#expect(blocks.count == 4)
|
||||
#expect(blocks.map(\.sourceLine) == [1, 3, 5, 8])
|
||||
let nsSrc = src as NSString
|
||||
let expected = nsSrc.range(of: "last para").location
|
||||
#expect(doc.bounds[3].sourceRange.location == expected)
|
||||
#expect(blocks[3].sourceRange.location == nsSrc.range(of: "last para").location)
|
||||
#expect(blocks[0].sourceRange.location == 0)
|
||||
}
|
||||
|
||||
/// Tables render as a mono grid: decoration carries column offsets,
|
||||
/// header/body rows are padded columns of text.
|
||||
/// Blocks over CRLF and multi-blank separators stay monotonic.
|
||||
@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
|
||||
func blocksSurviveBlankRuns() {
|
||||
let src = "one\n\n\n\n two"
|
||||
let blocks = ReadingPresenter.blocks(for: src)
|
||||
#expect(blocks.count == 2)
|
||||
#expect(blocks[0].sourceLine == 1)
|
||||
#expect(blocks[1].sourceLine == 5)
|
||||
}
|
||||
|
||||
/// Fenced code: mono face, markers hidden, content intact.
|
||||
// MARK: Pipeline (real apex binary)
|
||||
|
||||
/// Headings + paragraphs + inline marks render; markup never leaks.
|
||||
@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)
|
||||
func pipelineHeadingsAndInline() throws {
|
||||
let (html, blocks) = ReadingPresenter.page(for: "# Title\n\nBody with _em_ and `code`.", postFolder: nil)
|
||||
try assertRendered(html, blocks: blocks, count: 2)
|
||||
#expect(html.contains("<h1 data-source-line=\"1\""))
|
||||
#expect(html.contains("<em>em</em>"))
|
||||
#expect(html.contains("<code>code</code>"))
|
||||
#expect(!html.contains("_em_"))
|
||||
}
|
||||
|
||||
/// Blockquote: markers hidden, quote decoration on the bound.
|
||||
/// `1. 1. 1.` renumbers via native <ol> (the #69 hard requirement).
|
||||
@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
|
||||
}
|
||||
func pipelineOrderedRenumber() throws {
|
||||
let src = "1. one\n1. two\n1. three"
|
||||
let (html, blocks) = ReadingPresenter.page(for: src, postFolder: nil)
|
||||
try assertRendered(html, blocks: blocks, count: 1)
|
||||
#expect(html.contains("<ol data-source-line=\"1\">"))
|
||||
#expect(html.contains("<li>one</li>"))
|
||||
#expect(html.contains("<li>two</li>"))
|
||||
#expect(html.contains("<li>three</li>"))
|
||||
#expect(!html.contains("1. one"))
|
||||
}
|
||||
|
||||
/// Links styled + attribute retained (⌘-click opens).
|
||||
/// `--no-mixed-lists`: an ordered list after a bullet list stays two
|
||||
/// lists (default apex merges them — kills renumbering).
|
||||
@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)
|
||||
func pipelineMixedListsStaySplit() throws {
|
||||
let src = "- bullet\n- another\n\n1. one\n1. two"
|
||||
let (html, _) = ReadingPresenter.page(for: src, postFolder: nil)
|
||||
#expect(html.contains("<ul"))
|
||||
#expect(html.contains("<ol"))
|
||||
#expect(!html.contains("<li>one</li></ul>"))
|
||||
}
|
||||
|
||||
/// Images: hover payload carried on the bound, syntax stays text.
|
||||
/// Tables, quotes, fenced code, HR, links — the #69 set on one page.
|
||||
@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"))
|
||||
}
|
||||
func pipelineConstructs() throws {
|
||||
let src = """
|
||||
| A | B |
|
||||
| --- | --- |
|
||||
| one | two |
|
||||
|
||||
/// 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: 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
|
||||
> quoted
|
||||
|
||||
```txt
|
||||
code
|
||||
let x = 1
|
||||
```
|
||||
|
||||
| H |
|
||||
| --- |
|
||||
| c |
|
||||
|
||||

|
||||
|
||||
[link](https://x.de)
|
||||
See [the docs](https://example.net) now
|
||||
|
||||
* * *
|
||||
"""
|
||||
let doc = ReadingPresenter.document(for: sink, resolvingImagesIn: nil)
|
||||
#expect(doc.bounds.count >= 9)
|
||||
let (html, blocks) = ReadingPresenter.page(for: src, postFolder: nil)
|
||||
try assertRendered(html, blocks: blocks, count: 5)
|
||||
#expect(html.contains("<table data-source-line=\"1\">"))
|
||||
#expect(html.contains("<blockquote data-source-line=\"5\">"))
|
||||
#expect(html.contains("<pre"))
|
||||
#expect(html.contains("let x = 1"))
|
||||
#expect(!html.contains("```"))
|
||||
#expect(html.contains("<a href=\"https://example.net\">the docs</a>"))
|
||||
#expect(html.contains("<hr"))
|
||||
}
|
||||
|
||||
/// Images embed as base64 data URLs against the post folder (#69/#74).
|
||||
@Test
|
||||
func pipelineImageEmbeds() throws {
|
||||
let prose = URL(fileURLWithPath: #filePath) // …/MuseionTests/ReadingPresenterTests.swift
|
||||
.deletingLastPathComponent().deletingLastPathComponent()
|
||||
.appendingPathComponent("spike/prose")
|
||||
let src = ""
|
||||
let (html, blocks) = ReadingPresenter.page(for: src, postFolder: prose)
|
||||
try assertRendered(html, blocks: blocks, count: 1)
|
||||
#expect(html.contains("src=\"data:image/png;base64,"))
|
||||
}
|
||||
|
||||
/// Footnotes render natively (apex upgrade over #69's fallback).
|
||||
@Test
|
||||
func pipelineFootnotes() throws {
|
||||
let src = "Text with note[^1]\n\n[^1]: the note"
|
||||
let (html, _) = ReadingPresenter.page(for: src, postFolder: nil)
|
||||
#expect(html.contains("footnote"))
|
||||
#expect(html.contains("the note"))
|
||||
}
|
||||
|
||||
/// Fallback: apex failure → editing-treated source (#69 rule), escaped.
|
||||
@Test
|
||||
func fallbackIsSourceText() {
|
||||
let page = ReadingPresenter.fallbackPage("a < b & c > d")
|
||||
#expect(page.contains("<pre data-source-line=\"1\">a < b & c > d</pre>"))
|
||||
}
|
||||
|
||||
/// The stylesheet carries the live slot values as CSS variables.
|
||||
@Test
|
||||
func stylesheetCarriesSlots() {
|
||||
let css = ReadingPresenter.styleSheet()
|
||||
#expect(css.contains("--body-face:"))
|
||||
#expect(css.contains("--headings-size:"))
|
||||
#expect(css.contains("--code-face:"))
|
||||
#expect(css.contains("--lh:"))
|
||||
#expect(css.contains("[data-source-line]:hover"))
|
||||
}
|
||||
|
||||
private func assertRendered(_ html: String, blocks: [ReadingPresenter.Block], count: Int) throws {
|
||||
let stamped = try XCTRegexMatches(html, pattern: "data-source-line=\"\\d+\"")
|
||||
#expect(stamped == count)
|
||||
#expect(blocks.count == count)
|
||||
#expect(!html.contains("<!--S:"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Regex count helper (Swift Testing has no XCTest import here).
|
||||
private func XCTRegexMatches(_ s: String, pattern: String) throws -> Int {
|
||||
let re = try NSRegularExpression(pattern: pattern)
|
||||
return re.numberOfMatches(in: s, range: NSRange(s.startIndex..., in: s))
|
||||
}
|
||||
Reference in new issue
Block a user