Files
appRobotDriver/test/RobotController.test.js
2026-06-14 06:35:18 +02:00

101 lines
3.5 KiB
JavaScript

const RobotController = require('../robot/RobotController');
const createDummyRobot = require('./helpers/createDummyRobot');
describe('RobotController (ToDo_6)', () => {
test('applyCommand: G90 setzt Absolutmodus, ohne Bewegung', () => {
const robot = createDummyRobot();
robot.moveRelative = true;
RobotController.applyCommand(robot, { command: 'G90', params: {} });
expect(robot.moveRelative).toBe(false);
expect(robot.calculateAngles3D).not.toHaveBeenCalled();
expect(robot.sendCommand).not.toHaveBeenCalled();
});
test('applyCommand: G1 absolut setzt Koordinaten und bewegt', () => {
const robot = createDummyRobot();
robot.moveRelative = false;
RobotController.applyCommand(robot, { command: 'G1', params: { X: 10, Y: 20, Z: 30 } });
expect(robot.x).toBe(10);
expect(robot.y).toBe(20);
expect(robot.z).toBe(30);
expect(robot.calculateAngles3D).toHaveBeenCalledTimes(1);
expect(robot.sendCommand).toHaveBeenCalledTimes(1);
});
test('applyCommand: G1 relativ addiert', () => {
const robot = createDummyRobot();
robot.moveRelative = true;
robot.x = 5;
RobotController.applyCommand(robot, { command: 'G1', params: { X: 2 } });
expect(robot.x).toBe(7);
});
test('applyCommand: M1 verändert Motorwerte und rechnet vorwärts', () => {
const robot = createDummyRobot();
robot.moveRelative = false;
RobotController.applyCommand(robot, { command: 'M1', params: { X: 1, Y: 2, Z: 3, A: 4, B: 5, C: 6 } });
expect(robot.xMotor).toBe(1);
expect(robot.alpha).toBe(2);
expect(robot.beta).toBe(3);
expect(robot.a).toBe(4);
expect(robot.calculatePositionFromMotorAngles).toHaveBeenCalled();
expect(robot.sendCommand).toHaveBeenCalled();
});
test('applyCommand: M92 setzt Motorwerte absolut und sendet G92', () => {
const robot = createDummyRobot();
robot.createMotorPosition = jest.fn();
RobotController.applyCommand(robot, { command: 'M92', params: { X: 5, Y: 0.5, A: 0.3 } });
expect(robot.createMotorPosition).toHaveBeenCalledTimes(1);
expect(robot.xMotor).toBe(5);
expect(robot.alpha).toBe(0.5);
expect(robot.a).toBe(0.3);
expect(robot.calculatePositionFromMotorAngles).toHaveBeenCalled();
expect(robot.sendCommand).toHaveBeenCalledWith('G92');
});
test('applyCommand: G92 verhält sich identisch zu M92 (Bug 3)', () => {
const robot = createDummyRobot();
robot.createMotorPosition = jest.fn();
RobotController.applyCommand(robot, { command: 'G92', params: { X: 5, Y: 0.5, A: 0.3 } });
expect(robot.createMotorPosition).toHaveBeenCalledTimes(1);
expect(robot.xMotor).toBe(5);
expect(robot.alpha).toBe(0.5);
expect(robot.a).toBe(0.3);
expect(robot.calculatePositionFromMotorAngles).toHaveBeenCalled();
expect(robot.sendCommand).toHaveBeenCalledWith('G92');
});
test('applyCommand: ungültiger Befehl wird ignoriert', () => {
const robot = createDummyRobot();
RobotController.applyCommand(robot, null);
RobotController.applyCommand(robot, { command: 'G99', params: {} });
expect(robot.sendCommand).not.toHaveBeenCalled();
});
test('receive: parst rohe Nachricht und wendet mehrere Befehle an', () => {
const robot = createDummyRobot();
robot.moveRelative = true;
RobotController.receive(robot, 'G90 G1 X12 Y34');
expect(robot.moveRelative).toBe(false);
expect(robot.x).toBe(12);
expect(robot.y).toBe(34);
expect(robot.sendCommand).toHaveBeenCalledTimes(1);
});
});