test(folds): typing-budget operation-count pin at N/2N [fold-typing-budget-pin] #189
No files matched your search
@@ -121,8 +121,13 @@ nonisolated enum ProseList {
|
|||||||
let hiddenLineCount: Int
|
let hiddenLineCount: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
static func parents(in text: String) -> [ParentHint] {
|
/// `observeLines` (brief-24): reports how many lines the scan visited —
|
||||||
|
/// the typing-budget instrument. Nil in production; tests inject it to
|
||||||
|
/// pin that single-line typing edits scan ZERO lines (the #142 in-place
|
||||||
|
/// re-key) while full passes scan O(lines). Per-call, no shared state.
|
||||||
|
static func parents(in text: String, observeLines: ((Int) -> Void)? = nil) -> [ParentHint] {
|
||||||
let scanned = lines(in: text)
|
let scanned = lines(in: text)
|
||||||
|
observeLines?(scanned.count)
|
||||||
var out: [ParentHint] = []
|
var out: [ParentHint] = []
|
||||||
for (idx, line) in scanned.enumerated() where line.isList {
|
for (idx, line) in scanned.enumerated() where line.isList {
|
||||||
let bounds = blockBounds(scanned, from: idx, indent: line.indent)
|
let bounds = blockBounds(scanned, from: idx, indent: line.indent)
|
||||||
|
|||||||
@@ -90,6 +90,12 @@ final class ProseListTextView: NSTextView {
|
|||||||
/// rejected, or none) → the full pass.
|
/// rejected, or none) → the full pass.
|
||||||
var pendingPlainEdit: (range: NSRange, delta: Int)?
|
var pendingPlainEdit: (range: NSRange, delta: Int)?
|
||||||
|
|
||||||
|
/// Brief-24 typing-budget window for paths that call `refreshFolds()`
|
||||||
|
/// bare (the PostEditorView coordinator's textDidChange tail) — a
|
||||||
|
/// per-instance, test-owned hook; nil in production. The per-call
|
||||||
|
/// `observeScan` parameter takes precedence when both are set.
|
||||||
|
var onLineScan: ((Int) -> Void)?
|
||||||
|
|
||||||
override func shouldChangeText(in affectedRange: NSRange, replacementString: String?) -> Bool {
|
override func shouldChangeText(in affectedRange: NSRange, replacementString: String?) -> Bool {
|
||||||
let accepted = super.shouldChangeText(in: affectedRange, replacementString: replacementString)
|
let accepted = super.shouldChangeText(in: affectedRange, replacementString: replacementString)
|
||||||
pendingPlainEdit = nil
|
pendingPlainEdit = nil
|
||||||
@@ -202,7 +208,11 @@ final class ProseListTextView: NSTextView {
|
|||||||
/// recorded single-line edit takes the in-place re-key (no rescan, no
|
/// recorded single-line edit takes the in-place re-key (no rescan, no
|
||||||
/// storage swap); no folds → chrome-only refresh, everything else → the
|
/// storage swap); no folds → chrome-only refresh, everything else → the
|
||||||
/// full pass.
|
/// full pass.
|
||||||
func refreshFolds() {
|
/// `observeScan` (brief-24): the typing-budget instrument — reports the
|
||||||
|
/// line count of every full-buffer scan this refresh performs. Nil in
|
||||||
|
/// production; the typing-budget pins inject it to prove single-line
|
||||||
|
/// edits take the in-place re-key (zero scans) at any buffer size.
|
||||||
|
func refreshFolds(observeScan: ((Int) -> Void)? = nil) {
|
||||||
defer { pendingPlainEdit = nil }
|
defer { pendingPlainEdit = nil }
|
||||||
if let edit = pendingPlainEdit, !folds.isEmpty, rekeyAfterPlainEdit(edit) {
|
if let edit = pendingPlainEdit, !folds.isEmpty, rekeyAfterPlainEdit(edit) {
|
||||||
return
|
return
|
||||||
@@ -211,7 +221,7 @@ final class ProseListTextView: NSTextView {
|
|||||||
chrome.refresh()
|
chrome.refresh()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
refreshFoldRendering()
|
refreshFoldRendering(observeScan: observeScan)
|
||||||
}
|
}
|
||||||
|
|
||||||
func toggleFold(_ parent: ProseList.ParentHint) {
|
func toggleFold(_ parent: ProseList.ParentHint) {
|
||||||
@@ -227,8 +237,8 @@ final class ProseListTextView: NSTextView {
|
|||||||
/// same marker text; no match → the fold dies), restyle the document,
|
/// same marker text; no match → the fold dies), restyle the document,
|
||||||
/// re-hide every live scope, and park a caret that landed inside a
|
/// re-hide every live scope, and park a caret that landed inside a
|
||||||
/// hidden scope at its parent's marker end.
|
/// hidden scope at its parent's marker end.
|
||||||
func refreshFoldRendering() {
|
func refreshFoldRendering(observeScan: ((Int) -> Void)? = nil) {
|
||||||
let parents = ProseList.parents(in: string)
|
let parents = ProseList.parents(in: string, observeLines: observeScan ?? onLineScan)
|
||||||
// ponytail: nearest-same-marker re-key can move a fold across duplicate
|
// ponytail: nearest-same-marker re-key can move a fold across duplicate
|
||||||
// marker texts — track the edit delta in shouldChangeText if that bites.
|
// marker texts — track the edit delta in shouldChangeText if that bites.
|
||||||
var seenKeys = Set<Int>()
|
var seenKeys = Set<Int>()
|
||||||
|
|||||||
@@ -261,3 +261,109 @@ struct ProseRekeyMathTests {
|
|||||||
#expect(result.park == 15) // caret parks at the marker line's content end
|
#expect(result.park == 15) // caret parks at the marker line's content end
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - #142 typing budget (brief-24: fold-typing-budget-pin)
|
||||||
|
|
||||||
|
/// The typing-cost guard the landed re-key optimization never had: a quiet
|
||||||
|
/// regression that routes single-line typing edits back to the full pass
|
||||||
|
/// would keep every correctness pin above green (the full pass re-derives
|
||||||
|
/// CORRECT folds — it is only O(buffer) per keystroke). These pins count
|
||||||
|
/// scanned LINES through the real keystroke pipeline (shouldChangeText →
|
||||||
|
/// storage mutate → refreshFolds) — an operation count, never wall-time —
|
||||||
|
/// and hold the budget flat as the buffer doubles: the cheap path's cost
|
||||||
|
/// rides the fold count, not the document size.
|
||||||
|
extension ProseFoldRekeyTests {
|
||||||
|
/// One folded list + `filler` plain lines below it. Plain filler lines
|
||||||
|
/// are not list markers, so they add buffer without adding parents.
|
||||||
|
private func budgetText(filler: Int) -> String {
|
||||||
|
"intro\n\n- parent\n - child\n" + (0 ..< filler).map { "filler line \($0)\n" }.joined()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Type one character through the real pipeline at N and 2N filler lines
|
||||||
|
/// and return each run's scanned-line total.
|
||||||
|
private func typingScans(filler: Int) -> Int {
|
||||||
|
let text = budgetText(filler: filler)
|
||||||
|
let textView = makeView(text)
|
||||||
|
guard let parent = ProseList.parents(in: text).first else {
|
||||||
|
preconditionFailure("fixture must arm a parent")
|
||||||
|
}
|
||||||
|
textView.toggleFold(parent)
|
||||||
|
var scanned = 0
|
||||||
|
// The coordinator's keystroke tail with the budget instrument armed.
|
||||||
|
textView.shouldChangeText(in: NSRange(location: 0, length: 0), replacementString: "x")
|
||||||
|
textView.textStorage?.replaceCharacters(in: NSRange(location: 0, length: 0), with: "x")
|
||||||
|
textView.refreshFolds { scanned += $0 }
|
||||||
|
return scanned
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `typing beside a fold scans zero lines at N and 2N`() {
|
||||||
|
let atN = typingScans(filler: 200)
|
||||||
|
let at2N = typingScans(filler: 400)
|
||||||
|
#expect(atN == 0, "single-line typing edit took the full pass at N: \(atN) lines scanned")
|
||||||
|
#expect(at2N == 0, "single-line typing edit took the full pass at 2N: \(at2N) lines scanned")
|
||||||
|
// The budget itself: doubling the buffer must not move it.
|
||||||
|
#expect(atN == at2N)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `coordinator keystroke pipeline scans zero lines beside a fold`() throws {
|
||||||
|
// The through-seam pin (aegis ruling): the REAL entry — insertText
|
||||||
|
// through the PostEditorView coordinator's delegate tail
|
||||||
|
// (shouldChangeText → storage → textDidChange → refreshFolds) —
|
||||||
|
// never a direct test call. The instance hook (onLineScan) is the
|
||||||
|
// budget window for paths that call refreshFolds() bare.
|
||||||
|
let text = budgetText(filler: 200)
|
||||||
|
let textView = makeView(text)
|
||||||
|
let parent = try #require(ProseList.parents(in: text).first)
|
||||||
|
textView.toggleFold(parent)
|
||||||
|
var scanned = 0
|
||||||
|
textView.onLineScan = { scanned += $0 }
|
||||||
|
let session = EditorSession(
|
||||||
|
target: .externalFile(URL(fileURLWithPath: "/dev/null")),
|
||||||
|
debounce: .seconds(3600),
|
||||||
|
)
|
||||||
|
let coordinator = ProseTextView.Coordinator(
|
||||||
|
representable: ProseTextView(session: session, dropBridge: ProseDropBridge()),
|
||||||
|
)
|
||||||
|
textView.delegate = coordinator // production wiring (PostEditorView)
|
||||||
|
textView.setSelectedRange(NSRange(location: 0, length: 0))
|
||||||
|
textView.insertText("x", replacementRange: NSRange(location: 0, length: 0))
|
||||||
|
#expect(scanned == 0, "real keystroke took the full pass: \(scanned) lines")
|
||||||
|
#expect(textView.folds.count == 1) // and the fold survived the real pipeline
|
||||||
|
// Liveness in the same harness: a structural insert through the same
|
||||||
|
// coordinator MUST scan the whole buffer (206 — see the control's
|
||||||
|
// arithmetic) — proving the instance window counts real work.
|
||||||
|
scanned = 0
|
||||||
|
textView.setSelectedRange(NSRange(location: 0, length: 0))
|
||||||
|
textView.insertText("y\n\n", replacementRange: NSRange(location: 0, length: 0))
|
||||||
|
#expect(scanned == 206, "instance window lost exact accounting: \(scanned)")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func `full pass scans scale with the buffer at N and 2N`() {
|
||||||
|
/// The control that keeps the zero-pin honest: route the SAME edit
|
||||||
|
/// shape to the full pass (a multi-line replacement — structural
|
||||||
|
/// edits never take the cheap path) and the instrument must report
|
||||||
|
/// the whole buffer, growing with it. If the observer ever goes
|
||||||
|
/// dead (unwired, wrong seam), this fails — the zero-pin above can
|
||||||
|
/// never pass vacuously.
|
||||||
|
func fullPassScans(filler: Int) -> Int {
|
||||||
|
let text = budgetText(filler: filler)
|
||||||
|
let textView = makeView(text)
|
||||||
|
guard let parent = ProseList.parents(in: text).first else {
|
||||||
|
preconditionFailure("fixture must arm a parent")
|
||||||
|
}
|
||||||
|
textView.toggleFold(parent)
|
||||||
|
var scanned = 0
|
||||||
|
textView.shouldChangeText(in: NSRange(location: 0, length: 0), replacementString: "x\n\n")
|
||||||
|
textView.textStorage?.replaceCharacters(in: NSRange(location: 0, length: 0), with: "x\n\n")
|
||||||
|
textView.refreshFolds { scanned += $0 }
|
||||||
|
return scanned
|
||||||
|
}
|
||||||
|
let atN = fullPassScans(filler: 200)
|
||||||
|
let at2N = fullPassScans(filler: 400)
|
||||||
|
// 4 fixture lines + filler + 2 from the split intro line ("x\n\n"
|
||||||
|
// turns intro into three lines) — exactly the buffer's line count.
|
||||||
|
#expect(atN == 206, "control lost exact line accounting: \(atN)")
|
||||||
|
#expect(at2N == 406, "control lost exact line accounting: \(at2N)")
|
||||||
|
#expect(at2N > atN, "the instrument must scale with the buffer")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user