11import { describe , expect , it , vi } from 'vitest' ;
2- import { encryptMessage } from '../../nips/nip04' ;
2+ import { encryptMessage , decryptMessage } from '../../nips/nip04' ;
33
4- vi . mock ( 'nostr-crypto-utils' , ( ) => {
5- return {
6- generateKeyPair : ( ) => Promise . resolve ( {
7- publicKey : 'test-public-key' ,
8- privateKey : 'test-private-key'
9- } ) ,
10- encrypt : async ( message : string , privateKey : string , publicKey : string ) => {
11- if ( ! privateKey || ! publicKey ) {
12- throw new Error ( 'Invalid parameters' ) ;
13- }
14- return 'encrypted_message' ;
15- } ,
16- decrypt : async ( encryptedMessage : string , privateKey : string , publicKey : string ) => {
17- if ( ! privateKey || ! publicKey ) {
18- throw new Error ( 'Invalid parameters' ) ;
19- }
20- return 'decrypted_message' ;
4+ vi . mock ( 'nostr-crypto-utils/nips/nip-04' , ( ) => ( {
5+ encryptMessage : vi . fn ( ) . mockImplementation ( ( message : string , privateKey : string , publicKey : string ) => {
6+ if ( ! privateKey || ! publicKey ) {
7+ throw new Error ( 'Invalid parameters' ) ;
218 }
22- } ;
23- } ) ;
9+ if ( ! message ) {
10+ throw new Error ( 'Message cannot be empty' ) ;
11+ }
12+ if ( privateKey . length !== 64 || publicKey . length !== 64 ) {
13+ throw new Error ( 'Keys must be 32-byte hex strings' ) ;
14+ }
15+ return Promise . resolve ( 'encrypted_message' ) ;
16+ } ) ,
17+ decryptMessage : vi . fn ( ) . mockImplementation ( ( encryptedMessage : string , privateKey : string , publicKey : string ) => {
18+ if ( ! privateKey || ! publicKey ) {
19+ throw new Error ( 'Invalid parameters' ) ;
20+ }
21+ if ( privateKey . length !== 64 || publicKey . length !== 64 ) {
22+ throw new Error ( 'Keys must be 32-byte hex strings' ) ;
23+ }
24+ return Promise . resolve ( 'decrypted_message' ) ;
25+ } )
26+ } ) ) ;
2427
2528describe ( 'NIP-04: Encrypted Direct Messages' , ( ) => {
2629 describe ( 'Message Encryption/Decryption Integration' , ( ) => {
2730 const testMessage = 'Hello, World!' ;
28- const senderPrivateKey = 'test-private-key ' ;
29- const recipientPublicKey = 'recipient-public-key ' ;
31+ const senderPrivateKey = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef ' ;
32+ const recipientPublicKey = 'fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210 ' ;
3033
3134 it ( 'should successfully encrypt and decrypt a message' , async ( ) => {
3235 const encrypted = await encryptMessage ( testMessage , senderPrivateKey , recipientPublicKey ) ;
@@ -36,6 +39,12 @@ describe('NIP-04: Encrypted Direct Messages', () => {
3639 it ( 'should handle error cases appropriately' , async ( ) => {
3740 await expect ( encryptMessage ( 'test' , '' , recipientPublicKey ) )
3841 . rejects . toThrow ( 'Private key is required' ) ;
42+
43+ await expect ( encryptMessage ( '' , senderPrivateKey , recipientPublicKey ) )
44+ . rejects . toThrow ( 'Message cannot be empty' ) ;
45+
46+ await expect ( encryptMessage ( 'test' , 'invalid-key' , recipientPublicKey ) )
47+ . rejects . toThrow ( 'Keys must be 32-byte hex strings' ) ;
3948 } ) ;
4049 } ) ;
4150} ) ;
0 commit comments