Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,44 @@ class ProfileManager private constructor(
return ProfileRestrictedDirectory(directoryFile)
}

/**
* Renames an existing profile by updating its display name in
* the registry.
*
* All other metadata fields (version, creation timestamp) are
* preserved. The change is persisted immediately.
*
* @param profileId The [ProfileId] of the profile to rename.
* @param newDisplayName The new user-facing name.
*
* @throws IllegalArgumentException if [profileId] does not
* exist in the registry.
*/
fun renameProfile(
profileId: ProfileId,
newDisplayName: String,
Comment thread
criticalAY marked this conversation as resolved.
) {
Timber.d("ProfileManager::renameProfile called for $profileId")

require(newDisplayName.isNotBlank()) {
"Profile display name must not be blank"
}

val existing =
profileRegistry.getProfileMetadata(profileId)
?: throw IllegalArgumentException("Profile $profileId not found")

if (existing.displayName == newDisplayName) {
Timber.d("Rename skipped: New name matches existing name for $profileId")
return
}

val updated = existing.copy(displayName = newDisplayName)
profileRegistry.saveProfile(profileId, updated)

Timber.d("Renamed profile $profileId to '$newDisplayName'")
}

private fun triggerAppRestart() {
Timber.w("Restarting app to apply profile switch")
// TODO: Implement process restart logic (e.g. ProcessPhoenix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
import java.io.File
Expand Down Expand Up @@ -200,4 +201,65 @@ class ProfileManagerTest {
assertEquals(1, allProfiles.size)
assertTrue(allProfiles.containsKey(ProfileId.DEFAULT))
}

fun `renameProfile updates displayName in registry`() {
val manager = ProfileManager.create(context)
val profileId = manager.createNewProfile("Original Name")
val newName = "Updated Name"

manager.renameProfile(profileId, newName)

val json = prefs.getString(profileId.value, null)
val metadata = ProfileManager.ProfileMetadata.fromJson(json!!)

assertEquals(newName, metadata.displayName)
}

@Test
fun `renameProfile preserves version and createdTimestamp`() {
val manager = ProfileManager.create(context)
val profileId = manager.createNewProfile("Original Name")

val originalJson = prefs.getString(profileId.value, null)
val originalMetadata = ProfileManager.ProfileMetadata.fromJson(originalJson!!)

manager.renameProfile(profileId, "New Name")

val updatedJson = prefs.getString(profileId.value, null)
val updatedMetadata = ProfileManager.ProfileMetadata.fromJson(updatedJson!!)

assertEquals("Version must be preserved", originalMetadata.version, updatedMetadata.version)
assertEquals(
"Timestamp must be preserved",
originalMetadata.createdTimestamp,
updatedMetadata.createdTimestamp,
)
}

@Test
fun `renameProfile does not write to disk if name is identical`() {
val manager = ProfileManager.create(context)
val name = "No Change"
val profileId = manager.createNewProfile(name)

val originalJson = prefs.getString(profileId.value, null)

manager.renameProfile(profileId, name)

val currentJson = prefs.getString(profileId.value, null)
assertEquals("No disk write should occur for identical names", originalJson, currentJson)
}

@Test
fun `renameProfile throws IllegalArgumentException for missing profile`() {
val manager = ProfileManager.create(context)
val fakeId = ProfileId("p_ghost")

val exception =
assertThrows(IllegalArgumentException::class.java) {
manager.renameProfile(fakeId, "New Name")
}

assertTrue(exception.message!!.contains("not found"))
}
}
Loading