@coorpacademy/progression-engine
Version:
392 lines (391 loc) • 10.1 kB
JavaScript
"use strict";
var _ava = _interopRequireDefault(require("ava"));
var _selectRule = _interopRequireWildcard(require("../select-rule"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const createState = nextContent => ({
nextContent,
lives: 4,
stars: 0,
livesDisabled: true,
isCorrect: true,
slides: [],
requestedClues: [],
viewedResources: [],
step: {
current: 0
},
remainingLifeRequests: 0,
allAnswers: [],
hasViewedAResourceAtThisStep: false,
variables: {},
pendingSlides: []
});
(0, _ava.default)('should return null if no rules match', t => {
const state = createState({
type: 'slide',
ref: ''
});
const rule = (0, _selectRule.default)([], state);
t.is(rule, null);
});
const source = {
type: 'slide',
ref: '1.A1.1'
};
const destination = {
type: 'slide',
ref: '1.A1.2'
};
const defaultState = {
nextContent: source,
lives: 2,
stars: 0,
livesDisabled: true,
isCorrect: true,
slides: [],
requestedClues: [],
viewedResources: [],
step: {
current: 0
},
variables: {},
remainingLifeRequests: 0,
allAnswers: [],
hasViewedAResourceAtThisStep: false,
pendingSlides: []
};
const chapterRules = [{
source: destination,
destination: source,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'lives'
},
operator: 'EQUALS',
values: [2]
}],
priority: 0,
ref: '1'
}, {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'lives'
},
operator: 'EQUALS',
values: [2]
}],
priority: 2,
ref: 'low_priority'
}, {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'lives'
},
operator: 'EQUALS',
values: [2]
}],
priority: 0,
ref: 'high_priority'
}, {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'lives'
},
operator: 'EQUALS',
values: [2]
}],
priority: 1,
ref: 'mid_priority'
}, {
source: _selectRule.DEFAULT_SOURCE,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'lives'
},
operator: 'EQUALS',
values: [2]
}],
priority: 0,
ref: '3'
}];
(0, _ava.default)('should select right chapter from source and priority', t => {
const state = defaultState;
const actualChapterRule = (0, _selectRule.default)(chapterRules, state) || {};
t.is(actualChapterRule.ref, 'high_priority');
});
(0, _ava.default)('should select chapterRule with empty source if state is null', t => {
const rule = (0, _selectRule.default)(chapterRules, null);
t.is(rule && rule.ref, '3');
});
(0, _ava.default)('should return no chapterRule if none match', t => {
const state = {
...defaultState,
nextContent: {
type: 'slide',
ref: 'noop'
}
};
const actualChapterRule = (0, _selectRule.default)(chapterRules, state);
t.is(actualChapterRule, null);
});
(0, _ava.default)("should select chapterRule with 'slide' scope", t => {
const slideScopedRules = [{
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'slide',
ref: '1.A1.1',
field: 'answer'
},
operator: 'EQUALS',
values: [['otherValue']]
}],
priority: 0,
ref: 'answerOtherValue'
}, {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'slide',
ref: '1.A1.1',
field: 'answer'
},
operator: 'EQUALS',
values: [['foo']]
}],
priority: 0,
ref: 'answer'
}, {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'slide',
ref: '1.A1.1',
field: 'isCorrect'
},
operator: 'EQUALS',
values: [true]
}],
priority: 0,
ref: 'isCorrect'
}];
const answerState = {
...defaultState,
allAnswers: [{
slideRef: '1.A1.1',
answer: ['bar'],
isCorrect: true
}, {
slideRef: '1.A1.1',
answer: ['foo'],
isCorrect: false
}]
};
const answerChapterRule = (0, _selectRule.default)(slideScopedRules, answerState);
t.is((answerChapterRule || {}).ref, 'answer', "Don't support 'slide' scoped target with 'answer' field");
const noAnswerState = {
...defaultState,
allAnswers: []
};
const noAnswerChapterRule = (0, _selectRule.default)(slideScopedRules, noAnswerState);
t.is(noAnswerChapterRule, null, "Don't support 'slide' scoped target with 'answer' field");
const isCorrectState = {
...defaultState,
allAnswers: [{
slideRef: '1.A1.1',
answer: ['bar'],
isCorrect: true
}]
};
const isCorrectChapterRule = (0, _selectRule.default)(slideScopedRules, isCorrectState);
t.is((isCorrectChapterRule || {}).ref, 'isCorrect', "Don't support 'slide' scoped target with 'isCorrect' field");
const noIsCorrectState = {
...defaultState,
allAnswers: [{
slideRef: '1.A1.1',
answer: ['bar'],
isCorrect: false
}]
};
const noIsCorrectChapterRule = (0, _selectRule.default)(slideScopedRules, noIsCorrectState);
t.is(noIsCorrectChapterRule, null, "Don't support 'slide' scoped target with 'isCorrect' field");
});
(0, _ava.default)("should select chapterRule with 'variables' scope", t => {
const slideScopedRules = [{
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'foo'
},
operator: 'EQUALS',
values: [1]
}],
priority: 0,
ref: 'lives'
}, {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'lives'
},
operator: 'GTE',
values: [42]
}],
priority: 0,
ref: 'lives'
}, {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'stars'
},
operator: 'LTE',
values: [-1]
}],
priority: 0,
ref: 'stars'
}, {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'custom'
},
operator: 'BETWEEN',
values: [41, 43]
}],
priority: 0,
ref: 'custom'
}];
const livesState = {
...defaultState,
lives: 42
};
const livesChapterRule = (0, _selectRule.default)(slideScopedRules, livesState);
t.is((livesChapterRule || {}).ref, 'lives', "Don't support 'variable' scoped target with 'lives' field");
const noLivesState = defaultState;
const noLivesChapterRule = (0, _selectRule.default)(slideScopedRules, noLivesState);
t.is(noLivesChapterRule, null, "Don't support 'variable' scoped target with 'lives' field");
const starsState = {
...defaultState,
stars: -1
};
const starsChapterRule = (0, _selectRule.default)(slideScopedRules, starsState);
t.is((starsChapterRule || {}).ref, 'stars', "Don't support 'variable' scoped target with 'stars' field");
const noStarsState = defaultState;
const noStarsChapterRule = (0, _selectRule.default)(slideScopedRules, noStarsState);
t.is(noStarsChapterRule, null, "Don't support 'variable' scoped target with 'stars' field");
const customState = {
...defaultState,
variables: {
custom: 42
}
};
const customChapterRule = (0, _selectRule.default)(slideScopedRules, customState);
t.is((customChapterRule || {}).ref, 'custom', "Don't support 'variable' scoped target with custom field");
const nocustomState = defaultState;
const nocustomChapterRule = (0, _selectRule.default)(slideScopedRules, nocustomState);
t.is(nocustomChapterRule, null, "Don't support 'variable' scoped target with custom field");
});
(0, _ava.default)("should select a rule with source.ref:'*'", t => {
const value = 111;
const baseRule = {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'foo'
},
operator: 'EQUALS',
values: [value]
}],
ref: '1'
};
const sameSourceLowPriority = {
...baseRule,
priority: 1
};
const globalSourceHighPriority = {
...baseRule,
source: {
type: 'slide',
ref: '*'
},
priority: 0
};
const rules = [sameSourceLowPriority, globalSourceHighPriority];
const state = {
...defaultState,
variables: {
foo: value
}
};
const selectedRule = (0, _selectRule.default)(rules, state);
t.deepEqual(selectedRule, globalSourceHighPriority);
});
(0, _ava.default)("should select a random rule with source.ref:'*'", t => {
const value = 111;
const baseRule = {
source,
destination,
instructions: [],
conditions: [{
target: {
scope: 'variable',
field: 'foo'
},
operator: 'EQUALS',
values: [value]
}],
ref: '1',
priority: 1
};
const rules = [baseRule, baseRule, baseRule];
const state = {
...defaultState,
variables: {
foo: value
}
};
const selectedRule = (0, _selectRule.default)(rules, state);
t.deepEqual(selectedRule, baseRule);
});
//# sourceMappingURL=select-rule.js.map