test(folds): pin the typing budget at N/2N
CI / ci (push) Canceled after 0s

The #142 in-place re-key had correctness coverage but no typing-cost
guard — a quiet fallback to the full pass keeps every correctness
pin green while restoring O(buffer) work per keystroke. Add an
operation-count instrument (never wall-time): ProseList.parents
gains an observeLines reporter, refreshFolds/refreshFoldRendering
thread a per-call observer, and a per-instance onLineScan hook
windows the paths that call refreshFolds() bare (the PostEditorView
coordinator tail). No shared state — the hosted suite is parallel.

Three pins: zero scanned lines for typing beside a fold at N=204
and 2N=404 lines; an exact-count control (206/406) proving the
instrument is live and scaling; and a through-seam pin driving real
insertText through a real ProseTextView.Coordinator into
refreshFolds() — zero scans, fold survives, self-controlling with
its own 206 liveness assert. Mutation-checked: severing the single
observer firing fails the control and the through-seam pin.
This commit is contained in:
AlexKucera committed 2026-11-13 17:21:16 +01:00
1 parent 7ed304124e
commit c460545c59
3 files changed
+126 -5

No files matched your search

+106
View File
@@ -261,3 +261,109 @@ struct ProseRekeyMathTests {
#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")
}
}