@@ -210,19 +210,29 @@ describe('Edge cases and additional coverage', () => {
210210 } )
211211
212212 it ( 'should handle very long component values' , ( ) => {
213- // Tests no length limits on components (stress test)
214- const longString = 'a' . repeat ( 1000 )
213+ // Test with maximum allowed lengths for components.
214+ const maxNamespace = 'a' . repeat ( 512 ) // Max namespace length.
215+ const maxName = 'b' . repeat ( 214 ) // Max name length.
216+ const maxVersion = 'c' . repeat ( 256 ) // Max version length.
215217 const purl = new PackageURL (
216218 'type' ,
217- longString ,
218- longString ,
219- longString ,
219+ maxNamespace ,
220+ maxName ,
221+ maxVersion ,
220222 undefined ,
221223 undefined ,
222224 )
223- expect ( purl . namespace ) . toBe ( longString )
224- expect ( purl . name ) . toBe ( longString )
225- expect ( purl . version ) . toBe ( longString )
225+ expect ( purl . namespace ) . toBe ( maxNamespace )
226+ expect ( purl . name ) . toBe ( maxName )
227+ expect ( purl . version ) . toBe ( maxVersion )
228+
229+ // Test that exceeding limits throws errors.
230+ expect (
231+ ( ) => new PackageURL ( 'type' , 'a' . repeat ( 513 ) , 'name' , undefined ) ,
232+ ) . toThrow ( '"namespace" exceeds maximum length of 512 characters' )
233+ expect ( ( ) => new PackageURL ( 'type' , undefined , 'a' . repeat ( 215 ) ) ) . toThrow (
234+ '"name" exceeds maximum length of 214 characters' ,
235+ )
226236 } )
227237
228238 it ( 'should preserve exact qualifier order in toString' , ( ) => {
@@ -2414,9 +2424,7 @@ describe('Edge cases and additional coverage', () => {
24142424 undefined ,
24152425 undefined ,
24162426 ) ,
2417- ) . toThrow (
2418- 'npm "namespace" and "name" components can not collectively be more than 214 characters' ,
2419- )
2427+ ) . toThrow ( '"name" exceeds maximum length of 214 characters' )
24202428
24212429 // Test npm core module name with throws (line 355)
24222430 // Use a builtin that's not a legacy name (legacy names skip the builtin check)
@@ -2855,4 +2863,45 @@ describe('Edge cases and additional coverage', () => {
28552863 expect ( result3 ) . toBe ( false )
28562864 } )
28572865 } )
2866+
2867+ describe ( 'Length validation' , ( ) => {
2868+ it ( 'should reject names exceeding maximum length' , ( ) => {
2869+ const longName = 'x' . repeat ( 215 )
2870+ expect ( validateName ( longName , { throws : false } ) ) . toBe ( false )
2871+ expect ( ( ) => validateName ( longName , { throws : true } ) ) . toThrow (
2872+ '"name" exceeds maximum length of 214 characters' ,
2873+ )
2874+ } )
2875+
2876+ it ( 'should accept names at maximum length' , ( ) => {
2877+ const maxName = 'x' . repeat ( 214 )
2878+ expect ( validateName ( maxName , { throws : false } ) ) . toBe ( true )
2879+ } )
2880+
2881+ it ( 'should reject namespaces exceeding maximum length' , ( ) => {
2882+ const longNamespace = 'x' . repeat ( 513 )
2883+ expect ( validateNamespace ( longNamespace , { throws : false } ) ) . toBe ( false )
2884+ expect ( ( ) => validateNamespace ( longNamespace , { throws : true } ) ) . toThrow (
2885+ '"namespace" exceeds maximum length of 512 characters' ,
2886+ )
2887+ } )
2888+
2889+ it ( 'should accept namespaces at maximum length' , ( ) => {
2890+ const maxNamespace = 'x' . repeat ( 512 )
2891+ expect ( validateNamespace ( maxNamespace , { throws : false } ) ) . toBe ( true )
2892+ } )
2893+
2894+ it ( 'should reject versions exceeding maximum length' , ( ) => {
2895+ const longVersion = 'x' . repeat ( 257 )
2896+ expect ( validateVersion ( longVersion , { throws : false } ) ) . toBe ( false )
2897+ expect ( ( ) => validateVersion ( longVersion , { throws : true } ) ) . toThrow (
2898+ '"version" exceeds maximum length of 256 characters' ,
2899+ )
2900+ } )
2901+
2902+ it ( 'should accept versions at maximum length' , ( ) => {
2903+ const maxVersion = 'x' . repeat ( 256 )
2904+ expect ( validateVersion ( maxVersion , { throws : false } ) ) . toBe ( true )
2905+ } )
2906+ } )
28582907} )
0 commit comments