@@ -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,13 +64,11 @@ class AgentNotificationsTest {
6664 " alert_body" to " Done: Test project" ,
6765 " alert_path" to " /threads/environment/thread" ,
6866 )
67+ }
6968
70- private fun finished (alertId : String , expiresAt : Long ) = update(alertId, false ) + mapOf (
71- " activity_line_0" to " Done\t Test thread\t Test project" ,
72- " activity_phase" to " completed" ,
73- " activity_expires_at" to expiresAt.toString(),
74- )
75-
69+ @RunWith(RobolectricTestRunner ::class )
70+ @Config(sdk = [24 , 26 , 33 , 36 ], manifest = Config .NONE )
71+ class AgentNotificationsTest : AgentNotificationsFixture () {
7672 @Test
7773 fun alertHistoryEvictsOnlyTheOldestEntryAfterCapacity () {
7874 lifecycle.currentState = Lifecycle .State .RESUMED
@@ -124,50 +120,6 @@ class AgentNotificationsTest {
124120 assertTrue(manager.activeNotifications.isEmpty())
125121 }
126122
127- @Test
128- fun backgroundCompletionAlertsAndClearsOngoingActivity () {
129- lifecycle.currentState = Lifecycle .State .RESUMED
130- AgentNotifications .receive(context, update(" running" , true ))
131- lifecycle.currentState = Lifecycle .State .CREATED
132-
133- val expiresAt = System .currentTimeMillis() + 900000
134- val completion = finished(" completion" , expiresAt)
135- AgentNotifications .receive(context, completion)
136- AgentNotifications .receive(context, completion)
137- AgentNotifications .receive(context, completion.filterKeys { ! it.startsWith(" alert_" ) })
138-
139- val alert = manager.activeNotifications.single()
140- assertEquals(" t3-agent-alert" , alert.tag)
141- assertEquals(" Test thread" , alert.notification.extras.getString(Notification .EXTRA_TITLE ))
142- assertFalse(alert.notification.flags and Notification .FLAG_ONGOING_EVENT != 0 )
143- AgentNotifications .receive(context, update(" next-run" , true ))
144- assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
145-
146- // An alert for another thread, or a card that also lists another result, stays.
147- AgentNotifications .receive(
148- context,
149- finished(" other-thread" , expiresAt + 60000 ) + (" alert_path" to " /threads/other/thread" )
150- )
151- assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
152- AgentNotifications .receive(
153- context,
154- finished(" with-muted-result" , expiresAt + 120000 ) +
155- (" activity_line_1" to " Done\t Muted thread\t Other project" )
156- )
157- assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
158- }
159-
160- @Test
161- @Config(sdk = [26 , 33 , 36 ])
162- fun blockedAlertChannelKeepsTheFinishedCard () {
163- manager.getNotificationChannel(" agent-alerts" ).importance = NotificationManager .IMPORTANCE_NONE
164- AgentNotifications .receive(
165- context,
166- finished(" completion" , System .currentTimeMillis() + 900000 )
167- )
168- assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
169- }
170-
171123 @Test
172124 fun retryOfForegroundSuppressedAlertDoesNotAppearAfterBackgrounding () {
173125 lifecycle.currentState = Lifecycle .State .RESUMED
@@ -349,40 +301,6 @@ class AgentNotificationsTest {
349301 assertTrue(manager.activeNotifications.isEmpty())
350302 }
351303
352- @Test
353- fun reorderedActivityDoesNotEraseNewerCardOrDropAnIndependentAlert () {
354- val now = System .currentTimeMillis()
355- AgentNotifications .receive(context, update(" new" , true ) + (" updated_at" to now.toString()))
356- AgentNotifications .receive(
357- context,
358- finished(" older-alert" , now + 900000 ) + (" updated_at" to (now - 1000 ).toString())
359- )
360- assertEquals(4 , manager.activeNotifications.size)
361- assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
362- // A newer replay of a finish can show its card before the original alert lands.
363- val finish = finished(" finish" , now + 960000 )
364- AgentNotifications .receive(
365- context,
366- finish.filterKeys { ! it.startsWith(" alert_" ) } + (" updated_at" to (now + 2000 ).toString())
367- )
368- assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
369- AgentNotifications .receive(context, finish + (" updated_at" to (now + 1000 ).toString()))
370- assertTrue(manager.activeNotifications.none { it.tag == " t3-agent-activity" })
371- assertTrue(manager.activeNotifications.any { it.id == " finish" .hashCode() })
372- AgentNotifications .receive(
373- context,
374- finished(" previous-run" , now + 900000 ) + (" updated_at" to now.toString())
375- )
376- AgentNotifications .receive(
377- context,
378- finish.filterKeys { ! it.startsWith(" alert_" ) } + (" updated_at" to (now + 3000 ).toString())
379- )
380- assertTrue(manager.activeNotifications.none { it.tag == " t3-agent-activity" })
381- shadowOf(manager).setNotificationsEnabled(false )
382- AgentNotifications .receive(context, update(" revoked-permission" , true ))
383- assertEquals(5 , manager.activeNotifications.size)
384- }
385-
386304 @Test
387305 fun severalThreadsListEveryRowWithItsStatusAndActionsFollowThePriorityThread () {
388306 lifecycle.currentState = Lifecycle .State .RESUMED
@@ -716,3 +634,91 @@ class AgentNotificationsTest {
716634 assertEquals(2 , manager.activeNotifications.size)
717635 }
718636}
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