ivr-tester
Version:
An automated testing framework for IVR call flows
139 lines (138 loc) • 6.4 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TranscriptRecorder = exports.transcriptRecorderPlugin = void 0;
const fs = __importStar(require("fs"));
const fs_1 = require("fs");
const path = __importStar(require("path"));
const TwilioCall_1 = require("../TwilioCall");
const twilio_1 = require("../twilio");
const ivrNumberAndTestNameFilename_1 = require("./filename/ivrNumberAndTestNameFilename");
const ConfigurationError_1 = require("../../configuration/ConfigurationError");
const TwilioCaller_1 = require("../TwilioCaller");
const transcriptRecorderPlugin = (config) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
if (!((_a = config.recording) === null || _a === void 0 ? void 0 : _a.transcript)) {
return {
initialise() {
// Intentionally empty
},
};
}
const recorderConfig = {
outputPath: (_c = (_b = config.recording) === null || _b === void 0 ? void 0 : _b.transcript) === null || _c === void 0 ? void 0 : _c.outputPath,
filename: ((_e = (_d = config.recording) === null || _d === void 0 ? void 0 : _d.transcript) === null || _e === void 0 ? void 0 : _e.filename) || ivrNumberAndTestNameFilename_1.ivrNumberAndTestNameFilename,
includeResponse: (_h = (_g = (_f = config.recording) === null || _f === void 0 ? void 0 : _f.transcript) === null || _g === void 0 ? void 0 : _g.includeResponse) !== null && _h !== void 0 ? _h : false,
};
if (!recorderConfig.outputPath) {
throw new ConfigurationError_1.ConfigurationError("recording.transcript.outputPath", "Path must be defined");
}
if (!fs.existsSync(recorderConfig.outputPath)) {
throw new ConfigurationError_1.ConfigurationError("recording.transcript.outputPath", "Path does not exist");
}
return {
initialise() {
// Intentionally empty
},
testStarted(testSession) {
new TranscriptRecorder(testSession, recorderConfig);
},
};
};
exports.transcriptRecorderPlugin = transcriptRecorderPlugin;
class TranscriptRecorder {
constructor(testSession, config) {
this.testSession = testSession;
this.config = config;
this.saveMatchedPromptRef = this.saveMatchedPrompts.bind(this);
this.testSession.callFlowSession.on("promptMatched", this.saveMatchedPromptRef);
this.closeRef = this.close.bind(this);
this.testSession.callFlowSession.on("allPromptsMatched", this.closeRef);
this.saveTimedOutPromptThenCloseRef = this.saveTimedOutPromptThenClose.bind(this);
this.testSession.callFlowSession.on("timeoutWaitingForMatch", this.saveTimedOutPromptThenCloseRef);
this.processTwilioMessageRef = this.processTwilioMessage.bind(this);
const connection = this.testSession.call.getStream();
connection.on(TwilioCall_1.WebSocketEvents.Message, this.processTwilioMessageRef);
}
processTwilioMessage(message) {
const data = JSON.parse(message);
if (data.event === twilio_1.TwilioConnectionEvents.MediaStreamStart) {
this.createFile(data);
}
}
saveTimedOutPromptThenClose(event) {
const prompt = [];
if (this.config.includeResponse) {
prompt.push(`Them: ${event.transcription}`);
prompt.push("You: Ended test as prompt did not match condition within timeout period");
}
else {
prompt.push(`${event.transcription}`);
}
this.writeStream.write(`${prompt.join("\n")}\n\n`);
this.close();
}
saveMatchedPrompts(event) {
const prompt = [];
if (this.config.includeResponse) {
prompt.push(`Them: ${event.transcription}`);
prompt.push(`You: ${event.promptDefinition.then.describe()}`);
}
else {
prompt.push(`${event.transcription}`);
}
this.writeStream.write(`${prompt.join("\n")}\n\n`);
}
createFilename(event) {
const call = TwilioCaller_1.TwilioCaller.extractParameters(event);
let filename;
if (typeof this.config.filename === "string") {
filename = this.config.filename;
}
else if (typeof this.config.filename === "function") {
filename = this.config.filename({
sid: event.streamSid,
call,
}, this.testSession.scenario, TranscriptRecorder.FILENAME_SUFFIX);
}
return `${filename}.${TranscriptRecorder.FILE_EXT}`;
}
createFile(event) {
const filename = this.createFilename(event);
const filepath = path.join(this.config.outputPath, filename);
console.log(`Recording transcript to '${filepath}'`);
fs_1.mkdirSync(this.config.outputPath, { recursive: true });
this.writeStream = fs_1.createWriteStream(filepath);
}
close() {
const callFlowSession = this.testSession.callFlowSession;
callFlowSession.off("promptMatched", this.saveMatchedPromptRef);
callFlowSession.off("allPromptsMatched", this.closeRef);
callFlowSession.off("timeoutWaitingForMatch", this.saveTimedOutPromptThenCloseRef);
const connection = this.testSession.call.getStream();
connection.off(TwilioCall_1.WebSocketEvents.Close, this.closeRef);
this.writeStream.close();
this.writeStream = null;
}
}
exports.TranscriptRecorder = TranscriptRecorder;
TranscriptRecorder.FILE_EXT = "txt";
TranscriptRecorder.FILENAME_SUFFIX = "transcript";