|
| 1 | +import FamilyControls |
| 2 | +import Foundation |
| 3 | +@testable import Normal |
| 4 | +import Testing |
| 5 | + |
| 6 | +struct ShieldExclusionTests { |
| 7 | + private let cal: Calendar = { |
| 8 | + var c = Calendar(identifier: .gregorian) |
| 9 | + c.timeZone = TimeZone(identifier: "UTC")! |
| 10 | + return c |
| 11 | + }() |
| 12 | + |
| 13 | + // Wednesday, June 5 2024, 12:00 UTC. |
| 14 | + private var noonWednesday: Date { |
| 15 | + cal.date(from: DateComponents(year: 2024, month: 6, day: 5, hour: 12))! |
| 16 | + } |
| 17 | + |
| 18 | + private func unblock( |
| 19 | + id: String, |
| 20 | + apps: [UInt8], |
| 21 | + domains: [String] = [], |
| 22 | + endDate: Date = .now.addingTimeInterval(.hours(1)) |
| 23 | + ) throws -> TimedUnblockDTO { |
| 24 | + try TimedUnblockDTO( |
| 25 | + id: id, |
| 26 | + selectionData: TokenFixtures.selection(apps: apps).toData(), |
| 27 | + endDate: endDate, |
| 28 | + activityName: "timedUnblock_\(id)", |
| 29 | + isGroupUnblock: id != TimedUnblockService.mainID, |
| 30 | + customDomains: domains |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + private func schedule( |
| 35 | + apps: [UInt8], |
| 36 | + domains: [String] = [], |
| 37 | + startHour: Int = 10, |
| 38 | + durationMinutes: Int = 240, |
| 39 | + shouldBlock: Bool = true |
| 40 | + ) throws -> ScheduleDTO { |
| 41 | + try ScheduleDTO( |
| 42 | + id: UUID(), |
| 43 | + name: "S", |
| 44 | + selectionData: TokenFixtures.selection(apps: apps).toData(), |
| 45 | + startHour: startHour, |
| 46 | + startMinute: 0, |
| 47 | + durationMinutes: durationMinutes, |
| 48 | + weekdays: [4], |
| 49 | + shouldBlock: shouldBlock, |
| 50 | + isTimed: true, |
| 51 | + customDomains: domains |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + @Test func removesAppsAndDomainsOfActiveGroupUnblocks() throws { |
| 56 | + let exclusion = try ShieldExclusion.groupUnblocks( |
| 57 | + [unblock(id: "g1", apps: [2], domains: ["b.com"])], |
| 58 | + at: .now |
| 59 | + ) |
| 60 | + |
| 61 | + let remaining = exclusion.applied(to: TokenFixtures.selection(apps: [1, 2])) |
| 62 | + #expect(remaining.applicationTokens == [TokenFixtures.app(1)]) |
| 63 | + #expect(exclusion.applied(to: ["a.com", "b.com"]) == ["a.com"]) |
| 64 | + } |
| 65 | + |
| 66 | + @Test func ignoresExpiredGroupUnblocksAndMainUnblock() throws { |
| 67 | + let exclusion = try ShieldExclusion.groupUnblocks( |
| 68 | + [ |
| 69 | + unblock(id: "g1", apps: [1], endDate: .now.addingTimeInterval(-60)), |
| 70 | + unblock(id: TimedUnblockService.mainID, apps: [2]), |
| 71 | + ], |
| 72 | + at: .now |
| 73 | + ) |
| 74 | + |
| 75 | + let remaining = exclusion.applied(to: TokenFixtures.selection(apps: [1, 2])) |
| 76 | + #expect(remaining.applicationTokens == [TokenFixtures.app(1), TokenFixtures.app(2)]) |
| 77 | + } |
| 78 | + |
| 79 | + @Test func keepsAppsOfOtherActiveSchedulesOfSameKind() throws { |
| 80 | + let ending = try schedule(apps: [1, 2], domains: ["a.com", "b.com"], startHour: 9, durationMinutes: 180) |
| 81 | + let stillRunning = try schedule(apps: [2, 3], domains: ["b.com"]) |
| 82 | + |
| 83 | + let keep = ShieldExclusion.activeSchedules( |
| 84 | + [ending, stillRunning], |
| 85 | + shouldBlock: true, |
| 86 | + at: noonWednesday, |
| 87 | + excluding: ending.id, |
| 88 | + calendar: cal |
| 89 | + ) { _ in true } |
| 90 | + |
| 91 | + #expect(keep.applied(to: TokenFixtures.selection(apps: [1, 2])).applicationTokens == [TokenFixtures.app(1)]) |
| 92 | + #expect(keep.applied(to: ["a.com", "b.com"]) == ["a.com"]) |
| 93 | + } |
| 94 | + |
| 95 | + @Test func ignoresDisabledInactiveAndOtherKindSchedules() throws { |
| 96 | + let ending = try schedule(apps: [1], startHour: 9, durationMinutes: 180) |
| 97 | + let disabled = try schedule(apps: [1]) |
| 98 | + let inactive = try schedule(apps: [1], startHour: 14, durationMinutes: 60) |
| 99 | + let allowing = try schedule(apps: [1], shouldBlock: false) |
| 100 | + |
| 101 | + let keep = ShieldExclusion.activeSchedules( |
| 102 | + [ending, disabled, inactive, allowing], |
| 103 | + shouldBlock: true, |
| 104 | + at: noonWednesday, |
| 105 | + excluding: ending.id, |
| 106 | + calendar: cal |
| 107 | + ) { $0.id != disabled.id } |
| 108 | + |
| 109 | + #expect(keep.applied(to: TokenFixtures.selection(apps: [1])).applicationTokens == [TokenFixtures.app(1)]) |
| 110 | + } |
| 111 | + |
| 112 | + @Test func unionCombinesBothExclusions() throws { |
| 113 | + let groups = try ShieldExclusion.groupUnblocks([unblock(id: "g1", apps: [1])], at: .now) |
| 114 | + let schedules = try ShieldExclusion.activeSchedules( |
| 115 | + [schedule(apps: [2])], |
| 116 | + shouldBlock: true, |
| 117 | + at: noonWednesday, |
| 118 | + excluding: UUID(), |
| 119 | + calendar: cal |
| 120 | + ) { _ in true } |
| 121 | + |
| 122 | + let remaining = groups.union(schedules).applied(to: TokenFixtures.selection(apps: [1, 2, 3])) |
| 123 | + #expect(remaining.applicationTokens == [TokenFixtures.app(3)]) |
| 124 | + } |
| 125 | +} |
0 commit comments