Repository Public
1import MCCCore2import XCTest3@testable import MCC45final class MaintenanceTests: XCTestCase {6 private let toronto = TimeZone(identifier: "America/Toronto")!7 private let paris = TimeZone(identifier: "Europe/Paris")!89 private func cal(_ tz: TimeZone) -> Calendar {10 var c = Calendar(identifier: .gregorian)11 c.timeZone = tz12 c.locale = Locale(identifier: "en_CA")13 c.firstWeekday = 114 return c15 }1617 private func date(_ y: Int, _ m: Int, _ d: Int, _ h: Int = 0, _ min: Int = 0, tz: TimeZone) -> Date {18 cal(tz).date(from: DateComponents(year: y, month: m, day: d, hour: h, minute: min))!19 }2021 private func window(id: String = "w1", start: Date, end: Date, recurrence: API.MaintenanceRecurrence = .none, until: Date? = nil,22 status: API.MaintenanceStatus = .scheduled, nodes: [String] = ["n1"]) -> API.MaintenanceWindow {23 API.MaintenanceWindow(id: id, title: "macOS update", description: "Reboot required", category: .update, startsAt: start, endsAt: end,24 timezone: "America/Toronto", recurrence: recurrence, recurrenceUntil: until, scope: .init(nodeIds: nodes),25 setMaintenanceMode: true, silenceAlerts: false, status: status, createdAt: start, updatedAt: start)26 }2728 private func note(_ id: String, pinned: Bool, at: TimeInterval) -> API.MaintenanceNote {29 API.MaintenanceNote(id: id, authorId: "u", authorDisplayName: "SPB", body: id, pinned: pinned, createdAt: Date(timeIntervalSince1970: at))30 }3132 func testTimeZoneChangeKeepsWallClock() {33 let s = date(2026, 11, 20, 22, 0, tz: toronto)34 let moved = MaintenanceLogic.reinterpret(s, from: toronto, to: paris)35 XCTAssertEqual(moved, date(2026, 11, 20, 22, 0, tz: paris))36 XCTAssertEqual(s.timeIntervalSince(moved), 6 * 3600, accuracy: 1) // Paris is 6 h ahead of Toronto in November3738 var form = WindowForm(day: date(2026, 11, 20, tz: toronto), timeZone: toronto)39 XCTAssertEqual(form.start, s) // default 22:00 on the chosen day40 XCTAssertEqual(form.end.timeIntervalSince(form.start), 3600)41 form.setTimeZone("Europe/Paris")42 XCTAssertEqual(form.timeZoneID, "Europe/Paris")43 XCTAssertEqual(form.start, date(2026, 11, 20, 22, 0, tz: paris))44 XCTAssertEqual(form.end, date(2026, 11, 20, 23, 0, tz: paris))45 form.setTimeZone("Not/AZone") // ignored46 XCTAssertEqual(form.timeZoneID, "Europe/Paris")47 }4849 func testFormValidation() {50 var form = WindowForm(day: date(2026, 11, 20, tz: toronto), timeZone: toronto)51 XCTAssertEqual(Set(form.problems), [.titleMissing, .noNodes])52 form.title = " Update "53 form.nodeIDs = ["n2", "n1"]54 XCTAssertTrue(form.isValid)55 form.end = form.start56 XCTAssertEqual(form.problems, [.endNotAfterStart])57 form.end = form.start.addingTimeInterval(60)58 form.recurrence = .weekly59 form.hasUntil = true60 form.until = form.start.addingTimeInterval(-86_400)61 XCTAssertEqual(form.problems, [.untilBeforeStart])62 form.recurrence = .none // "until" is irrelevant without recurrence63 XCTAssertTrue(form.isValid)64 for p in WindowForm.Problem.allCases { XCTAssertFalse(p.message.hasPrefix("maintenance."), "missing string for \(p)") }65 }6667 func testCreateRequest() {68 var form = WindowForm(day: date(2026, 11, 20, tz: toronto), nodeID: "n9", timeZone: toronto)69 form.title = " Update "70 form.details = " "71 form.nodeIDs.insert("n1")72 form.recurrence = .monthly73 form.hasUntil = false74 let r = form.createRequest()75 XCTAssertEqual(r.title, "Update")76 XCTAssertNil(r.description)77 XCTAssertEqual(r.scope.nodeIds, ["n1", "n9"])78 XCTAssertEqual(r.timezone, "America/Toronto")79 XCTAssertEqual(r.startsAt, date(2026, 11, 20, 22, 0, tz: toronto)) // a UTC instant + the IANA zone80 XCTAssertEqual(r.recurrence, .monthly)81 XCTAssertNil(r.recurrenceUntil)82 XCTAssertEqual(r.setMaintenanceMode, true)83 form.hasUntil = true84 XCTAssertEqual(form.createRequest().recurrenceUntil, form.until)85 form.recurrence = .none86 XCTAssertNil(form.createRequest().recurrenceUntil)87 }8889 func testUpdateRequestSendsOnlyChanges() {90 let s = date(2026, 11, 20, 22, 0, tz: toronto)91 let w = window(start: s, end: s.addingTimeInterval(3600))92 var form = WindowForm(window: w)93 XCTAssertFalse(form.hasChanges(from: w))94 XCTAssertEqual(form.updateRequest(original: w), API.UpdateMaintenanceWindowRequest())95 form.title = "Firmware"96 form.silenceAlerts = true97 form.nodeIDs = ["n1", "n2"]98 let r = form.updateRequest(original: w)99 XCTAssertEqual(r.title, "Firmware")100 XCTAssertEqual(r.silenceAlerts, true)101 XCTAssertEqual(r.scope?.nodeIds, ["n1", "n2"])102 XCTAssertNil(r.startsAt); XCTAssertNil(r.endsAt); XCTAssertNil(r.category); XCTAssertNil(r.setMaintenanceMode); XCTAssertNil(r.timezone)103 XCTAssertTrue(form.hasChanges(from: w))104 // An unknown zone on the server falls back to the Mac's zone in the form.105 var odd = w; odd.timezone = "Mars/Olympus"106 XCTAssertEqual(WindowForm(window: odd).timeZoneID, TimeZone.current.identifier)107 }108109 func testNotesPinnedFirstThenNewest() {110 let sorted = MaintenanceLogic.sorted([note("old", pinned: false, at: 10), note("pin-old", pinned: true, at: 5),111 note("new", pinned: false, at: 30), note("pin-new", pinned: true, at: 20)])112 XCTAssertEqual(sorted.map(\.id), ["pin-new", "pin-old", "new", "old"])113 XCTAssertTrue(MaintenanceLogic.sorted([]).isEmpty)114 }115116 func testRecurrenceSummary() {117 XCTAssertEqual(MaintenanceLogic.recurrenceSummary(.none, until: Date()), API.MaintenanceRecurrence.none.label)118 XCTAssertEqual(MaintenanceLogic.recurrenceSummary(.weekly, until: nil), API.MaintenanceRecurrence.weekly.label)119 let text = MaintenanceLogic.recurrenceSummary(.weekly, until: date(2026, 12, 12, tz: toronto), timeZone: toronto)120 XCTAssertTrue(text.hasPrefix(API.MaintenanceRecurrence.weekly.label))121 XCTAssertTrue(text.contains("12"))122 XCTAssertFalse(text.contains("%"))123 }124125 func testDayExtentClipsMultiDayOccurrences() throws {126 let c = cal(toronto)127 let day = date(2026, 11, 20, tz: toronto)128 let inside = try XCTUnwrap(MaintenanceLogic.dayExtent(start: date(2026, 11, 20, 6, tz: toronto), end: date(2026, 11, 20, 12, tz: toronto), day: day, calendar: c))129 XCTAssertEqual(inside.top, 0.25, accuracy: 0.001)130 XCTAssertEqual(inside.bottom, 0.5, accuracy: 0.001)131 // 22:00 → 02:00 next day: clipped on both days.132 let s = date(2026, 11, 20, 22, tz: toronto), e = date(2026, 11, 21, 2, tz: toronto)133 let first = try XCTUnwrap(MaintenanceLogic.dayExtent(start: s, end: e, day: day, calendar: c))134 XCTAssertEqual(first.top, 22.0 / 24, accuracy: 0.001)135 XCTAssertEqual(first.bottom, 1)136 let second = try XCTUnwrap(MaintenanceLogic.dayExtent(start: s, end: e, day: date(2026, 11, 21, tz: toronto), calendar: c))137 XCTAssertEqual(second.top, 0)138 XCTAssertEqual(second.bottom, 2.0 / 24, accuracy: 0.001)139 XCTAssertNil(MaintenanceLogic.dayExtent(start: s, end: e, day: date(2026, 11, 22, tz: toronto), calendar: c))140 // Ending exactly at midnight does not spill onto the next day.141 XCTAssertNil(MaintenanceLogic.dayExtent(start: s, end: date(2026, 11, 21, tz: toronto), day: date(2026, 11, 21, tz: toronto), calendar: c))142 }143144 func testOverlapColumns() {145 let t = { (h: Double) in Date(timeIntervalSince1970: h * 3600) }146 let cols = MaintenanceLogic.columns(for: [(t(1), t(3)), (t(2), t(4)), (t(3), t(5)), (t(8), t(9))])147 XCTAssertEqual(cols.map(\.column), [0, 1, 0, 0])148 XCTAssertEqual(cols.map(\.count), [2, 2, 2, 1])149 XCTAssertTrue(MaintenanceLogic.columns(for: []).isEmpty)150 }151152 func testIntervalsAndNavigation() throws {153 let c = cal(toronto)154 let anchor = date(2026, 11, 18, 15, tz: toronto)155 let month = try XCTUnwrap(MaintenanceLogic.interval(mode: .month, anchor: anchor, calendar: c, nodeID: "n1"))156 XCTAssertEqual(month.from, date(2026, 11, 1, tz: toronto)) // 1 Nov 2026 is a Sunday157 XCTAssertEqual(month.to, date(2026, 12, 13, tz: toronto)) // six full weeks158 XCTAssertEqual(month.nodeID, "n1")159 let week = try XCTUnwrap(MaintenanceLogic.interval(mode: .week, anchor: anchor, calendar: c, nodeID: nil))160 XCTAssertEqual(week.from, date(2026, 11, 15, tz: toronto))161 XCTAssertEqual(week.to, date(2026, 11, 22, tz: toronto))162 let agenda = try XCTUnwrap(MaintenanceLogic.interval(mode: .agenda, anchor: anchor, calendar: c, nodeID: nil))163 XCTAssertEqual(agenda.from, date(2026, 11, 18, tz: toronto))164 XCTAssertEqual(c.dateComponents([.day], from: agenda.from, to: agenda.to).day, MaintenanceLogic.agendaDays)165 XCTAssertNil(MaintenanceLogic.interval(mode: .notes, anchor: anchor, calendar: c, nodeID: nil))166 XCTAssertEqual(MaintenanceLogic.shift(anchor, mode: .month, by: 1, calendar: c), date(2026, 12, 1, tz: toronto))167 XCTAssertEqual(MaintenanceLogic.shift(date(2026, 1, 31, tz: toronto), mode: .month, by: 1, calendar: c), date(2026, 2, 1, tz: toronto))168 XCTAssertEqual(MaintenanceLogic.shift(anchor, mode: .week, by: -1, calendar: c), date(2026, 11, 11, 15, tz: toronto))169 XCTAssertFalse(MaintenanceLogic.periodTitle(mode: .month, anchor: anchor, calendar: c).isEmpty)170 XCTAssertTrue(MaintenanceLogic.periodTitle(mode: .month, anchor: anchor, calendar: c).contains("2026"))171 }172173 func testDisplayStatusPerOccurrence() {174 let s = date(2026, 11, 20, 22, tz: toronto)175 let recurring = window(start: s, end: s.addingTimeInterval(3600), recurrence: .daily, status: .inProgress)176 let active = API.MaintenanceOccurrence(window: recurring, occurrenceStart: s, occurrenceEnd: s.addingTimeInterval(3600), active: true, noteCount: 0)177 let later = API.MaintenanceOccurrence(window: recurring, occurrenceStart: s.addingTimeInterval(86_400), occurrenceEnd: s.addingTimeInterval(90_000), noteCount: 2)178 XCTAssertEqual(MaintenanceLogic.displayStatus(active), .inProgress)179 XCTAssertEqual(MaintenanceLogic.displayStatus(later), .scheduled) // only the active occurrence is in progress180 let cancelled = API.MaintenanceOccurrence(window: window(start: s, end: s.addingTimeInterval(60), status: .cancelled), occurrenceStart: s,181 occurrenceEnd: s.addingTimeInterval(60), noteCount: 0)182 XCTAssertEqual(MaintenanceLogic.displayStatus(cancelled), .cancelled)183 XCTAssertNotEqual(OccurrenceRef(active).id, OccurrenceRef(later).id)184 XCTAssertEqual(OccurrenceRef(active).id, active.occurrenceID)185 XCTAssertTrue(MaintenanceLogic.summary(of: active, timeZone: toronto).contains("macOS update"))186 }187188 func testTimeRangeAndModes() {189 let s = date(2026, 11, 20, 22, tz: toronto)190 let same = MaintenanceLogic.timeRange(start: s, end: s.addingTimeInterval(1800), timeZone: toronto)191 let across = MaintenanceLogic.timeRange(start: s, end: s.addingTimeInterval(4 * 3600), timeZone: toronto)192 XCTAssertTrue(same.contains("–"))193 XCTAssertGreaterThan(across.count, same.count) // dates are spelled out when the days differ194 XCTAssertEqual(MaintenanceMode.allCases.count, 4)195 for m in MaintenanceMode.allCases { XCTAssertFalse(m.title.hasPrefix("maintenance."), "missing title for \(m)") }196 XCTAssertFalse(MaintenanceMode.notes.isCalendar)197 XCTAssertTrue(NoteTarget().isEmpty)198 XCTAssertFalse(NoteTarget(nodeID: "n").isEmpty)199 }200}201