Files
museion/MuseionTests/ReadingPresenterTests.swift
T
AlexKucera cc04ec236e 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.
2026-10-06 18:26:22 +02:00

174 lines
6.0 KiB
Swift

//
// ReadingPresenterTests.swift
// MuseionTests
//
// #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 Foundation
@testable import Museion
import Testing
struct ReadingPresenterTests {
// MARK: Source map
/// Chunker: sentinels before chunks, blank structure preserved (spike
/// self-check fixture).
@Test
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")
}
/// Stamper: sentinel → attribute on the following element; multi-digit.
@Test
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>")
}
/// 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 blocksMapChunks() {
let src = "# A\n\nfirst para\n\n- l1\n- l2\n\nlast para"
let blocks = ReadingPresenter.blocks(for: src)
#expect(blocks.count == 4)
#expect(blocks.map(\.sourceLine) == [1, 3, 5, 8])
let nsSrc = src as NSString
#expect(blocks[3].sourceRange.location == nsSrc.range(of: "last para").location)
#expect(blocks[0].sourceRange.location == 0)
}
/// Blocks over CRLF and multi-blank separators stay monotonic.
@Test
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)
}
// MARK: Pipeline (real apex binary)
/// Headings + paragraphs + inline marks render; markup never leaks.
@Test
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_"))
}
/// `1. 1. 1.` renumbers via native <ol> (the #69 hard requirement).
@Test
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"))
}
/// `--no-mixed-lists`: an ordered list after a bullet list stays two
/// lists (default apex merges them — kills renumbering).
@Test
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>"))
}
/// Tables, quotes, fenced code, HR, links — the #69 set on one page.
@Test
func pipelineConstructs() throws {
let src = """
| A | B |
| --- | --- |
| one | two |
> quoted
```txt
let x = 1
```
See [the docs](https://example.net) now
* * *
"""
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 = "![alt text](assets/kitchen-sink.png)"
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 &lt; b &amp; c &gt; 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))
}