Files
museion/MuseionTests/ProsePresenterTests.swift
T
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

176 lines
7.8 KiB
Swift
Raw Permalink 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 `outline rows use the body face at its natural line box`() {
// #109 cursor-jank fix: the live outline row must NOT carry the prose
// 1.8 lineHeightMultiple — an inflated fragment grows the focused row
// and detaches the bullet. Natural box, body face, nothing else.
let attrs = ProseTypography.outlineRowAttributes
let font = attrs[.font] as? NSFont
#expect(font === ProseTypography.bodyFont)
guard let style = attrs[.paragraphStyle] as? NSParagraphStyle else {
return // no paragraph style = natural line box — the point
}
#expect(style.lineHeightMultiple == 0) // set → would inflate the row
}
@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)
}
}