@ovotech/genesys-web-messaging-tester
Version:
45 lines (44 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebMessageServerConnectionFixture = void 0;
const webMessagePayloads_1 = require("./webMessagePayloads");
class WebMessageServerConnectionFixture {
constructor(ws) {
this.ws = ws;
}
async waitForMessage() {
return new Promise((resolve, reject) => {
this.ws.on('message', function message(data) {
if (!Buffer.isBuffer(data)) {
reject(new Error('Expected data as Buffer type'));
}
try {
resolve(JSON.parse(data.toString('utf-8')));
}
catch (err) {
reject(err);
}
});
});
}
async waitForConnectionToClose() {
return new Promise((resolve) => this.ws.on('close', resolve));
}
simulateSessionResponseMessage() {
const payload = webMessagePayloads_1.webMessagePayloads.sessionResponse();
this.ws.send(JSON.stringify(payload));
}
simulateOutboundTextStructuredMessage(text, date = new Date()) {
const payload = webMessagePayloads_1.webMessagePayloads.outboundTextStructuredMessage(text, date);
this.ws.send(JSON.stringify(payload));
}
simulateOutboundDisconnectEventStructuredMessage(date = new Date()) {
const payload = webMessagePayloads_1.webMessagePayloads.outboundDisconnectEventStructuredMessage(date);
this.ws.send(JSON.stringify(payload));
}
simulateInboundTextStructuredMessage(text, date = new Date()) {
const payload = webMessagePayloads_1.webMessagePayloads.inboundStructuredMessage(text, date);
this.ws.send(JSON.stringify(payload));
}
}
exports.WebMessageServerConnectionFixture = WebMessageServerConnectionFixture;