Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Normal/Models/UsageLimitEditPolicy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ nonisolated enum UsageLimitEditPolicy {

static func lockState(anchor: Date?, now: Date = .now) -> UsageLimitLockState {
guard let anchor else { return .unlocked }
let unlock = anchor + cooldownInterval
if now < anchor {
return .locked(until: unlock)
}
let graceEnds = anchor + graceInterval
if now < graceEnds {
return .grace(until: graceEnds)
}
let unlock = anchor + cooldownInterval
return now < unlock ? .locked(until: unlock) : .unlocked
}

Expand Down
18 changes: 18 additions & 0 deletions NormalTests/Models/UsageLimitEditPolicyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ struct UsageLimitEditPolicyTests {
#expect(current == anchor)
}

@Test(arguments: [
UsageLimitEdit.adjust(from: 30, to: 60, dropsCoverage: false),
.adjust(from: 60, to: 60, dropsCoverage: true),
.delete(minutes: 30),
.changeReset,
])
func windingTheClockBackBeforeTheAnchorDoesNotReopenGrace(edit: UsageLimitEdit) {
let decision = UsageLimitEditPolicy.decide(edit, anchor: anchor, now: anchor - .days(1))
#expect(decision == .locked(until: anchor + .days(7)))
}

@Test func restoringTheClockAfterAFutureAnchorStaysLocked() {
#expect(
UsageLimitEditPolicy.lockState(anchor: anchor, now: anchor - 1)
== .locked(until: anchor + .days(7))
)
}

// MARK: - Anchor bookkeeping

@Test func onlyCooldownElapsedStartsANewWeek() {
Expand Down
Loading