diff --git a/server/package.json b/server/package.json index f52d3d9..f0c3ceb 100644 --- a/server/package.json +++ b/server/package.json @@ -5,7 +5,7 @@ "main": "main.js", "bin": "main.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "node test/serial-send-endline.test.js", "start": "node main.js", "make": "npm i -g pkg && pkg . && chmod -R 777 build" }, diff --git a/server/test/serial-send-endline.test.js b/server/test/serial-send-endline.test.js new file mode 100644 index 0000000..5468e10 --- /dev/null +++ b/server/test/serial-send-endline.test.js @@ -0,0 +1,37 @@ +const assert = require("assert"); +const fs = require("fs"); +const path = require("path"); +const vm = require("vm"); + +const www = path.join(__dirname, "..", "www"); +const html = fs.readFileSync(path.join(www, "index.html"), "utf8"); +const expectedHandler = "input.sendText(input.textToSend, input.endlineToSend)"; + +const serialInput = html.match(/]*v-model="input\.textToSend")[^>]*>/); +const sendButton = html.match(/]*>Send<\/button>/); + +assert.ok(serialInput, "serial text input must exist"); +assert.ok(sendButton, "serial send button must exist"); +const enterHandler = serialInput[0].match(/v-on:keyup\.enter="([^"]+)"/); +const sendHandler = sendButton[0].match(/@click="([^"]+)"/); +assert.ok(enterHandler, "serial text input must define an Enter handler"); +assert.ok(sendHandler, "serial send button must define a click handler"); +assert.strictEqual(enterHandler[1], expectedHandler); +assert.strictEqual(sendHandler[1], expectedHandler); + +const dataInput = fs.readFileSync(path.join(www, "classes", "communication", "data", "DataInput.js"), "utf8"); +const dataInputSerial = fs.readFileSync(path.join(www, "classes", "communication", "data", "DataInputSerial.js"), "utf8"); +const context = { module: { exports: {} } }; +vm.runInNewContext(`${dataInput}\n${dataInputSerial}\nmodule.exports = DataInputSerial;`, context); + +const messages = []; +const connection = { sendServerCommand: message => messages.push(message) }; +const serial = new context.module.exports(connection, "Serial"); +messages.length = 0; + +for (const [lineEnding, expected] of [["\\r\\n", "\r\n"], ["\\n", "\n"], ["\\r", "\r"], ["", ""]]) { + serial.sendText("STATUS", lineEnding); + assert.strictEqual(messages.pop().text, `STATUS${expected}`); +} + +console.log("serial send Enter and button handlers preserve the selected line ending"); diff --git a/server/www/index.html b/server/www/index.html index aaf434b..caef4a1 100644 --- a/server/www/index.html +++ b/server/www/index.html @@ -298,7 +298,7 @@
- +