-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathnativeMessagingProtocol.test.js
More file actions
91 lines (77 loc) · 2.62 KB
/
Copy pathnativeMessagingProtocol.test.js
File metadata and controls
91 lines (77 loc) · 2.62 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { TextEncoder } from 'util'
import { unwrapMessage } from './nativeMessagingProtocol'
import {
NATIVE_MESSAGING_CONFIG,
NATIVE_MESSAGING_ERRORS
} from '../shared/constants/nativeMessaging'
import { logger } from '../shared/utils/logger'
global.TextEncoder = TextEncoder
jest.mock('../shared/utils/logger', () => ({
logger: {
error: jest.fn()
}
}))
describe('unwrapMessage', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('should correctly unwrap a valid message and return the message payload', () => {
const messagePayload = { action: 'ping', data: 'test' }
const wrappedMessage = {
length: 31, // Calculated length of JSON.stringify(messagePayload)
message: messagePayload
}
const result = unwrapMessage(wrappedMessage)
expect(result).toEqual(messagePayload)
expect(logger.error).not.toHaveBeenCalled()
})
it('should return null and log an error if the wrapped message has an invalid structure', () => {
const invalidMessages = [
null,
undefined,
{},
{ length: 10 },
{ message: 'test' },
{ length: '10', message: 'test' }
]
invalidMessages.forEach((invalidMessage) => {
const result = unwrapMessage(invalidMessage)
expect(result).toBeNull()
expect(logger.error).toHaveBeenCalledWith(
NATIVE_MESSAGING_CONFIG.PROTOCOL_PREFIX,
NATIVE_MESSAGING_ERRORS.INVALID_MESSAGE_STRUCTURE
)
jest.clearAllMocks()
})
})
it('should return null and log an error if the message length does not match the provided length', () => {
const messagePayload = { data: 'test' }
const actualLength = 15 // Real length of JSON.stringify(messagePayload)
const incorrectLength = 10
const wrappedMessage = {
length: incorrectLength,
message: messagePayload
}
const result = unwrapMessage(wrappedMessage)
expect(result).toBeNull()
expect(logger.error).toHaveBeenCalledWith(
NATIVE_MESSAGING_CONFIG.PROTOCOL_PREFIX,
`${NATIVE_MESSAGING_ERRORS.LENGTH_MISMATCH} - expected: ${incorrectLength}, actual: ${actualLength}`
)
})
it('should correctly handle messages with unicode characters', () => {
const messagePayload = { data: '123' }
// JSON.stringify({data:"123"}) is '{"data":"123"}'
// The byte length is 16
const actualLength = new TextEncoder().encode(
JSON.stringify(messagePayload)
).length
const wrappedMessage = {
length: actualLength,
message: messagePayload
}
const result = unwrapMessage(wrappedMessage)
expect(result).toEqual(messagePayload)
expect(logger.error).not.toHaveBeenCalled()
})
})