Files
museion/MuseionTests/FocusModeTests.swift
T
AlexKucera b0ab800d3d feat(workspace): focus mode — recession, hover reveal, soft caret centering (#87)
Map #21 build child. Toggle via View menu / ⌘⇧F; panes and toolbar
recede, overlay hover reveal (left: rail+library, right: mentor),
soft caret centering while typing, loud save failure, ⌘S armed in
focus. Full suite green; calibration gate #88 judges calm.
2026-10-09 11:11:36 +02:00

65 lines
2.2 KiB
Swift

//
// FocusModeTests.swift
// Museion
//
// #21 (map #21): the soft-centering band math — the one piece of focus
// logic pure enough to pin. In-band stays put, out-of-band recenters,
// document clamp parks at the bottom (no jump), already-clamped no-ops.
//
import CoreFoundation
@testable import Museion
import Testing
struct FocusModeTests {
/// A tall viewport: 800pt visible, 4000pt document.
@Test func `caret inside band leaves scroll alone`() {
let center = 400.0 // visibleMinY 0 + 800/2
#expect(CaretCentering.targetOffset(
caretY: center, visibleMinY: 0, visibleHeight: 800, documentHeight: 4000,
) == nil)
#expect(CaretCentering.targetOffset(
caretY: center + CaretCentering.band - 1, visibleMinY: 0, visibleHeight: 800, documentHeight: 4000,
) == nil)
}
@Test func `caret below band recenters down`() throws {
let target = CaretCentering.targetOffset(
caretY: 900, visibleMinY: 0, visibleHeight: 800, documentHeight: 4000,
)
#expect(target != nil)
#expect(try abs(#require(target) - 500) < 0.5) // 900 - 800/2
}
@Test func `caret above band recenters up`() throws {
// Scrolled deep (visibleMinY 2000), caret back at 1800.
let target = CaretCentering.targetOffset(
caretY: 1800, visibleMinY: 2000, visibleHeight: 800, documentHeight: 4000,
)
#expect(target != nil)
#expect(try abs(#require(target) - 1400) < 0.5) // 1800 - 800/2
}
@Test func `document bottom clamps no jump`() throws {
// Typing at the very end of a short-ish document: the raw target
// (2600) would over-scroll past the last line; the clamp parks at
// documentHeight - visibleHeight and FOLLOWS line by line.
let target = CaretCentering.targetOffset(
caretY: 3000, visibleMinY: 1000, visibleHeight: 800, documentHeight: 3100,
)
#expect(target != nil)
#expect(try abs(#require(target) - 2300) < 0.5)
// Already parked at the clamp: no-op, no jump while typing the tail.
#expect(CaretCentering.targetOffset(
caretY: 3050, visibleMinY: 2300, visibleHeight: 800, documentHeight: 3100,
) == nil)
}
@Test func `short document never scrolls`() {
// Document shorter than the viewport: clamp floors at 0.
#expect(CaretCentering.targetOffset(
caretY: 300, visibleMinY: 0, visibleHeight: 800, documentHeight: 400,
) == nil)
}
}