diff --git a/notifications/src/main/java/com/rousecontext/notifications/NotificationAction.kt b/notifications/src/main/java/com/rousecontext/notifications/NotificationAction.kt deleted file mode 100644 index 5032740c..00000000 --- a/notifications/src/main/java/com/rousecontext/notifications/NotificationAction.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.rousecontext.notifications - -/** - * Actions the notification state machine emits. - * The [NotificationAdapter] translates these into Android NotificationManager calls. - */ -sealed interface NotificationAction { - /** Show or update the ongoing foreground service notification. */ - data class ShowForeground(val message: String) : NotificationAction - - /** Post a session summary after disconnect. */ - data class PostSummary(val toolCallCount: Int) : NotificationAction - - /** Post a warning (e.g. disconnect with no tool calls). */ - data class PostWarning(val message: String) : NotificationAction - - /** Post an error notification. */ - data class PostError(val message: String, val streamId: Int? = null) : NotificationAction - - /** Post a per-tool-call usage notification. */ - data class PostToolUsage(val toolName: String, val provider: String) : NotificationAction - - /** Post an informational notification. */ - data class PostInfo(val message: String) : NotificationAction - - /** Post a high-priority security alert. */ - data class PostAlert(val message: String) : NotificationAction -} diff --git a/notifications/src/main/java/com/rousecontext/notifications/NotificationAdapter.kt b/notifications/src/main/java/com/rousecontext/notifications/NotificationAdapter.kt deleted file mode 100644 index d09a5a95..00000000 --- a/notifications/src/main/java/com/rousecontext/notifications/NotificationAdapter.kt +++ /dev/null @@ -1,113 +0,0 @@ -package com.rousecontext.notifications - -import android.app.NotificationManager -import android.content.Context -import androidx.core.app.NotificationCompat -import com.rousecontext.api.R as ApiR - -/** - * Translates [NotificationAction]s into Android [NotificationManager] calls. - */ -class NotificationAdapter(private val context: Context) { - - private val manager = context.getSystemService(NotificationManager::class.java) - private var nextId = FIRST_DYNAMIC_ID - - /** - * Execute a list of notification actions. - */ - fun execute(actions: List) { - actions.forEach { execute(it) } - } - - /** - * Execute a single notification action. - */ - fun execute(action: NotificationAction) { - when (action) { - is NotificationAction.ShowForeground -> { - val notification = ForegroundNotifier.build(context, action.message) - manager.notify(FOREGROUND_ID, notification) - } - - is NotificationAction.PostSummary -> { - val suffix = - if (action.toolCallCount == 1) "" else "s" - val body = - "${action.toolCallCount} tool call$suffix processed" - val notification = buildSessionNotification( - "Session Complete", - body - ) - manager.notify(nextId(), notification) - } - - is NotificationAction.PostWarning -> { - val notification = buildSessionNotification("Warning", action.message) - manager.notify(nextId(), notification) - } - - is NotificationAction.PostError -> { - val text = if (action.streamId != null) { - "Stream ${action.streamId}: ${action.message}" - } else { - action.message - } - val notification = buildErrorNotification(text) - manager.notify(nextId(), notification) - } - - is NotificationAction.PostToolUsage -> { - val notification = buildSessionNotification( - "Tool Call", - "${action.toolName} (${action.provider})" - ) - manager.notify(nextId(), notification) - } - - is NotificationAction.PostInfo -> { - val notification = buildSessionNotification("Info", action.message) - manager.notify(nextId(), notification) - } - - is NotificationAction.PostAlert -> { - val notification = buildAlertNotification(action.message) - manager.notify(nextId(), notification) - } - } - } - - private fun buildSessionNotification(title: String, text: String) = - NotificationCompat.Builder(context, NotificationChannels.SESSION_CHANNEL_ID) - .setContentTitle(title) - .setContentText(text) - .setSmallIcon(ApiR.drawable.ic_stat_rouse) - .setPriority(NotificationCompat.PRIORITY_DEFAULT) - .setAutoCancel(true) - .build() - - private fun buildErrorNotification(text: String) = - NotificationCompat.Builder(context, NotificationChannels.ERROR_CHANNEL_ID) - .setContentTitle("Error") - .setContentText(text) - .setSmallIcon(ApiR.drawable.ic_stat_rouse) - .setPriority(NotificationCompat.PRIORITY_HIGH) - .setAutoCancel(true) - .build() - - private fun buildAlertNotification(text: String) = - NotificationCompat.Builder(context, NotificationChannels.ALERT_CHANNEL_ID) - .setContentTitle("Security Alert") - .setContentText(text) - .setSmallIcon(ApiR.drawable.ic_stat_rouse) - .setPriority(NotificationCompat.PRIORITY_HIGH) - .setAutoCancel(true) - .build() - - private fun nextId(): Int = nextId++ - - companion object { - const val FOREGROUND_ID = 1 - private const val FIRST_DYNAMIC_ID = 100 - } -} diff --git a/notifications/src/test/java/com/rousecontext/notifications/NotificationAdapterTest.kt b/notifications/src/test/java/com/rousecontext/notifications/NotificationAdapterTest.kt deleted file mode 100644 index c3813e86..00000000 --- a/notifications/src/test/java/com/rousecontext/notifications/NotificationAdapterTest.kt +++ /dev/null @@ -1,79 +0,0 @@ -package com.rousecontext.notifications - -import android.app.NotificationManager -import android.content.Context -import androidx.test.core.app.ApplicationProvider -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Assert.assertTrue -import org.junit.Before -import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -import org.robolectric.Shadows - -@RunWith(RobolectricTestRunner::class) -class NotificationAdapterTest { - - private lateinit var context: Context - private lateinit var manager: NotificationManager - private lateinit var adapter: NotificationAdapter - - @Before - fun setUp() { - context = ApplicationProvider.getApplicationContext() - manager = context.getSystemService(NotificationManager::class.java) - NotificationChannels.createAll(context) - adapter = NotificationAdapter(context) - } - - @Test - fun `ShowForeground posts notification with foreground ID`() { - adapter.execute(NotificationAction.ShowForeground("Connected")) - - val shadowManager = Shadows.shadowOf(manager) - val notification = shadowManager.getNotification(NotificationAdapter.FOREGROUND_ID) - assertNotNull(notification) - } - - @Test - fun `PostSummary posts notification with session complete title`() { - adapter.execute(NotificationAction.PostSummary(toolCallCount = 3)) - - val shadowManager = Shadows.shadowOf(manager) - val notifications = shadowManager.allNotifications - // foreground might not be posted, but at least one dynamic notification should exist - assertTrue(notifications.isNotEmpty()) - } - - @Test - fun `PostError posts notification`() { - adapter.execute(NotificationAction.PostError("Connection failed", streamId = null)) - - val shadowManager = Shadows.shadowOf(manager) - assertTrue(shadowManager.allNotifications.isNotEmpty()) - } - - @Test - fun `PostAlert posts notification`() { - adapter.execute(NotificationAction.PostAlert("Suspicious activity")) - - val shadowManager = Shadows.shadowOf(manager) - assertTrue(shadowManager.allNotifications.isNotEmpty()) - } - - @Test - fun `multiple actions post unique notification IDs`() { - adapter.execute( - listOf( - NotificationAction.PostInfo("Info 1"), - NotificationAction.PostInfo("Info 2"), - NotificationAction.PostInfo("Info 3") - ) - ) - - val shadowManager = Shadows.shadowOf(manager) - // Each should have a unique ID, so all 3 should be present - assertEquals(3, shadowManager.allNotifications.size) - } -} diff --git a/notifications/src/test/java/com/rousecontext/notifications/NotificationIconTest.kt b/notifications/src/test/java/com/rousecontext/notifications/NotificationIconTest.kt index b2c45bff..47f936f0 100644 --- a/notifications/src/test/java/com/rousecontext/notifications/NotificationIconTest.kt +++ b/notifications/src/test/java/com/rousecontext/notifications/NotificationIconTest.kt @@ -12,104 +12,64 @@ import org.robolectric.RobolectricTestRunner import org.robolectric.Shadows /** - * Verifies that every notification type uses [ApiR.drawable.ic_stat_rouse] + * Verifies that every production notifier uses [ApiR.drawable.ic_stat_rouse] * as the small icon, not a system drawable or placeholder. + * + * Covers each [com.rousecontext.notifications] notifier in one place so icon + * regressions surface even if per-notifier tests drift. */ @RunWith(RobolectricTestRunner::class) class NotificationIconTest { private lateinit var context: Context private lateinit var manager: NotificationManager - private lateinit var adapter: NotificationAdapter @Before fun setUp() { context = ApplicationProvider.getApplicationContext() manager = context.getSystemService(NotificationManager::class.java) NotificationChannels.createAll(context) - adapter = NotificationAdapter(context) } @Test - fun `foreground notification uses ic_stat_rouse`() { + fun `ForegroundNotifier build uses ic_stat_rouse`() { val notification = ForegroundNotifier.build(context, "Connected") assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) } @Test - fun `ShowForeground action posts notification with ic_stat_rouse`() { - adapter.execute(NotificationAction.ShowForeground("Active")) + fun `FgsLimitNotifier postLimitReachedNotification uses ic_stat_rouse`() { + FgsLimitNotifier.postLimitReachedNotification(context) + val active = manager.activeNotifications.single() + assertEquals(ApiR.drawable.ic_stat_rouse, active.notification.smallIcon.resId) + } + + @Test + fun `SecurityCheckNotifier postAlert uses ic_stat_rouse`() { + val notifier = SecurityCheckNotifier(context) + notifier.postAlert(SecurityCheckNotifier.SecurityCheck.SELF_CERT, "bad cert") val shadow = Shadows.shadowOf(manager) - val notification = shadow.getNotification(NotificationAdapter.FOREGROUND_ID) + val notification = shadow.getNotification( + SecurityCheckNotifier.NOTIFICATION_ID_SELF_CERT_ALERT + ) assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) } @Test - fun `PostSummary notification uses ic_stat_rouse`() { - adapter.execute(NotificationAction.PostSummary(toolCallCount = 5)) + fun `SecurityCheckNotifier postInfo uses ic_stat_rouse`() { + val notifier = SecurityCheckNotifier(context) + notifier.postInfo(SecurityCheckNotifier.SecurityCheck.CT_LOG, "ok") val shadow = Shadows.shadowOf(manager) - val notification = shadow.allNotifications.first() + val notification = shadow.getNotification( + SecurityCheckNotifier.NOTIFICATION_ID_CT_LOG_INFO + ) assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) } @Test - fun `PostWarning notification uses ic_stat_rouse`() { - adapter.execute(NotificationAction.PostWarning("Low battery")) - - val shadow = Shadows.shadowOf(manager) - val notification = shadow.allNotifications.first() - assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) - } - - @Test - fun `PostError notification uses ic_stat_rouse`() { - adapter.execute(NotificationAction.PostError("Connection lost", streamId = null)) - - val shadow = Shadows.shadowOf(manager) - val notification = shadow.allNotifications.first() - assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) - } - - @Test - fun `PostError with streamId notification uses ic_stat_rouse`() { - adapter.execute(NotificationAction.PostError("TLS failed", streamId = 42)) - - val shadow = Shadows.shadowOf(manager) - val notification = shadow.allNotifications.first() - assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) - } - - @Test - fun `PostToolUsage notification uses ic_stat_rouse`() { - adapter.execute(NotificationAction.PostToolUsage("get_steps", "health")) - - val shadow = Shadows.shadowOf(manager) - val notification = shadow.allNotifications.first() - assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) - } - - @Test - fun `PostInfo notification uses ic_stat_rouse`() { - adapter.execute(NotificationAction.PostInfo("Session started")) - - val shadow = Shadows.shadowOf(manager) - val notification = shadow.allNotifications.first() - assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) - } - - @Test - fun `PostAlert notification uses ic_stat_rouse`() { - adapter.execute(NotificationAction.PostAlert("Suspicious activity detected")) - - val shadow = Shadows.shadowOf(manager) - val notification = shadow.allNotifications.first() - assertEquals(ApiR.drawable.ic_stat_rouse, notification.smallIcon.resId) - } - - @Test - fun `AuthRequestNotifier notification uses ic_stat_rouse`() { + fun `AuthRequestNotifier post uses ic_stat_rouse`() { val notifier = AuthRequestNotifier( context = context, receiverClass = AuthRequestNotifierTest.TestReceiver::class.java, diff --git a/notifications/src/test/java/com/rousecontext/notifications/preview/NotificationScreenshotTest.kt b/notifications/src/test/java/com/rousecontext/notifications/preview/NotificationScreenshotTest.kt index 236b708b..eda26dfa 100644 --- a/notifications/src/test/java/com/rousecontext/notifications/preview/NotificationScreenshotTest.kt +++ b/notifications/src/test/java/com/rousecontext/notifications/preview/NotificationScreenshotTest.kt @@ -9,8 +9,8 @@ import androidx.core.app.NotificationCompat import androidx.test.core.app.ApplicationProvider import com.github.takahirom.roborazzi.captureRoboImage import com.rousecontext.api.R as ApiR -import com.rousecontext.notifications.NotificationChannels import com.rousecontext.notifications.ForegroundNotifier +import com.rousecontext.notifications.NotificationChannels import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith diff --git a/work/src/main/kotlin/com/rousecontext/work/TunnelForegroundService.kt b/work/src/main/kotlin/com/rousecontext/work/TunnelForegroundService.kt index 64549dd2..3666f740 100644 --- a/work/src/main/kotlin/com/rousecontext/work/TunnelForegroundService.kt +++ b/work/src/main/kotlin/com/rousecontext/work/TunnelForegroundService.kt @@ -273,6 +273,9 @@ class TunnelForegroundService : LifecycleService() { TunnelState.ACTIVE -> "Active session" TunnelState.DISCONNECTING -> "Disconnecting..." } + // Update the foreground notification in place via the same id passed to + // startForeground(). Android treats a NotificationManager.notify() with + // the FGS id as an update to the ongoing notification. val notification = ForegroundNotifier.build(this, message) val manager = getSystemService(android.app.NotificationManager::class.java) manager.notify(NOTIFICATION_ID, notification)