Commit 0b37d60f authored by Tom JORQUERA's avatar Tom JORQUERA
Browse files

Merge branch 'issue-3' into 'master'

client: define and implement libReco

Closes #3 and #13

See merge request !20
parents fb1a8bbd 2655ee67
'use strict';
/* global robotLib:true Stomp SockJS XMLHttpRequest */
robotLib.reco = function (config) {
let recoStompClient;
let connected = false;
function tryConnect() {
recoStompClient = Stomp.over(new SockJS('http://' + config.reco.host + ':' + config.reco.port + '/chat'));
recoStompClient.connect(
{},
() => {
connected = true;
},
err => {
connected = false;
console.error('Online reco: STOMP failed to connect to %s:%s (trying again in %d ms)',
config.reco.host, config.reco.port, config.reco.reconnectInterval);
console.error('Online reco: ' + err);
setTimeout(tryConnect, config.reco.reconnectInterval);
});
}
tryConnect();
return {
start: confid => {
const xhttp = new XMLHttpRequest();
xhttp.open('GET', 'http://' + config.reco.host + ':' + config.reco.port + '/stream?action=START&id=' + confid, false);
xhttp.send();
},
stop: confid => {
const xhttp = new XMLHttpRequest();
xhttp.open('GET', 'http://' + config.reco.host + ':' + config.reco.port + '/stream?action=STOP&id=' + confid, false);
xhttp.send();
},
send: content => {
if (connected) {
recoStompClient.send('/app/chat', {}, JSON.stringify(content));
} else {
console.error('Online reco: not connected but trying to send %j', content);
}
},
getOnlineReco: confId => {
return new Promise((resolve, reject) => {
const xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = () => {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) {
resolve(xmlHttp.responseText);
} else {
console.error('Online reco: error trying to reach http://%s:%s/resources', config.reco.host, config.reco.port);
reject(xmlHttp.statusText);
}
}
};
const url = 'http://' + config.reco.host + ':' + config.reco.port + '/resources?id=' + confId + '&resources=keywords;wiki';
xmlHttp.open('GET', url, true);
xmlHttp.setRequestHeader('Content-type', 'application/json');
xmlHttp.send(null);
});
}
};
};
'use strict';
const robotLib = {};
function SockJS() {
return {
type: 'Sock'
};
}
const StompclientMock = {
connect: (f1, f2) => {
f2();
},
send: jest.fn().mockImplementation(() => ({}))
};
const StompMock = {
over: () => StompclientMock
};
const XMLHttpRequestMock = {
open: jest.fn().mockImplementation(() => ({})),
setRequestHeader: () => {},
send: jest.fn().mockImplementation(() => {
XMLHttpRequestMock.readyState = 4;
XMLHttpRequestMock.status = 200;
XMLHttpRequestMock.responseText = 'somedata';
XMLHttpRequestMock.onreadystatechange();
return {};
}),
onreadystatechange: () => {}
};
const XMLHttpRequest = function () {
// Reset mock
XMLHttpRequestMock.readyState = 0;
XMLHttpRequestMock.status = 0;
XMLHttpRequestMock.responseText = '';
XMLHttpRequestMock.onreadystatechange = () => {};
return XMLHttpRequestMock;
};
const config = {
reco: {
host: 'localhost',
port: 8080,
reconnectInterval: 1000
}
};
describe('client/lib/reco', () => {
beforeEach(() => {
global.robotLib = robotLib;
global.Stomp = StompMock;
global.SockJS = SockJS;
global.XMLHttpRequest = XMLHttpRequest;
/* eslint-disable import/no-unassigned-import */
require('./live-reco.js');
/* eslint-enable */
});
global.robotReco = {
getRecommendation: id => ({type: 'Recommendation', id})
};
test('should define robotLib.reco', () => {
expect(global.robotLib.reco).toBeDefined();
});
test('should make correct REST call on start', () => {
const confId = 'testConf';
const urlExpected = 'http://localhost:8080/stream?action=START&id=' + confId;
const reco = global.robotLib.reco(config);
reco.start(confId);
expect(XMLHttpRequestMock.open).toHaveBeenCalledWith('GET', urlExpected, expect.anything());
expect(XMLHttpRequestMock.send).toHaveBeenCalled();
});
test('should make correct REST call on stop', () => {
const confId = 'testConf';
const urlExpected = 'http://localhost:8080/stream?action=STOP&id=' + confId;
const reco = global.robotLib.reco(config);
reco.stop(confId);
expect(XMLHttpRequestMock.open).toHaveBeenCalledWith('GET', urlExpected, expect.anything());
expect(XMLHttpRequestMock.send).toHaveBeenCalled();
});
test('should make correct STOMP call when sending data', () => {
const content = {
data: 'testData'
};
const reco = global.robotLib.reco(config);
reco.send(content);
expect(StompclientMock.send).toHaveBeenCalledWith('/app/chat', {}, JSON.stringify(content));
});
test('should make correct REST call when getting recommendation', done => {
const reco = global.robotLib.reco(config);
reco.getOnlineReco('testconf')
.then(res => {
expect(res).toBe('somedata');
done();
});
});
});
......@@ -9,6 +9,8 @@
"chromeOptions": {
"args" : [
"--disable-web-security",
"--user-data-dir=$HOME/tmp/chromium",
"--allow-running-insecure-content",
"--use-fake-device-for-media-stream",
"--use-fake-ui-for-media-stream"
]
......@@ -19,7 +21,15 @@
"client": {
"name": "hublot",
"gstreamerURL": "ws://localhost:8888",
"externalLibs": []
"archive": "http://localhost:8080/api"
"externalLibs": [
"https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.4/sockjs.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"
],
"archive": "http://localhost:8080/api",
"reco": {
"host": "localhost",
"port": 8090,
"reconnectInterval": 5000
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment