@@ -4,16 +4,45 @@ jest.mock('tweetnacl', () => ({
44 // Return a stable mock array for testing
55 return new Uint8Array ( Array . from ( { length } , ( _ , i ) => i % 256 ) ) ;
66 } ) ,
7- secretbox : jest . fn ( ( ) => new Uint8Array ( 32 ) ) , // Mock encrypted result
7+ secretbox : jest . fn ( ( message : Uint8Array , nonce : Uint8Array , key : Uint8Array ) => {
8+ // Return a mock encrypted result that's deterministic
9+ return new Uint8Array ( message . length + 16 ) ; // Add overhead for box
10+ } )
811} ) ) ;
912
13+ // Add a proper secretbox.open mock
14+ jest . mock ( 'tweetnacl' , ( ) => {
15+ const mockSecretbox = jest . fn ( ( message : Uint8Array , nonce : Uint8Array , key : Uint8Array ) => {
16+ return new Uint8Array ( message . length + 16 ) ;
17+ } ) ;
18+
19+ mockSecretbox . open = jest . fn ( ( ciphertext : Uint8Array , nonce : Uint8Array , key : Uint8Array ) => {
20+ if ( ciphertext . length <= 16 ) return null ;
21+ return ciphertext . slice ( 16 ) ;
22+ } ) ;
23+
24+ mockSecretbox . keyLength = 32 ;
25+ mockSecretbox . nonceLength = 24 ;
26+ mockSecretbox . overheadLength = 16 ;
27+
28+ return {
29+ randomBytes : jest . fn ( ( length : number ) => {
30+ return new Uint8Array ( Array . from ( { length } , ( _ , i ) => i % 256 ) ) ;
31+ } ) ,
32+ secretbox : mockSecretbox
33+ } ;
34+ } ) ;
35+
1036// Mock crypto-browserify pbkdf2
1137jest . mock ( 'crypto-browserify' , ( ) => ( {
1238 pbkdf2 : jest . fn ( ( password , salt , iterations , keyLength , digest , callback ) => {
1339 // Simulate async operation
1440 setTimeout ( ( ) => {
15- // Return stable mock key
16- const mockKey = Buffer . from ( Array . from ( { length : keyLength } , ( _ , i ) => i % 256 ) ) ;
41+ // Return stable mock key based on password for deterministic testing
42+ const passwordBytes = Buffer . from ( password , 'utf8' ) ;
43+ const mockKey = Buffer . from ( Array . from ( { length : keyLength } , ( _ , i ) =>
44+ ( passwordBytes [ i % passwordBytes . length ] + i ) % 256
45+ ) ) ;
1746 callback ( null , mockKey ) ;
1847 } , 0 ) ;
1948 } ) ,
@@ -22,11 +51,18 @@ jest.mock('crypto-browserify', () => ({
2251// Mock argon2-browser
2352jest . mock ( 'argon2-browser' , ( ) => ( {
2453 hash : jest . fn ( async ( options ) => {
54+ // Create deterministic hash based on password
55+ const passwordBytes = Buffer . from ( options . pass , 'utf8' ) ;
2556 return {
26- hash : new Uint8Array ( Array . from ( { length : 32 } , ( _ , i ) => i % 256 ) ) ,
57+ hash : new Uint8Array ( Array . from ( { length : 32 } , ( _ , i ) =>
58+ ( passwordBytes [ i % passwordBytes . length ] + i ) % 256
59+ ) ) ,
2760 hashHex : 'abcdef123456789' ,
2861 } ;
2962 } ) ,
63+ ArgonType : {
64+ Argon2id : 2
65+ }
3066} ) ) ;
3167
3268import {
0 commit comments