Files
museion/MuseionTests/OutlineTreeTests.swift
T
AlexKucera 0b172f73ef
CI / ci (push) Canceled after 0s
feat(outline): mixed-content open guard — S7 (#111)
Opening a type: outline piece whose body parses with dropped prose
(paragraph lines between bullets) now asks before the outliner mounts:
Open in Prose (default, nothing drops), Open as Outline (explicit
consent to save normalization), or Cancel. The detector shares the
parser's item definition, the ask presents over an empty editor so
nothing can normalize on save before the choice, and the prose answer
rides a routing override that dies with the next open.

Audit round pinned the lifecycle: reselect of the open piece is a
no-op (an armed debounce must never flush mid-ask), and a draft or
guest staged during the ask displaces it without being destroyed.

Closes #111

[Generated by AI]
2026-10-15 18:31:26 +02:00

274 lines
11 KiB
Swift

//
// OutlineTreeTests.swift
// MuseionTests
//
// #109: the Outline tree model — lenient parse (markers, indents, clamping,
// preamble), canonical-serialize round-trips, and the structural ops
// (indent/outdent/move/enter) as whole-subtree rearrangements with stable
// node ids.
//
@testable import Museion
import Testing
struct OutlineTreeTests {
@Test func `canonical round trip flat and nested`() {
let flat = "- a\n- b"
let parsedFlat = OutlineTree.parse(flat)
#expect(parsedFlat.preamble.isEmpty)
#expect(parsedFlat.nodes.map(\.text) == ["a", "b"])
#expect(parsedFlat.nodes.map(\.children.count) == [0, 0])
#expect(OutlineTree.serialize(preamble: parsedFlat.preamble, nodes: parsedFlat.nodes) == flat)
let nested = "- a\n - a1\n - a1x\n- b"
let parsed = OutlineTree.parse(nested)
#expect(parsed.preamble.isEmpty)
#expect(parsed.nodes.count == 2)
#expect(parsed.nodes[0].text == "a")
#expect(parsed.nodes[0].children.map(\.text) == ["a1"])
#expect(parsed.nodes[0].children[0].children.map(\.text) == ["a1x"])
#expect(parsed.nodes[1].text == "b")
#expect(OutlineTree.serialize(preamble: parsed.preamble, nodes: parsed.nodes) == nested)
}
@Test func `lenient markers indents and depth clamping`() {
// `*` markers parse; canonical `- ` comes back out.
let star = OutlineTree.parse("* a\n* b")
#expect(star.nodes.map(\.text) == ["a", "b"])
#expect(OutlineTree.serialize(preamble: [], nodes: star.nodes) == "- a\n- b")
// 4-space file: floor(4/2) = 2 would orphan — clamp makes children.
let four = OutlineTree.parse("- a\n - a1\n - a2")
#expect(four.nodes.count == 1)
#expect(four.nodes[0].text == "a")
#expect(four.nodes[0].children.map(\.text) == ["a1", "a2"])
// 8-space jump clamps to prevDepth + 1, not +4.
let jump = OutlineTree.parse("- a\n - a1\n - deep")
#expect(jump.nodes[0].children[0].children.map(\.text) == ["deep"])
#expect(OutlineTree.serialize(preamble: [], nodes: jump.nodes) == "- a\n - a1\n - deep")
// Tab counts as 2 spaces (single tab = depth 1, mixed tab+space too).
let tabbed = OutlineTree.parse("- a\n\t- a1\n\t - a1x")
#expect(tabbed.nodes[0].children.map(\.text) == ["a1"])
#expect(tabbed.nodes[0].children[0].children.map(\.text) == ["a1x"])
}
@Test func `preamble kept verbatim and post item prose dropped`() {
let titled = OutlineTree.parse("# Title\n\n- a\n - a1")
#expect(titled.preamble == ["# Title", ""])
#expect(titled.nodes.map(\.text) == ["a"])
#expect(titled.nodes[0].children.map(\.text) == ["a1"])
#expect(OutlineTree.serialize(preamble: titled.preamble, nodes: titled.nodes) == "# Title\n\n- a\n - a1")
// Non-item lines AFTER the first item are dropped (normalize on save).
let stray = OutlineTree.parse("- a\nprose\n- b")
#expect(stray.preamble.isEmpty)
#expect(stray.nodes.map(\.text) == ["a", "b"])
#expect(OutlineTree.serialize(preamble: stray.preamble, nodes: stray.nodes) == "- a\n- b")
}
@Test func `round trip is idempotent and empty parses to empty`() {
#expect(OutlineTree.parse("") == ([], []))
#expect(OutlineTree.serialize(preamble: [], nodes: []) == "")
for source in ["- a\n- b", "- a\n - a1\n - a1x\n- b", "# T\n\n- a\n - a1", "* a\n* b"] {
let first = OutlineTree.parse(source)
let canon = OutlineTree.serialize(preamble: first.preamble, nodes: first.nodes)
let second = OutlineTree.parse(canon)
#expect(OutlineTree.serialize(preamble: second.preamble, nodes: second.nodes) == canon)
#expect(second.nodes.map(\.text) == first.nodes.map(\.text))
}
}
@Test func `indent nests node under previous sibling`() {
var nodes = [
OutlineNode(text: "a", children: [OutlineNode(text: "a1")]),
OutlineNode(text: "b"),
]
let bID = nodes[1].id
let newPath = OutlineTree.indent(&nodes, path: [1])
#expect(newPath == [0, 1]) // after a's existing child
#expect(nodes.count == 1)
#expect(nodes[0].children.map(\.text) == ["a1", "b"])
#expect(nodes[0].children[1].id == bID) // id travels
// First sibling has no previous → no-op.
#expect(OutlineTree.indent(&nodes, path: [0]) == nil)
#expect(nodes[0].children.map(\.text) == ["a1", "b"]) // untouched
// Children travel with the moved node.
var deep = [
OutlineNode(text: "a"),
OutlineNode(text: "b", children: [OutlineNode(text: "b1"), OutlineNode(text: "b2")]),
OutlineNode(text: "c"),
]
let b1ID = deep[1].children[0].id
let indented = OutlineTree.indent(&deep, path: [1])
#expect(indented == [0, 0])
#expect(deep.count == 2)
#expect(deep[0].children.map(\.text) == ["b"])
#expect(deep[0].children[0].children.map(\.text) == ["b1", "b2"])
#expect(deep[0].children[0].children[0].id == b1ID)
}
@Test func `outdent lands after parent with subtree intact`() {
var nodes = [
OutlineNode(text: "a", children: [OutlineNode(text: "a1", children: [OutlineNode(text: "a1x")])]),
OutlineNode(text: "b"),
]
let a1ID = nodes[0].children[0].id
// Root → no-op.
#expect(OutlineTree.outdent(&nodes, path: [0]) == nil)
#expect(nodes.count == 2)
let newPath = OutlineTree.outdent(&nodes, path: [0, 0])
#expect(newPath == [1]) // right after its parent
#expect(nodes.map(\.text) == ["a", "a1", "b"])
#expect(nodes[1].id == a1ID)
#expect(nodes[1].children.map(\.text) == ["a1x"]) // deep subtree travels
#expect(OutlineTree.serialize(preamble: [], nodes: nodes) == "- a\n- a1\n - a1x\n- b")
}
@Test func `move swaps with sibling as a unit`() {
var nodes = [
OutlineNode(text: "a"),
OutlineNode(text: "b", children: [OutlineNode(text: "b1")]),
OutlineNode(text: "c"),
]
let bID = nodes[1].id
let b1ID = nodes[1].children[0].id
#expect(OutlineTree.move(&nodes, path: [0], up: true) == nil) // boundary
#expect(OutlineTree.move(&nodes, path: [2], up: false) == nil) // boundary
let down = OutlineTree.move(&nodes, path: [1], up: false)
#expect(down == [2])
#expect(nodes.map(\.text) == ["a", "c", "b"])
#expect(nodes[2].children.map(\.text) == ["b1"]) // subtree travels
#expect(nodes[2].id == bID && nodes[2].children[0].id == b1ID)
let movedUp = OutlineTree.move(&nodes, path: [2], up: true)
#expect(movedUp == [1])
#expect(nodes.map(\.text) == ["a", "b", "c"])
}
@Test func `move crosses its parent at a sibling boundary`() {
// test / second / third-level nest — the user's exact case:
// ⌥↑ on "third level" (first child, no previous sibling) lifts it
// OUT to sit directly before its parent.
var nodes = [
OutlineNode(text: "test"),
OutlineNode(text: "second level", children: [OutlineNode(text: "third level")]),
OutlineNode(text: "after"),
]
let thirdID = nodes[1].children[0].id
#expect(OutlineTree.move(&nodes, path: [1, 0], up: true) == [1])
#expect(nodes.map(\.text) == ["test", "third level", "second level", "after"])
#expect(nodes[1].id == thirdID)
// down across the boundary: LAST child lands directly after parent
var last = [
OutlineNode(text: "a"),
OutlineNode(text: "parent", children: [OutlineNode(text: "kid")]),
]
#expect(OutlineTree.move(&last, path: [1, 0], up: false) == [2])
#expect(last.map(\.text) == ["a", "parent", "kid"])
// down with an uncle below: lands between parent and uncle —
// one visible row per press, never skips
var uncle = [
OutlineNode(text: "a"),
OutlineNode(text: "parent", children: [OutlineNode(text: "kid")]),
OutlineNode(text: "uncle", children: [OutlineNode(text: "nephew")]),
]
#expect(OutlineTree.move(&uncle, path: [1, 0], up: false) == [2])
#expect(uncle.map(\.text) == ["a", "parent", "kid", "uncle"])
#expect(uncle[3].children.map(\.text) == ["nephew"])
// deeper nesting: crossing from depth 2 keeps the remaining path
var deep = [
OutlineNode(text: "root", children: [
OutlineNode(text: "mid", children: [OutlineNode(text: "leaf")]),
]),
]
#expect(OutlineTree.move(&deep, path: [0, 0, 0], up: true) == [0, 0])
#expect(deep[0].children.map(\.text) == ["leaf", "mid"])
}
@Test func `enter splits at caret`() {
// Split mid-text: whitespace at the split boundary rides the tail's
// head off (pinned example: "hello world" @5 → "hello" + "world").
var nodes = [OutlineNode(text: "hello world")]
let result = OutlineTree.enter(&nodes, path: [0], caretOffset: 5)
#expect(result == .newNode(path: [1], caret: 0))
#expect(nodes.map(\.text) == ["hello", "world"])
// Caret at end → empty new sibling, focused at 0.
var endSplit = [OutlineNode(text: "done")]
let endResult = OutlineTree.enter(&endSplit, path: [0], caretOffset: 4)
#expect(endResult == .newNode(path: [1], caret: 0))
#expect(endSplit.map(\.text) == ["done", ""])
// Offset clamps both ways (negative / beyond length).
var over = [OutlineNode(text: "abc")]
_ = OutlineTree.enter(&over, path: [0], caretOffset: 99)
#expect(over.map(\.text) == ["abc", ""])
// Split inside a nested run: new sibling lands right below, in place.
var nested = [
OutlineNode(text: "a", children: [
OutlineNode(text: "one two"),
OutlineNode(text: "tail"),
]),
OutlineNode(text: "b"),
]
let nestedResult = OutlineTree.enter(&nested, path: [0, 0], caretOffset: 3)
#expect(nestedResult == .newNode(path: [0, 1], caret: 0))
#expect(nested[0].children.map(\.text) == ["one", "two", "tail"])
}
@Test func `enter on empty node outdents or siblings`() {
// Empty node at depth > 0 → outdented, right after its parent.
var nested = [
OutlineNode(text: "a", children: [OutlineNode(text: "")]),
OutlineNode(text: "b"),
]
let out = OutlineTree.enter(&nested, path: [0, 0], caretOffset: 0)
#expect(out == .outdented(path: [1]))
#expect(nested.map(\.text) == ["a", "", "b"])
// Whitespace-only counts as empty.
var spaced = [
OutlineNode(text: "a", children: [OutlineNode(text: " ")]),
]
let spacedOut = OutlineTree.enter(&spaced, path: [0, 0], caretOffset: 0)
#expect(spacedOut == .outdented(path: [1]))
#expect(spaced.map(\.text) == ["a", " "])
// Empty node at root → new empty sibling below, focused.
var rooted = [OutlineNode(text: "a"), OutlineNode(text: "")]
let rootResult = OutlineTree.enter(&rooted, path: [1], caretOffset: 0)
#expect(rootResult == .newNode(path: [2], caret: 0))
#expect(rooted.map(\.text) == ["a", "", ""])
}
// #111 (S7): the mixed-content detector behind the open guard.
@Test func `parsesWithLoss flags prose between bullets and stays quiet otherwise`() {
// The exact hazard: prose after the first item would drop on save.
#expect(OutlineTree.parsesWithLoss("- one\nan stray paragraph\n- two"))
// Deep prose counts anywhere after the first item.
#expect(OutlineTree.parsesWithLoss("- a\n - b\n prose under b"))
// Clean canonical outline — no loss.
#expect(!OutlineTree.parsesWithLoss("- one\n- two\n - nested"))
// Preamble is verbatim by design — never a loss.
#expect(!OutlineTree.parsesWithLoss("# Title\n\n- one\n- two"))
// Blank lines between items are formatting, not prose.
#expect(!OutlineTree.parsesWithLoss("- one\n\n- two\n"))
#expect(!OutlineTree.parsesWithLoss(""))
}
}