forked from tmcgee123/karma-spec-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (35 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var colors = require('colors');
var HueReporter = function (baseReporterDecorator, formatError, config) {
baseReporterDecorator(this);
this.HTTPRequest = require('HTTPRequest');
if(!config){
this.writeCommonMsg(('Please configure your hue report in karma.conf.js hueReporter').red);
}
var failCount = 0;
this.onRunComplete = function (browsers, results) {
if(failCount >= 3){
return;
}
var url = `http://${config.hueReporter.ip}/api/${config.hueReporter.user}/${config.hueReporter.applyTo}/${config.hueReporter.applyToId}/${config.hueReporter.applyTo == 'lights' ? 'state' : 'action'}`
//0 - red 25500 - green
var totalTests = results.failed + results.success;
var successRatio = (results.success / totalTests) * 100;
var hue = Math.round((25500 / totalTests) * results.success
+ (results.failed ? -(90 * successRatio) : 0));
hue < 0 ? hue = 0 : hue = hue;
try{
this.HTTPRequest.put(url, `{"on":true, "hue":${hue}}`, (status, headers, response) => {
if (status != 200 || response.error){
this.writeCommonMsg(('Can not set hue... check your config').red);
}
});
}
catch (e){
failCount ++;
}
};
};
HueReporter.$inject = ['baseReporterDecorator', 'formatError', 'config'];
module.exports = {
'reporter:hue': ['type', HueReporter]
};