Files
museion/MuseionTests/OutlinePresenterTests.swift
T
AlexKucera 21e2f27450
CI / ci (push) Canceled after 0s
feat(outline): drag-reorder + zoom — S6 (#112)
Pointer drag of a block run into any sibling seam or onto a block to
nest as its child (wf113 variant A: fat-gap 30/40/30 bands, seam
line at the future bullet column); the block selection rides the
drag, the moved run becomes the selection, and Esc cancels mid-gesture.
Double-click any bullet zooms to its children with breadcrumbs back
out. New OutlineTree extract/insertRun splice subtrees id-preserving;
the live row's NSTextView moved to OutlineRowTextView.

Audit round: the gesture is self-contained from arm time (strong
window + callback captures — a mid-drag scroll that drops the origin
row cannot strand drag state), a second arming cancels a stuck
predecessor, rowFrames prefers the grip frame, hitTestDrop is y-up
only, and the rails read DropGeometry. Pinned in OutlineDragTests
(monitors fire on sendEvent with a real window number).

[Generated by AI]
2026-10-16 14:49:57 +02:00

371 lines
17 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// 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 CoreGraphics
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 })
}
// #112 (S6): drag-reorder + zoom over the presenter.
@Test func `draggedIDs rides the selection deduped or goes solo`() {
let tree = [node("a", [node("a1", [node("a1x")]), node("a2")]), node("b"), node("c")]
// grab a selected row → the whole selection rides, preorder order,
// a selected ancestor carries its selected descendants
let sel = [tree[0].id, tree[0].children[0].id, tree[2].id]
#expect(
OutlinePresenter.draggedIDs(grabbing: tree[0].id, selected: sel, in: tree)
== [tree[0].id, tree[2].id],
)
// grab an UNselected row while a selection exists → solo
#expect(
OutlinePresenter.draggedIDs(grabbing: tree[1].id, selected: sel, in: tree)
== [tree[1].id],
)
// no selection at all → solo, works at any depth
#expect(
OutlinePresenter.draggedIDs(grabbing: tree[0].children[1].id, selected: nil, in: tree)
== [tree[0].children[1].id],
)
}
@Test func `isValidDrop rejects refs at or inside the moving run`() {
let tree = [node("a", [node("a1", [node("a1x")]), node("a2")]), node("b")]
// the ref itself moves
#expect(!OutlinePresenter.isValidDrop(moving: [tree[1].id], target: .before(tree[1].id), in: tree))
// the ref sits inside a moving subtree (any depth)
#expect(!OutlinePresenter.isValidDrop(moving: [tree[0].id], target: .lastChild(tree[0].children[0].id), in: tree))
#expect(!OutlinePresenter.isValidDrop(
moving: [tree[0].children[0].id],
target: .before(tree[0].children[0].children[0].id),
in: tree,
))
// dropping back into one's own PARENT as its last child is legal —
// extraction happens first, the target never ends up inside the run
#expect(OutlinePresenter.isValidDrop(moving: [tree[0].children[0].id], target: .lastChild(tree[0].id), in: tree))
// an unrelated row is fine
#expect(OutlinePresenter.isValidDrop(moving: [tree[0].id], target: .before(tree[1].id), in: tree))
// unknown ref → invalid
#expect(!OutlinePresenter.isValidDrop(moving: [tree[0].id], target: .before(UUID()), in: tree))
}
@Test func `applyDrop lands before afterLast and lastChild precisely`() {
// before: b moves up, directly before a
var beforeNodes = OutlineTree.parse("- a\n- b\n- c").nodes
#expect(OutlinePresenter.applyDrop(.before(beforeNodes[0].id), moving: [beforeNodes[1].id], in: &beforeNodes))
#expect(beforeNodes.map(\.text) == ["b", "a", "c"])
// afterLast: the end-of-list seam — lands after the LAST row at
// its depth, subtree pulled whole
var afterNodes = OutlineTree.parse("- a\n- b\n - b1\n- c").nodes
let movingID = afterNodes[0].id
#expect(OutlinePresenter.applyDrop(.afterLast(afterNodes[2].id), moving: [movingID], in: &afterNodes))
#expect(afterNodes.map(\.text) == ["b", "c", "a"])
// lastChild: onto — appended AFTER existing children (never first)
var ontoNodes = OutlineTree.parse("- a\n - a1\n- b").nodes
let bID = ontoNodes[1].id
#expect(OutlinePresenter.applyDrop(.lastChild(ontoNodes[0].id), moving: [bID], in: &ontoNodes))
#expect(ontoNodes.map(\.text) == ["a"])
#expect(ontoNodes[0].children.map(\.text) == ["a1", "b"])
// a seam below a deeper follower nests: dropping before a's first
// child (the seam between a and a1) makes it the FIRST child
var nestNodes = OutlineTree.parse("- a\n - a1\n- b").nodes
let a1ID = nestNodes[0].children[0].id
#expect(OutlinePresenter.applyDrop(.before(a1ID), moving: [nestNodes[1].id], in: &nestNodes))
#expect(nestNodes.map(\.text) == ["a"])
#expect(nestNodes[0].children.map(\.text) == ["b", "a1"])
}
@Test func `applyDrop moves a deduped batch as one contiguous run`() {
// a (with a1) and c selected together → they land adjacent, a
// before c, a's subtree riding inside
var nodes = OutlineTree.parse("- a\n - a1\n- b\n- c").nodes
let aID = nodes[0].id
let cID = nodes[2].id
#expect(OutlinePresenter.applyDrop(.afterLast(nodes[1].id), moving: [aID, cID], in: &nodes))
#expect(nodes.map(\.text) == ["b", "a", "c"])
#expect(nodes[1].children.map(\.text) == ["a1"])
// ids travel — focus/selection can follow the run
#expect(nodes[1].id == aID && nodes[2].id == cID)
}
@Test func `applyDrop no-ops a drop back at origin and refuses invalid targets`() {
// b is ALREADY immediately before c — extract + insert-before-c
// reproduces the tree → no-op, storage untouched
var nodes = OutlineTree.parse("- a\n- b\n- c").nodes
let snapshot = nodes
#expect(!OutlinePresenter.applyDrop(.before(nodes[2].id), moving: [nodes[1].id], in: &nodes))
#expect(nodes == snapshot) // storage untouched
// onto own subtree — invalid, dashed-indicator case
var selfNodes = OutlineTree.parse("- a\n - a1").nodes
let selfSnapshot = selfNodes
#expect(!OutlinePresenter.applyDrop(.lastChild(selfNodes[0].children[0].id), moving: [selfNodes[0].id], in: &selfNodes))
#expect(selfNodes == selfSnapshot)
// dropping onto one's own parent when ALREADY its last child → no-op
var tailNodes = OutlineTree.parse("- a\n - a1\n - a2").nodes
let tailSnapshot = tailNodes
#expect(!OutlinePresenter.applyDrop(.lastChild(tailNodes[0].id), moving: [tailNodes[0].children[1].id], in: &tailNodes))
#expect(tailNodes == tailSnapshot)
}
@Test func `applyDrop crosses parents through a drag`() {
// keyboard batch moves stay sibling-only; a drag is atomic
// extract+splice, so a1 can leave a and land among the roots
var nodes = OutlineTree.parse("- a\n - a1\n- b").nodes
let a1ID = nodes[0].children[0].id
#expect(OutlinePresenter.applyDrop(.before(nodes[1].id), moving: [a1ID], in: &nodes))
#expect(nodes.map(\.text) == ["a", "a1", "b"])
#expect(nodes[0].children.isEmpty)
}
@Test func `visibleRows zoom roots at the node with depths rebased`() {
let tree = [node("a", [node("a1", [node("a1x")]), node("a2")]), node("b")]
let rows = OutlinePresenter.visibleRows(tree, folded: [], zoomRootID: tree[0].id)
#expect(rows.map(\.text) == ["a1", "a1x", "a2"])
#expect(rows.map(\.depth) == [0, 1, 0]) // depth 2 re-based to 0
// the zoom root's own fold is ignored while it IS the root —
// its children are the document now
let folded = OutlinePresenter.visibleRows(tree, folded: [tree[0].id], zoomRootID: tree[0].id)
#expect(folded.map(\.text) == ["a1", "a1x", "a2"])
// folding INSIDE the zoom still hides
let inner = OutlinePresenter.visibleRows(tree, folded: [tree[0].children[0].id], zoomRootID: tree[0].id)
#expect(inner.map(\.text) == ["a1", "a2"])
#expect(inner[0].foldedDescendantCount == 1)
// unknown zoom id → the whole document
#expect(
OutlinePresenter.visibleRows(tree, folded: [], zoomRootID: UUID()).map(\.text)
== ["a", "a1", "a1x", "a2", "b"],
)
}
@Test func `rangeIDs under zoom is nil across the zoom boundary`() {
let tree = [node("a", [node("a1")]), node("b")]
// a is outside the zoom rooted at a — invisible → nil
#expect(OutlinePresenter.rangeIDs(
from: tree[0].id,
to: tree[1].id,
in: tree,
folded: [],
zoomRootID: tree[0].id,
) == nil)
// both ends inside → the span still works
#expect(OutlinePresenter.rangeIDs(
from: tree[0].children[0].id,
to: tree[0].children[0].id,
in: tree,
folded: [],
zoomRootID: tree[0].id,
) == [tree[0].children[0].id])
}
@Test func `hitTestDrop follows the fat-gap bands`() {
// window-space y-up frames (the only orientation both callers
// feed): a highest at y 60–90, c lowest at 0–30
let rowA = OutlineNode(text: "a")
let rowB = OutlineNode(text: "b")
let rowC = OutlineNode(text: "c")
let rows = OutlinePresenter.visibleRows([rowA, rowB, rowC], folded: [])
let frames: [UUID: CGRect] = [
rowA.id: CGRect(x: 0, y: 60, width: 400, height: 30),
rowB.id: CGRect(x: 0, y: 30, width: 400, height: 30),
rowC.id: CGRect(x: 0, y: 0, width: 400, height: 30),
]
let pairs = rows.compactMap { row in frames[row.id].map { (row, $0) } }
// pointer visually above a (y 95) → before a
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 95), rows: pairs) == .before(rowA.id))
// top 30% of b (y 55, rel 1/6 from the top) → the seam above b
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 55), rows: pairs) == .before(rowB.id))
// middle 40% of b (y 45) → onto (last child, ring)
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 45), rows: pairs) == .lastChild(rowB.id))
// bottom 30% of b (y 35) → the seam below → before the FOLLOWING row
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 35), rows: pairs) == .before(rowC.id))
// below c (y -10) → after c (end-of-list seam)
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: -10), rows: pairs) == .afterLast(rowC.id))
// rows without frames are simply unreachable
#expect(OutlinePresenter.hitTestDrop(point: CGPoint(x: 50, y: 45), rows: []) == nil)
}
}