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

106 lines
4.0 KiB
JavaScript
Executable File

var TenetSender = require('../robot/TelnetSenderGRBL.js')
describe("TelnetSenderGRBL.execCommand", () => {
test("writes correct G-code to mocked tSocket", () => {
// Create instance (will try real connection, but we override tSocket immediately)
const sender = new TenetSender(urlGRBL = "test.test", maxSpeedF = 2300, xAxisGrbl = "x", yAxisGrbl = "y", zAxisGrbl = "z");
// Mock tSocket.write
sender.tSocket = {
written: "",
write: function(txt) {
this.written = txt; // store what was written
}
};
// Provide some sample motion data
const mOld = { x: 0, y: 0, z: 0, a:0, b:0, c:0, e:0 }; // not used in your code
const mNew = { x: 12.34, y: Math.PI/2, z: 0, a:0, b:0, c:0, e:0 };
mNew.xMotorChanged = true;
mNew.yMotorChanged = true;
mNew.zMotorChanged = true;
sender.execCommand("G1", mOld, mNew);
// ✅ verify output — G1 moves must be sent in absolute mode (G90 prefix)
expect(sender.tSocket.written).toBe("G90 G1 x12.34 y90.00 z-90.00 f2300.00\r\n");
});
test("writes correct G-code to mocked tSocket Ellbow", () => {
// Create instance (will try real connection, but we override tSocket immediately)
const sender = new TenetSender(urlGRBL = "test.test", maxSpeedF = 2300, xAxisGrbl = "a", yAxisGrbl = null, zAxisGrbl = null );
// Mock tSocket.write
sender.tSocket = {
written: "",
write: function(txt) {
this.written = txt; // store what was written
}
};
// Provide some sample motion data
const mOld = { x: 0, y: 0, z: 0, a:Math.PI, b:0, c:0, e:0 }; // not used in your code
const mNew = { x: 12.34, y: Math.PI/2, z: 0, a:Math.PI/8, b:0, c:0, e:0 };
mNew.xMotorChanged = true;
mNew.yMotorChanged = true;
mNew.zMotorChanged = true;
mNew.aMotorChanged = true;
sender.execCommand("G1", mOld, mNew);
// ✅ verify output — G1 moves must be sent in absolute mode (G90 prefix)
expect(sender.tSocket.written).toBe("G90 G1 x22.50 f2300.00\r\n");
});
test("writes correct G-code G92 to mocked tSocket", () => {
// Create instance (will try real connection, but we override tSocket immediately)
const sender = new TenetSender(urlGRBL = "test.test", maxSpeedF = 2300, xAxisGrbl = "x", yAxisGrbl = "y", zAxisGrbl = "z");
// Mock tSocket.write
sender.tSocket = {
written: "",
write: function(txt) {
this.written = txt; // store what was written
}
};
// Provide some sample motion data
const mOld = { x: 0, y: 0, z: 0, a:0, b:0, c:0, e:0 }; // not used in your code
const mNew = { x: 12.34, y: 1.0, z: 2.0, a:0, b:0, c:0, e:0 };
mNew.xMotorChanged = true;
mNew.yMotorChanged = true;
mNew.zMotorChanged = true;
sender.execCommand("G92", mOld, mNew);
// ✅ verify output
expect(sender.tSocket.written).toBe("G92 x12.34 y57.30 z57.30\r\n");
});
test("aAxisGrbl guard prüft mNew.x, nicht mNew.y (Bug 5)", () => {
// Sender mit aAxisGrbl = "x": GRBL-a-Achse bekommt xMotor
const sender = new TenetSender("test.test", 2300, null, null, null, "x");
sender.tSocket = {
written: "",
write: function(txt) { this.written = txt; }
};
const mOld = { x: 0, y: 0, z: 0, a: 0, b: 0, c: 0, e: 0 };
// mNew.x ist NaN (ungültig), mNew.y ist finite — alter Bug würde Guard passieren lassen
const mNew = { x: NaN, y: 1.0, z: 0, a: 0, b: 0, c: 0, e: 0 };
mNew.xMotorChanged = true;
sender.execCommand("G1", mOld, mNew);
// Guard muss auf mNew.x prüfen — da NaN, darf kein "a"-Wert gesendet werden
expect(sender.tSocket.written).not.toContain(" a");
});
});