@@ -24,12 +24,10 @@ import org.robolectric.RuntimeEnvironment
2424import org.robolectric.Shadows.shadowOf
2525import org.robolectric.annotation.Config
2626
27- @RunWith(RobolectricTestRunner ::class )
28- @Config(sdk = [24 , 26 , 33 , 36 ], manifest = Config .NONE )
29- class AgentNotificationsTest {
30- private lateinit var context: Application
31- private lateinit var manager: NotificationManager
32- private lateinit var lifecycle: LifecycleRegistry
27+ abstract class AgentNotificationsFixture {
28+ protected lateinit var context: Application
29+ protected lateinit var manager: NotificationManager
30+ protected lateinit var lifecycle: LifecycleRegistry
3331
3432 @Before
3533 fun setUp () {
@@ -52,7 +50,7 @@ class AgentNotificationsTest {
5250 AgentNotifications .setThreadOnScreen(" /threads/environment/thread" )
5351 }
5452
55- private fun update (alertId : String , active : Boolean ) = mapOf (
53+ protected fun update (alertId : String , active : Boolean ) = mapOf (
5654 " device_id" to " device" ,
5755 " user_id" to " user" ,
5856 " updated_at" to System .currentTimeMillis().toString(),
@@ -66,7 +64,11 @@ class AgentNotificationsTest {
6664 " alert_body" to " Done: Test project" ,
6765 " alert_path" to " /threads/environment/thread" ,
6866 )
67+ }
6968
69+ @RunWith(RobolectricTestRunner ::class )
70+ @Config(sdk = [24 , 26 , 33 , 36 ], manifest = Config .NONE )
71+ class AgentNotificationsTest : AgentNotificationsFixture () {
7072 @Test
7173 fun alertHistoryEvictsOnlyTheOldestEntryAfterCapacity () {
7274 lifecycle.currentState = Lifecycle .State .RESUMED
@@ -118,20 +120,6 @@ class AgentNotificationsTest {
118120 assertTrue(manager.activeNotifications.isEmpty())
119121 }
120122
121- @Test
122- fun backgroundCompletionAlertsAndClearsOngoingActivity () {
123- lifecycle.currentState = Lifecycle .State .RESUMED
124- AgentNotifications .receive(context, update(" running" , true ))
125- lifecycle.currentState = Lifecycle .State .CREATED
126-
127- AgentNotifications .receive(context, update(" completion" , false ))
128-
129- val alert = manager.activeNotifications.single()
130- assertEquals(" t3-agent-alert" , alert.tag)
131- assertEquals(" Test thread" , alert.notification.extras.getString(Notification .EXTRA_TITLE ))
132- assertFalse(alert.notification.flags and Notification .FLAG_ONGOING_EVENT != 0 )
133- }
134-
135123 @Test
136124 fun retryOfForegroundSuppressedAlertDoesNotAppearAfterBackgrounding () {
137125 lifecycle.currentState = Lifecycle .State .RESUMED
@@ -313,21 +301,6 @@ class AgentNotificationsTest {
313301 assertTrue(manager.activeNotifications.isEmpty())
314302 }
315303
316- @Test
317- fun reorderedActivityDoesNotEraseNewerCardOrDropAnIndependentAlert () {
318- val now = System .currentTimeMillis()
319- AgentNotifications .receive(context, update(" new" , true ) + (" updated_at" to now.toString()))
320- AgentNotifications .receive(
321- context,
322- update(" older-alert" , false ) + (" updated_at" to (now - 1000 ).toString())
323- )
324- assertEquals(4 , manager.activeNotifications.size)
325- assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
326- shadowOf(manager).setNotificationsEnabled(false )
327- AgentNotifications .receive(context, update(" revoked-permission" , true ))
328- assertEquals(4 , manager.activeNotifications.size)
329- }
330-
331304 @Test
332305 fun severalThreadsListEveryRowWithItsStatusAndActionsFollowThePriorityThread () {
333306 lifecycle.currentState = Lifecycle .State .RESUMED
@@ -661,3 +634,91 @@ class AgentNotificationsTest {
661634 assertEquals(2 , manager.activeNotifications.size)
662635 }
663636}
637+
638+ @RunWith(RobolectricTestRunner ::class )
639+ @Config(sdk = [24 , 26 , 33 , 36 ], manifest = Config .NONE )
640+ class AgentNotificationsCompletionTest : AgentNotificationsFixture () {
641+ private fun finished (alertId : String , expiresAt : Long ) = update(alertId, false ) + mapOf (
642+ " activity_line_0" to " Done\t Test thread\t Test project" ,
643+ " activity_phase" to " completed" ,
644+ " activity_expires_at" to expiresAt.toString(),
645+ )
646+
647+ @Test
648+ fun backgroundCompletionAlertsAndClearsOngoingActivity () {
649+ lifecycle.currentState = Lifecycle .State .RESUMED
650+ AgentNotifications .receive(context, update(" running" , true ))
651+ lifecycle.currentState = Lifecycle .State .CREATED
652+
653+ val expiresAt = System .currentTimeMillis() + 900000
654+ val completion = finished(" completion" , expiresAt)
655+ AgentNotifications .receive(context, completion)
656+ AgentNotifications .receive(context, completion)
657+ AgentNotifications .receive(context, completion.filterKeys { ! it.startsWith(" alert_" ) })
658+
659+ val alert = manager.activeNotifications.single()
660+ assertEquals(" t3-agent-alert" , alert.tag)
661+ assertEquals(" Test thread" , alert.notification.extras.getString(Notification .EXTRA_TITLE ))
662+ assertFalse(alert.notification.flags and Notification .FLAG_ONGOING_EVENT != 0 )
663+ AgentNotifications .receive(context, update(" next-run" , true ))
664+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
665+
666+ // An alert for another thread, or a card that also lists another result, stays.
667+ AgentNotifications .receive(
668+ context,
669+ finished(" other-thread" , expiresAt + 60000 ) + (" alert_path" to " /threads/other/thread" )
670+ )
671+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
672+ AgentNotifications .receive(
673+ context,
674+ finished(" with-muted-result" , expiresAt + 120000 ) +
675+ (" activity_line_1" to " Done\t Muted thread\t Other project" )
676+ )
677+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
678+ }
679+
680+ @Test
681+ @Config(sdk = [26 , 33 , 36 ])
682+ fun blockedAlertChannelKeepsTheFinishedCard () {
683+ manager.getNotificationChannel(" agent-alerts" ).importance = NotificationManager .IMPORTANCE_NONE
684+ AgentNotifications .receive(
685+ context,
686+ finished(" completion" , System .currentTimeMillis() + 900000 )
687+ )
688+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
689+ }
690+
691+ @Test
692+ fun reorderedActivityDoesNotEraseNewerCardOrDropAnIndependentAlert () {
693+ val now = System .currentTimeMillis()
694+ AgentNotifications .receive(context, update(" new" , true ) + (" updated_at" to now.toString()))
695+ AgentNotifications .receive(
696+ context,
697+ finished(" older-alert" , now + 900000 ) + (" updated_at" to (now - 1000 ).toString())
698+ )
699+ assertEquals(4 , manager.activeNotifications.size)
700+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
701+ // A newer replay of a finish can show its card before the original alert lands.
702+ val finish = finished(" finish" , now + 960000 )
703+ AgentNotifications .receive(
704+ context,
705+ finish.filterKeys { ! it.startsWith(" alert_" ) } + (" updated_at" to (now + 2000 ).toString())
706+ )
707+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
708+ AgentNotifications .receive(context, finish + (" updated_at" to (now + 1000 ).toString()))
709+ assertTrue(manager.activeNotifications.none { it.tag == " t3-agent-activity" })
710+ assertTrue(manager.activeNotifications.any { it.id == " finish" .hashCode() })
711+ AgentNotifications .receive(
712+ context,
713+ finished(" previous-run" , now + 900000 ) + (" updated_at" to now.toString())
714+ )
715+ AgentNotifications .receive(
716+ context,
717+ finish.filterKeys { ! it.startsWith(" alert_" ) } + (" updated_at" to (now + 3000 ).toString())
718+ )
719+ assertTrue(manager.activeNotifications.none { it.tag == " t3-agent-activity" })
720+ shadowOf(manager).setNotificationsEnabled(false )
721+ AgentNotifications .receive(context, update(" revoked-permission" , true ))
722+ assertEquals(5 , manager.activeNotifications.size)
723+ }
724+ }
0 commit comments