//
// 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 `chunker stamps sentinels`() {
let md = "# T\n\nA\n\nB\n1. x\n\n\nC"
#expect(ReadingPresenter.stampSource(md) == "\n# T\n\n\nA\n\n\nB\n1. x\n\n\nC")
}
/// Stamper: sentinel → attribute on the following element; multi-digit.
@Test
func `stamper rewrites attributes`() {
let html = "
T
\n\nA
\n\n"
let expected = "T
\nA
\n"
#expect(ReadingPresenter.stampHTML(html) == expected)
}
/// 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 `blocks map chunks`() {
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 `blocks survive blank runs`() {
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 `pipeline headings and inline`() 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("em"))
#expect(html.contains("code"))
#expect(!html.contains("_em_"))
}
/// `1. 1. 1.` renumbers via native (the #69 hard requirement).
@Test
func `pipeline ordered renumber`() 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(""))
#expect(html.contains("- one
"))
#expect(html.contains("- two
"))
#expect(html.contains("- three
"))
#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 `pipeline mixed lists stay split`() {
let src = "- bullet\n- another\n\n1. one\n1. two"
let (html, _) = ReadingPresenter.page(for: src, postFolder: nil)
#expect(html.contains(""))
}
/// #109: an outline body (canonical nested bullets, exactly what
/// OutlineTree.serialize emits) renders as NESTED through the
/// unchanged apex pipeline — reading mode needs no outline-specific
/// surface (ADR-0013: "nested lists already render").
@Test
func `pipeline nested bullets render nested lists`() throws {
let src = "- intro\n - first point\n - deep evidence\n - second point\n- outro"
let (html, blocks) = ReadingPresenter.page(for: src, postFolder: nil)
try assertRendered(html, blocks: blocks, count: 1)
#expect(html.contains(""))
// apex emits bare - intro + a nested
for the children
#expect(html.contains("- intro\n
"))
#expect(html.contains("- deep evidence
"))
#expect(html.contains("- second point
"))
#expect(html.contains("- outro
"))
}
/// Tables, quotes, fenced code, HR, links — the #69 set on one page.
@Test
func `pipeline constructs`() 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(""))
#expect(html.contains(""))
#expect(html.contains("the docs"))
#expect(html.contains("
d")
#expect(page.contains("a < b & c > d
"))
}
/// The stylesheet carries the live slot values as CSS variables.
@Test
func `stylesheet carries slots`() {
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("