@@ -33,15 +33,27 @@ describe('encrypt', () => {
3333 expect ( encrypted . slice ( - 2 ) ) . toBe ( '==' )
3434 } )
3535 it ( 'throws for anything other than a string' , async ( ) => {
36- const keyPair = await generateKeyPair ( )
36+ const keyPair = ( await generateKeyPair ( ) ) . unwrap ( )
3737 // @ts -expect-error
38- await expect ( encrypt ( keyPair . publicKey , { } ) ) . rejects . toThrow ( )
38+ expect ( toOneLine ( ( await encrypt ( keyPair . publicKey ) ) . val . toString ( ) ) ) . toBe (
39+ toOneLine (
40+ `because encrypting failed due to invalid plaintext type. Expected "string", received ",".`
41+ )
42+ )
3943 } )
4044
4145 it ( 'throws for invalid public key' , async ( ) => {
4246 // @ts -expect-error
43- await expect ( encrypt ( { } , 'Hello, World!' ) ) . rejects . toThrow ( )
44- await expect ( encrypt ( 'invalid public key' , 'Hello, World!' ) ) . rejects . toThrow ( )
47+ expect ( toOneLine ( ( await encrypt ( { } , 'Hello, World!' ) ) . val . toString ( ) ) ) . toBe (
48+ toOneLine (
49+ `because encrypting failed due to invalid public key type. Expected "string", received "[object Object]".`
50+ )
51+ )
52+ expect ( toOneLine ( ( await encrypt ( 'invalid public key' , 'Hello, World!' ) ) . val . toString ( ) ) ) . toBe (
53+ toOneLine (
54+ `because base64 decoding the value "," errored. Error: InvalidCharacterError: The string to be decoded is not correctly encoded.`
55+ )
56+ )
4557 } )
4658
4759 it ( 'encrypts large amount of text' , async ( ) => {
@@ -59,10 +71,10 @@ describe('decrypt', () => {
5971 const plaintext = 'Hello, World!'
6072 const keyPair = ( await generateKeyPair ( ) ) . unwrap ( )
6173 const encrypted = ( await encrypt ( keyPair . publicKey , plaintext ) ) . unwrap ( )
62- const decrypted = await decrypt ( keyPair . privateKey , encrypted )
74+ const decrypted = await ( await decrypt ( keyPair . privateKey , encrypted ) ) . unwrap ( )
6375 expect ( decrypted ) . toBe ( plaintext )
6476 } )
65- it . only ( 'throws when decrypting with wrong private key', async ( ) => {
77+ it ( 'Returns Err result when decrypting with wrong private key', async ( ) => {
6678 const plaintext = 'Hello, World!'
6779 const keyPair = ( await generateKeyPair ( ) ) . unwrap ( )
6880 const encrypted = ( await encrypt ( keyPair . publicKey , plaintext ) ) . unwrap ( )
@@ -75,15 +87,27 @@ describe('decrypt', () => {
7587 // @ts -expect-error
7688 expect ( toOneLine ( ( await decrypt ( { } , encrypted ) ) . val . toString ( ) ) ) . toBe (
7789 toOneLine (
78- `because encrypting failed due to invalid private key type. Expected "string", received "[object Object]"`
90+ `because decrypting failed due to invalid private key type. Expected "string", received "[object Object]". `
7991 )
8092 )
8193 } )
82- it ( 'throws when input isnt a string' , async ( ) => {
94+ it ( 'Returns Err result when input isnt a string' , async ( ) => {
8395 const keyPair = ( await generateKeyPair ( ) ) . unwrap ( )
96+
8497 // @ts -expect-error
85- await expect ( decrypt ( keyPair . privateKey , { } ) ) . rejects . toThrow ( )
86- await expect ( decrypt ( keyPair . privateKey , 'invalid ciphertext' ) ) . rejects . toThrow ( )
98+ expect ( toOneLine ( ( await decrypt ( keyPair . privateKey , { } ) ) . val . toString ( ) ) ) . toBe (
99+ toOneLine (
100+ `because decrypting failed due to invalid ciphertext type. Expected "string", received "[object Object]".`
101+ )
102+ )
103+
104+ expect (
105+ toOneLine ( ( await decrypt ( keyPair . privateKey , 'invalid ciphertext' ) ) . val . toString ( ) )
106+ ) . toBe (
107+ toOneLine (
108+ `because base64 decoding the value "invalid ciphertext" errored. Error: InvalidCharacterError: The string to be decoded is not correctly encoded.`
109+ )
110+ )
87111 } )
88112
89113 it ( 'decrypts large amount of text' , async ( ) => {
0 commit comments