-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
244 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ function getStatus() { | |
|
||
return { | ||
connected: this.connected, | ||
sunspecIps: this.sunspecIps, | ||
}; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const proxyquire = require('proxyquire'); | ||
const { EVENTS, WEBSOCKET_MESSAGE_TYPES } = require('../../../../utils/constants'); | ||
const { ServiceNotConfiguredError } = require('../../../../utils/coreErrors'); | ||
|
||
const { fake, assert } = sinon; | ||
const ModbusTCPMock = require('./utils/ModbusTCPMock.test'); | ||
const ScannerClassMock = require('./utils/ScannerClassMock.test'); | ||
|
||
const SERVICE_ID = 'faea9c35-759a-44d5-bcc9-2af1de37b8b4'; | ||
|
||
const SunSpecManager = proxyquire('../../../../services/sunspec/lib', { | ||
ModbusTCP: { ModbusTCP: ModbusTCPMock }, | ||
ScannerClass: { ScannerClass: ScannerClassMock }, | ||
}); | ||
|
||
describe('SunSpec scan', () => { | ||
// PREPARE | ||
let gladys; | ||
let sunSpecManager; | ||
|
||
beforeEach(() => { | ||
gladys = { | ||
event: { | ||
emit: fake.resolves(null), | ||
}, | ||
variable: { | ||
getValue: fake.resolves('[{"ip":"192.168.1.0/24"}]'), | ||
}, | ||
}; | ||
sunSpecManager = new SunSpecManager(gladys, ModbusTCPMock, ScannerClassMock, SERVICE_ID); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.reset(); | ||
}); | ||
|
||
it('should not scan - not configured', async () => { | ||
try { | ||
await sunSpecManager.scan(); | ||
assert.fail(); | ||
} catch (error) { | ||
expect(error).to.be.an.instanceof(ServiceNotConfiguredError); | ||
} | ||
}); | ||
|
||
it('should not scan - already scanning', async () => { | ||
sunSpecManager.scanning = true; | ||
await sunSpecManager.scan(); | ||
assert.notCalled(gladys.variable.getValue); | ||
}); | ||
|
||
it('should find Sunspec device', async () => { | ||
await sunSpecManager.scan(); | ||
assert.callCount(sunSpecManager.eventManager.emit, 3); | ||
assert.calledWithExactly(sunSpecManager.eventManager.emit, EVENTS.WEBSOCKET.SEND_ALL, { | ||
type: WEBSOCKET_MESSAGE_TYPES.SUNSPEC.SCANNING, | ||
payload: { | ||
scanning: true, | ||
}, | ||
}); | ||
assert.calledWithExactly(sunSpecManager.eventManager.emit, EVENTS.WEBSOCKET.SEND_ALL, { | ||
type: WEBSOCKET_MESSAGE_TYPES.SUNSPEC.SCANNING, | ||
payload: { | ||
scanning: false, | ||
success: true, | ||
}, | ||
}); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
server/test/services/sunspec/lib/utils/ScannerClassMock.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class ScannerClassMock { | ||
// eslint-disable-next-line class-methods-use-this | ||
on(step, cb) { | ||
cb([ | ||
{ | ||
ip: '192.168.1.xx', | ||
openPorts: [502], | ||
}, | ||
]); | ||
} | ||
} | ||
|
||
module.exports = ScannerClassMock; |
Oops, something went wrong.