diff --git a/Museion/WritingGoal.swift b/Museion/WritingGoal.swift index ca4925c..236d512 100644 --- a/Museion/WritingGoal.swift +++ b/Museion/WritingGoal.swift @@ -48,8 +48,11 @@ nonisolated struct WritingGoalRun: Equatable { /// Record the live editor text. The caller passes the whole /// document every time; the net falls out of the delta. - mutating func update(text: String) { - currentWords = DraftStats.words(in: text) + /// `countWords` is the injectable word rail — the pins count + /// rail trips through this seam (a global counter would race + /// the hosted suite's parallel DraftStats calls). + mutating func update(text: String, countWords: (String) -> Int = DraftStats.words(in:)) { + currentWords = countWords(text) } /// Net new words since arm. Deleting costs — net, not gross; a diff --git a/MuseionTests/DraftStatsTests.swift b/MuseionTests/DraftStatsTests.swift index 44f6405..81d723b 100644 --- a/MuseionTests/DraftStatsTests.swift +++ b/MuseionTests/DraftStatsTests.swift @@ -75,3 +75,40 @@ import Testing #expect(abs(DraftStats.compute(body: "Dog dog DOG").typeTokenRatio - 1.0 / 3.0) < 0.001) #expect(abs(DraftStats.compute(body: "writer's note writer").typeTokenRatio - 3.0 / 4.0) < 0.001) } + +@Test func `word rail equals the split tokenizer over a mixed corpus`() { + /// The count-only word rail (opt pass 2026-11-09) must answer exactly + /// what the lowercased-token array answered: split on + /// !(isLetter || isNumber), empty subsequences omitted. The reference + /// IS the old implementation — the corpus (mixed scripts, emoji, + /// punctuation, numbers, empties) makes any semantic drift (e.g. a + /// whitespace count) fail here. + func reference(_ text: String) -> Int { + text.split(whereSeparator: { !($0.isLetter || $0.isNumber) }).count + } + let corpus = [ + "", + " \t\n ", + "\n\n\n", + "— — !!!", + "one two three", + "writer's note", + "## Chapter Two — draft 3", + "abc123def", + "3.14 and 2,718,281", + "$5,000 USD", + "naïve café Zürich", + "Привет мир", + "日本語のテキストです", + "مرحبا بالعالم", + "emoji 👨‍👩‍👧‍👦 are not words", + "word—word, word; word: word", + "One two three. Four five! Six?", + ] + for text in corpus { + #expect( + DraftStats.words(in: text) == reference(text), + "corpus mismatch for \(text.debugDescription)", + ) + } +} diff --git a/MuseionTests/WritingGoalTests.swift b/MuseionTests/WritingGoalTests.swift index 8d03552..cc825ff 100644 --- a/MuseionTests/WritingGoalTests.swift +++ b/MuseionTests/WritingGoalTests.swift @@ -103,6 +103,41 @@ struct WritingGoalTests { #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