Skip to content

Commit

Permalink
[PORT] [activityfactory] Improve missing type error (#1922)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danieladu authored Mar 18, 2020
1 parent 9d060f2 commit 7a7b458
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 13 additions & 4 deletions libraries/botbuilder-core/src/activityFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export class ActivityFactory {
activity = MessageFactory.attachment(this.getAttachment(lgValue));
} else if (type === 'activity') {
activity = this.buildActivity(lgValue);
} else if (lgValue){
activity = this.buildActivityFromText(JSON.stringify(lgValue).trim());
}

return activity;
Expand Down Expand Up @@ -370,6 +372,10 @@ export class ActivityFactory {

try {
lgStructuredResult = JSON.parse(lgStringResult);
const type = this.getStructureType(lgStringResult);
if (!type || type.trim() === '') {
return undefined;
}
} catch (error) {
return undefined;
}
Expand All @@ -380,15 +386,18 @@ export class ActivityFactory {
private static checkStructuredResult(input: any): string[] {
const result: string[] = [];
const type: string = this.getStructureType(input);

if (!type || type.trim() === '') {
return result;
}

if (this.genericCardTypeMapping.has(type) || type === 'attachment') {
result.push(...this.checkAttachment(input));
} else if (type === 'activity') {
result.push(...this.checkActivity(input));
} else {
const diagnosticMessage: string = (type === undefined || type === '') ?
`'${ this.lgType }' does not exist in lg output json object.`
: `Type '${ type }' is not supported currently.`;
result.push(this.buildDiagnostic(diagnosticMessage));
const diagnosticMessage = `Type '${ type }' is not supported currently.`;
result.push(this.buildDiagnostic(diagnosticMessage, false));
}

return result;
Expand Down
10 changes: 4 additions & 6 deletions libraries/botbuilder-lg/tests/ActivityFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe('ActivityFactoryTest', function() {
});

it('NotSupportStructuredType', function() {
assert.throws(() => {getActivity('notSupport', undefined); }, Error);
const result = getActivity('notSupport', undefined);
assert.strictEqual(result.attachments, undefined);
assert.strictEqual(result.text.replace(/\r\n/g, '\n').replace(/\n/g, '').replace(/\s+/g, ''), '{"lgType":"Acti","key":"value"}');
});

it('HerocardWithCardAction', function() {
Expand Down Expand Up @@ -266,16 +268,12 @@ describe('ActivityFactoryTest', function() {
let diagnostics = ActivityFactory.checkLGResult('Not a valid json');
assert(diagnostics.length === 1);
assert.strictEqual(diagnostics[0], '[WARNING]LG output is not a json object, and will fallback to string format.');

diagnostics = ActivityFactory.checkLGResult('{}');
assert(diagnostics.length === 1);
assert.strictEqual(diagnostics[0], `[ERROR]'lgType' does not exist in lg output json object.`);
});

it('CheckStructuredLGDiagnostics', function() {
let diagnostics = getDiagnostics('ErrorStructuredType', undefined);
assert(diagnostics.length === 1);
assert.strictEqual(diagnostics[0], `[ERROR]Type 'mystruct' is not supported currently.`);
assert.strictEqual(diagnostics[0], `[WARNING]Type 'mystruct' is not supported currently.`);

diagnostics = getDiagnostics('ErrorActivityType', undefined);
assert(diagnostics.length === 2);
Expand Down

0 comments on commit 7a7b458

Please sign in to comment.