|
| 1 | +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package software.aws.toolkits.jetbrains.core.notifications |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 7 | +import org.junit.jupiter.api.Assertions.assertFalse |
| 8 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 9 | +import org.junit.jupiter.api.BeforeEach |
| 10 | +import org.junit.jupiter.api.Test |
| 11 | +import java.time.Instant |
| 12 | +import java.time.temporal.ChronoUnit |
| 13 | + |
| 14 | +class NotificationDismissalStateTest { |
| 15 | + private lateinit var state: NotificationDismissalState |
| 16 | + |
| 17 | + @BeforeEach |
| 18 | + fun setUp() { |
| 19 | + state = NotificationDismissalState() |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `notifications less than 2 months old are not removed`() { |
| 24 | + val recentNotification = DismissedNotification( |
| 25 | + id = "recent-notification", |
| 26 | + dismissedAt = Instant.now().minus(30, ChronoUnit.DAYS).toEpochMilli().toString() |
| 27 | + ) |
| 28 | + |
| 29 | + state.loadState(NotificationDismissalConfiguration(mutableSetOf(recentNotification))) |
| 30 | + |
| 31 | + val persistedState = state.getState() |
| 32 | + |
| 33 | + assertEquals(1, persistedState.dismissedNotifications.size) |
| 34 | + assertTrue(persistedState.dismissedNotifications.any { it.id == "recent-notification" }) |
| 35 | + assertTrue(state.isDismissed("recent-notification")) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `notifications older than 2 months are removed`() { |
| 40 | + val oldNotification = DismissedNotification( |
| 41 | + id = "old-notification", |
| 42 | + dismissedAt = Instant.now().minus(61, ChronoUnit.DAYS).toEpochMilli().toString() |
| 43 | + ) |
| 44 | + |
| 45 | + state.loadState(NotificationDismissalConfiguration(mutableSetOf(oldNotification))) |
| 46 | + |
| 47 | + val persistedState = state.getState() |
| 48 | + |
| 49 | + assertEquals(0, persistedState.dismissedNotifications.size) |
| 50 | + assertFalse(state.isDismissed("old-notification")) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `mixed age notifications are handled correctly`() { |
| 55 | + val recentNotification = DismissedNotification( |
| 56 | + id = "recent-notification", |
| 57 | + dismissedAt = Instant.now().minus(30, ChronoUnit.DAYS).toEpochMilli().toString() |
| 58 | + ) |
| 59 | + val oldNotification = DismissedNotification( |
| 60 | + id = "old-notification", |
| 61 | + dismissedAt = Instant.now().minus(61, ChronoUnit.DAYS).toEpochMilli().toString() |
| 62 | + ) |
| 63 | + |
| 64 | + state.loadState( |
| 65 | + NotificationDismissalConfiguration( |
| 66 | + mutableSetOf(recentNotification, oldNotification) |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + val persistedState = state.getState() |
| 71 | + |
| 72 | + assertEquals(1, persistedState.dismissedNotifications.size) |
| 73 | + assertTrue(state.isDismissed("recent-notification")) |
| 74 | + assertFalse(state.isDismissed("old-notification")) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `dismissing new notification retains it`() { |
| 79 | + state.dismissNotification("new-notification") |
| 80 | + |
| 81 | + assertTrue(state.isDismissed("new-notification")) |
| 82 | + } |
| 83 | +} |
0 commit comments