Inject the view model's store, and cover its lifecycle !21

merged merged by cmc on 2026-10-24 08:47 UTC · krz/domain-dig:viewmodel-store-seam into main

Discussion

cmc

DomainViewModel read and wrote every persisted property through UserDefaults.standard. No test could drive its lifecycle without changing the store of whichever app shares the simulator — DomainViewModelInputTests was already writing "localhost" into the real store on every run.

The seam

init(defaults: UserDefaults = .standard) replaces the ten property initializers. Threaded through DomainViewModel, its export, monitoring and workflow extensions, and MonitoringStorage's forwarders. beginLookup, isCurrentLookup and addRecentSearch become internal so the lifecycle is reachable — the same seam pass as !18 and !19.

Coverage

18 tests, 185 to 203:

  • the seam: a view model reads the store it was given, its writes do not reach .standard, and two view models on separate stores do not see each other
  • history pruning at the auto-prune option, at the 250 hard cap under "unlimited", keeping the newest rows, and a store already over the cap still loading every row
  • recent searches truncating at 20 and deduping case-insensitively
  • tracking: persisted to the injected store, idempotent across case, refused at the entitlement ceiling with the upgrade prompt, and a store over the ceiling still loading in full
  • batch cancellation resolving every pending row
  • Review Focus #4: a second Run invalidates the first lookup's generation, so a late section result cannot land on the new snapshot

Per E1, releasing a main-actor object aborts the runner on iOS 18.6. The test class holds every view model it builds for the life of the process, so none is released. No app code changed for it.

Still open

CloudSyncService's own UserDefaults is not injected, so deleting a tracked domain writes a tombstone to .standard whatever store the view model has. Delete coverage was left out rather than made to pollute the simulator; the gap is recorded under C7.

Verification

Each tier run as its own invocation: 203 tests, 0 failures on iOS 18.6 (iPhone 16) and iOS 27.0 (iPhone Air).

./Scripts/test.sh with no argument is unreliable and this branch did not change that — the second tier of a back-to-back pair sometimes never launches its runner, exiting non-zero with no diagnostic or hanging after the build. Recorded as E2 with the measurements.