Skip to content

test(sudoku): freeze the session clock in the Ready-window test - #1092

Merged
wei18 merged 1 commit into
mainfrom
test/1082-freeze-ready-window-clock
Sep 15, 2026
Merged

wei18 merged 1 commit into
mainfrom
test/1082-freeze-ready-window-clock

Conversation

@wei18

@wei18 wei18 commented Sep 15, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #1082. Test-only: BoardLeaveOrPauseStateTests "live startOrResume() lands in the reachable Ready window" now builds its GameSession on a test-local frozen MonotonicClock. Assertions are unchanged (no elapsedSeconds <= 1 widening), and no production code is touched.

Root cause (confirmed, not assumed)

  • GameSession.elapsedSeconds is accumulatedSeconds + Int(clock.now - runningSince) (SudokuCoreKit/.../GameSession.swift:77). Int(Double) truncates, which for non-negative spans is floor.
  • start() sets runningSince = clock.now (:101). GameViewModel.startOrResume() then calls resyncFromSession(), which reads session.snapshot().elapsedSeconds (GameViewModel.swift:719-723).
  • On LiveMonotonicClock, once ≥ 1.0 s of real time passes between start() and that resync, the value reads 1. The path crosses several actor / MainActor hops, and only a loaded batch runner stalls that long. :123 then fails (elapsedSeconds → 1) and :126 fails (the state leaves .leaveReady), which is exactly the pair seen in the fix(monetization): restore cold-launch gate resolution + consent-before-request ordering #1062 batch runs.

Red repro (temporary, not committed). A test-local clock whose first read (inside start()) returns 0 and every later read returns step. I parameterized the test over step ∈ [0.999, 1.0]:

Test "live startOrResume() lands in the reachable Ready window" recorded an issue with 1 argument step → 1.0 at BoardLeaveOrPauseStateTests.swift:124:9: Expectation failed: (viewModel.elapsedSeconds → 1) == 0
Test "live startOrResume() lands in the reachable Ready window" recorded an issue with 1 argument step → 1.0 at BoardLeaveOrPauseStateTests.swift:127:9: Expectation failed: (boardView.leaveOrPauseState → .pause) == .leaveReady
Test run with 7 tests in 1 suite failed after 0.012 seconds with 2 issues.

step → 0.999 recorded no issue, so the threshold is exactly 1.0 s. The repro's temporary import Foundation shifted lines by one, which is why this output says :124/:127 instead of :123/:126.

Change

  • private struct FrozenMonotonicClock: MonotonicClock { let now: TimeInterval = 0 }, local to the test file.
  • GameSession(puzzle: puzzle, clock: FrozenMonotonicClock()) in the Ready-window test only.
  • A comment says "live" means the real GameSession + GameViewModel path, not live time, so nobody swaps it back to LiveMonotonicClock.

Verification

Mutation. Each run below was swift test --filter BoardLeaveOrPauseStateTests with a temporary edit to BoardView+AccessibilityHeader.swift leaveOrPauseState. Every edit was reverted, so the final diff touches only the test file.

  • M1, the PM example: drop the elapsedSeconds == 0 clause. Only the sibling snapshot-seam test goes red: "playing, elapsed > 0, pencil-only resolves to pause" at :83 (.leaveReady ≠ .pause). The Ready-window test stays green. This is expected: the frozen clock pins elapsed at 0, so M1 does not change this test's input.
  • M2: invert the clause to elapsedSeconds != 0. The Ready-window test goes red at :133 (.pause ≠ .leaveReady). The two snapshot-seam siblings go red too (:66, :83).
  • Restored: 7/7 green. The first board batch below also covers the restored run.

Stability. The SudokuKit board batch ran 5 consecutive times, one at a time, with the exact board --filter string from #1062's phase-2d comment:

Run Batch summary line Ready-window test The 1 red test
1 Test run with 188 tests in 43 suites failed after 9.485 seconds with 1 issue. passed after 7.758 s ToastTests.autoDismiss_firesAfterDuration (#1087)
2 Test run with 188 tests in 43 suites failed after 11.504 seconds with 1 issue. passed after 10.132 s ToastTests.autoDismiss_firesAfterDuration (#1087)
3 Test run with 188 tests in 43 suites failed after 8.131 seconds with 1 issue. passed after 6.764 s ToastTests.autoDismiss_firesAfterDuration (#1087)
4 Test run with 188 tests in 43 suites failed after 10.312 seconds with 1 issue. passed after 8.516 s ToastTests.autoDismiss_firesAfterDuration (#1087)
5 Test run with 188 tests in 43 suites failed after 12.095 seconds with 1 issue. passed after 10.438 s ToastTests.autoDismiss_firesAfterDuration (#1087)

The batch is red in all 5 runs, with 1 failure (1 issue) per run. The failure is the separately tracked #1087 wall-clock flake (ToastTests.swift:57), not this test. BoardLeaveOrPauseStateTests recorded no issue in any run. Under batch load the Ready-window test took 6.8–10.4 s of wall time per run. That is far past the 1.0 s floor, so on the live clock it would have failed.

Lint. swiftlint lint --strict on the changed file reported 0 violations. The lefthook pre-commit hooks (gitleaks, hygiene, swiftlint) passed.

Follow-up (not done here)

FakeMonotonicClock now exists as private copies in SudokuCoreKit's and PersistenceKit's test targets, plus this frozen variant. Consolidating them into a shared test-support target is a possible follow-up and is out of scope for this PR.

🤖 Generated with Claude Code

BoardLeaveOrPauseStateTests' "live startOrResume() lands in the reachable
Ready window" built GameSession(puzzle:) on LiveMonotonicClock.
GameSession.elapsedSeconds floors `clock.now - runningSince` (Int(_:)
truncation), so when a loaded batch runner stalls >= 1.0 s between start()
and startOrResume()'s resync, elapsedSeconds reads 1 and the state leaves
.leaveReady. That is the intermittent failure at the elapsedSeconds and
leaveOrPauseState expectations.

The test now builds its session with a test-local FrozenMonotonicClock.
Assertions unchanged; no production code touched.

Part of #1082.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@wei18

wei18 commented Sep 15, 2026

Copy link
Copy Markdown
Owner Author

Fresh-context T6 acceptance: #1082 (head 097bb70d)

The reviewer was an independent agent that did not write the change. It worked read-only and read the PR head via git show.

# Check Verdict Evidence
A Diff scope PASS git show --stat 097bb70d and git diff --stat origin/main...097bb70d both show 1 file, BoardLeaveOrPauseStateTests.swift, 15 insertions and 1 deletion. No production file, no Package.swift, no .xcstrings.
B Root-cause claims PASS origin/main:GameSession.swift:77 is return accumulatedSeconds + Int(clock.now - runningSince) and :101 is runningSince = clock.now, both verbatim. GameViewModel.resyncFromSession() assigns self.elapsedSeconds = snap.elapsedSeconds at :722. Int(Double) on a non-negative value is floor. FrozenMonotonicClock satisfies MonotonicClock (var now: TimeInterval { get }, Sendable).
C No assertion widened PASS Every #expect is byte-identical to origin/main. Only line numbers moved: +1 for the import and +6 for the comment.
D Evidence logs PASS Repro: step 1.0 is red at the elapsed and state expectations, 0.999 records no issue. M1: only :83 red. M2: the Ready-window test is red at :133, and the siblings at :66 and :83 too. Batches 1–5: 188 tests / 43 suites with 1 issue each, and the issue is always ToastTests.swift:57 (#1087). BoardLeaveOrPauseState recorded 0 issues. The repro patch is not in the commit.
E Independent green run PASS HEAD = 097bb70d5f5e…, clean tree. swift test --filter BoardLeaveOrPauseStateTests gave Test run with 7 tests in 1 suite passed after 0.010 seconds.
F PR body references PASS Closes #1082 is the only closing keyword. #1062 (MERGED) and #1087 (OPEN) are plain references, and both exist.
G TODO/FIXME/stub/placeholder in the diff PASS 0 hits.

總判定:ACCEPT

@wei18
wei18 merged commit aeb80f0 into main Sep 15, 2026
7 checks passed
@wei18
wei18 deleted the test/1082-freeze-ready-window-clock branch September 15, 2026 06:09
wei18 added a commit that referenced this pull request Sep 15, 2026
Merge origin/main (c2959d0) into session/brisk-stork-isio. Main advanced
by two commits after the first #1080 merge (68f840a):
- aeb80f0 test(sudoku): freeze the session clock in the Ready-window test (#1092)
- c2959d0 chore(l10n): remove proven-orphan keys and relabel live stale keys in both catalogs (#1093)

A read-only `git merge-tree` dry run was clean. The five files main touched
(both app catalogs, the SudokuAppCompositionTests L10n fixture,
BoardLeaveOrPauseStateTests.swift, mise-tasks/scan/l10n) do not overlap with
any file this branch changes.

L10n check: this branch adds no localized string literals. None of the 14
Sudoku keys #1093 removed is referenced by branch-added code.

part of #1024

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(sudoku): BoardLeaveOrPauseStateTests "reachable Ready window" flakes in batch runs — wall-clock second tick under load

1 participant