Coverage Summary for Class: SelectWorkspaceUseCase (com.georgeci.moneysurfer.domain.usecase)

Class Method, % Branch, % Line, % Instruction, %
SelectWorkspaceUseCase 100% (2/2) 100% (2/2) 100% (18/18) 100% (121/121)
SelectWorkspaceUseCase$Companion
SelectWorkspaceUseCase$invoke$1
Total 100% (2/2) 100% (2/2) 100% (18/18) 100% (121/121)


 package com.georgeci.moneysurfer.domain.usecase
 
 import arrow.core.Either
 import co.touchlab.kermit.Logger
 import com.georgeci.moneysurfer.domain.auth.SessionMutator
 import com.georgeci.moneysurfer.domain.auth.SessionPointers
 import com.georgeci.moneysurfer.domain.logging.redactUid
 import com.georgeci.moneysurfer.domain.primitives.WorkspaceId
 import com.georgeci.moneysurfer.domain.repositories.UserRemoteRepository
 import kotlinx.coroutines.flow.first
 import org.koin.core.annotation.Single
 
 /**
  * Pins [workspaceId] as the active workspace.
  *
  *  1. Local: writes `session.currentWorkspaceId` (DataStore). This is what `Get*UseCase`s
  *     observe; the active screens repaint immediately.
  *  2. Remote (best-effort): pushes `users/{uid}.defaultWorkspaceId` so a fresh device after
  *     re-login lands on the same workspace without prompting the selector. Skipped for the
  *     local/demo session (no Firebase uid). Network failures are swallowed — the local
  *     pointer remains source of truth for the running session.
  */
 @Single
 class SelectWorkspaceUseCase(
     private val session: SessionPointers,
     private val sessionMutator: SessionMutator,
     private val userRemoteRepository: UserRemoteRepository,
 ) {
     private val log = Logger.withTag(TAG)
 
     suspend operator fun invoke(workspaceId: WorkspaceId) {
         sessionMutator.setCurrentWorkspace(workspaceId)
 
         val firebaseUid = session.currentFirebaseUid.first()
         if (firebaseUid != null) {
             Either.catch { userRemoteRepository.setDefaultWorkspace(firebaseUid, workspaceId) }
                 .onLeft {
                     log.w(it) {
                         "[remote] setDefaultWorkspace failed uid=${firebaseUid.redactUid()} wid=${workspaceId.value}"
                     }
                 }
                 .onRight {
                     log.i {
                         "[remote] setDefaultWorkspace ok uid=${firebaseUid.redactUid()} wid=${workspaceId.value}"
                     }
                 }
         } else {
             log.i { "[remote] skipped (no Firebase uid — local/demo session)" }
         }
     }
 
     private companion object {
         const val TAG = "SelectWorkspace"
     }
 }