clwoz-models
Version:
Models for ConversationLearner
371 lines • 16 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
var Action_1 = require("./Action");
var createEmptyAction = function () { return ({
actionId: '',
createdDateTime: '',
payload: '',
isTerminal: false,
requiredEntitiesFromPayload: [],
requiredEntities: [],
requiredConditions: [],
negativeEntities: [],
negativeConditions: [],
suggestedEntity: '',
version: 0,
packageCreationId: 0,
packageDeletionId: 0,
actionType: Action_1.ActionTypes.TEXT,
entityId: undefined,
enumValueId: undefined,
}); };
var expectedSimpleTextPayload = 'simple text payload';
var textPayloadWithNoEntities = {
json: {
kind: 'value',
document: {
kind: 'document',
data: {},
nodes: [
{
kind: 'block',
type: 'line',
isVoid: false,
data: {},
nodes: [
{
kind: 'text',
leaves: [
{
kind: 'leaf',
text: expectedSimpleTextPayload,
marks: []
}
]
}
]
}
]
}
}
};
var textPayloadWithRequiredEntity = {
json: {
kind: 'value',
document: {
kind: 'document',
data: {},
nodes: [
{
kind: 'block',
type: 'line',
isVoid: false,
data: {},
nodes: [
{
kind: 'text',
leaves: [
{
kind: 'leaf',
text: 'The value of custom is: ',
marks: []
}
]
},
{
kind: 'inline',
type: 'mention-inline-node',
isVoid: false,
data: {
completed: true,
option: {
id: '627a43be-4675-4b98-84a7-537262561be6',
name: 'custom'
}
},
nodes: [
{
kind: 'text',
leaves: [
{
kind: 'leaf',
text: '$custom',
marks: []
}
]
}
]
},
{
kind: 'text',
leaves: [
{
kind: 'leaf',
text: '',
marks: []
}
]
}
]
}
]
}
}
};
var textAction1 = __assign(__assign({}, createEmptyAction()), { actionType: Action_1.ActionTypes.TEXT, payload: JSON.stringify(textPayloadWithNoEntities) });
var textAction2 = __assign(__assign({}, createEmptyAction()), { actionType: Action_1.ActionTypes.TEXT, payload: JSON.stringify(textPayloadWithRequiredEntity) });
var expectedCardPayloadValue = 'customTemplateName';
var cardActionArguments = [
{
parameter: 'p1',
value: textPayloadWithNoEntities
},
{
parameter: 'p2',
value: {
json: {}
}
}
];
var expectedCardActionArguments = cardActionArguments.map(function (aa) { return new Action_1.ActionArgument(aa); });
var cardAction = __assign(__assign({}, createEmptyAction()), { actionType: Action_1.ActionTypes.CARD, payload: JSON.stringify({
payload: expectedCardPayloadValue,
arguments: cardActionArguments
}) });
var expectedApiPayloadValue = 'myCallback';
var apiAction = __assign(__assign({}, createEmptyAction()), { actionType: Action_1.ActionTypes.API_LOCAL, payload: JSON.stringify({
payload: expectedApiPayloadValue,
logicArguments: [
{
parameter: 'p1',
value: textPayloadWithNoEntities
},
{
parameter: 'p2',
value: {
json: {}
}
}
],
renderArguments: [
{
parameter: 'p1',
value: textPayloadWithNoEntities
},
{
parameter: 'p2',
value: {
json: {}
}
}
]
}) });
describe('Action', function () {
describe('ActionBase', function () {
test('given object representing action should assign all properties to object', function () {
// Arrange
var actionLikeObject = {
actionId: 'fake-action-id',
actionType: Action_1.ActionTypes.TEXT,
createdDateTime: new Date().toJSON(),
payload: 'fake-action-payload',
isTerminal: false,
requiredEntitiesFromPayload: [],
requiredEntities: [],
negativeEntities: [],
requiredConditions: [],
negativeConditions: [],
suggestedEntity: 'fake-action',
version: 1,
packageCreationId: 1,
packageDeletionId: 0,
entityId: undefined,
enumValueId: undefined,
};
// Act
var action = new Action_1.ActionBase(actionLikeObject);
// Assert
expect(action.actionId).toEqual(actionLikeObject.actionId);
});
});
describe('GetPayload', function () {
test('given action with invalid payload, should throw exception when attempting to parse it as JSON', function () {
// Arrange
var corruptAction = new Action_1.ActionBase({
actionId: 'fake-action-id',
actionType: Action_1.ActionTypes.TEXT,
createdDateTime: new Date().toJSON(),
payload: 'fake-action-payload',
isTerminal: false,
requiredEntitiesFromPayload: [],
requiredEntities: [],
requiredConditions: [],
negativeEntities: [],
negativeConditions: [],
suggestedEntity: 'fake-action',
version: 1,
packageCreationId: 1,
packageDeletionId: 0,
entityId: undefined,
enumValueId: undefined,
});
// Act
var thrower = function () { return Action_1.ActionBase.GetPayload(corruptAction, new Map()); };
// Assert
expect(thrower).toThrowError();
});
test("given action with unknown type return raw payload since we don't reliably know how to parse it", function () {
// Arrange
var unknownAction = new Action_1.ActionBase({
actionId: 'fake-action-id',
actionType: 'fake-action-type',
createdDateTime: new Date().toJSON(),
payload: 'fake-action-payload',
isTerminal: false,
requiredEntitiesFromPayload: [],
requiredEntities: [],
requiredConditions: [],
negativeEntities: [],
negativeConditions: [],
suggestedEntity: 'fake-action',
version: 1,
packageCreationId: 1,
packageDeletionId: 0,
entityId: undefined,
enumValueId: undefined,
});
// Act
var payload = Action_1.ActionBase.GetPayload(unknownAction, new Map());
// Assert
expect(payload).toEqual(unknownAction.payload);
});
test('given text action should return the plain text string', function () {
// Act
var actualTextPayloadValue = Action_1.ActionBase.GetPayload(textAction1, new Map());
// Assert
expect(actualTextPayloadValue).toEqual('simple text payload');
});
test('given text action containing entity reference should return the plain text string with value replaced', function () {
// Act
var actualTextPayloadValue = Action_1.ActionBase.GetPayload(textAction2, new Map([['627a43be-4675-4b98-84a7-537262561be6', 'customValue']]));
// Assert
expect(actualTextPayloadValue).toEqual('The value of custom is: customValue');
});
test('given card action should return card template name', function () {
// Act
var actualCardPayloadValue = Action_1.ActionBase.GetPayload(cardAction, new Map());
// Assert
expect(actualCardPayloadValue).toEqual(expectedCardPayloadValue);
});
test('given api action should return callback name', function () {
// Act
var actualApiPayloadValue = Action_1.ActionBase.GetPayload(apiAction, new Map());
// Assert
expect(actualApiPayloadValue).toEqual(expectedApiPayloadValue);
});
});
describe('GetActionArguments', function () {
test('given text action should return empty array because text actions do not have arguments', function () {
// Act
var actionArguments = Action_1.ActionBase.GetActionArguments(textAction1);
// Assert
expect(actionArguments).toEqual([]);
});
test('given card action or api action should return arguments', function () {
// Act
var actionArguments = Action_1.ActionBase.GetActionArguments(cardAction);
// Assert
expect(actionArguments).toEqual(expectedCardActionArguments);
});
});
describe('ActionArgument', function () {
test('given action argument render it with given entity values', function () {
var actionArguments = Action_1.ActionBase.GetActionArguments(cardAction);
var renderedValue = actionArguments[0].renderValue(new Map());
expect(renderedValue).toContain(expectedSimpleTextPayload);
});
});
describe('TextAction', function () {
test('given action without text type throw exception during construction', function () {
var thrower = function () { return new Action_1.TextAction(cardAction); };
expect(thrower).toThrowError();
});
test('given action with text type should parse payload and assign to value', function () {
var textAction = new Action_1.TextAction(textAction1);
expect(textAction.value).toBeDefined();
});
test("given text action render it's value", function () {
var textAction = new Action_1.TextAction(textAction1);
var renderedValue = textAction.renderValue(new Map());
expect(renderedValue).toContain(expectedSimpleTextPayload);
});
test("given text action render it's value with options", function () {
var textAction = new Action_1.TextAction(textAction1);
var renderedValue = textAction.renderValue(new Map(), { fallbackToOriginal: true });
expect(renderedValue).toContain(expectedSimpleTextPayload);
});
});
describe('CardAction', function () {
test('given action with type mismatch throw exception during construction', function () {
var thrower = function () { return new Action_1.CardAction(textAction1); };
expect(thrower).toThrowError();
});
test('given action with card type should parse payload and extract templateName and arguments', function () {
var action = new Action_1.CardAction(cardAction);
expect(action.templateName).toBeDefined();
expect(action.arguments).toBeDefined();
});
test("given card action render it's value", function () {
var action = new Action_1.CardAction(cardAction);
var renderedArguments = action.renderArguments(new Map());
expect(renderedArguments[0].value).toEqual(expectedSimpleTextPayload);
});
test("given card action render it's value with options", function () {
var action = new Action_1.CardAction(cardAction);
var renderedArguments = action.renderArguments(new Map(), { fallbackToOriginal: true });
expect(renderedArguments[0].value).toEqual(expectedSimpleTextPayload);
});
});
describe('ApiAction', function () {
test('given action with type mismatch throw exception during construction', function () {
var thrower = function () { return new Action_1.ApiAction(textAction1); };
expect(thrower).toThrowError();
});
test('given action with api type should parse payload and extract name and arguments', function () {
var action = new Action_1.ApiAction(apiAction);
expect(action.name).toBeDefined();
expect(action.logicArguments).toBeDefined();
expect(action.renderArguments).toBeDefined();
});
test("given api action render it's arguments", function () {
var action = new Action_1.ApiAction(apiAction);
var renderedLogicArguments = action.renderLogicArguments(new Map());
var renderedRenderArguments = action.renderRenderArguments(new Map());
expect(renderedLogicArguments[0].value).toEqual(expectedSimpleTextPayload);
expect(renderedRenderArguments[0].value).toEqual(expectedSimpleTextPayload);
});
test("given api action render it's value with options", function () {
var action = new Action_1.ApiAction(apiAction);
var renderedLogicArguments = action.renderLogicArguments(new Map(), { fallbackToOriginal: true });
var renderedRenderArguments = action.renderRenderArguments(new Map(), { fallbackToOriginal: true });
expect(renderedLogicArguments[0].value).toEqual(expectedSimpleTextPayload);
expect(renderedRenderArguments[0].value).toEqual(expectedSimpleTextPayload);
});
});
});
//# sourceMappingURL=action.test.js.map