UNPKG

@ovotech/genesys-web-messaging-tester-cli

Version:
181 lines (180 loc) 8.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Ui = void 0; const chalk_1 = __importDefault(require("chalk")); const genesys_web_messaging_tester_1 = require("@ovotech/genesys-web-messaging-tester"); const humanize_duration_1 = __importDefault(require("humanize-duration")); class Ui { /** * Given the extensive use of Chalk in here, this utility method makes it clear * that trailing newlines are safer encoded outside the Chalk string. * * @see https://github.com/ovotech/genesys-web-messaging-tester/issues/20#issuecomment-1246630078 */ static trailingNewline(text, count = 1) { return `${text}${'\n'.repeat(count)}`; } errorReadingTestScriptFile(error) { return Ui.trailingNewline(chalk_1.default.red(error.message)); } titleOfTask(scenario, isRetryDueToUnorderedMsgFailure = false) { if (isRetryDueToUnorderedMsgFailure) { return `${scenario.name} (RETRYING)`; } else { return scenario.name; } } titleOfFinishedTask(scenario, hasPassed) { if (hasPassed) { return `${scenario.name} (${chalk_1.default.bold.green('PASS')})`; } else { return `${scenario.name} (${chalk_1.default.bold.red('FAIL')})`; } } messageTranscribed(event) { return Ui.trailingNewline(`${chalk_1.default.bold.grey(`${event.who}:`)} ${chalk_1.default.grey(event.message)}`); } retryingTestDueToFailureLikelyByUnorderedMessage() { return Ui.trailingNewline('Test failed. Retrying as unordered messages detected'); } firstLineOfMessageTranscribed(event) { const lines = event.message.trim().split('\n'); const message = `${chalk_1.default.bold.grey(`${event.who}:`)} ${chalk_1.default.grey(lines[0])}`; if (lines.length > 1) { return `${message}${chalk_1.default.grey('...')}`; } return message; } validatingTestScriptFailed(error) { return Ui.trailingNewline(chalk_1.default.red(error?.message ?? 'Failed to validate Test Script')); } validatingSessionConfigFailed(error) { return Ui.trailingNewline(chalk_1.default.red(error?.message ?? 'Failed to validate Session config')); } preflightCheckOfAssociateConvoIdFailed(error) { if (error.errorType === 'missing-permissions') { return Ui.trailingNewline(chalk_1.default.red('Your OAuth Client does not have the necessary permissions to associate a conversation IDs to tests:\n' + `'${error.reasonForError}'`)); } return Ui.trailingNewline(chalk_1.default.red('There was a problem checking whether your OAuth Client has the necessary permissions to associate a conversation IDs to tests:\n' + error.reasonForError)); } validatingAssociateConvoIdEnvValidationFailed(error) { return Ui.trailingNewline(chalk_1.default.red(error?.message ?? 'Failed to validate environment variables containing Genesys OAuth Client credentials')); } async scenarioTestResult(result) { let suffix = ''; let conversationIdFailure = undefined; if (result.conversationId.associateId) { const conversationIdResult = await result.conversationId.conversationIdGetter(); if (conversationIdResult.successful) { suffix = ` - ${chalk_1.default.green(conversationIdResult.id)}`; } else { conversationIdFailure = conversationIdResult; suffix = ` - ${chalk_1.default.yellow('unable to associate conversation ID')}`; } } const title = result.hasPassed ? `${chalk_1.default.bold(result.scenario.name)} (${chalk_1.default.green('PASS')}${suffix})` : `${chalk_1.default.bold(result.scenario.name)} (${chalk_1.default.red('FAIL')})${suffix}`; const lines = [ '', title, '---------------------', ...result.transcription.map((t) => `${chalk_1.default.bold(`${t.who}:`)} ${t.message}`), ]; if (!result.hasPassed) { lines.push(''); const error = result.reasonForError; if (error instanceof genesys_web_messaging_tester_1.BotDisconnectedWaitingForResponseError) { if (error.responsesReceived.length === 0) { lines.push(chalk_1.default.red([ `Bot disconnected from the conversation whilst waiting a message containing:`, ` ${error.expectedResponse}`, `No messages were received before disconnection`, ].join('\n'))); } else { lines.push(chalk_1.default.red([ `Bot disconnected from the conversation whilst waiting a message containing:`, ` ${error.expectedResponse}`, 'Received the following messages before disconnection:', ...error.responsesReceived.map((m) => ` - ${m.text}`), ].join('\n'))); } } else if (error instanceof genesys_web_messaging_tester_1.TimeoutWaitingForResponseError) { if (error.responsesReceived.length === 0) { lines.push(chalk_1.default.red([ `Expected a message within ${(0, humanize_duration_1.default)(error.timeoutInMs)} containing:`, ` ${error.expectedResponse}`, `Didn't receive any response`, ].join('\n'))); } else { lines.push(chalk_1.default.red([ `Expected a message within ${(0, humanize_duration_1.default)(error.timeoutInMs)} containing:`, ` ${error.expectedResponse}`, 'Received:', ...error.responsesReceived.map((m) => ` - ${m.text}`), ].join('\n'))); } } else { lines.push(chalk_1.default.red(result.reasonForError.message)); } } if (result.conversationId.associateId && conversationIdFailure) { let errorMsg = `WARNING: Could not find Conversation ID for test `; switch (conversationIdFailure.reason) { case 'not-received-message': errorMsg += 'as your test did not receive a response from your flow.'; break; case 'convo-id-not-in-response': errorMsg += "as the response from your flow did not contain a Message ID, which is necessary for finding the test's Conversation ID."; break; case 'unknown-error': if (conversationIdFailure.error instanceof Error) { errorMsg += `due to an unexpected error: ${conversationIdFailure.error.message}.`; } if (typeof conversationIdFailure.error === 'string') { errorMsg += `due to an unexpected error: ${conversationIdFailure.error}.`; } errorMsg += `due to an unexpected error.`; break; default: errorMsg += `due to an unexpected error.`; break; } lines.push(chalk_1.default.yellow(errorMsg)); } return Ui.trailingNewline(lines.join('\n'), 1); } testScriptSummary(results) { const lines = []; for (const r of results) { if (r.hasPassed) { lines.push(chalk_1.default.green(`PASS - ${r.scenario.name}`)); } else { lines.push(chalk_1.default.red(`FAIL - ${r.scenario.name}`)); } if (r.wasRetriedDueToUnorderedMessageFailure) { lines.push(chalk_1.default.yellow(' ^ This test was retried following a failure that coincided with unordered messages being being received from Genesys\n' + ' Read more here: https://github.com/ovotech/genesys-web-messaging-tester/blob/main/docs/cli/unordered-messages.md')); } } return Ui.trailingNewline(`\n${chalk_1.default.bold('Scenario Test Results')} --------------------- ${lines.join('\n')}`); } } exports.Ui = Ui;