@@ -39,6 +39,7 @@ import org.junit.Assert.assertEquals
3939import org.junit.Assert.assertTrue
4040import org.junit.Before
4141import org.junit.Test
42+ import org.junit.jupiter.api.Assertions.assertThrows
4243import org.junit.runner.RunWith
4344import org.robolectric.annotation.Config
4445import java.io.File
@@ -200,4 +201,65 @@ class ProfileManagerTest {
200201 assertEquals(1 , allProfiles.size)
201202 assertTrue(allProfiles.containsKey(ProfileId .DEFAULT ))
202203 }
204+
205+ fun `renameProfile updates displayName in registry` () {
206+ val manager = ProfileManager .create(context)
207+ val profileId = manager.createNewProfile(" Original Name" )
208+ val newName = " Updated Name"
209+
210+ manager.renameProfile(profileId, newName)
211+
212+ val json = prefs.getString(profileId.value, null )
213+ val metadata = ProfileManager .ProfileMetadata .fromJson(json!! )
214+
215+ assertEquals(newName, metadata.displayName)
216+ }
217+
218+ @Test
219+ fun `renameProfile preserves version and createdTimestamp` () {
220+ val manager = ProfileManager .create(context)
221+ val profileId = manager.createNewProfile(" Original Name" )
222+
223+ val originalJson = prefs.getString(profileId.value, null )
224+ val originalMetadata = ProfileManager .ProfileMetadata .fromJson(originalJson!! )
225+
226+ manager.renameProfile(profileId, " New Name" )
227+
228+ val updatedJson = prefs.getString(profileId.value, null )
229+ val updatedMetadata = ProfileManager .ProfileMetadata .fromJson(updatedJson!! )
230+
231+ assertEquals(" Version must be preserved" , originalMetadata.version, updatedMetadata.version)
232+ assertEquals(
233+ " Timestamp must be preserved" ,
234+ originalMetadata.createdTimestamp,
235+ updatedMetadata.createdTimestamp,
236+ )
237+ }
238+
239+ @Test
240+ fun `renameProfile does not write to disk if name is identical` () {
241+ val manager = ProfileManager .create(context)
242+ val name = " No Change"
243+ val profileId = manager.createNewProfile(name)
244+
245+ val originalJson = prefs.getString(profileId.value, null )
246+
247+ manager.renameProfile(profileId, name)
248+
249+ val currentJson = prefs.getString(profileId.value, null )
250+ assertEquals(" No disk write should occur for identical names" , originalJson, currentJson)
251+ }
252+
253+ @Test
254+ fun `renameProfile throws IllegalArgumentException for missing profile` () {
255+ val manager = ProfileManager .create(context)
256+ val fakeId = ProfileId (" p_ghost" )
257+
258+ val exception =
259+ assertThrows(IllegalArgumentException ::class .java) {
260+ manager.renameProfile(fakeId, " New Name" )
261+ }
262+
263+ assertTrue(exception.message!! .contains(" not found" ))
264+ }
203265}
0 commit comments