CI / ci (push) Canceled after 0s
In-place outline→post conversion on the piece: id, folder, Pass State, origin kept, type frontmatter flips to post, body transforms once. The ask picks the transform — Verbatim Bullets (default, lossless) or Headings ladder (roots ##, stepping to the ###### floor, deeper stays bullets), with a warning when the ladder would drop paragraph lines between bullets. The open session flushes first, then reopens routed to prose via proseOverridePostID (lastDraftWasOutline's machinery reversed). An outline that went lossy on disk behind an unwarned ask aborts the convert; an unanswered #67 conflict aborts every flush-first op (shared flushOrAbort seam); rescan no longer hands a same-id reopened session its predecessor's stale disk text; a held #111 guard ask dies with the outline it pointed at. Audit round (finish-audit, three reviewers) included: 13 new pins, 206 tests green, pin discrimination verified. Record: docs/devlog/20261016-issue114-convert-to-post_log.md
327 lines
13 KiB
Swift
327 lines
13 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(""))
|
|
}
|
|
|
|
// #112 (S6): the drag primitives — extract + insertRun.
|
|
|
|
@Test func `extract removes and returns the whole subtree by path`() {
|
|
var nodes = OutlineTree.parse("- a\n - a1\n- b").nodes
|
|
let extracted = OutlineTree.extract(&nodes, path: [0])
|
|
#expect(extracted?.text == "a")
|
|
#expect(extracted?.children.map(\.text) == ["a1"])
|
|
#expect(nodes.map(\.text) == ["b"])
|
|
// ids travel — the drag run re-inserts the same identities
|
|
#expect(extracted?.children[0].id != nil)
|
|
// unresolvable paths are nil no-ops
|
|
#expect(OutlineTree.extract(&nodes, path: [5]) == nil)
|
|
#expect(nodes.map(\.text) == ["b"])
|
|
}
|
|
|
|
@Test func `insertRun splices siblings at any parent and clamps the index`() {
|
|
var nodes = OutlineTree.parse("- a\n - a1\n- b").nodes
|
|
|
|
// into the root, before b
|
|
#expect(OutlineTree.insertRun([OutlineNode(text: "x")], parentPath: [], index: 1, in: &nodes))
|
|
#expect(nodes.map(\.text) == ["a", "x", "b"])
|
|
|
|
// a nested parent; index clamps to count (append)
|
|
#expect(OutlineTree.insertRun([OutlineNode(text: "a2")], parentPath: [0], index: 99, in: &nodes))
|
|
#expect(nodes[0].children.map(\.text) == ["a1", "a2"])
|
|
|
|
// a whole run lands contiguous, in order
|
|
let run = [OutlineNode(text: "r1"), OutlineNode(text: "r2")]
|
|
#expect(OutlineTree.insertRun(run, parentPath: [], index: 0, in: &nodes))
|
|
#expect(nodes.map(\.text) == ["r1", "r2", "a", "x", "b"])
|
|
|
|
// unresolvable parent → false, untouched; empty run refused
|
|
#expect(!OutlineTree.insertRun([OutlineNode(text: "z")], parentPath: [7], index: 0, in: &nodes))
|
|
#expect(!OutlineTree.insertRun([], parentPath: [], index: 0, in: &nodes))
|
|
#expect(nodes.map(\.text) == ["r1", "r2", "a", "x", "b"])
|
|
}
|
|
|
|
@Test func `headingsLadderBody maps levels to headings with a bullet floor`() {
|
|
let body = "# Title\n- a\n - a1\n - a2\n - a3\n - a4\n - a4x\n - deep"
|
|
// roots ##, one heading per level to the ###### floor, deeper
|
|
// levels stay bullets re-based flush under it; preamble verbatim.
|
|
#expect(
|
|
OutlineTree.headingsLadderBody(body)
|
|
== "# Title\n## a\n### a1\n#### a2\n##### a3\n###### a4\n- a4x\n - deep",
|
|
)
|
|
// mixed content: prose between bullets drops (the ask warns)
|
|
#expect(OutlineTree.headingsLadderBody("- a\nprose\n- b") == "## a\n## b")
|
|
// identity edges: empty in → empty out; prose-only body is all
|
|
// preamble → rides verbatim (nothing to ladder)
|
|
#expect(OutlineTree.headingsLadderBody("") == "")
|
|
#expect(OutlineTree.headingsLadderBody("# Just a title\nsome words") == "# Just a title\nsome words")
|
|
}
|
|
}
|