185 lines
4.2 KiB
JavaScript
185 lines
4.2 KiB
JavaScript
const Robot = require('../robot/kinematics/Arm3SegmentLinearX')
|
|
const GCode = require('../robot/GCode.js');
|
|
const MockCmdReceiver = require('./helpers/mockCmdReceiver')
|
|
|
|
describe('Robot.sendCommand & cmdReceivers', () => {
|
|
|
|
let robot
|
|
|
|
|
|
beforeAll(() => {
|
|
jest.spyOn(console, 'log').mockImplementation(() => {})
|
|
})
|
|
|
|
afterAll(() => {
|
|
jest.restoreAllMocks()
|
|
})
|
|
|
|
|
|
beforeEach(() => {
|
|
robot = new Robot(10, 10, 5)
|
|
|
|
// definierten Startzustand erzwingen
|
|
robot.xMotor = 1
|
|
robot.alpha = 2
|
|
robot.beta = 3
|
|
robot.a = 4
|
|
robot.b = 5
|
|
robot.c = 6
|
|
robot.eMotor = 7
|
|
})
|
|
|
|
test('sendCommand ruft Receiver.moveTo genau einmal auf', () => {
|
|
const r = new MockCmdReceiver()
|
|
|
|
robot.cmdReceivers.push(r)
|
|
robot.sendCommand()
|
|
|
|
expect(r.callCount).toBe(1)
|
|
})
|
|
|
|
test('moveTo bekommt oldPos und newPos', () => {
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
|
|
robot.sendCommand()
|
|
|
|
const call = r.lastCall()
|
|
expect(call.oldPos).toBeDefined()
|
|
expect(call.newPos).toBeDefined()
|
|
})
|
|
|
|
test('oldPos und newPos sind unterschiedliche Objekte', () => {
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
|
|
robot.sendCommand()
|
|
|
|
const { oldPos, newPos } = r.lastCall()
|
|
expect(oldPos).not.toBe(newPos)
|
|
})
|
|
|
|
test('oldPos enthält vorherige Motorwerte', () => {
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
|
|
robot.sendCommand()
|
|
|
|
const { oldPos } = r.lastCall()
|
|
expect(oldPos.x).toBe(1)
|
|
expect(oldPos.y).toBe(2)
|
|
expect(oldPos.z).toBe(3)
|
|
})
|
|
|
|
test('newPos enthält aktuelle Motorwerte', () => {
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
|
|
robot.xMotor = 10
|
|
robot.alpha = 20
|
|
|
|
robot.sendCommand()
|
|
|
|
const { newPos } = r.lastCall()
|
|
expect(newPos.x).toBe(10)
|
|
expect(newPos.y).toBe(20)
|
|
})
|
|
|
|
test('newPos enthält aktuelle Motorwerte GCode', () => {
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
|
|
GCode.receiveGCode(robot, "G90 G1 x10 y200 z0 f300")
|
|
|
|
robot.sendCommand()
|
|
|
|
const { newPos } = r.lastCall()
|
|
expect(newPos.x).toBe(5) // Dummy Test, soll eventuell anders sein.
|
|
})
|
|
|
|
test('mehrere Receiver werden alle benachrichtigt', () => {
|
|
const r1 = new MockCmdReceiver('r1')
|
|
const r2 = new MockCmdReceiver('r2')
|
|
|
|
robot.cmdReceivers.push(r1, r2)
|
|
robot.sendCommand()
|
|
|
|
expect(r1.callCount).toBe(1)
|
|
expect(r2.callCount).toBe(1)
|
|
})
|
|
|
|
test('Receiver-Reihenfolge bleibt erhalten', () => {
|
|
const order = []
|
|
|
|
const r1 = { execCommand: () => order.push('r1') }
|
|
const r2 = { execCommand: () => order.push('r2') }
|
|
|
|
robot.cmdReceivers.push(r1, r2)
|
|
robot.sendCommand()
|
|
|
|
expect(order).toEqual(['r1', 'r2'])
|
|
})
|
|
|
|
test('sendCommand funktioniert ohne Receiver (kein Crash)', () => {
|
|
expect(() => robot.sendCommand()).not.toThrow()
|
|
})
|
|
|
|
test('mehrere sendCommand Aufrufe triggern mehrere Calls', () => {
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
|
|
robot.sendCommand()
|
|
robot.sendCommand()
|
|
|
|
expect(r.callCount).toBe(2)
|
|
})
|
|
|
|
test('MotorPosition wird neu erstellt bei jedem sendCommand', () => {
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
|
|
robot.sendCommand()
|
|
const first = r.lastCall().newPos
|
|
|
|
robot.xMotor = 99
|
|
robot.sendCommand()
|
|
const second = r.lastCall().newPos
|
|
|
|
expect(first.x).not.toBe(second.x)
|
|
})
|
|
|
|
|
|
test('sendCommand ruft Receiver auf ohne Log-Ausgabe', () => {
|
|
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
|
|
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
|
|
robot.sendCommand()
|
|
|
|
expect(r.callCount).toBe(1)
|
|
|
|
//expect(logSpy).toHaveBeenCalled(2)
|
|
logSpy.mockRestore()
|
|
})
|
|
|
|
test('sendCommand multiple Receivers', () => {
|
|
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
|
|
|
|
const r = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r)
|
|
const r2 = new MockCmdReceiver()
|
|
robot.cmdReceivers.push(r2)
|
|
|
|
robot.sendCommand()
|
|
|
|
expect(r.callCount).toBe(1)
|
|
expect(r2.callCount).toBe(1)
|
|
|
|
//console.log(logSpy)
|
|
//expect(logSpy).toHaveBeenCalled(2)
|
|
logSpy.mockRestore()
|
|
})
|
|
|
|
|
|
}) |