feat(editor): reading-parity presentation for the editing surface (#77)

ProsePresenter rewritten to mirror the calm-ruled reading surface (#78):
- leading calibrated to CSS 1.8 via font metrics (not the raw number)
- heading ladder 30/22/19-w600, h4+ bold body, new h3/h4+/fence/blank kinds
- half-height blank separators (9pt)
- quotes secondary-ink + 20pt indent; fence interiors mono-14 on substrate
- inline traits: bold, italic (CommonMark intraword _ rule), code, links
  (label ink + underline, marks dim); list markers dim
- 11 new tests pin the ladder, blanks, quote, fence, traits
This commit is contained in:
AlexKucera committed 2026-10-08 13:33:21 +02:00
1 parent 1a5f04c9f4
commit 8fe5f48e0e
3 files changed
+593 -77

No files matched your search

+140
View File
@@ -0,0 +1,140 @@
//
// ProsePresenterTests.swift
// Museion
//
// #77: editing-side reading-parity — pins the attribute map: heading ladder,
// half-height blanks, quote/indent styling, fence interiors, inline traits
// (bold/italic/code/link), and line-height calibration.
//
import AppKit
@testable import Museion
import Testing
@Suite(.serialized)
struct ProsePresenterTests {
// MARK: Line kinds
@Test func `heading kinds`() {
#expect(ProsePresenter.lineKind("# T") == .heading1)
#expect(ProsePresenter.lineKind("## T") == .heading2)
#expect(ProsePresenter.lineKind("### T") == .heading3)
#expect(ProsePresenter.lineKind("#### T") == .heading4Plus)
#expect(ProsePresenter.lineKind("###### T") == .heading4Plus)
}
@Test func `fence context`() {
#expect(ProsePresenter.classify("plain", inFence: true) == .fenceInterior)
#expect(ProsePresenter.classify("## not a heading", inFence: true) == .fenceInterior)
#expect(ProsePresenter.classify("```", inFence: false) == .fence)
}
@Test func `list markers`() {
#expect(ProsePresenter.lineKind("- item") == .listMarker)
#expect(ProsePresenter.lineKind("* item") == .listMarker)
#expect(ProsePresenter.lineKind("1. item") == .listMarker)
#expect(ProsePresenter.lineKind("12) item") == .listMarker)
#expect(ProsePresenter.lineKind("plain text") == .body)
}
// MARK: Attribute map parity
@Test func `heading ladder matches reading CSS`() {
let attributed = ProsePresenter.attribute("# H1\n\n## H2\n\n### H3\n\n#### H4")
let ns = attributed.string as NSString
// sample at the heading TEXT, not offset 0 (the dimmed marker)
let h1 = attributed.attribute(.font, at: ns.range(of: "H1").location, effectiveRange: nil) as? NSFont
#expect(h1?.pointSize == 30)
#expect(h1?.fontName.contains("ConcourseOT") == true || h1?.fontName.contains("Semibold") == true || h1?.fontName.contains("Bold") == true)
let h2 = attributed.attribute(.font, at: ns.range(of: "H2").location, effectiveRange: nil) as? NSFont
#expect(h2?.pointSize == 22)
let h3 = attributed.attribute(.font, at: ns.range(of: "H3").location, effectiveRange: nil) as? NSFont
#expect(h3?.pointSize == 19)
#expect(h3?.fontName.contains("Valkyrie") == true || h3?.fontName.contains("Bold") == true)
let h4 = attributed.attribute(.font, at: ns.range(of: "H4").location, effectiveRange: nil) as? NSFont
#expect(h4?.pointSize == 17)
#expect(h4?.fontName.contains("Bold") == true)
}
@Test func `blank lines are half height`() {
let attributed = ProsePresenter.attribute("para\n\nnext")
let ns = attributed.string as NSString
let blankLoc = ns.range(of: "\n\n").location + 1
let font = attributed.attribute(.font, at: blankLoc, effectiveRange: nil) as? NSFont
#expect(font?.pointSize == 9)
}
@Test func `body leading calibrated`() {
// CSS parity: 17pt × 1.8 ≈ 30.6pt of line, via the font's natural box.
let font = ProseTypography.bodyFont
let multiple = ProseTypography.lineHeightMultiple(font: font, cssLineHeight: 1.8)
let natural = font.ascender - font.descender + font.leading
#expect(abs(natural * multiple - font.pointSize * 1.8) < 0.5)
}
@Test func `quote styling`() {
let attributed = ProsePresenter.attribute("> quoted words")
let ns = attributed.string as NSString
let loc = ns.range(of: "quoted").location
let color = attributed.attribute(.foregroundColor, at: loc, effectiveRange: nil) as? NSColor
#expect(color == ProseTypography.secondaryColor)
let style = attributed.attribute(.paragraphStyle, at: loc, effectiveRange: nil) as? NSParagraphStyle
#expect(style?.firstLineHeadIndent == 20)
#expect(style?.headIndent == 20)
// marker dims
let mark = attributed.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? NSColor
#expect(mark == ProseTypography.syntaxColor)
}
@Test func `fence interior is code`() {
let attributed = ProsePresenter.attribute("```\ncode line\n```")
let ns = attributed.string as NSString
let loc = ns.range(of: "code line").location
let font = attributed.attribute(.font, at: loc, effectiveRange: nil) as? NSFont
#expect(font?.pointSize == 14)
#expect(font?.fontName.contains("Mono") == true || font?.fontName.contains("monospaced") == true)
#expect(attributed.attribute(.foregroundColor, at: loc, effectiveRange: nil) as? NSColor == ProseTypography.foregroundColor)
}
@Test func `inline traits`() {
let text = "bold **word** and _em_ and `code` and [label](http://x)"
let attributed = ProsePresenter.attribute(text)
let ns = text as NSString
let bold = attributed.attribute(.font, at: ns.range(of: "word").location, effectiveRange: nil) as? NSFont
#expect(bold?.fontName.contains("Bold") == true)
let em = attributed.attribute(.font, at: ns.range(of: "em").location, effectiveRange: nil) as? NSFont
#expect(em?.fontName.contains("Italic") == true)
let code = attributed.attribute(.font, at: ns.range(of: "code").location, effectiveRange: nil) as? NSFont
#expect(code?.pointSize == 14)
let label = attributed.attribute(.underlineStyle, at: ns.range(of: "label").location, effectiveRange: nil) as? Int
#expect(label == NSUnderlineStyle.single.rawValue)
// marks dim: the ** before "word"
let mark = attributed.attribute(.foregroundColor, at: ns.range(of: "**word").location, effectiveRange: nil) as? NSColor
#expect(mark == ProseTypography.syntaxColor)
}
@Test func `fences interior retrofit`() {
// reattribute over a paragraph inside an existing fence — state comes
// from the prefix scan, not the paragraph alone.
let full = "```\nfirst\n```\n\npara\n"
let out = NSMutableAttributedString(attributedString: ProsePresenter.attribute(full))
let ns = full as NSString
let para = ns.paragraphRange(for: NSRange(location: 0, length: 0)) // "```"
ProsePresenter.reattribute(out, paragraphRange: para)
let loc = ns.range(of: "first").location
let font = out.attribute(.font, at: loc, effectiveRange: nil) as? NSFont
#expect(font?.pointSize == 14)
}
@Test func `underscore in word not italic`() {
let attributed = ProsePresenter.attribute("file_name_here stays plain")
let ns = attributed.string as NSString
let font = attributed.attribute(.font, at: ns.range(of: "name").location, effectiveRange: nil) as? NSFont
#expect(font?.fontName.contains("Italic") != true)
}
}