// // ReadingPresenterTests.swift // MuseionTests // // #71 (map #20): renderer coverage — the smallest checks that fail if the // block classification, auto-renumbering, source-range mapping, or inline // interpretation break. Kitchen-sink fixture strings inline (no file // fixtures per the suite's convention). // import AppKit @testable import Museion import Testing @MainActor struct ReadingPresenterTests { /// Blank-line split + heading fonting + markup hidden. @Test func `headings and paragraphs`() { let doc = ReadingPresenter.document(for: "# Title\n\nBody with _em_.\n\n### Deep", resolvingImagesIn: nil) #expect(doc.bounds.count == 3) let flat = doc.content.string #expect(!flat.contains("#")) #expect(flat.contains("Title")) #expect(flat.contains("Body with em.")) } /// Auto-renumbering: `1. 1. 1.` renders 1, 2, 3; source never rewritten. @Test func `ordered list auto renumbers`() { let src = "1. one\n1. two\n1. three" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(doc.bounds.count == 1) #expect(doc.content.string.contains("1. one")) #expect(doc.content.string.contains("2. two")) #expect(doc.content.string.contains("3. three")) #expect(!doc.content.string.contains("1. two")) } /// Nesting: deeper items indent further; mixed bullet/number nesting rides /// the same path. @Test func `list nesting`() { let src = "- top\n - nested\n- back" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(doc.bounds.count == 1) #expect(doc.content.string.contains("top")) #expect(doc.content.string.contains("nested")) } /// The #68 click→edit map: block source ranges are monotonic and cover /// every block; a location mid-document maps to its containing block. @Test func `source ranges map blocks`() { let src = "# A\n\nfirst para\n\n- l1\n- l2\n\nlast para" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(doc.bounds.count == 4) let locations = doc.bounds.map(\.sourceRange.location) #expect(locations == locations.sorted()) // "last para" starts at its own chunk offset let nsSrc = src as NSString let expected = nsSrc.range(of: "last para").location #expect(doc.bounds[3].sourceRange.location == expected) } /// Tables render as NSTextTable blocks with header bold. @Test func `table renders`() { let src = "| A | B |\n| --- | --- |\n| one | two |" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(doc.bounds.count == 1) #expect(doc.content.string.contains("one")) #expect(doc.content.string.contains("two")) let hasTableBlock = doc.content.enumerateAttribute( .paragraphStyle, in: NSRange(location: 0, length: doc.content.length), ) { value, _, stop in if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is NSTextTableBlock }) { stop.pointee = true } } _ = hasTableBlock // enumerate returns Void; assertion below is the check // direct check: at least one NSTextTableBlock anywhere var found = false doc.content.enumerateAttribute( .paragraphStyle, in: NSRange(location: 0, length: doc.content.length), ) { value, _, _ in if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is NSTextTableBlock }) { found = true } } #expect(found) } /// Fenced code: mono face, markers hidden, content intact. @Test func `fenced code`() { let src = "```swift\nlet x = 1\n```" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(!doc.content.string.contains("```")) #expect(doc.content.string.contains("let x = 1")) let attrs = doc.content.attributes(at: 0, effectiveRange: nil) #expect((attrs[.font] as? NSFont)?.fontName == ProseTypography.codeFont.fontName) } /// Blockquote: markers hidden, hairline block attached. @Test func blockquote() { let src = "> quoted _words_\n>> deeper" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(!doc.content.string.contains(">")) #expect(doc.content.string.contains("quoted words")) var foundQuoteBlock = false doc.content.enumerateAttribute( .paragraphStyle, in: NSRange(location: 0, length: doc.content.length), ) { value, _, _ in if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is QuoteTextBlock }) { foundQuoteBlock = true } } #expect(foundQuoteBlock) } /// Links styled + attribute retained (⌘-click opens). @Test func `links retained`() { let src = "See [the docs](https://example.de) now" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) let range = (doc.content.string as NSString).range(of: "the docs") #expect(range.location != NSNotFound) let attrs = doc.content.attributes(at: range.location, effectiveRange: nil) #expect(attrs[.link] != nil) } /// Images: hover payload carried on the bound, syntax stays text. @Test func `image hover payload`() { let src = "![alt text](assets/img.png)" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(doc.bounds.count == 1) #expect(doc.bounds[0].imageURL != nil) #expect(doc.content.string.contains("alt text")) } /// Image URL resolution against the post folder (#71). @Test func `image URL resolves against post folder`() { let src = "![x](assets/img.png)" let folder = URL(fileURLWithPath: "/tmp/posts/example") let doc = ReadingPresenter.document(for: src, resolvingImagesIn: folder) #expect(doc.bounds[0].imageURL?.path == "/tmp/posts/example/assets/img.png") } /// Footnotes are the one fallback: source text, no transformation. @Test func `footnotes stay fallback`() { let src = "Text with note[^1]\n\n[^1]: the note" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(doc.content.string.contains("[^1]")) } /// HR: hairline block, matcher shared with editing. @Test func `horizontal rule`() { let src = "above\n\n* * *\n\nbelow" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(doc.bounds.count == 3) var foundRule = false doc.content.enumerateAttribute( .paragraphStyle, in: NSRange(location: 0, length: doc.content.length), ) { value, _, _ in if let ps = value as? NSParagraphStyle, ps.textBlocks.contains(where: { $0 is RuleTextBlock }) { foundRule = true } } #expect(foundRule) } /// Soft-joined lines: one paragraph, single spaces. @Test func `soft joined lines`() { let src = "line one\nline two" let doc = ReadingPresenter.document(for: src, resolvingImagesIn: nil) #expect(doc.bounds.count == 1) #expect(doc.content.string == "line one line two") } /// Kitchen-sink smoke: every construct in one document without crashing. @Test func `kitchen sink renders`() { let sink = """ # Sink - bullet - another 1. one 1. two > quote ```txt code ``` | H | | --- | | c | ![i](a.png) [link](https://x.de) * * * """ let doc = ReadingPresenter.document(for: sink, resolvingImagesIn: nil) #expect(doc.bounds.count >= 9) } }