-
Notifications
You must be signed in to change notification settings - Fork 58
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
3 changed files
with
21 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,51 @@ | ||
import {EBB} from "../ebb"; | ||
import SerialPort from "serialport"; | ||
import {SerialPortSerialPort} from "../serialport-serialport"; | ||
import { default as NodeSerialPort } from "serialport"; | ||
import MockBinding from "@serialport/binding-mock"; | ||
|
||
(() => { | ||
let oldBinding: any; | ||
beforeAll(() => { | ||
oldBinding = SerialPort.Binding; | ||
SerialPort.Binding = MockBinding; | ||
oldBinding = NodeSerialPort.Binding; | ||
NodeSerialPort.Binding = MockBinding; | ||
}); | ||
afterAll(() => { | ||
SerialPort.Binding = oldBinding; | ||
NodeSerialPort.Binding = oldBinding; | ||
MockBinding.reset(); | ||
}); | ||
})(); | ||
|
||
describe("EBB.list", () => { | ||
afterEach(() => { | ||
MockBinding.reset(); | ||
}) | ||
|
||
it("is empty when no serial ports are available", async () => { | ||
expect(await EBB.list()).toEqual([]) | ||
}) | ||
|
||
it("doesn't return a port that doesn't look like an EBB", async () => { | ||
MockBinding.createPort('/dev/nonebb'); | ||
expect(await EBB.list()).toEqual([]) | ||
}) | ||
|
||
it("returns a port that does look like an EBB", async () => { | ||
MockBinding.createPort('/dev/ebb', { manufacturer: "SchmalzHaus" }); | ||
expect(await EBB.list()).toEqual(["/dev/ebb"]) | ||
}) | ||
|
||
it("handles 'SchmalzHaus LLC'", async () => { | ||
MockBinding.createPort('/dev/ebb', { manufacturer: "SchmalzHaus LLC" }); | ||
expect(await EBB.list()).toEqual(["/dev/ebb"]) | ||
}) | ||
|
||
it("handles no manufacturer but vendor id / product id", async () => { | ||
MockBinding.createPort('/dev/ebb', { vendorId: "04D8", productId: "FD92" }); | ||
expect(await EBB.list()).toEqual(["/dev/ebb"]) | ||
}) | ||
}) | ||
|
||
describe("EBB", () => { | ||
afterEach(() => { | ||
MockBinding.reset(); | ||
}) | ||
|
||
type TestPort = SerialPort & { | ||
binding: SerialPort.BaseBinding & { | ||
recording: Buffer; | ||
emitData: (data: Buffer) => void; | ||
}; | ||
}; | ||
|
||
async function openTestPort(path = '/dev/ebb'): Promise<TestPort> { | ||
async function openTestPort(path = '/dev/ebb'): Promise<SerialPort> { | ||
MockBinding.createPort(path, {record: true}); | ||
const port = new SerialPort(path); | ||
await new Promise(resolve => port.on('open', resolve)); | ||
const port = new SerialPortSerialPort(path); | ||
await port.open({ baudRate: 9600 }) | ||
return port as any; | ||
} | ||
|
||
it("firmware version", async () => { | ||
const port = await openTestPort(); | ||
const ebb = new EBB(port); | ||
port.binding.emitData(Buffer.from('aoeu\r\n')); | ||
(port as any)._port.binding.emitData(Buffer.from('aoeu\r\n')); | ||
expect(await ebb.firmwareVersion()).toEqual('aoeu'); | ||
expect(port.binding.recording).toEqual(Buffer.from("V\r")); | ||
expect((port as any)._port.binding.recording).toEqual(Buffer.from("V\r")); | ||
}) | ||
|
||
it("enable motors", async () => { | ||
const port = await openTestPort(); | ||
const ebb = new EBB(port); | ||
const oldWrite = port.write | ||
port.write = (data: string | Buffer | number[], ...args: any[]) => { | ||
if (data === 'V\r') | ||
port.binding.emitData(Buffer.from('test 2.5.3\r\n')) | ||
return oldWrite.apply(port, [data, ...args]) | ||
} | ||
port.binding.emitData(Buffer.from('OK\r\n')); | ||
const oldWrite = (port as any)._port.write; | ||
(port as any)._port.write = (data: string | Buffer | number[], ...args: any[]) => { | ||
if (data.toString() === 'V\r') | ||
(port as any)._port.binding.emitData(Buffer.from('test 2.5.3\r\n')) | ||
return oldWrite.apply((port as any)._port, [data, ...args]) | ||
}; | ||
(port as any)._port.binding.emitData(Buffer.from('OK\r\n')); | ||
await ebb.enableMotors(2); | ||
expect(port.binding.recording).toEqual(Buffer.from("EM,2,2\rV\r")); | ||
expect((port as any)._port.binding.recording).toEqual(Buffer.from("EM,2,2\rV\r")); | ||
}) | ||
}) |
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