Commit 2655ee67 authored by samy's avatar samy Committed by Tom Jorquera
Browse files

Add recommendation function to hublot client side

Extract keywords and propose recommendation links based on transcription segments which are sent by the participants
parent b09974ae
'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();
});
});
});
......@@ -21,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