Spike-locals renamed at lift time (identifier_name), literal tab in a string escape fixed (unprintable-ASCII build error), swiftlint force_try disable for the static regex. AGENTS.md reading-mode section rewritten for the apex HTML pipeline; native-only Work Guidance line rescoped to the editing side per ADR-0011.
174 lines
6.0 KiB
Swift
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 `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>"
|
|
#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 `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>"))
|
|
}
|
|
|
|
/// 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 = ""
|
|
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 < b & c > 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))
|
|
}
|