Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some more message grabbing logic #127

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion src/RokuDeploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
74 changes: 66 additions & 8 deletions src/RokuDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,27 +680,79 @@ 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:
break;
}
}

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;
}

Expand Down Expand Up @@ -1120,6 +1172,12 @@ export interface RokuMessages {
successes: string[];
}

enum RokuMessageType {
success = 'success',
info = 'info',
error = 'error'
}

export const DefaultFiles = [
'source/**/*.*',
'components/**/*.*',
Expand Down