diff --git a/src/RokuDeploy.spec.ts b/src/RokuDeploy.spec.ts index 74415e2..1c0b225 100644 --- a/src/RokuDeploy.spec.ts +++ b/src/RokuDeploy.spec.ts @@ -213,11 +213,79 @@ describe('index', () => { let results = rokuDeploy['getRokuMessagesFromResponseBody'](body); expect(results).to.eql({ - errors: ['Failure: Form Error: "archive" Field Not Found', 'Failure: Form Error: "archive" Field Not Found'], + errors: ['Failure: Form Error: "archive" Field Not Found'], infos: ['Some random info message'], successes: ['Screenshot ok'] }); }); + + it('pull many messages from the response body including json messages', () => { + let body = getFakeResponseBody(` + Shell.create('Roku.Message').trigger('Set message type', 'success').trigger('Set message content', 'Screenshot ok').trigger('Render', node); + Shell.create('Roku.Message').trigger('Set message type', 'info').trigger('Set message content', 'Some random info message').trigger('Render', node); + Shell.create('Roku.Message').trigger('Set message type', 'error').trigger('Set message content', 'Failure: Form Error: "archive" Field Not Found').trigger('Render', node); + Shell.create('Roku.Message').trigger('Set message type', 'error').trigger('Set message content', 'Failure: Form Error: "archive" Field Not Found').trigger('Render', node); + + var params = JSON.parse('{"messages":[{"text":"Application Received: 2500809 bytes stored.","text_type":"text","type":"success"},{"text":"Install Failure: Error parsing XML component SupportedFeaturesView.xml","text_type":"text","type":"error"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Screenshot ok","text_type":"text","type":"success"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + `); + + let results = rokuDeploy['getRokuMessagesFromResponseBody'](body); + expect(results).to.eql({ + errors: ['Failure: Form Error: "archive" Field Not Found', 'Install Failure: Error parsing XML component SupportedFeaturesView.xml'], + infos: ['Some random info message'], + successes: ['Screenshot ok', 'Application Received: 2500809 bytes stored.'] + }); + }); + + it('pull many messages from the response body including json messages and dedupe them', () => { + let bodyOne = getFakeResponseBody(` + Shell.create('Roku.Message').trigger('Set message type', 'success').trigger('Set message content', 'Screenshot ok').trigger('Render', node); + Shell.create('Roku.Message').trigger('Set message type', 'success').trigger('Set message content', 'Screenshot ok').trigger('Render', node); + Shell.create('Roku.Message').trigger('Set message type', 'info').trigger('Set message content', 'Some random info message').trigger('Render', node); + Shell.create('Roku.Message').trigger('Set message type', 'info').trigger('Set message content', 'Some random info message').trigger('Render', node); + Shell.create('Roku.Message').trigger('Set message type', 'error').trigger('Set message content', 'Failure: Form Error: "archive" Field Not Found').trigger('Render', node); + Shell.create('Roku.Message').trigger('Set message type', 'error').trigger('Set message content', 'Failure: Form Error: "archive" Field Not Found').trigger('Render', node); + + var params = JSON.parse('{"messages":[{"text":"Application Received: 2500809 bytes stored.","text_type":"text","type":"success"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Application Received: 2500809 bytes stored.","text_type":"text","type":"success"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Install Failure: Error parsing XML component SupportedFeaturesView.xml","text_type":"text","type":"error"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Install Failure: Error parsing XML component SupportedFeaturesView.xml","text_type":"text","type":"error"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Some random info message","text_type":"text","type":"info"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Some random info message","text_type":"text","type":"info"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"wont be added","text_type":"text","type":"unknown"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"doesn't look like a roku message","text_type":"text"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"doesn't look like a roku message","type":"info"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"type":"info"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('[]'); + `); + + let resultsOne = rokuDeploy['getRokuMessagesFromResponseBody'](bodyOne); + expect(resultsOne).to.eql({ + errors: ['Failure: Form Error: "archive" Field Not Found', 'Install Failure: Error parsing XML component SupportedFeaturesView.xml'], + infos: ['Some random info message'], + successes: ['Screenshot ok', 'Application Received: 2500809 bytes stored.'] + }); + + let bodyTwo = getFakeResponseBody(` + var params = JSON.parse('{"messages":[{"text":"Application Received: 2500809 bytes stored.","text_type":"text","type":"success"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Application Received: 2500809 bytes stored.","text_type":"text","type":"success"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Install Failure: Error parsing XML component SupportedFeaturesView.xml","text_type":"text","type":"error"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Install Failure: Error parsing XML component SupportedFeaturesView.xml","text_type":"text","type":"error"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Some random info message","text_type":"text","type":"info"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"messages":[{"text":"Some random info message","text_type":"text","type":"info"}],"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + var params = JSON.parse('{"metadata":{"dev_id":"123456789","dev_key":true,"voice_sdk":false},"packages":[]}'); + `); + + let resultsTwo = rokuDeploy['getRokuMessagesFromResponseBody'](bodyTwo); + expect(resultsTwo).to.eql({ + errors: ['Install Failure: Error parsing XML component SupportedFeaturesView.xml'], + infos: ['Some random info message'], + successes: ['Application Received: 2500809 bytes stored.'] + }); + }); }); describe('getDeviceInfo', () => { diff --git a/src/RokuDeploy.ts b/src/RokuDeploy.ts index 9d84358..8805b76 100644 --- a/src/RokuDeploy.ts +++ b/src/RokuDeploy.ts @@ -680,20 +680,25 @@ export class RokuDeploy { let errorRegex = /Shell\.create\('Roku\.Message'\)\.trigger\('[\w\s]+',\s+'(\w+)'\)\.trigger\('[\w\s]+',\s+'(.*?)'\)/igm; let match: RegExpExecArray; - // eslint-disable-next-line no-cond-assign - while (match = errorRegex.exec(body)) { + while ((match = errorRegex.exec(body))) { let [, messageType, message] = match; switch (messageType.toLowerCase()) { - case 'error': - result.errors.push(message); + case RokuMessageType.error: + if (!result.errors.includes(message)) { + result.errors.push(message); + } break; - case 'info': - result.infos.push(message); + case RokuMessageType.info: + if (!result.infos.includes(message)) { + result.infos.push(message); + } break; - case 'success': - result.successes.push(message); + case RokuMessageType.success: + if (!result.successes.includes(message)) { + result.successes.push(message); + } break; default: @@ -701,6 +706,53 @@ export class RokuDeploy { } } + let jsonParseRegex = /JSON\.parse\(('.+')\);/igm; + let jsonMatch: RegExpExecArray; + + while ((jsonMatch = jsonParseRegex.exec(body))) { + let [, jsonString] = jsonMatch; + let jsonObject = parseJsonc(jsonString); + if (typeof jsonObject === 'object' && !Array.isArray(jsonObject) && jsonObject !== null) { + let messages = jsonObject.messages; + + if (!Array.isArray(messages)) { + continue; + } + + for (let messageObject of messages) { + // Try to duck type the object to make sure it is some form of message to be displayed + if (typeof messageObject.type === 'string' && messageObject.text_type === 'text' && typeof messageObject.text === 'string') { + const messageType: string = messageObject.type; + const text: string = messageObject.text; + switch (messageType.toLowerCase()) { + case RokuMessageType.error: + if (!result.errors.includes(text)) { + result.errors.push(text); + } + break; + + case RokuMessageType.info: + if (!result.infos.includes(text)) { + result.infos.push(text); + } + break; + + case RokuMessageType.success: + if (!result.successes.includes(text)) { + result.successes.push(text); + } + + break; + + default: + break; + } + } + } + } + + } + return result; } @@ -1120,6 +1172,12 @@ export interface RokuMessages { successes: string[]; } +enum RokuMessageType { + success = 'success', + info = 'info', + error = 'error' +} + export const DefaultFiles = [ 'source/**/*.*', 'components/**/*.*',