Skip to content

Commit aea673e

Browse files
PR Batch Testerclaude
andcommitted
refactor(mobile): keep Android notification code under detekt limits
Factor the covered-terminal decision out of updateActivity so its complexity stays under the threshold, and split the finished-card tests into AgentNotificationsCompletionTest on a shared fixture so AgentNotificationsTest stays under LargeClass. No behavior or assertion changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 267fe90 commit aea673e

2 files changed

Lines changed: 107 additions & 96 deletions

File tree

‎apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,13 @@ object AgentNotifications {
229229
val terminal = singleTerminal(data)
230230
// Only a one-row card on the alert's own thread is known to show nothing more
231231
// than the alert. Grouped alerts and cards with other rows stay visible.
232-
val covered = alerted && terminal != null && data["alert_path"] == data["activity_path"]
232+
val covered = terminal?.takeIf { alerted && data["alert_path"] == data["activity_path"] }
233+
if (covered != null) coverTerminal(prefs, covered, updatedAt)
233234
if (updatedAt < prefs.getLong("lastUpdate", 0)) {
234235
// A newer replay of this same finish may already show the card.
235-
if (covered && prefs.getString("lastTerminal", null) == terminal) {
236-
prefs.edit().putString("coveredTerminal", terminal).apply()
237-
}
238236
if (isCovered(prefs, prefs.getString("lastTerminal", null))) cancelActivity(context)
239237
return
240238
}
241-
if (covered) prefs.edit().putString("coveredTerminal", terminal).apply()
242239
prefs.edit().putLong("lastUpdate", updatedAt).putString("lastTerminal", terminal).apply()
243240
val active = data["active"] == "true"
244241
// Use absolute state expiry: a replay must not extend a finished card or
@@ -277,6 +274,14 @@ object AgentNotifications {
277274
private fun isCovered(prefs: SharedPreferences, terminal: String?) =
278275
terminal != null && terminal == prefs.getString("coveredTerminal", null)
279276

277+
private fun coverTerminal(prefs: SharedPreferences, terminal: String?, updatedAt: Long) {
278+
if (updatedAt >= prefs.getLong("lastUpdate", 0) ||
279+
prefs.getString("lastTerminal", null) == terminal
280+
) {
281+
prefs.edit().putString("coveredTerminal", terminal).apply()
282+
}
283+
}
284+
280285
private fun showActivity(
281286
context: Context,
282287
scheme: String,

‎apps/mobile/modules/t3-agent-notifications/android/src/test/java/expo/modules/t3agentnotifications/AgentNotificationsTest.kt‎

Lines changed: 97 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ import org.robolectric.RuntimeEnvironment
2424
import org.robolectric.Shadows.shadowOf
2525
import 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\tTest thread\tTest 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\tMuted thread\tOther 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\tTest thread\tTest 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\tMuted thread\tOther 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

Comments
 (0)