// // ProseListTests.swift // Museion // // #115 (S5): the prose-list model pins — item scan (nesting, lazy // continuation, blanks, fences), Tab/⇧Tab deltas (the content-column // rule), sibling moves (separators preserved, boundaries no-op), fold // scopes — plus the view's fold rendering: the buffer never changes, // hidden scopes carry the folded attributes, folds re-key after edits. // import AppKit @testable import Museion import Testing struct ProseListTests { // MARK: Scan @Test func `item basics`() throws { let item = try #require(ProseList.item(at: 0, in: "- a\n- b\n- c\n")) #expect(item.indent == 0) #expect(item.block == NSRange(location: 0, length: 4)) #expect(item.foldScope == nil) #expect(item.markerEnd == 3) } @Test func `nested children and scope`() throws { let text = "- a\n - a1\n - a2\n- b\n" let parentItem = try #require(ProseList.item(at: 0, in: text)) #expect(parentItem.block == NSRange(location: 0, length: 18)) // "- a\n - a1\n - a2\n" #expect(parentItem.foldScope == NSRange(location: 4, length: 14)) #expect(parentItem.hiddenLineCount == 2) let secondItem = try #require(ProseList.item(at: 19, in: text)) #expect(secondItem.block == NSRange(location: 18, length: 4)) #expect(secondItem.foldScope == nil) } @Test func `lazy continuation belongs to item`() throws { let parentItem = try #require(ProseList.item(at: 0, in: "- a\ncont\n- b\n")) #expect(parentItem.block.length == 9) // "- a\n" + "cont\n" #expect(parentItem.foldScope == nil) // indent-0 prose is not a fold child } @Test func `blank separated child folds blank too`() throws { let text = "- a\n\n - a1\n- b\n" let parentItem = try #require(ProseList.item(at: 0, in: text)) #expect(parentItem.block.length == 12) // "- a\n" + "\n" + " - a1\n" #expect(parentItem.foldScope == NSRange(location: 4, length: 8)) // starts at the blank #expect(parentItem.hiddenLineCount == 2) } @Test func `fence blocks item detection`() { #expect(ProseList.item(at: 5, in: "```\n- x\n```\n- y\n") == nil) #expect(ProseList.item(at: 13, in: "```\n- x\n```\n- y\n") != nil) } @Test func `caret in leading whitespace still finds item`() { #expect(ProseList.item(at: 1, in: " - a\n") != nil) } @Test func `parents scan`() { let parentsList = ProseList.parents(in: "- a\n - a1\n- b\n - b1\n") #expect(parentsList.map(\.markerLine.location) == [0, 11]) #expect(parentsList.allSatisfy { $0.hiddenLineCount == 1 }) } // MARK: Indent / outdent @Test func `indent uses dash content column`() throws { let text = "- a\n- b\n" let item = try #require(ProseList.item(at: 5, in: text)) let edit = try #require(ProseList.indent(item, caret: 5, in: text)) #expect(edit.range == NSRange(location: 4, length: 4)) #expect(edit.replacement == " - b\n") #expect(edit.caret == 7) } @Test func `indent uses ordered content column`() throws { let text = "1. one\n1. two\n" let item = try #require(ProseList.item(at: 8, in: text)) let edit = try #require(ProseList.indent(item, caret: 8, in: text)) #expect(edit.replacement == " 1. two\n") // "1. " content column = 3 } @Test func `indent shifts subtree`() throws { let text = "- a\n- b\n - b1\n" let item = try #require(ProseList.item(at: 5, in: text)) let edit = try #require(ProseList.indent(item, caret: 5, in: text)) #expect(edit.replacement == " - b\n - b1\n") } @Test func `indent nests under previous sibling past its children`() throws { let text = "- a\n - a1\n- b\n" // walking back from b: a1 (deeper — skip), a (same indent) = sibling let item = try #require(ProseList.item(at: 11, in: text)) let edit = try #require(ProseList.indent(item, caret: 11, in: text)) #expect(edit.replacement == " - b\n") } @Test func `indent boundary after paragraph noop`() throws { let text = "intro\n\n- a\n- b\n" let item = try #require(ProseList.item(at: 8, in: text)) #expect(ProseList.indent(item, caret: 8, in: text) == nil) } @Test func `indent first child noop`() throws { let text = "- a\n - a1\n" let item = try #require(ProseList.item(at: 5, in: text)) #expect(ProseList.indent(item, caret: 5, in: text) == nil) } @Test func `indent crosses lazy prose to sibling`() throws { let text = "- a\ncont\n- b\n" let item = try #require(ProseList.item(at: 9, in: text)) let edit = try #require(ProseList.indent(item, caret: 9, in: text)) #expect(edit.replacement == " - b\n") } @Test func `indent stops at standalone paragraph`() throws { let text = "- a\n\npara\n\n- b\n" let item = try #require(ProseList.item(at: 12, in: text)) #expect(ProseList.indent(item, caret: 12, in: text) == nil) } @Test func `outdent removes two and shifts caret`() throws { let text = "- a\n - b1\n" let item = try #require(ProseList.item(at: 5, in: text)) let edit = try #require(ProseList.outdent(item, caret: 7, in: text)) #expect(edit.range == NSRange(location: 4, length: 7)) #expect(edit.replacement == "- b1\n") #expect(edit.caret == 5) } @Test func `outdent parks caret in stripped prefix`() throws { let text = "- a\n - b1\n" let item = try #require(ProseList.item(at: 5, in: text)) let edit = try #require(ProseList.outdent(item, caret: 5, in: text)) #expect(edit.replacement == "- b1\n") #expect(edit.caret == 4) // caret was inside the stripped spaces } @Test func `outdent rides subtree`() throws { let text = " - b\n - b1\n" let item = try #require(ProseList.item(at: 2, in: text)) let edit = try #require(ProseList.outdent(item, caret: 4, in: text)) #expect(edit.replacement == "- b\n - b1\n") } @Test func `outdent at root noop`() throws { let item = try #require(ProseList.item(at: 0, in: "- a\n")) #expect(ProseList.outdent(item, caret: 0, in: "- a\n") == nil) } // MARK: Moves @Test func `move down swaps sibling`() throws { let text = "- a\n- b\n- c\n" let item = try #require(ProseList.item(at: 0, in: text)) let edit = try #require(ProseList.move(item, caret: 1, direction: 1, in: text)) #expect(edit.range == NSRange(location: 0, length: 8)) #expect(edit.replacement == "- b\n- a\n") #expect(edit.caret == 5) } @Test func `move down rides subtree`() throws { let text = "- a\n - a1\n- b\n" let item = try #require(ProseList.item(at: 0, in: text)) let edit = try #require(ProseList.move(item, caret: 1, direction: 1, in: text)) #expect(edit.replacement == "- b\n- a\n - a1\n") #expect(edit.caret == 5) } @Test func `move down keeps sibling subtree with sibling`() throws { // #115 audit pin: the SIBLING's children must ride the sibling, not // get stolen by the moved item (marker-line swap bug). let text = "- a\n- b\n - b1\n- c\n" let item = try #require(ProseList.item(at: 0, in: text)) let edit = try #require(ProseList.move(item, caret: 1, direction: 1, in: text)) #expect(edit.replacement == "- b\n - b1\n- a\n") } @Test func `move up keeps sibling subtree with sibling`() throws { let text = "- a\n - a1\n- b\n" let item = try #require(ProseList.item(at: 11, in: text)) let edit = try #require(ProseList.move(item, caret: 12, direction: -1, in: text)) #expect(edit.replacement == "- b\n- a\n - a1\n") #expect(edit.caret == 1) } @Test func `move up keeps sibling lazy line with sibling`() throws { let text = "- a\ncont\n- b\n" let item = try #require(ProseList.item(at: 9, in: text)) let edit = try #require(ProseList.move(item, caret: 9, direction: -1, in: text)) #expect(edit.replacement == "- b\n- a\ncont\n") } @Test func `indent leaves blank lines empty`() throws { // #115 audit pin: blank lines inside the block must not gain // injected leading spaces. let text = "- p\n- a\n\n - a1\n" let item = try #require(ProseList.item(at: 5, in: text)) let edit = try #require(ProseList.indent(item, caret: 6, in: text)) #expect(edit.replacement == " - a\n\n - a1\n") } @Test func `move up swaps with previous`() throws { let text = "- a\n- b\n- c\n" let item = try #require(ProseList.item(at: 5, in: text)) let edit = try #require(ProseList.move(item, caret: 5, direction: -1, in: text)) #expect(edit.range == NSRange(location: 0, length: 8)) #expect(edit.replacement == "- b\n- a\n") #expect(edit.caret == 1) } @Test func `move boundaries noop`() throws { let text = "- a\n- b\n" let firstItem = try #require(ProseList.item(at: 0, in: text)) let lastItem = try #require(ProseList.item(at: 5, in: text)) #expect(ProseList.move(firstItem, caret: 0, direction: -1, in: text) == nil) #expect(ProseList.move(lastItem, caret: 5, direction: 1, in: text) == nil) } @Test func `move at deeper boundary noop`() throws { // a1's next list line is "- b" at a shallower indent — not a sibling let boundaryText = "- a\n - a1\n- b\n" let deepItem = try #require(ProseList.item(at: 5, in: boundaryText)) #expect(ProseList.move(deepItem, caret: 5, direction: 1, in: boundaryText) == nil) } @Test func `move preserves blank separators`() throws { let text = "- a\n\n- b\n\n- c\n" let item = try #require(ProseList.item(at: 0, in: text)) let edit = try #require(ProseList.move(item, caret: 1, direction: 1, in: text)) #expect(edit.replacement == "- b\n\n- a\n") } @Test func `move stops at paragraph boundary`() throws { let paraText = "- a\n\npara\n\n- b\n" let paraItem = try #require(ProseList.item(at: 0, in: paraText)) #expect(ProseList.move(paraItem, caret: 1, direction: 1, in: paraText) == nil) } } @MainActor struct ProseFoldRenderingTests { private func makeView(_ text: String) -> ProseListTextView { let textView = ProseListTextView() textView.isRichText = false textView.textStorage?.setAttributedString(ProsePresenter.attribute(text)) return textView } @Test func `fold hides scope without touching buffer`() throws { let text = "- parent\n - child one\n - child two\n- next\n" let textView = makeView(text) let parent = try #require(ProseList.parents(in: text).first) textView.toggleFold(parent) #expect(textView.string == text) // folding never edits the buffer #expect(textView.folds.count == 1) // hidden scope carries the folded attributes let hiddenFont = try #require( textView.textStorage?.attribute(.font, at: parent.foldScope.location, effectiveRange: nil) as? NSFont, ) #expect(hiddenFont.pointSize < 1) // the parent line itself stays body-size let bodyFont = try #require(textView.textStorage?.attribute(.font, at: 0, effectiveRange: nil) as? NSFont) #expect(bodyFont.pointSize > 10) // unfold restores body presentation let parentAgain = try #require(ProseList.parents(in: textView.string).first) textView.toggleFold(parentAgain) let restored = try #require( textView.textStorage?.attribute(.font, at: parent.foldScope.location, effectiveRange: nil) as? NSFont, ) #expect(restored.pointSize > 10) #expect(textView.folds.isEmpty) } @Test func `folds rekey after edit above`() throws { let text = "- parent\n - child\n" let textView = makeView(text) let parent = try #require(ProseList.parents(in: text).first) textView.toggleFold(parent) // an edit above shifts every line down by 3 ("x\n\n" prefix) textView.textStorage?.replaceCharacters(in: NSRange(location: 0, length: 0), with: "x\n\n") textView.refreshFoldRendering() #expect(textView.folds == [ ProseListTextView.Fold( key: parent.markerLine.location + 3, lineText: "- parent", scope: NSRange(location: parent.foldScope.location + 3, length: parent.foldScope.length), ), ]) let font = try #require(textView.textStorage?.attribute(.font, at: 13, effectiveRange: nil) as? NSFont) #expect(font.pointSize < 1) // the shifted child stays hidden } @Test func `fold dies when marker text vanishes`() throws { let text = "- parent\n - child\n" let textView = makeView(text) let parent = try #require(ProseList.parents(in: text).first) textView.toggleFold(parent) textView.textStorage?.replaceCharacters(in: NSRange(location: 0, length: 8), with: "# head\n") textView.refreshFoldRendering() #expect(textView.folds.isEmpty) } }