@@ -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+
206+ fun `renameProfile updates displayName in registry` () {
207+ val manager = ProfileManager .create(context)
208+ val profileId = manager.createNewProfile(" Original Name" )
209+ val newName = " Updated Name"
210+
211+ manager.renameProfile(profileId, newName)
212+
213+ val json = prefs.getString(profileId.value, null )
214+ val metadata = ProfileManager .ProfileMetadata .fromJson(json!! )
215+
216+ assertEquals(newName, metadata.displayName)
217+ }
218+
219+ @Test
220+ fun `renameProfile preserves version and createdTimestamp` () {
221+ val manager = ProfileManager .create(context)
222+ val profileId = manager.createNewProfile(" Original Name" )
223+
224+ val originalJson = prefs.getString(profileId.value, null )
225+ val originalMetadata = ProfileManager .ProfileMetadata .fromJson(originalJson!! )
226+
227+ manager.renameProfile(profileId, " New Name" )
228+
229+ val updatedJson = prefs.getString(profileId.value, null )
230+ val updatedMetadata = ProfileManager .ProfileMetadata .fromJson(updatedJson!! )
231+
232+ assertEquals(" Version must be preserved" , originalMetadata.version, updatedMetadata.version)
233+ assertEquals(
234+ " Timestamp must be preserved" ,
235+ originalMetadata.createdTimestamp,
236+ updatedMetadata.createdTimestamp
237+ )
238+ }
239+
240+ @Test
241+ fun `renameProfile does not write to disk if name is identical` () {
242+ val manager = ProfileManager .create(context)
243+ val name = " No Change"
244+ val profileId = manager.createNewProfile(name)
245+
246+ val originalJson = prefs.getString(profileId.value, null )
247+
248+ manager.renameProfile(profileId, name)
249+
250+ val currentJson = prefs.getString(profileId.value, null )
251+ assertEquals(" No disk write should occur for identical names" , originalJson, currentJson)
252+ }
253+
254+ @Test
255+ fun `renameProfile throws IllegalArgumentException for missing profile` () {
256+ val manager = ProfileManager .create(context)
257+ val fakeId = ProfileId (" p_ghost" )
258+
259+ val exception = assertThrows(IllegalArgumentException ::class .java) {
260+ manager.renameProfile(fakeId, " New Name" )
261+ }
262+
263+ assertTrue(exception.message!! .contains(" not found" ))
264+ }
203265}
0 commit comments