@@ -49,6 +49,8 @@ class AgentNotificationsTest {
4949 )
5050 AgentNotifications .clear(context)
5151 AgentNotifications .configure(context, " device" , " user" , " t3code-dev" , true )
52+ // The fixtures alert for this thread; a resumed app is showing it.
53+ AgentNotifications .setThreadOnScreen(" /threads/environment/thread" )
5254 }
5355
5456 private fun update (alertId : String , active : Boolean ) = mapOf (
@@ -60,6 +62,7 @@ class AgentNotificationsTest {
6062 " activity_body" to " Test thread · Working" ,
6163 " activity_path" to " /threads/environment/thread" ,
6264 " alert_id" to alertId,
65+ " alert_group" to " environment/thread" ,
6366 " alert_title" to " Test thread" ,
6467 " alert_body" to " Done: Test project" ,
6568 " alert_path" to " /threads/environment/thread" ,
@@ -76,6 +79,12 @@ class AgentNotificationsTest {
7679 assertEquals(" alert-0" .hashCode(), manager.activeNotifications.single().id)
7780 }
7881
82+ @Test
83+ fun alertsStackByThreadGroup () {
84+ AgentNotifications .receive(context, update(" grouped" , false ))
85+ assertEquals(" environment/thread" , manager.activeNotifications.single().notification.group)
86+ }
87+
7988 @Test
8089 fun missingLauncherDoesNotDiscardTheAlert () {
8190 shadowOf(context.packageManager).removeActivity(ComponentName (context, Activity ::class .java))
@@ -86,6 +95,14 @@ class AgentNotificationsTest {
8695 )
8796 }
8897
98+ @Test
99+ fun foregroundAlertsForThreadsThatAreNotOnScreen () {
100+ lifecycle.currentState = Lifecycle .State .RESUMED
101+ AgentNotifications .setThreadOnScreen(" /threads/environment/other" )
102+ AgentNotifications .receive(context, update(" elsewhere" , false ))
103+ assertEquals(" t3-agent-alert" , manager.activeNotifications.single().tag)
104+ }
105+
89106 @Test
90107 fun foregroundSuppressesAlertsWhileOngoingActivityStillUpdatesAndClears () {
91108 lifecycle.currentState = Lifecycle .State .RESUMED
@@ -142,6 +159,22 @@ class AgentNotificationsTest {
142159 assertEquals(" background-completion" .hashCode(), manager.activeNotifications.single().id)
143160 }
144161
162+ @Test
163+ fun repeatedThreadAlertsHaveOneSilentSummaryAndClearTogether () {
164+ AgentNotifications .receive(context, update(" first" , false ) + (" alert_group" to " thread-group" ))
165+ AgentNotifications .receive(context, update(" second" , false ) + (" alert_group" to " thread-group" ))
166+
167+ val summary = manager.activeNotifications.single {
168+ it.notification.flags and Notification .FLAG_GROUP_SUMMARY != 0
169+ }
170+ assertEquals(" thread-group" , summary.notification.group)
171+ assertEquals(3 , manager.activeNotifications.size)
172+ assertEquals(null , summary.notification.sound)
173+
174+ AgentNotifications .clear(context)
175+ assertTrue(manager.activeNotifications.isEmpty())
176+ }
177+
145178 @Test
146179 fun groupedAlertDisplaysEveryThreadAndRetriesStaySilent () {
147180 val titles = (1 .. 5 ).map { " Thread $it " + " x" .repeat(111 ) }.joinToString(" , " )
@@ -164,18 +197,21 @@ class AgentNotificationsTest {
164197 }
165198
166199 @Test
167- fun foregroundSuppressedGroupCannotAppearOnBackgroundRetry () {
200+ fun foregroundGroupForOtherThreadsAlertsOnceAcrossBackgroundRetry () {
168201 val grouped = update(" group-attention" , true ) + mapOf (
169202 " alert_title" to " 2 agents need attention" ,
170203 " alert_body" to " First thread, Second thread" ,
171204 " alert_path" to " /" ,
172205 )
173206 lifecycle.currentState = Lifecycle .State .RESUMED
174207 AgentNotifications .receive(context, grouped)
208+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-alert" })
175209 lifecycle.currentState = Lifecycle .State .CREATED
176210 AgentNotifications .receive(context, grouped)
177211
178- assertEquals(" t3-agent-activity" , manager.activeNotifications.single().tag)
212+ assertEquals(2 , manager.activeNotifications.size)
213+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
214+ assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-alert" })
179215 }
180216
181217 @Test
@@ -286,11 +322,11 @@ class AgentNotificationsTest {
286322 context,
287323 update(" older-alert" , false ) + (" updated_at" to (now - 1000 ).toString())
288324 )
289- assertEquals(3 , manager.activeNotifications.size)
325+ assertEquals(4 , manager.activeNotifications.size)
290326 assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
291327 shadowOf(manager).setNotificationsEnabled(false )
292328 AgentNotifications .receive(context, update(" revoked-permission" , true ))
293- assertEquals(3 , manager.activeNotifications.size)
329+ assertEquals(4 , manager.activeNotifications.size)
294330 }
295331
296332 @Test
@@ -503,7 +539,8 @@ class AgentNotificationsTest {
503539 AgentNotifications .expire(context, expiresAt + 60_000 )
504540 assertEquals(1 , manager.activeNotifications.count { it.tag == " t3-agent-activity" })
505541 AgentNotifications .expire(context, expiresAt + 2 * 60 * 60 * 1000L )
506- assertTrue(manager.activeNotifications.all { it.tag == " t3-agent-alert" })
542+ assertEquals(2 , manager.activeNotifications.count { it.tag == " t3-agent-alert" })
543+ assertTrue(manager.activeNotifications.none { it.tag == " t3-agent-activity" })
507544 assertTrue(alarms.scheduledAlarms.isEmpty())
508545 }
509546
0 commit comments