// // 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)) } }