@@ -1949,6 +1949,115 @@ describe('AccountTreeController', () => {
1949
1949
controller . state . accountWalletsMetadata [ nonExistentWalletId ] ,
1950
1950
) . toBeUndefined ( ) ;
1951
1951
} ) ;
1952
+
1953
+ it ( 'allows setting the same name for the same group' , ( ) => {
1954
+ const { controller } = setup ( {
1955
+ accounts : [ MOCK_HD_ACCOUNT_1 ] ,
1956
+ keyrings : [ MOCK_HD_KEYRING_1 ] ,
1957
+ } ) ;
1958
+
1959
+ controller . init ( ) ;
1960
+
1961
+ const wallets = controller . getAccountWalletObjects ( ) ;
1962
+ const groups = Object . values ( wallets [ 0 ] . groups ) ;
1963
+ const groupId = groups [ 0 ] . id ;
1964
+
1965
+ const customName = 'My Custom Group' ;
1966
+
1967
+ // Set the name first time - should succeed
1968
+ controller . setAccountGroupName ( groupId , customName ) ;
1969
+
1970
+ // Set the same name again for the same group - should succeed
1971
+ expect ( ( ) => {
1972
+ controller . setAccountGroupName ( groupId , customName ) ;
1973
+ } ) . not . toThrow ( ) ;
1974
+ } ) ;
1975
+
1976
+ it ( 'prevents setting duplicate names across different groups' , ( ) => {
1977
+ const { controller } = setup ( {
1978
+ accounts : [ MOCK_HD_ACCOUNT_1 , MOCK_HD_ACCOUNT_2 ] ,
1979
+ keyrings : [ MOCK_HD_KEYRING_1 , MOCK_HD_KEYRING_2 ] ,
1980
+ } ) ;
1981
+
1982
+ controller . init ( ) ;
1983
+
1984
+ const wallets = controller . getAccountWalletObjects ( ) ;
1985
+
1986
+ // We should have 2 wallets (one for each keyring)
1987
+ expect ( wallets ) . toHaveLength ( 2 ) ;
1988
+
1989
+ const wallet1 = wallets [ 0 ] ;
1990
+ const wallet2 = wallets [ 1 ] ;
1991
+ const groups1 = Object . values ( wallet1 . groups ) ;
1992
+ const groups2 = Object . values ( wallet2 . groups ) ;
1993
+
1994
+ expect ( groups1 . length ) . toBeGreaterThanOrEqual ( 1 ) ;
1995
+ expect ( groups2 . length ) . toBeGreaterThanOrEqual ( 1 ) ;
1996
+
1997
+ const groupId1 = groups1 [ 0 ] . id ;
1998
+ const groupId2 = groups2 [ 0 ] . id ;
1999
+ const duplicateName = 'Duplicate Group Name' ;
2000
+
2001
+ // Set name for first group - should succeed
2002
+ controller . setAccountGroupName ( groupId1 , duplicateName ) ;
2003
+
2004
+ // Try to set the same name for second group - should throw
2005
+ expect ( ( ) => {
2006
+ controller . setAccountGroupName ( groupId2 , duplicateName ) ;
2007
+ } ) . toThrow ( 'Account group name already exists' ) ;
2008
+ } ) ;
2009
+
2010
+ it ( 'ensures unique names when generating default names' , ( ) => {
2011
+ const { controller } = setup ( {
2012
+ accounts : [ MOCK_HD_ACCOUNT_1 , MOCK_HD_ACCOUNT_2 ] ,
2013
+ keyrings : [ MOCK_HD_KEYRING_1 ] ,
2014
+ } ) ;
2015
+
2016
+ controller . init ( ) ;
2017
+
2018
+ const wallets = controller . getAccountWalletObjects ( ) ;
2019
+ const groups = Object . values ( wallets [ 0 ] . groups ) ;
2020
+
2021
+ // All groups should have unique names by default
2022
+ const names = groups . map ( ( group ) => group . metadata . name ) ;
2023
+ const uniqueNames = new Set ( names ) ;
2024
+
2025
+ expect ( uniqueNames . size ) . toBe ( names . length ) ;
2026
+ expect ( names . every ( ( name ) => name . length > 0 ) ) . toBe ( true ) ;
2027
+ } ) ;
2028
+
2029
+ it ( 'prevents duplicate names when comparing trimmed names' , ( ) => {
2030
+ const { controller } = setup ( {
2031
+ accounts : [ MOCK_HD_ACCOUNT_1 , MOCK_HD_ACCOUNT_2 ] ,
2032
+ keyrings : [ MOCK_HD_KEYRING_1 , MOCK_HD_KEYRING_2 ] ,
2033
+ } ) ;
2034
+
2035
+ controller . init ( ) ;
2036
+
2037
+ const wallets = controller . getAccountWalletObjects ( ) ;
2038
+ expect ( wallets ) . toHaveLength ( 2 ) ;
2039
+
2040
+ const wallet1 = wallets [ 0 ] ;
2041
+ const wallet2 = wallets [ 1 ] ;
2042
+ const groups1 = Object . values ( wallet1 . groups ) ;
2043
+ const groups2 = Object . values ( wallet2 . groups ) ;
2044
+
2045
+ expect ( groups1 . length ) . toBeGreaterThanOrEqual ( 1 ) ;
2046
+ expect ( groups2 . length ) . toBeGreaterThanOrEqual ( 1 ) ;
2047
+
2048
+ const groupId1 = groups1 [ 0 ] . id ;
2049
+ const groupId2 = groups2 [ 0 ] . id ;
2050
+
2051
+ // Set name for first group with trailing spaces
2052
+ const nameWithSpaces = ' My Group Name ' ;
2053
+ controller . setAccountGroupName ( groupId1 , nameWithSpaces ) ;
2054
+
2055
+ // Try to set the same name for second group with different spacing - should throw
2056
+ const nameWithDifferentSpacing = ' My Group Name ' ;
2057
+ expect ( ( ) => {
2058
+ controller . setAccountGroupName ( groupId2 , nameWithDifferentSpacing ) ;
2059
+ } ) . toThrow ( 'Account group name already exists' ) ;
2060
+ } ) ;
1952
2061
} ) ;
1953
2062
1954
2063
describe ( 'Fallback Naming' , ( ) => {
0 commit comments