11import { decodeTextFromUrl } from '@/utils/urlDecoder' ;
2+ import { encodeTextToUrl } from '@/utils/urlEncoder' ;
23
34describe ( 'decodeTextFromUrl' , ( ) => {
4- it ( 'should decode Base64 to simple text' , ( ) => {
5- const encoded = 'SGVsbG8gV29ybGQ=' ;
5+ it ( 'should decode compressed text correctly' , ( ) => {
6+ const originalText = 'Hello World' ;
7+ const encoded = encodeTextToUrl ( originalText ) ;
68 const decoded = decodeTextFromUrl ( encoded ) ;
7- expect ( decoded ) . toBe ( 'Hello World' ) ;
9+ expect ( decoded ) . toBe ( originalText ) ;
810 } ) ;
911
1012 it ( 'should handle empty string' , ( ) => {
@@ -15,40 +17,42 @@ describe('decodeTextFromUrl', () => {
1517
1618 it ( 'should decode Japanese text correctly' , ( ) => {
1719 const originalText = 'こんにちは世界' ;
18- const encoded = Buffer . from ( originalText , 'utf-8' ) . toString ( 'base64' ) ;
20+ const encoded = encodeTextToUrl ( originalText ) ;
1921 const decoded = decodeTextFromUrl ( encoded ) ;
2022 expect ( decoded ) . toBe ( originalText ) ;
2123 } ) ;
2224
2325 it ( 'should handle special characters' , ( ) => {
2426 const originalText = '!@#$%^&*()_+-=[]{}|;\':",./<>?' ;
25- const encoded = Buffer . from ( originalText , 'utf-8' ) . toString ( 'base64' ) ;
27+ const encoded = encodeTextToUrl ( originalText ) ;
2628 const decoded = decodeTextFromUrl ( encoded ) ;
2729 expect ( decoded ) . toBe ( originalText ) ;
2830 } ) ;
2931
3032 it ( 'should handle multi-line text' , ( ) => {
3133 const originalText = 'Line 1\nLine 2\nLine 3' ;
32- const encoded = Buffer . from ( originalText , 'utf-8' ) . toString ( 'base64' ) ;
34+ const encoded = encodeTextToUrl ( originalText ) ;
3335 const decoded = decodeTextFromUrl ( encoded ) ;
3436 expect ( decoded ) . toBe ( originalText ) ;
3537 } ) ;
3638
3739 it ( 'should decode emoji correctly' , ( ) => {
3840 const originalText = '😀😃😄😁😆' ;
39- const encoded = Buffer . from ( originalText , 'utf-8' ) . toString ( 'base64' ) ;
41+ const encoded = encodeTextToUrl ( originalText ) ;
4042 const decoded = decodeTextFromUrl ( encoded ) ;
4143 expect ( decoded ) . toBe ( originalText ) ;
4244 } ) ;
4345
44- it ( 'should handle invalid Base64 gracefully' , ( ) => {
45- const invalidEncoded = 'This is not valid Base64!@#$%' ;
46- expect ( ( ) => decodeTextFromUrl ( invalidEncoded ) ) . toThrow ( 'Invalid encoded text' ) ;
46+ it ( 'should handle invalid compressed data gracefully' , ( ) => {
47+ // LZ-string returns null for invalid data, which should result in an error
48+ const invalidEncoded = 'This is not valid compressed data!@#$%' ;
49+ expect ( ( ) => decodeTextFromUrl ( invalidEncoded ) ) . toThrowError ( 'Invalid compressed data' ) ;
4750 } ) ;
4851
49- it ( 'should handle corrupted Base64' , ( ) => {
50- const corruptedEncoded = 'SGVsbG8gV29ybGQ' ; // Missing padding
51- const decoded = decodeTextFromUrl ( corruptedEncoded ) ;
52- expect ( decoded ) . toBe ( 'Hello World' ) ;
52+ it ( 'should handle very long text' , ( ) => {
53+ const originalText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' . repeat ( 50 ) ;
54+ const encoded = encodeTextToUrl ( originalText ) ;
55+ const decoded = decodeTextFromUrl ( encoded ) ;
56+ expect ( decoded ) . toBe ( originalText ) ;
5357 } ) ;
5458} ) ;
0 commit comments