|
| 1 | +import BaseCall, { |
| 2 | + EXECUTE_ANSWER_ERROR_CAUSE_CODE, |
| 3 | + EXECUTE_ATTACH_ERROR_CAUSE_CODE, |
| 4 | + REMOTE_SDP_ERROR_CAUSE_CODE, |
| 5 | +} from '../../src/webrtc/BaseCall' |
| 6 | +import { Bye } from '../../src/messages/Verto' |
| 7 | +import { State } from '../../src/webrtc/constants' |
| 8 | + |
| 9 | +// Mock dependencies |
| 10 | +jest.mock('../../src/messages/Verto') |
| 11 | +jest.mock('../../src/util/logger', () => { |
| 12 | + return { |
| 13 | + __esModule: true, |
| 14 | + default: { |
| 15 | + trace: jest.fn(), |
| 16 | + debug: jest.fn(), |
| 17 | + error: jest.fn(), |
| 18 | + info: jest.fn(), |
| 19 | + warn: jest.fn(), |
| 20 | + }, |
| 21 | + } |
| 22 | +}) |
| 23 | + |
| 24 | +// Get mocked version |
| 25 | +const MockedBye = Bye as jest.MockedClass<typeof Bye> |
| 26 | + |
| 27 | +// Create a concrete implementation of the abstract BaseCall class for testing |
| 28 | +class TestCall extends BaseCall { |
| 29 | + constructor(session, opts) { |
| 30 | + super(session, opts) |
| 31 | + } |
| 32 | + |
| 33 | + invite() { |
| 34 | + throw new Error('Method not implemented.') |
| 35 | + } |
| 36 | + |
| 37 | + answer(params) { |
| 38 | + throw new Error('Method not implemented.') |
| 39 | + } |
| 40 | + |
| 41 | + // Add protected method accessor for testing |
| 42 | + testExecute(msg) { |
| 43 | + return this['_execute'](msg) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +describe('BaseCall', () => { |
| 48 | + let mockSession |
| 49 | + let call |
| 50 | + let mockExecute |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + jest.clearAllMocks() |
| 54 | + |
| 55 | + mockExecute = jest.fn().mockResolvedValue({}) |
| 56 | + |
| 57 | + mockSession = { |
| 58 | + sessionid: 'test-session-id', |
| 59 | + execute: mockExecute, |
| 60 | + uuid: 'test-uuid', |
| 61 | + mediaConstraints: { |
| 62 | + audio: true, |
| 63 | + video: false, |
| 64 | + }, |
| 65 | + options: { |
| 66 | + userVariables: {}, |
| 67 | + }, |
| 68 | + calls: {}, |
| 69 | + } |
| 70 | + |
| 71 | + call = new TestCall(mockSession, { |
| 72 | + id: 'test-call-id', |
| 73 | + destinationNumber: '1234567890', |
| 74 | + }) |
| 75 | + |
| 76 | + // Mock the internal execute method by overriding it directly |
| 77 | + call['_execute'] = mockExecute |
| 78 | + |
| 79 | + // Mock peer if needed |
| 80 | + call.peer = { |
| 81 | + instance: { |
| 82 | + close: jest.fn(), |
| 83 | + }, |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + describe('hangup method', () => { |
| 88 | + describe('cause and causeCode handling', () => { |
| 89 | + it('should use default cause and causeCode when not provided', () => { |
| 90 | + call.hangup() |
| 91 | + |
| 92 | + expect(call.cause).toBe('NORMAL_CLEARING') |
| 93 | + expect(call.causeCode).toBe(16) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should use custom cause and causeCode when provided', () => { |
| 97 | + call.hangup({ cause: 'USER_BUSY', causeCode: 17 }) |
| 98 | + |
| 99 | + expect(call.cause).toBe('USER_BUSY') |
| 100 | + expect(call.causeCode).toBe(17) |
| 101 | + }) |
| 102 | + |
| 103 | + it('should use error cause and code for REMOTE_SDP_ERROR_CAUSE_CODE', () => { |
| 104 | + call.hangup({ |
| 105 | + cause: 'NORMAL_CLEARING', |
| 106 | + causeCode: REMOTE_SDP_ERROR_CAUSE_CODE, |
| 107 | + }) |
| 108 | + |
| 109 | + expect(call.cause).toBe('NORMAL_CLEARING') |
| 110 | + expect(call.causeCode).toBe(REMOTE_SDP_ERROR_CAUSE_CODE) |
| 111 | + }) |
| 112 | + |
| 113 | + it('should use error cause and code for EXECUTE_ANSWER_ERROR_CAUSE_CODE', () => { |
| 114 | + call.hangup({ |
| 115 | + cause: 'NORMAL_CLEARING', |
| 116 | + causeCode: EXECUTE_ANSWER_ERROR_CAUSE_CODE, |
| 117 | + }) |
| 118 | + |
| 119 | + expect(call.cause).toBe('NORMAL_CLEARING') |
| 120 | + expect(call.causeCode).toBe(EXECUTE_ANSWER_ERROR_CAUSE_CODE) |
| 121 | + }) |
| 122 | + |
| 123 | + it('should use error cause and code for EXECUTE_ATTACH_ERROR_CAUSE_CODE', () => { |
| 124 | + call.hangup({ |
| 125 | + cause: 'NORMAL_CLEARING', |
| 126 | + causeCode: EXECUTE_ATTACH_ERROR_CAUSE_CODE, |
| 127 | + }) |
| 128 | + |
| 129 | + expect(call.cause).toBe('NORMAL_CLEARING') |
| 130 | + expect(call.causeCode).toBe(EXECUTE_ATTACH_ERROR_CAUSE_CODE) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + describe('Bye message creation', () => { |
| 135 | + it('should create Bye message with cause and causeCode when execute is true', () => { |
| 136 | + const mockByeInstance = { method: 'verto.bye' } as any |
| 137 | + MockedBye.mockImplementation(() => mockByeInstance) |
| 138 | + |
| 139 | + call.hangup({ cause: 'TEST_CAUSE', causeCode: 999 }) |
| 140 | + |
| 141 | + expect(MockedBye).toHaveBeenCalledWith({ |
| 142 | + sessid: 'test-session-id', |
| 143 | + dialogParams: call.options, |
| 144 | + cause: 'TEST_CAUSE', |
| 145 | + causeCode: 999, |
| 146 | + }) |
| 147 | + }) |
| 148 | + |
| 149 | + it('should not create Bye message when execute is false', () => { |
| 150 | + call.hangup({}, false) |
| 151 | + |
| 152 | + expect(MockedBye).not.toHaveBeenCalled() |
| 153 | + }) |
| 154 | + |
| 155 | + it('should execute the Bye message when execute is true', () => { |
| 156 | + const mockByeInstance = { method: 'verto.bye' } as any |
| 157 | + MockedBye.mockImplementation(() => mockByeInstance) |
| 158 | + |
| 159 | + call.hangup() |
| 160 | + |
| 161 | + expect(mockExecute).toHaveBeenCalledWith(mockByeInstance) |
| 162 | + }) |
| 163 | + }) |
| 164 | + }) |
| 165 | + |
| 166 | + describe('verto.bye message payload', () => { |
| 167 | + it('should include cause and causeCode in the Bye message parameters', () => { |
| 168 | + const mockByeInstance = { method: 'verto.bye' } as any |
| 169 | + MockedBye.mockImplementation(() => mockByeInstance) |
| 170 | + |
| 171 | + call.hangup({ cause: 'CUSTOM_CAUSE', causeCode: 123 }) |
| 172 | + |
| 173 | + const byeCallArgs = MockedBye.mock.calls[0][0] |
| 174 | + expect(byeCallArgs).toEqual({ |
| 175 | + sessid: 'test-session-id', |
| 176 | + dialogParams: call.options, |
| 177 | + cause: 'CUSTOM_CAUSE', |
| 178 | + causeCode: 123, |
| 179 | + }) |
| 180 | + }) |
| 181 | + |
| 182 | + it('should include session ID and dialog parameters', () => { |
| 183 | + const mockByeInstance = { method: 'verto.bye' } as any |
| 184 | + MockedBye.mockImplementation(() => mockByeInstance) |
| 185 | + |
| 186 | + call.hangup() |
| 187 | + |
| 188 | + const byeCallArgs = MockedBye.mock.calls[0][0] |
| 189 | + expect(byeCallArgs.sessid).toBe('test-session-id') |
| 190 | + expect(byeCallArgs.dialogParams).toBe(call.options) |
| 191 | + }) |
| 192 | + |
| 193 | + it('should use default values when no parameters provided', () => { |
| 194 | + const mockByeInstance = { method: 'verto.bye' } as any |
| 195 | + MockedBye.mockImplementation(() => mockByeInstance) |
| 196 | + |
| 197 | + call.hangup() |
| 198 | + |
| 199 | + const byeCallArgs = MockedBye.mock.calls[0][0] |
| 200 | + expect(byeCallArgs.cause).toBe('NORMAL_CLEARING') |
| 201 | + expect(byeCallArgs.causeCode).toBe(16) |
| 202 | + }) |
| 203 | + }) |
| 204 | +}) |
0 commit comments