Add filter rule tree with relative due ranges (#172)

This commit is contained in:
makiolaj committed 2026-11-26 16:22:32 +01:00
1 parent 8a1e16c5f9
commit 697e2e8554
4 files changed
+319 -2

No files matched your search

@@ -0,0 +1,166 @@
package de.jeanlucmakiola.agendula.domain
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneId
import kotlin.time.Instant
class RuleTest {
private val zone = ZoneId.of("Europe/Berlin")
private fun clockOn(day: LocalDate, weekStart: DayOfWeek = DayOfWeek.MONDAY, zone: ZoneId = this.zone) =
RuleClock(startOfDay(day, zone), startOfDay(day.plusDays(1), zone), zone, weekStart)
private fun dueOn(day: LocalDate) = testTask(due = allDayInstantOf(day), isAllDay = true)
private fun dueAt(day: LocalDate, time: LocalTime, zone: ZoneId = this.zone) =
testTask(due = Instant.fromEpochMilliseconds(day.atTime(time).atZone(zone).toInstant().toEpochMilli()))
private fun Rule.matches(task: Task, day: LocalDate, weekStart: DayOfWeek = DayOfWeek.MONDAY) =
matches(task, clockOn(day, weekStart))
private fun DueSpec.Range.daysIn(today: LocalDate, weekStart: DayOfWeek = DayOfWeek.MONDAY): List<LocalDate> =
(-40L..40L).map { today.plusDays(it) }.filter { Rule.Due(this).matches(dueOn(it), today, weekStart) }
@Test
fun `groups combine their children`() {
val work = Rule.HasTag("work")
val high = Rule.MinPriority(Priority.HIGH)
val task = testTask(tags = listOf("work"), priority = Priority.LOW)
val day = LocalDate.of(2026, 11, 26)
assertThat(Rule.All(listOf(work, high)).matches(task, day)).isFalse()
assertThat(Rule.Any(listOf(work, high)).matches(task, day)).isTrue()
assertThat(Rule.Not(work).matches(task, day)).isFalse()
assertThat(Rule.Not(high).matches(task, day)).isTrue()
assertThat(Rule.All(emptyList()).matches(task, day)).isTrue()
assertThat(Rule.Any(emptyList()).matches(task, day)).isFalse()
}
@Test
fun `next actions are a list or a tag`() {
val day = LocalDate.of(2026, 11, 26)
val rule = Rule.All(
listOf(
Rule.Any(listOf(Rule.InList(setOf(7L)), Rule.HasTag("next"))),
Rule.Not(Rule.Any(listOf(Rule.HasTag("someday"), Rule.InList(setOf(9L))))),
),
)
assertThat(rule.matches(testTask(listId = 7), day)).isTrue()
assertThat(rule.matches(testTask(listId = 1, tags = listOf("next")), day)).isTrue()
assertThat(rule.matches(testTask(listId = 1), day)).isFalse()
assertThat(rule.matches(testTask(listId = 7, tags = listOf("someday")), day)).isFalse()
assertThat(rule.matches(testTask(listId = 9, tags = listOf("next")), day)).isFalse()
assertThat(Rule.Not(Rule.Not(Rule.InList(setOf(7L)))).matches(testTask(listId = 7), day)).isTrue()
}
@Test
fun `tags compare exactly and other stores' lists never match here`() {
val day = LocalDate.of(2026, 11, 26)
assertThat(Rule.HasTag("work").matches(testTask(tags = listOf("Work")), day)).isFalse()
assertThat(Rule.InList(emptySet(), setOf("EXTERNAL:1")).matches(testTask(listId = 1), day)).isFalse()
}
@Test
fun `due windows read as before`() {
val day = LocalDate.of(2026, 11, 26)
fun window(w: DueWindow) = Rule.Due(DueSpec.Window(w))
assertThat(window(DueWindow.TODAY).matches(dueAt(day, LocalTime.of(23, 59)), day)).isTrue()
assertThat(window(DueWindow.OVERDUE).matches(dueOn(day.minusDays(1)), day)).isTrue()
assertThat(window(DueWindow.UPCOMING).matches(dueOn(day.plusDays(1)), day)).isTrue()
assertThat(window(DueWindow.NO_DATE).matches(testTask(), day)).isTrue()
assertThat(window(DueWindow.NO_DATE).matches(dueOn(day), day)).isFalse()
}
@Test
fun `today is the day itself, timed or not`() {
val day = LocalDate.of(2026, 11, 26)
val today = Rule.Due(DueSpec.Today)
assertThat(DueSpec.Today.daysIn(day)).containsExactly(day)
assertThat(today.matches(dueAt(day, LocalTime.MIDNIGHT), day)).isTrue()
assertThat(today.matches(dueAt(day, LocalTime.of(23, 59)), day)).isTrue()
assertThat(today.matches(dueAt(day.plusDays(1), LocalTime.MIDNIGHT), day)).isFalse()
assertThat(today.matches(testTask(), day)).isFalse()
}
@Test
fun `this week starts on the week start`() {
// A Thursday.
val thursday = LocalDate.of(2026, 11, 26)
val monday = LocalDate.of(2026, 11, 23)
val sunday = LocalDate.of(2026, 11, 22)
val saturday = LocalDate.of(2026, 11, 21)
assertThat(DueSpec.ThisWeek.daysIn(thursday, DayOfWeek.MONDAY)).isEqualTo((0L..6L).map { monday.plusDays(it) })
assertThat(DueSpec.ThisWeek.daysIn(thursday, DayOfWeek.SUNDAY)).isEqualTo((0L..6L).map { sunday.plusDays(it) })
assertThat(DueSpec.ThisWeek.daysIn(thursday, DayOfWeek.SATURDAY)).isEqualTo((0L..6L).map { saturday.plusDays(it) })
// On the week start itself, and on the last day of the week.
assertThat(DueSpec.ThisWeek.daysIn(monday, DayOfWeek.MONDAY).first()).isEqualTo(monday)
assertThat(DueSpec.ThisWeek.daysIn(sunday, DayOfWeek.MONDAY).first()).isEqualTo(LocalDate.of(2026, 11, 16))
assertThat(DueSpec.ThisWeek.daysIn(sunday, DayOfWeek.SUNDAY).first()).isEqualTo(sunday)
}
@Test
fun `this week crosses the year`() {
val newYear = LocalDate.of(2027, 1, 1)
assertThat(DueSpec.ThisWeek.daysIn(newYear, DayOfWeek.MONDAY))
.isEqualTo((0L..6L).map { LocalDate.of(2026, 12, 28).plusDays(it) })
}
@Test
fun `this month is the calendar month`() {
assertThat(DueSpec.ThisMonth.daysIn(LocalDate.of(2026, 12, 31)))
.isEqualTo((1..31).map { LocalDate.of(2026, 12, it) })
assertThat(DueSpec.ThisMonth.daysIn(LocalDate.of(2027, 1, 1)))
.isEqualTo((1..31).map { LocalDate.of(2027, 1, it) })
assertThat(DueSpec.ThisMonth.daysIn(LocalDate.of(2028, 2, 29)))
.isEqualTo((1..29).map { LocalDate.of(2028, 2, it) })
assertThat(DueSpec.ThisMonth.daysIn(LocalDate.of(2027, 2, 15)))
.isEqualTo((1..28).map { LocalDate.of(2027, 2, it) })
}
@Test
fun `next days run from today and reach as far as upcoming does`() {
val day = LocalDate.of(2026, 12, 29)
assertThat(DueSpec.NextDays(0).daysIn(day)).containsExactly(day)
assertThat(DueSpec.NextDays(7).daysIn(day)).isEqualTo((0L..7L).map { day.plusDays(it) })
val now = Instant.fromEpochMilliseconds(day.atTime(12, 0).atZone(zone).toInstant().toEpochMilli())
assertThat(DueSpec.NextDays(7).days(day, DayOfWeek.MONDAY).second)
.isEqualTo(UpcomingRange.WEEK.lastDay(day)!!.plusDays(1))
assertThat(startOfDay(DueSpec.NextDays(7).days(day, DayOfWeek.MONDAY).second, zone))
.isEqualTo(UpcomingRange.WEEK.end(now, zone))
}
@Test
fun `an all-day due is read as its date west of utc`() {
val west = ZoneId.of("America/New_York")
val day = LocalDate.of(2026, 11, 30)
val clock = clockOn(day, zone = west)
assertThat(Rule.Due(DueSpec.Today).matches(dueOn(day), clock)).isTrue()
assertThat(Rule.Due(DueSpec.ThisMonth).matches(dueOn(LocalDate.of(2026, 12, 1)), clock)).isFalse()
assertThat(Rule.Due(DueSpec.ThisMonth).matches(dueAt(day, LocalTime.of(23, 30), west), clock)).isTrue()
}
@Test
fun `a list condition losing its lists drops out`() {
val gone: (Long) -> Boolean = { it == 1L }
assertThat(Rule.InList(setOf(1L, 2L)).withoutLists(gone)).isEqualTo(Rule.InList(setOf(2L)))
assertThat(Rule.InList(setOf(1L)).withoutLists(gone)).isNull()
assertThat(Rule.InList(setOf(1L), setOf("EXTERNAL:1")).withoutLists(gone))
.isEqualTo(Rule.InList(emptySet(), setOf("EXTERNAL:1")))
val rule = Rule.All(
listOf(
Rule.HasTag("a"),
Rule.Not(Rule.InList(setOf(1L))),
Rule.Any(listOf(Rule.InList(setOf(1L)), Rule.HasTag("b"))),
Rule.Any(listOf(Rule.InList(setOf(1L)))),
),
)
assertThat(rule.withoutLists(gone)).isEqualTo(Rule.All(listOf(Rule.HasTag("a"), Rule.Any(listOf(Rule.HasTag("b"))))))
assertThat(Rule.All(listOf(Rule.InList(setOf(1L)))).withoutLists(gone)).isNull()
assertThat(Rule.Everything.withoutLists(gone)).isEqualTo(Rule.Everything)
assertThat(rule.withoutLists { false }).isEqualTo(rule)
}
}