fix: enforce open-limit rules proactively in the foreground
RuleEnforcer.refresh only shielded a limit rule once its budget was spent, so it tore down the background's proactive open-limit "turnstile" shield (the shield is how opens are counted) whenever the app was foregrounded, and a freshly created open-limit rule did nothing until the next midnight reset. refresh now applies the same proactive gate as LimitEnforcement.handleDayStart: an enabled, scheduled-today, un-paused open-limit rule is shielded even before its budget is spent, so it takes effect immediately. A new app-group OpenSessionStore records a granted "Open" session's expiry so neither enforcement path re-shields (and cuts short) a sanctioned ~15-minute session. Overlapping rules already enforce strictly: each rule shields its own ManagedSettingsStore and Screen Time unions them, so an app is blocked if any covering rule blocks it. Document that model in the spec (§4.8) and lock it in with tests, including a time limit blocking during an open-limit's granted session and a daily opens reset.
This commit is contained in:
@@ -170,17 +170,38 @@ struct UsageEnforcementTests {
|
||||
#expect(shields.appliedAdultContentFlags[rule.id] == false)
|
||||
}
|
||||
|
||||
@Test("A limit rule with budget left is not shielded")
|
||||
func leavesUnspentLimitAlone() {
|
||||
@Test("A time-limit rule with budget left is not shielded")
|
||||
func leavesUnspentTimeLimitAlone() {
|
||||
let shields = MockShieldController()
|
||||
let ledger = MockUsageLedger()
|
||||
let enforcer = RuleEnforcer(shields: shields, usage: ledger)
|
||||
let rule = BlockingRule(
|
||||
name: "Time Keeper",
|
||||
configuration: .timeLimit(TimeLimitConfig(dailyLimitMinutes: 45)),
|
||||
days: Weekday.everyDay)
|
||||
ledger.usageByRule[rule.id] = RuleUsage(minutesUsed: 20)
|
||||
|
||||
enforcer.refresh(rules: [rule], at: mondayMorning, calendar: utc)
|
||||
|
||||
// Time limits let the OS meter usage on the unshielded app, so nothing
|
||||
// is shielded until the budget is spent. (Open limits differ — the
|
||||
// shield is the meter, so they are gated proactively; see
|
||||
// RuleEnforcerTests.proactivelyShieldsOpenLimit.)
|
||||
#expect(shields.shieldedRuleIDs.isEmpty)
|
||||
}
|
||||
|
||||
@Test("An open-limit rule not scheduled today is not gated")
|
||||
func leavesOffDayOpenLimitAlone() {
|
||||
let shields = MockShieldController()
|
||||
let ledger = MockUsageLedger()
|
||||
let enforcer = RuleEnforcer(shields: shields, usage: ledger)
|
||||
let rule = BlockingRule(
|
||||
name: "Gate Keeper",
|
||||
configuration: .openLimit(OpenLimitConfig(maxOpens: 5)),
|
||||
days: Weekday.everyDay)
|
||||
days: Weekday.weekends)
|
||||
ledger.usageByRule[rule.id] = RuleUsage(opensUsed: 2)
|
||||
|
||||
// mondayMorning is a weekday, so the weekend-only rule does not gate.
|
||||
enforcer.refresh(rules: [rule], at: mondayMorning, calendar: utc)
|
||||
|
||||
#expect(shields.shieldedRuleIDs.isEmpty)
|
||||
|
||||
Reference in New Issue
Block a user