Files
museion/MuseionTests/WritingGoalTests.swift
T
AlexKucera e3195d68fd
CI / ci (push) Canceled after 0s
feat(cold-turkey): pure goal engine (#161)
Wayfinder map #158, Build S1: the pure logic slice the writing
lock consumes — no chrome, no clock of its own.

WritingGoal (words | minutes, one goal per block, fixed at arm)
+ WritingGoalRun: update(text:) takes whole-document text and
netNewWords falls out of the delta (deleting costs — net, not
gross; a big paste counts). now is injected into every call, so
the countdown cannot pause by construction; the minutes deadline
is arm+minutes (inclusive), isMet is live truth, unlatched
(delete below target un-mets — complete on the first true tick),
and progress(now:) is the clamped 0…1 the readout hairline rides.
Word counting goes through the new internal DraftStats.words(in:)
— net-words and first-draft stats share one tokenizer and can
never drift.

Finish-audit fanout: the Int() conversion trap in secondsRemaining
fixed at the seam (isFinite + Double(Int.max).nextDown cap — the
naive cap is itself off-by-one at 2^63; flip-verified pin), two
unpinnable > 0 guards in progress deleted, baselineWords narrowed
to let. Equatable and the ?? armedAt fallback kept deliberately
(S2 consumes the former; removing the latter costs formula
duplication). S2 throttle note landed on #162.

Suite 602/602 (xcresult count), swiftformat + swiftlint clean in
app and test targets. Two traps filed to the swift wiki (bare
@Suite stripped by swiftformat; the Double(Int.max) cap).
2026-11-04 12:50:02 +01:00

140 lines
4.9 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 `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)
}
}