-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
131 additions
and
155 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
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 |
---|---|---|
@@ -1,137 +1,126 @@ | ||
// import * as chai from 'chai'; | ||
// import { _testing } from '../api'; | ||
// import { SerialPort, SerialPortMock } from 'serialport'; | ||
|
||
// const expect = chai.expect; | ||
|
||
// const API = _testing.API; | ||
|
||
// suite('API Test Suite', function () { | ||
// const defaultPortName = '/dev/echoserialport'; | ||
|
||
// const createPortMock = (data: string, opts?: any) => { | ||
// return SerialPortMock.binding.createPort(defaultPortName, { | ||
// echo: true, | ||
// record: true, | ||
// ...opts, | ||
// readyData: Buffer.from(data), | ||
// }); | ||
// }; | ||
|
||
// suite('Waits for the API to be ready', function () { | ||
// test.only('Waits for API to be ready', async function () { | ||
// createPortMock('{"m":0, "r": "response", "i": "1234"}\r'); | ||
// const port = new SerialPort({ path: defaultPortName, baudRate: 9600 }); | ||
// const api = new API(port); | ||
|
||
// await api.waitAPIready(); | ||
|
||
// const portMock = await SerialPortMock.binding.open({ | ||
// path: defaultPortName, | ||
// baudRate: 9600, | ||
// }); | ||
// expect(portMock.recording.toString()).to.equal('\x04\r'); | ||
// }); | ||
|
||
// test('Times out if the device does not emit the correct sequence', async function () { | ||
// createPortMock(''); | ||
// const port = new SerialPort({ path: defaultPortName, baudRate: 9600 }); | ||
// const api = new API(port); | ||
|
||
// try { | ||
// await api.waitAPIready(500); | ||
// } catch (error) { | ||
// expect(error).instanceOf(Error); | ||
// expect((error as Error).message).to.equal( | ||
// "Timeout while processing message '\x04'", | ||
// ); | ||
// } | ||
// }); | ||
// }); | ||
|
||
// suite('Sends request', function () { | ||
// test('and retrieve the corresponding response', async function () { | ||
// const reqId = '1234'; | ||
|
||
// createPortMock(`{"m":0, "r": "response", "i": "${reqId}"}\r`); | ||
// const portMock = await SerialPortMock.binding.open({ | ||
// path: defaultPortName, | ||
// baudRate: 9600, | ||
// }); | ||
// const port = new SerialPort({ path: defaultPortName, baudRate: 9600 }); | ||
// const api = new API(port); | ||
|
||
// const response = await api.sendRequest( | ||
// 'request', | ||
// { param1: '1', param2: 2 }, | ||
// 500, | ||
// undefined, | ||
// reqId, | ||
// ); | ||
|
||
// expect(portMock.recording.toString()).to.equal( | ||
// '{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\r', | ||
// ); | ||
// expect(response).to.equal('response'); | ||
// }); | ||
|
||
// test('and raises an exception in case of error', async function () { | ||
// const reqId = '1234'; | ||
|
||
// createPortMock( | ||
// `{"m":0, "e": "eyJtZXNzYWdlIjogImVycm9yIn0=", "i": "${reqId}"}\r`, | ||
// ); | ||
// const portMock = await SerialPortMock.binding.open({ | ||
// path: defaultPortName, | ||
// baudRate: 9600, | ||
// }); | ||
// const port = new SerialPort({ path: defaultPortName, baudRate: 9600 }); | ||
// const api = new API(port); | ||
|
||
// try { | ||
// await api.sendRequest( | ||
// 'request', | ||
// { param1: '1', param2: 2 }, | ||
// 500, | ||
// undefined, | ||
// reqId, | ||
// ); | ||
// } catch (error) { | ||
// expect(portMock.recording.toString()).to.equal( | ||
// '{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\r', | ||
// ); | ||
// expect(error).instanceOf(Error); | ||
// expect((error as Error).message).to.equal('error'); | ||
// } | ||
// }); | ||
|
||
// test('and times out if the response does not come back in time', async function () { | ||
// const reqId = '1234'; | ||
// createPortMock('', { echo: false }); | ||
// const portMock = await SerialPortMock.binding.open({ | ||
// path: defaultPortName, | ||
// baudRate: 9600, | ||
// }); | ||
// const port = new SerialPort({ path: defaultPortName, baudRate: 9600 }); | ||
// const api = new API(port); | ||
|
||
// try { | ||
// await api.sendRequest( | ||
// 'request', | ||
// { param1: '1', param2: 2 }, | ||
// 500, | ||
// undefined, | ||
// reqId, | ||
// ); | ||
// } catch (error) { | ||
// expect(portMock.recording.toString()).to.equal( | ||
// '{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\r', | ||
// ); | ||
// expect(error).instanceOf(Error); | ||
// expect((error as Error).message).to.equal( | ||
// 'Timeout while processing message \'{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\'', | ||
// ); | ||
// } | ||
// }); | ||
// }); | ||
// }); | ||
import { expect } from 'chai'; | ||
import { APIRequest } from '../api'; | ||
import { SerialPortMock } from 'serialport'; | ||
|
||
describe('API Test Suite', function () { | ||
const defaultPortName = '/dev/echoserialport'; | ||
|
||
const createPortMock = (data: string, opts?: any) => { | ||
return SerialPortMock.binding.createPort(defaultPortName, { | ||
echo: true, | ||
record: true, | ||
...opts, | ||
readyData: Buffer.from(data), | ||
}); | ||
}; | ||
|
||
describe('Sends request', function () { | ||
it('retrieves the corresponding response', async function () { | ||
const reqId = '1234'; | ||
createPortMock(`{"m":0, "r": "response", "i": "${reqId}"}\r`); | ||
const portMock = new SerialPortMock({ | ||
path: defaultPortName, | ||
baudRate: 9600, | ||
}); | ||
const response = await APIRequest( | ||
portMock, | ||
'request', | ||
{ param1: '1', param2: 2 }, | ||
500, | ||
undefined, | ||
reqId, | ||
); | ||
|
||
expect(portMock.port?.recording.toString()).to.equal( | ||
'{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\r', | ||
); | ||
expect(response).to.equal('response'); | ||
}); | ||
|
||
it('raises an exception in case of error', async function () { | ||
const reqId = '1234'; | ||
|
||
createPortMock( | ||
`{"m":0, "e": "eyJtZXNzYWdlIjogImVycm9yIn0=", "i": "${reqId}"}\r`, | ||
); | ||
const portMock = new SerialPortMock({ | ||
path: defaultPortName, | ||
baudRate: 9600, | ||
}); | ||
|
||
try { | ||
await APIRequest( | ||
portMock, | ||
'request', | ||
{ param1: '1', param2: 2 }, | ||
500, | ||
undefined, | ||
reqId, | ||
); | ||
} catch (error) { | ||
expect(portMock.port?.recording.toString()).to.equal( | ||
'{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\r', | ||
); | ||
expect(error).instanceOf(Error); | ||
expect((error as Error).message).to.equal('error'); | ||
} | ||
}); | ||
|
||
it("times out if the response doesn't come back in time", async function () { | ||
const reqId = '1234'; | ||
createPortMock('', { echo: false }); | ||
const portMock = new SerialPortMock({ | ||
path: defaultPortName, | ||
baudRate: 9600, | ||
}); | ||
|
||
try { | ||
await APIRequest( | ||
portMock, | ||
'request', | ||
{ param1: '1', param2: 2 }, | ||
20, | ||
undefined, | ||
reqId, | ||
); | ||
} catch (error) { | ||
expect(portMock.port?.recording.toString()).to.equal( | ||
'{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\r', | ||
); | ||
expect(error).instanceOf(Error); | ||
expect((error as Error).message).to.equal( | ||
'Timeout while processing message \'{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\'', | ||
); | ||
} | ||
}); | ||
|
||
it('uses the command resolver if provided', async function () { | ||
const reqId = '1234'; | ||
createPortMock( | ||
`A line\r\nAnother line\r\ntreasure\r\nAnd a final line\r\n`, | ||
); | ||
const portMock = new SerialPortMock({ | ||
path: defaultPortName, | ||
baudRate: 9600, | ||
}); | ||
const response = await APIRequest( | ||
portMock, | ||
'request', | ||
{ param1: '1', param2: 2 }, | ||
500, | ||
(line) => { | ||
if (line.includes('treasure')) { | ||
return { resolve: true, returnValue: 'Success' }; | ||
} | ||
return { resolve: false }; | ||
}, | ||
reqId, | ||
); | ||
|
||
expect(portMock.port?.recording.toString()).to.equal( | ||
'{"m":"request","p":{"param1":"1","param2":2},"i":"1234"}\r', | ||
); | ||
expect(response).to.equal('Success'); | ||
}); | ||
}); | ||
}); |