// // 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) } }