Sfoglia il codice sorgente

Adds response handling tests

craigsdennis 9 anni fa
parent
commit
22bd0bd5d0
2 ha cambiato i file con 34 aggiunte e 3 eliminazioni
  1. 4 3
      back-end/rom_comm.js
  2. 30 0
      test/rom_comm_test.js

+ 4 - 3
back-end/rom_comm.js

@@ -106,14 +106,14 @@ class RomComm extends EventEmitter {
         if (data.length < 8) {
             log.error("Missing header");
             // Not throwing error, let it fall through
-            return;
+            return false;
         }
         let headerBytes = data.slice(0, 8);
         let header = this.headerPacketFrom(headerBytes);
         if (header.direction != 0x01) {
             log.error("Invaid direction", header.direction);
             // Again, intentionally not throwing error, it will communicate correctly eventually
-            return;
+            return false;
         }
         let commandName = commandToKey(header.command);
         let body = data.slice(8, 8 + header.size);
@@ -126,6 +126,7 @@ class RomComm extends EventEmitter {
         }
         log.info("Emitting", commandName, body);
         this.emit("RECEIVED-" + commandName, body);
+        return true;
     }
 
     /**
@@ -202,7 +203,7 @@ class RomComm extends EventEmitter {
                             reject(error);
                         }
                         log.info("Port flushed");
-                        
+
                         resolve();
                     });
             }).then(() => this.sync(true));

+ 30 - 0
test/rom_comm_test.js

@@ -12,3 +12,33 @@ describe('ESP8266', () => {
         expect(esp.board).to.be.an.instanceof(boards.Esp12);
     });
 });
+
+describe('RomComm', () => {
+   const esp = new RomComm({
+       portName: "/dev/ttys000",
+       baud: 9600
+   });
+   describe('handleResponse', () => {
+       // Helper function for response creation, takes *args
+      const handle = function() {
+        let data = new Buffer(Uint8Array.from(arguments));
+        return esp.handleResponse(data);
+      }
+      const SUCCESS = new Buffer([0x00, 0x00]);
+      it('ignores missing headers', () => {
+          expect(handle(1, 8, 2)).to.be.false;
+      });
+      it('ensures direction', () => {
+         expect(handle(114, 108, 0, 108, 156, 158, 124, 0, 140)).to.be.false;
+      });
+      it('emits on success', done => {
+          esp.on('RECEIVED-FLASH_DOWNLOAD_BEGIN', body => {
+              expect(body).to.deep.equal(SUCCESS);
+              done();
+          });
+          // Result from flash download begin
+          let result = handle(1, 2, 2, 0, 7, 7, 18, 32, 0 ,0);
+          expect(result).to.be.true;
+      })
+   });
+});