-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathSubstitutionCipher.test.js
44 lines (36 loc) · 1.35 KB
/
SubstitutionCipher.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { describe, it, expect } from 'vitest'
import {
substitutionCipherEncryption,
substitutionCipherDecryption
} from '../SubstitutionCipher.js'
describe('Substitution Cipher', () => {
const key = 'QWERTYUIOPASDFGHJKLZXCVBNM'
it('correctly encrypts a message', () => {
const encrypted = substitutionCipherEncryption('HELLO WORLD', key)
expect(encrypted).toBe('ITSSG VGKSR')
})
it('correctly decrypts a message', () => {
const decrypted = substitutionCipherDecryption('ITSSG VGKSR', key)
expect(decrypted).toBe('HELLO WORLD')
})
it('handles non-alphabetic characters', () => {
const encrypted = substitutionCipherEncryption('Test! 123', key)
expect(encrypted).toBe('ZTLZ! 123')
})
it('throws error for invalid key', () => {
expect(() => substitutionCipherEncryption('HELLO', 'BADKEY')).toThrow(
RangeError
)
})
it('encrypts using default key if none provided', () => {
const encrypted = substitutionCipherEncryption('HELLO WORLD')
expect(encrypted).toBe('ITSSG VGKSR')
})
it('decrypts using default key if none provided', () => {
const decrypted = substitutionCipherDecryption('ITSSG VGKSR')
expect(decrypted).toBe('HELLO WORLD')
})
it('throws error for invalid key in decryption', () => {
expect(() => substitutionCipherDecryption('HELLO', 'BADKEY')).toThrow(RangeError)
})
})