Files
museion/MuseionTests/ReadingPresenterTests.swift
AlexKucera d7fd11cf0d
CI / ci (push) Canceled after 0s
chore(lint): repo-wide green becomes the contract
Close the lint arc #194-#197: deliberate-style rule disables with
rationales (#194), filter-safe test renames + --test-case-name-format
preserve (#195), every surviving warning fixed through the sanctioned
lint-stable shapes (#196), and the docs now state the true contract
(#197): bare swiftlint . reports zero violations and swiftformat .
reformats nothing, while push gates stay scoped to the push's own
files (settled #190 design) — the pre-push hook's stale 'pre-existing
violations' rationale blocks refreshed, gate logic untouched.

Also in the sweep: quiesce the FSEvent-burst watcher race in
LibrarySearchTests (two rescans double-count one shared PostScanCache
miss; benign revision over-bump), pin the learnings scan's symlink-skip
contract (flip-verified — defended in depth by the lstat guard AND
contentsOfDirectory's own symlink refusal), reorder that scan's guard
so cheap rejections precede the directory listing, and park the roster
chmod window with a ceiling comment.

Finish-audit: 3 reviewers, 0 blocking; the one code finding was
disproved by probe (&& out-binds ?? — the guard was correct). Suite
693/693 via scripts/full-test.sh; swiftlint 0 violations; swiftformat
0/141; hook rig 23/23.

Closes #197
2026-11-17 16:29:46 +01:00

192 lines
6.9 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 `chunker stamps sentinels`() {
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 `stamper rewrites attributes`() {
let html = "<h1>T</h1>\n<!--S:1-->\n<p>A</p>\n<!--S:12-->\n<ul>\n<li>B</li>\n</ul>"
let expected = "<h1>T</h1>\n<p data-source-line=\"1\">A</p>\n<ul data-source-line=\"12\">\n<li>B</li>\n</ul>"
#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("<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 `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("<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 `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("<ul"))
#expect(html.contains("<ol"))
#expect(!html.contains("<li>one</li></ul>"))
}
/// #109: an outline body (canonical nested bullets, exactly what
/// OutlineTree.serialize emits) renders as NESTED <ul> 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("<ul data-source-line=\"1\">"))
// apex emits bare <li>intro + a nested <ul> for the children
#expect(html.contains("<li>intro\n<ul>"))
#expect(html.contains("<li>deep evidence</li>"))
#expect(html.contains("<li>second point</li>"))
#expect(html.contains("<li>outro</li>"))
}
/// 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("<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 `pipeline image embeds`() 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 `pipeline footnotes`() {
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 `fallback is source text`() {
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 `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("<!--S:"))
}
}
/// Regex count helper (Swift Testing has no XCTest import here).
private func XCTRegexMatches(_ text: String, pattern: String) throws -> Int {
let regex = try NSRegularExpression(pattern: pattern)
return regex.numberOfMatches(in: text, range: NSRange(text.startIndex..., in: text))
}