Files
museion/MuseionTests/OutlineDragTests.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

106 lines
4.1 KiB
Swift

//
// OutlineDragTests.swift
// MuseionTests
//
// #112 pins for the drag plumbing's lifetime + geometry contracts.
// The gesture must be self-contained from arm time on: the tracker
// holds its own copies of the callbacks (nothing reads the catcher's
// state at fire time), so a mid-drag scroll that drops the origin row
// out of the lazy stack cannot strand the surface's drag state.
// (The dealloc itself can't be reproduced in this harness — AppKit
// keeps extra internal retains on any once-installed view — so the
// pin swaps the callbacks AFTER arming: only an arm-time snapshot
// fires.) Also pinned: rowFrames reports the grip catcher's full-row
// frame (not the body catcher's text-only frame) regardless of
// subview order, and arming a second drag cancels a stuck
// predecessor instead of stacking trackers. Monitors fire on
// NSApp.sendEvent — but only for events carrying a real window
// number (window-0 events never reach monitors; probe-pinned).
//
import AppKit
@testable import Museion
import Testing
@MainActor
@Suite(.serialized)
struct OutlineDragTests {
private func makeWindow() -> NSWindow {
NSWindow(contentRect: NSRect(x: 0, y: 0, width: 300, height: 300),
styleMask: [.titled], backing: .buffered, defer: false)
}
private func mouseEvent(_ type: NSEvent.EventType, in window: NSWindow) -> NSEvent? {
NSEvent.mouseEvent(with: type, location: NSPoint(x: 20, y: 280), modifierFlags: [],
timestamp: 0, windowNumber: window.windowNumber, context: nil,
eventNumber: 0, clickCount: 1, pressure: 1)
}
@Test func `the tracker holds its own callbacks - a post-arm swap never fires`() throws {
let window = makeWindow()
var armTime = 0
var swappedIn = 0
let catcher = CatcherView()
catcher.kind = .grip // grip drags always arm — no selection dance
catcher.frame = NSRect(x: 0, y: 273, width: 300, height: 27)
catcher.onDragEnded = { _, _ in armTime += 1 }
window.contentView?.addSubview(catcher)
// arm (installs the tracker; its monitors retain it)
try catcher.mouseDown(with: #require(mouseEvent(.leftMouseDown, in: window)))
// the origin row dematerializes and SwiftUI re-sets the catcher's
// closures — a gesture that read live view state at fire time
// would now target the wrong (or gone) row
catcher.onDragEnded = { _, _ in swappedIn += 1 }
// mouse-up must end the gesture through the ARM-TIME callbacks
try NSApp.sendEvent(#require(mouseEvent(.leftMouseUp, in: window)))
#expect(armTime == 1)
#expect(swappedIn == 0)
}
@Test func `arming a second drag cancels a stuck predecessor`() throws {
let window = makeWindow()
var firstEnded = 0
let first = CatcherView()
first.kind = .grip
first.onDragEnded = { _, _ in firstEnded += 1 }
window.contentView?.addSubview(first)
try first.mouseDown(with: #require(mouseEvent(.leftMouseDown, in: window)))
let second = CatcherView()
second.kind = .grip
window.contentView?.addSubview(second)
try second.mouseDown(with: #require(mouseEvent(.leftMouseDown, in: window)))
// the predecessor resolved as a cancel instead of stacking
#expect(firstEnded == 1)
// end the live gesture so no monitors outlive the test
try NSApp.sendEvent(#require(mouseEvent(.leftMouseUp, in: window)))
}
@Test func `rowFrames prefers the grip frame regardless of subview order`() {
let window = makeWindow()
let id = UUID()
let grip = CatcherView()
grip.kind = .grip
grip.rowID = id
grip.frame = NSRect(x: 0, y: 0, width: 300, height: 27) // full row
let body = CatcherView()
body.kind = .body
body.rowID = id
body.frame = NSRect(x: 0, y: 2, width: 300, height: 23) // text only
window.contentView?.addSubview(body) // reverse of declaration order
window.contentView?.addSubview(grip)
#expect(CatcherView.rowFrames(in: window)[id] == grip.convert(grip.bounds, to: nil))
grip.removeFromSuperview()
body.removeFromSuperview()
window.contentView?.addSubview(grip) // declaration order
window.contentView?.addSubview(body)
#expect(CatcherView.rowFrames(in: window)[id] == grip.convert(grip.bounds, to: nil))
}
}