import Foundation import Testing @testable import NotifierCore struct TurnTimerTests { private let start = Date(timeIntervalSince1970: 1_000) @Test func aTurnLongerThanTheMinimumNotifies() { #expect(TurnTimer.shouldNotify(startedAt: start, endedAt: start.addingTimeInterval(45), minimumSeconds: 30)) } @Test func aQuickTurnIsSuppressed() { #expect(!TurnTimer.shouldNotify(startedAt: start, endedAt: start.addingTimeInterval(5), minimumSeconds: 30)) } @Test func aTurnExactlyAtTheMinimumNotifies() { #expect(TurnTimer.shouldNotify(startedAt: start, endedAt: start.addingTimeInterval(30), minimumSeconds: 30)) } @Test func aMissingStartErrsTowardNotifying() { #expect(TurnTimer.shouldNotify(startedAt: nil, endedAt: start, minimumSeconds: 30)) } @Test func recordedStartIsReadBack() throws { let directory = NSTemporaryDirectory() + "turntimer-\(UUID().uuidString)" defer { try? FileManager.default.removeItem(atPath: directory) } try TurnTimer.recordStart(at: start, directory: directory) #expect(TurnTimer.readStart(directory: directory) == start) } @Test func aMissingStartFileReadsAsNil() { #expect(TurnTimer.readStart(directory: "/tmp/does-not-exist-\(UUID().uuidString)") == nil) } @Test func aCorruptStartFileReadsAsNilInsteadOfCrashing() throws { let directory = NSTemporaryDirectory() + "turntimer-\(UUID().uuidString)" defer { try? FileManager.default.removeItem(atPath: directory) } try FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true) try "not a number".write(toFile: TurnTimer.startFilePath(directory: directory), atomically: true, encoding: .utf8) #expect(TurnTimer.readStart(directory: directory) == nil) } }