-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from SuperFlyTV/fix/nrcs-returns-empty-time-ob…
…ject fix: NRCS returns empty objects
- Loading branch information
Showing
6 changed files
with
260 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
packages/connector/src/__tests__/Profile0-open-media.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import { | ||
checkMessageSnapshot, | ||
clearMocks, | ||
decode, | ||
doBeforeAll, | ||
encode, | ||
getMessageId, | ||
getMosConnection, | ||
getMosDevice, | ||
getXMLReply, | ||
setupMocks, | ||
} from './lib' | ||
import { MosConnection, MosDevice, IMOSObject, IMOSListMachInfo } from '..' | ||
import { SocketMock } from '../__mocks__/socket' | ||
import { xmlData, xmlApiData } from '../__mocks__/testData' | ||
|
||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
// @ts-ignore imports are unused | ||
import { Socket } from 'net' | ||
/* eslint-enable @typescript-eslint/no-unused-vars */ | ||
|
||
beforeAll(() => { | ||
setupMocks() | ||
}) | ||
beforeEach(() => { | ||
clearMocks() | ||
}) | ||
describe('Profile 0 - non strict', () => { | ||
let mosDevice: MosDevice | ||
let mosConnection: MosConnection | ||
|
||
let socketMockLower: SocketMock | ||
let socketMockUpper: SocketMock | ||
let socketMockQuery: SocketMock | ||
|
||
let serverSocketMockLower: SocketMock | ||
let serverSocketMockUpper: SocketMock | ||
let serverSocketMockQuery: SocketMock | ||
|
||
let onRequestMachineInfo: jest.Mock<any, any> | ||
let onRequestMOSObject: jest.Mock<any, any> | ||
let onRequestAllMOSObjects: jest.Mock<any, any> | ||
|
||
beforeAll(async () => { | ||
mosConnection = await getMosConnection( | ||
{ | ||
'0': true, | ||
'1': true, // Must support at least one other profile | ||
}, | ||
false | ||
) | ||
mosDevice = await getMosDevice(mosConnection) | ||
|
||
// Profile 0: | ||
onRequestMachineInfo = jest.fn(async () => { | ||
return xmlApiData.machineInfo | ||
}) | ||
mosDevice.onRequestMachineInfo(async (): Promise<IMOSListMachInfo> => { | ||
return onRequestMachineInfo() | ||
}) | ||
// Profile 1: | ||
onRequestMOSObject = jest.fn(async () => { | ||
return xmlApiData.mosObj | ||
}) | ||
onRequestAllMOSObjects = jest.fn(async () => { | ||
return [xmlApiData.mosObj, xmlApiData.mosObj2] | ||
}) | ||
mosDevice.onRequestMOSObject(async (objId: string): Promise<IMOSObject | null> => { | ||
return onRequestMOSObject(objId) | ||
}) | ||
mosDevice.onRequestAllMOSObjects(async (): Promise<Array<IMOSObject>> => { | ||
return onRequestAllMOSObjects() | ||
}) | ||
const b = doBeforeAll() | ||
socketMockLower = b.socketMockLower | ||
socketMockUpper = b.socketMockUpper | ||
socketMockQuery = b.socketMockQuery | ||
serverSocketMockLower = b.serverSocketMockLower | ||
serverSocketMockUpper = b.serverSocketMockUpper | ||
serverSocketMockQuery = b.serverSocketMockQuery | ||
|
||
mosDevice.checkProfileValidness() | ||
mosConnection.checkProfileValidness() | ||
}) | ||
afterAll(async () => { | ||
await mosDevice.dispose() | ||
await mosConnection.dispose() | ||
}) | ||
beforeEach(() => { | ||
onRequestMOSObject.mockClear() | ||
onRequestAllMOSObjects.mockClear() | ||
|
||
serverSocketMockLower.mockClear() | ||
serverSocketMockUpper.mockClear() | ||
if (serverSocketMockQuery) serverSocketMockQuery.mockClear() | ||
socketMockLower.mockClear() | ||
socketMockUpper.mockClear() | ||
if (socketMockQuery) socketMockQuery.mockClear() | ||
}) | ||
test('init', async () => { | ||
expect(mosDevice).toBeTruthy() | ||
expect(socketMockLower).toBeTruthy() | ||
expect(socketMockUpper).toBeTruthy() | ||
expect(serverSocketMockLower).toBeTruthy() | ||
}) | ||
test('requestMachineInfo - missing <time>', async () => { | ||
// Prepare mock server response: | ||
const mockReply = jest.fn((data) => { | ||
const str = decode(data) | ||
const messageID = getMessageId(str) | ||
const replyMessage = xmlData.machineInfoOpenMedia.replace(/<time>.*<\/time>/, '') | ||
const repl = getXMLReply(messageID, replyMessage) | ||
return encode(repl) | ||
}) | ||
socketMockLower.mockAddReply(mockReply) | ||
if (!xmlApiData.mosObj.ID) throw new Error('xmlApiData.mosObj.ID not set') | ||
|
||
const returnedMachineInfo: IMOSListMachInfo = await mosDevice.requestMachineInfo() | ||
expect(mockReply).toHaveBeenCalledTimes(1) | ||
const msg = decode(mockReply.mock.calls[0][0]) | ||
expect(msg).toMatch(/<reqMachInfo\/>/) | ||
checkMessageSnapshot(msg) | ||
|
||
const replyMessage = { ...xmlApiData.machineInfoOpenMediaReply } | ||
replyMessage.time = returnedMachineInfo.time | ||
replyMessage.opTime = returnedMachineInfo.opTime | ||
|
||
expect(returnedMachineInfo).toMatchObject(replyMessage) | ||
// expect(returnedMachineInfo.opTime).toBeUndefined() | ||
}) | ||
test('requestMachineInfo - empty <time>', async () => { | ||
// Prepare mock server response: | ||
const mockReply = jest.fn((data) => { | ||
const str = decode(data) | ||
const messageID = getMessageId(str) | ||
const replyMessage = xmlData.machineInfoOpenMedia.replace(/<time>.*<\/time>/, '<time></time>') | ||
const repl = getXMLReply(messageID, replyMessage) | ||
return encode(repl) | ||
}) | ||
socketMockLower.mockAddReply(mockReply) | ||
if (!xmlApiData.mosObj.ID) throw new Error('xmlApiData.mosObj.ID not set') | ||
|
||
const returnedMachineInfo: IMOSListMachInfo = await mosDevice.requestMachineInfo() | ||
expect(mockReply).toHaveBeenCalledTimes(1) | ||
const msg = decode(mockReply.mock.calls[0][0]) | ||
expect(msg).toMatch(/<reqMachInfo\/>/) | ||
checkMessageSnapshot(msg) | ||
|
||
const replyMessage = { ...xmlApiData.machineInfoOpenMediaReply } | ||
replyMessage.time = returnedMachineInfo.time | ||
replyMessage.opTime = returnedMachineInfo.opTime | ||
|
||
expect(returnedMachineInfo).toMatchObject(replyMessage) | ||
// expect(returnedMachineInfo.opTime).toBeUndefined() | ||
}) | ||
test('requestMachineInfo - bad formatted <time>', async () => { | ||
// Prepare mock server response: | ||
const mockReply = jest.fn((data) => { | ||
const str = decode(data) | ||
const messageID = getMessageId(str) | ||
const replyMessage = xmlData.machineInfoOpenMedia.replace(/<time>.*<\/time>/, '<time>BAD DATA</time>') | ||
const repl = getXMLReply(messageID, replyMessage) | ||
return encode(repl) | ||
}) | ||
socketMockLower.mockAddReply(mockReply) | ||
if (!xmlApiData.mosObj.ID) throw new Error('xmlApiData.mosObj.ID not set') | ||
|
||
let caughtError: Error | undefined = undefined | ||
await mosDevice.requestMachineInfo().catch((err) => { | ||
caughtError = err | ||
}) | ||
expect(mockReply).toHaveBeenCalledTimes(1) | ||
|
||
expect(String(caughtError)).toMatch(/Unable to parse MOS reply.*Invalid timestamp/i) | ||
}) | ||
test('requestMachineInfo - missing <opTime>', async () => { | ||
// Prepare mock server response: | ||
const mockReply = jest.fn((data) => { | ||
const str = decode(data) | ||
const replyMessage = xmlData.machineInfoOpenMedia.replace(/<opTime>.*<\/opTime>/, '') | ||
const repl = getXMLReply(getMessageId(str), replyMessage) | ||
return encode(repl) | ||
}) | ||
socketMockLower.mockAddReply(mockReply) | ||
if (!xmlApiData.mosObj.ID) throw new Error('xmlApiData.mosObj.ID not set') | ||
|
||
const returnedMachineInfo: IMOSListMachInfo = await mosDevice.requestMachineInfo() | ||
expect(mockReply).toHaveBeenCalledTimes(1) | ||
const msg = decode(mockReply.mock.calls[0][0]) | ||
expect(msg).toMatch(/<reqMachInfo\/>/) | ||
checkMessageSnapshot(msg) | ||
|
||
const replyMessage = { ...xmlApiData.machineInfoOpenMediaReply } | ||
replyMessage.opTime = returnedMachineInfo.opTime | ||
replyMessage.time = returnedMachineInfo.time | ||
|
||
expect(returnedMachineInfo).toMatchObject(replyMessage) | ||
expect(returnedMachineInfo.opTime).toBeUndefined() | ||
}) | ||
}) |
28 changes: 28 additions & 0 deletions
28
packages/connector/src/__tests__/__snapshots__/Profile0-open-media.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Profile 0 - non strict requestMachineInfo - empty <time> 1`] = ` | ||
"<mos> | ||
<ncsID>their.mos.id</ncsID> | ||
<mosID>our.mos.id</mosID> | ||
<messageID>xx</messageID> | ||
<reqMachInfo/> | ||
</mos>" | ||
`; | ||
exports[`Profile 0 - non strict requestMachineInfo - missing <opTime> 1`] = ` | ||
"<mos> | ||
<ncsID>their.mos.id</ncsID> | ||
<mosID>our.mos.id</mosID> | ||
<messageID>xx</messageID> | ||
<reqMachInfo/> | ||
</mos>" | ||
`; | ||
exports[`Profile 0 - non strict requestMachineInfo - missing <time> 1`] = ` | ||
"<mos> | ||
<ncsID>their.mos.id</ncsID> | ||
<mosID>our.mos.id</mosID> | ||
<messageID>xx</messageID> | ||
<reqMachInfo/> | ||
</mos>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters