Files
museion/MuseionTests/OutlinePresenterTests.swift
T
AlexKucera c23ee0f62f
CI / ci (push) Canceled after 0s
feat(outline): outline type + outliner — S3 (#109)
Dedicated block surface (ADR-0013, #105 variant B): OutlineTree
parses/serializes plain nested-bullet markdown leniently and
normalizes on save; the outliner promotes one NSTextView over the
focused row with LogSeq-style keys (Tab/⇧Tab, ⌥↑/⌥↓ subtree move,
↑/↓ hop, ⇧↑/⇧↓ block selection, ⌘↑/⌥↓ fold, Enter split), folds
session-scoped by runtime node UUID. New Outline ⇧⌘N stages a
type: outline draft through the existing autosave/conflict
machinery; reading renders the serialized markdown unchanged.

Outline rows use the body face at its natural line box — the prose
CSS-1.8 lineHeightMultiple grew the focused row and detached the
bullet (test-pinned). Finish-audit fixes included: paste-multiline
sanitizing at setText, dead Enter disjunct, dead Row.path.

Closes #109
2026-10-15 13:09:31 +02:00

174 lines
8.0 KiB
Swift

//
// OutlinePresenterTests.swift
// MuseionTests
//
// #109: the outliner surface's pure layer — visible-row projection
// (preorder, fold skipping, hidden-descendant counts), id→path lookup
// through deep trees, and setText mutation by node identity.
//
import Foundation
@testable import Museion
import Testing
struct OutlinePresenterTests {
private func node(_ text: String, _ children: [OutlineNode] = []) -> OutlineNode {
OutlineNode(text: text, children: children)
}
@Test func `visibleRows walks preorder with depths paths and child counts`() {
let tree = [node("a", [node("a1", [node("a1x")]), node("a2")]), node("b")]
let rows = OutlinePresenter.visibleRows(tree, folded: [])
#expect(rows.map(\.text) == ["a", "a1", "a1x", "a2", "b"])
#expect(rows.map(\.depth) == [0, 1, 2, 1, 0])
#expect(rows.map(\.childCount) == [2, 1, 0, 0, 0])
#expect(rows.allSatisfy { !$0.isFolded && $0.foldedDescendantCount == 0 })
// ids are the nodes' own — fold/view state keys on them
#expect(rows.map(\.id) == [
tree[0].id,
tree[0].children[0].id,
tree[0].children[0].children[0].id,
tree[0].children[1].id,
tree[1].id,
])
}
@Test func `visibleRows skips folded subtrees and counts hidden descendants`() {
let tree = [node("a", [node("a1", [node("a1x")]), node("a2")]), node("b")]
// fold "a": a1, a1x, a2 (3 nodes) hide behind the fold
var rows = OutlinePresenter.visibleRows(tree, folded: [tree[0].id])
#expect(rows.map(\.text) == ["a", "b"])
#expect(rows[0].isFolded)
#expect(rows[0].foldedDescendantCount == 3)
#expect(rows[0].childCount == 2) // structure unchanged — folds are view state
#expect(!rows[1].isFolded)
// fold the inner "a1" only: just a1x hides
rows = OutlinePresenter.visibleRows(tree, folded: [tree[0].children[0].id])
#expect(rows.map(\.text) == ["a", "a1", "a2", "b"])
#expect(rows[1].foldedDescendantCount == 1)
// nested folds: the outer fold already hides the inner head
rows = OutlinePresenter.visibleRows(tree, folded: [tree[0].id, tree[0].children[0].id])
#expect(rows.map(\.text) == ["a", "b"])
#expect(rows[0].foldedDescendantCount == 3)
}
@Test func `path resolves by id through deep trees and node at inverts it`() {
let parsed = OutlineTree.parse("- a\n - a1\n - a1x\n- b")
let deep = parsed.nodes[0].children[0].children[0]
#expect(OutlinePresenter.path(of: deep.id, in: parsed.nodes) == [0, 0, 0])
#expect(OutlinePresenter.path(of: parsed.nodes[1].id, in: parsed.nodes) == [1])
#expect(OutlinePresenter.path(of: parsed.nodes[0].id, in: []) == nil)
#expect(OutlinePresenter.path(of: UUID(), in: parsed.nodes) == nil)
// node(at:) is the inverse — focus routing after structural ops
#expect(OutlinePresenter.node(at: [0, 0, 0], in: parsed.nodes)?.id == deep.id)
#expect(OutlinePresenter.node(at: [0, 1], in: parsed.nodes) == nil)
#expect(OutlinePresenter.node(at: [2], in: parsed.nodes) == nil)
}
@Test func `setText mutates the node with the id`() {
var parsed = OutlineTree.parse("- a\n - a1")
let child = parsed.nodes[0].children[0]
#expect(OutlinePresenter.setText("a1 edited", id: child.id, in: &parsed.nodes))
#expect(parsed.nodes[0].children[0].text == "a1 edited")
#expect(parsed.nodes[0].text == "a")
#expect(!OutlinePresenter.setText("ghost", id: UUID(), in: &parsed.nodes))
#expect(OutlinePresenter.setText("", id: child.id, in: &parsed.nodes))
#expect(parsed.nodes[0].children[0].text.isEmpty)
}
@Test func `setText flattens pasted newlines - round-trip can't drop content`() {
// HIGH-1 pin: paste "l1\nl2\nl3" into a row → the node stays
// single-line (space-joined), so serialize→parse keeps every word.
var nodes = [OutlineNode(text: "row")]
#expect(OutlinePresenter.setText("l1\nl2\r\nl3", id: nodes[0].id, in: &nodes))
#expect(nodes[0].text == "l1 l2 l3")
#expect(!nodes[0].text.contains("\n") && !nodes[0].text.contains("\r"))
// the loss this prevents: an embedded \n serializes as one item +
// stray prose lines, and the NEXT parse drops the strays
let bad = OutlineTree.parse(OutlineTree.serialize(preamble: [], nodes: [OutlineNode(text: "l1\nl2")]))
#expect(bad.nodes.count == 1)
#expect(bad.nodes[0].text == "l1")
}
@Test func `rangeIDs returns the inclusive visible span in order`() {
let tree = [node("a"), node("b"), node("c"), node("d")]
// forward and backward anchors both give visible order
#expect(OutlinePresenter.rangeIDs(from: tree[0].id, to: tree[2].id, in: tree, folded: []) ==
[tree[0].id, tree[1].id, tree[2].id])
#expect(OutlinePresenter.rangeIDs(from: tree[2].id, to: tree[0].id, in: tree, folded: []) ==
[tree[0].id, tree[1].id, tree[2].id])
#expect(OutlinePresenter.rangeIDs(from: tree[1].id, to: tree[1].id, in: tree, folded: []) == [tree[1].id])
// an id hidden by a fold is invisible → nil
let nested = [node("a", [node("a1")]), node("b")]
#expect(OutlinePresenter.rangeIDs(from: nested[0].id, to: nested[0].children[0].id, in: nested, folded: [nested[0].id]) == nil)
#expect(OutlinePresenter.rangeIDs(from: nested[0].id, to: nested[1].id, in: nested, folded: []) != nil)
}
@Test func `batch structural ops compose in the right order`() {
// moveUp: run as a unit, forward composes (run excludes the first
// row — a boundary leader no-ops the whole batch)
var upNodes = OutlineTree.parse("- a\n- b\n- c\n- d").nodes
let upIds = [upNodes[1].id, upNodes[2].id, upNodes[3].id]
#expect(OutlinePresenter.applyBatch(.moveUp, ids: upIds, to: &upNodes))
#expect(upNodes.map(\.text) == ["b", "c", "d", "a"])
// moveDown: run as a unit, reversed composes (run excludes the
// last row — a boundary leader no-ops the whole batch)
var downNodes = OutlineTree.parse("- a\n- b\n- c\n- d").nodes
let downIds = [downNodes[0].id, downNodes[1].id, downNodes[2].id]
#expect(OutlinePresenter.applyBatch(.moveDown, ids: downIds, to: &downNodes))
#expect(downNodes.map(\.text) == ["d", "a", "b", "c"])
// run touching the bottom boundary → no-op, never scatters
var bottomNodes = OutlineTree.parse("- a\n- b\n- c\n- d").nodes
let bottomIds = [bottomNodes[1].id, bottomNodes[2].id, bottomNodes[3].id]
#expect(!OutlinePresenter.applyBatch(.moveDown, ids: bottomIds, to: &bottomNodes))
#expect(bottomNodes.map(\.text) == ["a", "b", "c", "d"])
// boundary leader → whole batch no-ops (run must not scatter)
var boundaryNodes = OutlineTree.parse("- a\n- b\n- c").nodes
let boundaryIds = [boundaryNodes[0].id, boundaryNodes[1].id]
#expect(!OutlinePresenter.applyBatch(.moveUp, ids: boundaryIds, to: &boundaryNodes))
#expect(boundaryNodes.map(\.text) == ["a", "b", "c"])
// batch moves are sibling-only: a run whose members sit at
// different depths never crosses parents (regression pin —
// parent-crossing move tore such runs apart)
var mixed = OutlineTree.parse("- a\n- b\n - b1\n - b2\n- c").nodes
let mixedIds = [
mixed[1].children[0].id, // b1 (depth 1)
mixed[1].children[0].children[0].id, // b2 (depth 2)
mixed[2].id, // c (depth 0)
]
#expect(!OutlinePresenter.applyBatch(.moveUp, ids: mixedIds, to: &mixed))
#expect(mixed.map(\.text) == ["a", "b", "c"])
#expect(mixed[1].children.map(\.text) == ["b1"])
#expect(mixed[1].children[0].children.map(\.text) == ["b2"])
// indent: per-node, run keeps its internal order (both land
// under a, in order — the earlier move doesn't strand the later)
var inNodes = OutlineTree.parse("- a\n- b\n- c").nodes
let inIds = [inNodes[1].id, inNodes[2].id]
#expect(OutlinePresenter.applyBatch(.indent, ids: inIds, to: &inNodes))
#expect(inNodes.map(\.text) == ["a"])
#expect(inNodes[0].children.map(\.text) == ["b", "c"])
// outdent: reversed composes (bottom→top un-nests the run)
var outNodes = OutlineTree.parse("- a\n - a1\n - a2\n - a3").nodes
let outIds = [outNodes[0].children[0].id, outNodes[0].children[1].id, outNodes[0].children[2].id]
#expect(OutlinePresenter.applyBatch(.outdent, ids: outIds, to: &outNodes))
#expect(outNodes.map(\.text) == ["a", "a1", "a2", "a3"])
#expect(outNodes[1...].allSatisfy { node in node.children.isEmpty })
}
}