Skip to content

Commit

Permalink
test: fix ebb tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Jul 1, 2023
1 parent e0cd773 commit 45ee2f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 54 deletions.
72 changes: 18 additions & 54 deletions src/__tests__/ebb.test.ts
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"));
})
})
1 change: 1 addition & 0 deletions src/ebb.ts
Original file line number Diff line number Diff line change
@@ -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] {
Expand Down
2 changes: 2 additions & 0 deletions src/regex-transform-stream.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 45ee2f0

Please sign in to comment.