// // 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 `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) } }