Files
museion/MuseionTests/ProsePresenterTests.swift
T
AlexKucera 1f7557a2ed fix(editor): audit fixes — fence staleness, font caching, lint shrink (#77)
- HIGH: fence delimiter edits now restyle the whole document (delimiter
  count fingerprint in the coordinator; count moved = full re-attribute,
  caret preserved) — typing/deleting a ``` flipped every line until
  the match before
- derived fonts + measureWidth cached as nonisolated(unsafe) static let
  (were NSFontManager.convert per line per pass)
- h2/h3/h4 line height calibrated from their own face's metrics
- 7+ hashes fall to body (CommonMark cap); tab-indented heading marker
  dims the hash, not the tab
- intraword _ spans unclaim on reject so the fallback char-dim takes them
- shrink: fenceState reuses contentLines+classify (isDelim deleted),
  marker regexes as literals (helper + 2 lint disables deleted),
  charBefore/After via NSString, dead colors + forwarders deleted
- vacuous retrofit test replaced with a real one + fenceDelimCount test
2026-10-08 14:11:47 +02:00

159 lines
7.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// 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 a paragraph INSIDE a fence at location > 0 — the
// fence state must come from the prefix scan, not the paragraph.
let full = "para before\n\n```\nfirst code\n```\n\nafter"
let out = NSMutableAttributedString(attributedString: ProsePresenter.attribute(full))
let ns = full as NSString
let firstRange = ns.range(of: "first code")
// clobber the line to body, then reattribute its paragraph
out.setAttributes(ProseTypography.bodyAttributes, range: firstRange)
let para = ns.paragraphRange(for: NSRange(location: firstRange.location, length: 0))
ProsePresenter.reattribute(out, paragraphRange: para)
let font = out.attribute(.font, at: firstRange.location, effectiveRange: nil) as? NSFont
#expect(font?.pointSize == 14)
// Outside the fence (prefix scan returns false): body styling.
let afterRange = ns.range(of: "after")
out.setAttributes([.font: ProseTypography.codeFont], range: afterRange)
let afterPara = ns.paragraphRange(for: NSRange(location: afterRange.location, length: 0))
ProsePresenter.reattribute(out, paragraphRange: afterPara)
let afterFont = out.attribute(.font, at: afterRange.location, effectiveRange: nil) as? NSFont
#expect(afterFont?.pointSize == 17)
}
@Test func `fence delim count tracks delimiters`() {
#expect(ProsePresenter.fenceDelimCount("plain") == 0)
#expect(ProsePresenter.fenceDelimCount("```\ncode\n```") == 2)
#expect(ProsePresenter.fenceDelimCount("~~~\ncode\n```") == 2)
#expect(ProsePresenter.fenceDelimCount("```js\nlet x = 1\n```") == 2)
#expect(ProsePresenter.fenceDelimCount("not ``` inline, a fence needs line start") == 0)
}
@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)
}
}