Files
museion/MuseionTests/WritingGoalTests.swift
AlexKucera dde99c8a75
CI / ci (push) Canceled after 0s
test(goal): pin zero word-rail trips for minutes updates (RED)
The argus opt pass (2026-11-09, unticketed digest finding
cold-turkey-full-tokenization): every editor keystroke feeds the whole
document through WritingGoalRun.update, which counted words for ALL
goal types — a minutes verdict ignores the count, so the feed paid
whole-document tokenization (lowercased [String] allocation) per
keystroke on the main actor for nothing (argus probe: ~16.6 ms/update
at 50k words, linear in document size).

Pins, RED-first where constructible:
- WritingGoalTests 'a minutes update takes zero word-rail trips' —
  RED at base (base takes one trip per update).
- WritingGoalTests 'a words update takes exactly one word-rail trip' —
  the skip boundary: words goals keep live accounting.
- DraftStatsTests 'word rail equals the split tokenizer over a mixed
  corpus' — semantic equivalence pin (old split expression as inline
  reference over mixed scripts, emoji, punctuation, numbers,
  empties); green at base by design — it is the equivalence proof.

Instrument: update(text:countWords:) gains an injectable word-rail
seam (default DraftStats.words) — per-call counting instead of a
global counter, which would race the hosted suite's parallel
DraftStats calls. No behavior change in this commit; update still
counts for every goal.
2026-11-13 04:20:15 +01:00

175 lines
5.8 KiB
Swift

//
// WritingGoalTests.swift
// MuseionTests
//
// #161 (Cold Turkey S1): the pure goal engine — net-words delta on
// the DraftStats tokenizer, a never-pausing countdown, and the
// goal-met transitions. Scenario: arm a run over a small body, feed
// it whole-document text and injected `now`s; every expectation is
// hand-derived. No clock, no UI — the run answers, the lock asks.
//
import Foundation
@testable import Museion
import Testing
@Suite("WritingGoalTests")
struct WritingGoalTests {
private static let arm = Date(timeIntervalSince1970: 1_000_000)
private static func run(
_ goal: WritingGoal,
body: String = "One two three four five",
at date: Date = WritingGoalTests.arm,
) -> WritingGoalRun {
WritingGoalRun(goal: goal, armedAt: date, body: body)
}
@Test
func `arm baselines the body and starts at zero net`() {
let block = Self.run(.words(500))
#expect(block.baselineWords == 5)
#expect(block.netNewWords == 0)
#expect(!block.isMet(now: Self.arm))
}
@Test
func `typing completes a words goal exactly at target`() {
var block = Self.run(.words(500))
// baseline words ride inside the live text — the NEW words are the delta
block.update(text: "One two three four five " + String(repeating: "word ", count: 500))
#expect(block.netNewWords == 500)
#expect(block.isMet(now: Self.arm))
block.update(text: "One two three four five " + String(repeating: "word ", count: 499))
#expect(block.netNewWords == 499)
#expect(!block.isMet(now: Self.arm))
}
@Test
func `deleting costs net and can go below the baseline`() {
var block = Self.run(.words(3), body: "One two three four five six seven eight nine ten")
block.update(text: "One two three four five six seven eight nine ten eleven twelve thirteen")
#expect(block.netNewWords == 3)
block.update(text: "One two three four five six seven eight")
#expect(block.netNewWords == -2)
#expect(block.progress(now: Self.arm) == 0)
}
@Test
func `a big paste counts in full`() {
var block = Self.run(.words(2000), body: "")
block.update(text: String(repeating: "word ", count: 2000))
#expect(block.netNewWords == 2000)
#expect(block.isMet(now: Self.arm))
}
@Test
func `undo back to the original body nets zero`() {
let body = "One two three four five"
var block = Self.run(.words(1), body: body)
block.update(text: body + " six seven")
#expect(block.netNewWords == 2)
block.update(text: body)
#expect(block.netNewWords == 0)
#expect(!block.isMet(now: Self.arm))
}
@Test
func `a words goal never consults the clock`() {
let block = Self.run(.words(500))
#expect(!block.isMet(now: Self.arm.addingTimeInterval(10 * 3600)))
#expect(block.secondsRemaining(now: Self.arm) == nil)
}
@Test
func `a minutes goal completes exactly at the deadline`() {
let block = Self.run(.minutes(15))
let deadline = Self.arm.addingTimeInterval(15 * 60)
#expect(!block.isMet(now: deadline.addingTimeInterval(-1)))
#expect(block.isMet(now: deadline))
#expect(block.secondsRemaining(now: deadline.addingTimeInterval(-60)) == 60)
#expect(block.secondsRemaining(now: deadline.addingTimeInterval(30)) == 0)
}
@Test
func `a minutes goal ignores words and never pauses`() {
var block = Self.run(.minutes(30))
block.update(text: String(repeating: "word ", count: 2000))
#expect(!block.isMet(now: Self.arm.addingTimeInterval(29 * 60)))
#expect(block.secondsRemaining(now: Self.arm.addingTimeInterval(60)) == 29 * 60)
}
@Test
func `a minutes update takes zero word-rail trips`() {
// Opt pass 2026-11-09 (unticketed digest finding): a minutes
// verdict rides the clock alone, so the per-keystroke feed must
// not count — let alone tokenize — the whole document. One long
// feed, zero rail trips through the injectable seam.
var block = Self.run(.minutes(30))
var railTrips = 0
block.update(
text: String(repeating: "word ", count: 2000),
countWords: { _ in
railTrips += 1
return 0
},
)
#expect(railTrips == 0)
}
@Test
func `a words update takes exactly one word-rail trip`() {
// The skip boundary: words goals keep live accounting — exactly
// one rail trip per update, and the count lands.
var block = Self.run(.words(500))
var railTrips = 0
block.update(
text: "one two three four five six seven",
countWords: { _ in
railTrips += 1
return 7
},
)
#expect(railTrips == 1)
#expect(block.currentWords == 7)
}
@Test
func `an absurd minutes goal never traps the countdown`() {
// Int() traps past Int.max — the clamp at the conversion seam is the pin
let block = Self.run(.minutes(.max), body: "")
#expect(block.secondsRemaining(now: Self.arm) == 9_223_372_036_854_774_784)
#expect(block.progress(now: Self.arm) == 0)
#expect(!block.isMet(now: Self.arm))
}
@Test
func `token semantics match DraftStats words`() {
#expect(Self.run(.words(1), body: "writer's").baselineWords == 2)
#expect(Self.run(.words(1), body: "writer's note").baselineWords == 3)
#expect(Self.run(.words(1), body: "## Chapter Two").baselineWords == 2)
#expect(Self.run(.words(1), body: "Draft 2026 shipped").baselineWords == 3)
#expect(Self.run(.words(1), body: "").baselineWords == 0)
#expect(Self.run(.words(1), body: "— — !!!").baselineWords == 0)
}
@Test
func `progress rides net for words and elapsed for minutes`() {
var words = Self.run(.words(100))
#expect(words.progress(now: Self.arm) == 0)
words.update(text: "One two three four five " + String(repeating: "word ", count: 50))
#expect(words.progress(now: Self.arm) == 0.5)
words.update(text: String(repeating: "word ", count: 150))
#expect(words.progress(now: Self.arm) == 1)
let minutes = Self.run(.minutes(10))
#expect(minutes.progress(now: Self.arm) == 0)
#expect(minutes.progress(now: Self.arm.addingTimeInterval(300)) == 0.5)
#expect(minutes.progress(now: Self.arm.addingTimeInterval(600)) == 1)
}
}