package kr.co.itosm.dutycalendar import org.junit.Assert.* import org.junit.Test import java.time.LocalDate import java.time.YearMonth /** * 반복 근무 계산에서 틀리기 쉬운 기준일 이전과 연말 및 윤년 경계를 확인한다. * 안드로이드 화면 없이 순수 날짜 계산을 검사해 기기 시간이나 네트워크 상태의 영향을 받지 않게 한다. * 화면 배치와 위젯 추가 및 공휴일 통신은 가상 기기에서 별도로 확인한다. */ class ScheduleTest { private val anchor = LocalDate.of(2026, 11, 20) @Test fun twoDayCycle() { val schedule = Schedule(anchor, Schedule.preset(2)) assertEquals(listOf(Shift.DUTY, Shift.OFF, Shift.DUTY, Shift.OFF), (0L..3L).map { schedule.shiftOn(anchor.plusDays(it)) }) } @Test fun threeDayCycle() { val schedule = Schedule(anchor, Schedule.preset(3)) assertEquals(Shift.REGULAR, schedule.shiftOn(anchor.plusDays(2))) assertEquals(Shift.DUTY, schedule.shiftOn(anchor.plusDays(3))) } @Test fun datesBeforeAnchorUseSameCycle() { val schedule = Schedule(anchor, Schedule.preset(3)) assertEquals(Shift.REGULAR, schedule.shiftOn(anchor.minusDays(1))) assertEquals(Shift.OFF, schedule.shiftOn(anchor.minusDays(2))) assertEquals(Shift.DUTY, schedule.shiftOn(anchor.minusDays(3))) } @Test fun customPatternIsPreservedAcrossYearBoundary() { val schedule = Schedule(LocalDate.of(2026, 12, 30), listOf(Shift.OFF, Shift.REGULAR, Shift.DUTY, Shift.OFF)) assertEquals(Shift.DUTY, schedule.shiftOn(LocalDate.of(2027, 1, 1))) } @Test fun leapDayAndMarchAreConsecutive() { val schedule = Schedule(LocalDate.of(2028, 2, 28), Schedule.preset(3)) assertEquals(Shift.OFF, schedule.shiftOn(LocalDate.of(2028, 2, 29))) assertEquals(Shift.REGULAR, schedule.shiftOn(LocalDate.of(2028, 3, 1))) } @Test fun monthGridIncludesAllDaysAndStartsSunday() { val days = calendarDays(YearMonth.of(2026, 8)) assertEquals(42, days.size) assertEquals(java.time.DayOfWeek.SUNDAY, days.first().dayOfWeek) assertTrue(days.contains(LocalDate.of(2026, 8, 31))) } @Test(expected = IllegalArgumentException::class) fun emptyCycleRejected() { Schedule(anchor, emptyList()) } @Test fun mondayGridHandlesSundayMonthStartAndYearBoundary() { assertEquals(LocalDate.of(2026,10,26),calendarDays(YearMonth.of(2026,11),1).first()) assertEquals(LocalDate.of(2026,11,1),calendarDays(YearMonth.of(2026,11),0).first()) assertEquals(LocalDate.of(2026,12,28),calendarDays(YearMonth.of(2027,1),1).first()) assertEquals(LocalDate.of(2028,2,29),calendarDays(YearMonth.of(2028,2),1).single{it==LocalDate.of(2028,2,29)}) assertEquals(LocalDate.of(2026,11,16),startOfWeek(LocalDate.of(2026,11,22),1)) assertEquals(listOf(1,2,3,4,5,6,0),calendarWeekdays(1)) } @Test fun changingWeekStartOnlyChangesLayout() { val sunday=Schedule(anchor,Schedule.preset(3),5,true,true,true) val monday=sunday.copy(weekStart=1) for(offset in -400L..400L) { val date=anchor.plusDays(offset) assertEquals(sunday.shiftOn(date),monday.shiftOn(date)) assertEquals(sunday.dateColor(date,true),monday.dateColor(date,true)) } for(year in 2024..2028)for(month in 1..12)for(start in 0..1) { val ym=YearMonth.of(year,month);val dates=calendarDays(ym,start) assertEquals(42,dates.size) assertEquals(ym.lengthOfMonth(),dates.count{YearMonth.from(it)==ym}) assertEquals(if(start==1)java.time.DayOfWeek.MONDAY else java.time.DayOfWeek.SUNDAY,dates.first().dayOfWeek) assertEquals(41L,java.time.temporal.ChronoUnit.DAYS.between(dates.first(),dates.last())) } } @Test(expected = IllegalArgumentException::class) fun invalidWeekStartRejected() { Schedule(anchor,Schedule.preset(2),weekStart=2) } @Test fun longestCycleWorks() { val schedule = Schedule(anchor, Schedule.preset(366)) assertEquals(Shift.REGULAR, schedule.shiftOn(anchor.plusDays(365))) assertEquals(Shift.DUTY, schedule.shiftOn(anchor.plusDays(366))) } @Test fun dutyNumberKeepsDutyOffAndFillsRemainingDays() { val schedule = Schedule(anchor, Schedule.preset(5)) assertEquals(listOf(Shift.DUTY, Shift.OFF, Shift.REGULAR, Shift.REGULAR, Shift.REGULAR, Shift.DUTY), (0L..5).map { schedule.shiftOn(anchor.plusDays(it)) }) } @Test fun nightOffAndRestRemainDistinctAcrossMonthBoundary() { val schedule = Schedule(LocalDate.of(2026, 11, 29), listOf(Shift.DAY, Shift.NIGHT, Shift.OFF, Shift.REST)) assertEquals(Shift.NIGHT, schedule.shiftOn(LocalDate.of(2026, 11, 30))) assertEquals(Shift.OFF, schedule.shiftOn(LocalDate.of(2026, 12, 1))) assertEquals(Shift.REST, schedule.shiftOn(LocalDate.of(2026, 12, 2))) assertEquals(Shift.DAY, schedule.shiftOn(LocalDate.of(2026, 12, 3))) assertFalse(Shift.REST.working) assertTrue(Shift.EVENING.working) } @Test(expected = IllegalArgumentException::class) fun oversizedCycleRejected() { Schedule(anchor, List(367) { Shift.DAY }) } @Test fun fiveDaySaturdayKeepsDutyAndRestsOtherShifts() { val saturday = LocalDate.of(2026, 11, 21) Shift.entries.forEach { shift -> assertEquals(if (shift == Shift.DUTY) Shift.DUTY else Shift.REST, Schedule(saturday, listOf(shift), 5).shiftOn(saturday)) } } @Test fun saturdayRestDoesNotMoveFollowingRotation() { val schedule = Schedule(anchor, listOf(Shift.DUTY, Shift.REGULAR, Shift.NIGHT), 5) assertEquals(Shift.REST, schedule.shiftOn(anchor.plusDays(1))) assertEquals(Shift.NIGHT, schedule.shiftOn(anchor.plusDays(2))) assertEquals(Shift.DUTY, schedule.shiftOn(anchor.plusDays(3))) } @Test fun sixDayAndLegacySaturdayKeepOriginalDuty() { val saturday = LocalDate.of(2026, 11, 21) Shift.entries.forEach { shift -> assertEquals(shift, Schedule(saturday, listOf(shift)).shiftOn(saturday)) assertEquals(shift, Schedule(saturday, listOf(shift), 6).shiftOn(saturday)) } } @Test fun saturdayColorsFollowWorkWeekAndHolidayWins() { val saturday = LocalDate.of(2026, 11, 21) val five = Schedule(anchor, Schedule.preset(3), 5) val six = five.copy(workWeek = 6) assertEquals(0xFFBD5163.toInt(), five.dateColor(saturday)) assertEquals(0xFF365FAD.toInt(), six.dateColor(saturday)) assertEquals(0xFFBD5163.toInt(), six.dateColor(saturday, holiday = true)) } @Test(expected = IllegalArgumentException::class) fun invalidWorkWeekRejected() { Schedule(anchor, Schedule.preset(3), 7) } @Test fun weekdayDutyChangesOnlyDateColor() { val duty = Schedule(anchor, listOf(Shift.DUTY), 5, true) listOf(LocalDate.of(2026, 11, 21), LocalDate.of(2026, 11, 22), LocalDate.of(2026, 12, 25)).forEach { assertEquals(0xFF1D2939.toInt(), duty.dateColor(it, true)) assertEquals(Shift.DUTY, duty.shiftOn(it)) assertEquals(0xFFBD5163.toInt(), duty.copy(dutyAsWeekday = false).dateColor(it, true)) } val sunday = LocalDate.of(2026, 11, 22) assertEquals(0xFFBD5163.toInt(), duty.copy(pattern = listOf(Shift.OFF)).dateColor(sunday)) } @Test fun offColorOptionDoesNotChangeRotationOrOtherShifts() { val monday = LocalDate.of(2026, 11, 23) val off = Schedule(monday, listOf(Shift.OFF), offAsHoliday = true) assertEquals(0xFFBD5163.toInt(), off.dateColor(monday)) assertEquals(0xFFBD5163.toInt(), off.shiftColor(monday)) assertEquals(Shift.OFF, off.shiftOn(monday)) assertEquals(Shift.OFF.color, off.copy(offAsHoliday = false).shiftColor(monday)) assertEquals(Shift.REGULAR.color, off.copy(pattern = listOf(Shift.REGULAR)).shiftColor(monday)) assertEquals(0xFF1D2939.toInt(), off.copy(pattern = listOf(Shift.DUTY), dutyAsWeekday = true).dateColor(monday, true)) } }