diff --git a/src/__tests__/ebb.test.ts b/src/__tests__/ebb.test.ts index dd376a2..49c6253 100644 --- a/src/__tests__/ebb.test.ts +++ b/src/__tests__/ebb.test.ts @@ -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 { + async function openTestPort(path = '/dev/ebb'): Promise { 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")); }) }) diff --git a/src/ebb.ts b/src/ebb.ts index 7f12aea..c9a7100 100644 --- a/src/ebb.ts +++ b/src/ebb.ts @@ -1,6 +1,7 @@ import {Block, Motion, PenMotion, Plan, XYMotion} from "./planning"; import { RegexParser } from "./regex-transform-stream"; import {Vec2, vsub} from "./vec"; +import { TextEncoder } from "util"; /** Split d into its fractional and integral parts */ function modf(d: number): [number, number] { diff --git a/src/regex-transform-stream.ts b/src/regex-transform-stream.ts index 97d4e46..f0b3d54 100644 --- a/src/regex-transform-stream.ts +++ b/src/regex-transform-stream.ts @@ -1,3 +1,5 @@ +import "web-streams-polyfill/es2018" +import { TextDecoder } from "util" export class RegexParser extends TransformStream { public constructor(opts: { regex: RegExp }) { if (opts.regex === undefined) {