109 lines
4.8 KiB
JavaScript
109 lines
4.8 KiB
JavaScript
import R from 'ramda'
|
|
import {expect} from 'chai'
|
|
import {rules, enrichRule, collectMissingVariables, findVariantsAndRecords2, findVariantsAndRecords, getObjectives} from '../source/engine/rules'
|
|
import {analyseSituation} from '../source/engine/traverse'
|
|
|
|
let stateSelector = (state, name) => null
|
|
|
|
describe('enrichRule', function() {
|
|
|
|
it('should extract the type of the rule', function() {
|
|
let rule = {cotisation:{}}
|
|
expect(enrichRule(rule)).to.have.property('type','cotisation')
|
|
});
|
|
|
|
it('should extract the dotted name of the rule', function() {
|
|
let rule = {espace:"contrat salarié", nom: "CDD"}
|
|
expect(enrichRule(rule)).to.have.property('name','CDD')
|
|
expect(enrichRule(rule)).to.have.property('dottedName','contrat salarié . CDD')
|
|
});
|
|
|
|
it('should render Markdown in sub-questions', function() {
|
|
let rule = {"sous-question":"**wut**"}
|
|
expect(enrichRule(rule)).to.have.property('subquestion','<p><strong>wut</strong></p>\n')
|
|
});
|
|
});
|
|
|
|
describe('collectMissingVariables', function() {
|
|
|
|
it('should derive objectives from the root rule', function() {
|
|
let rawRules = [
|
|
{nom: "startHere", formule: {somme: [2, "deux"]}, espace: "sum"},
|
|
{nom: "deux", formule: 2, "non applicable si" : "sum . evt . ko", espace: "sum"},
|
|
{nom: "evt", espace: "sum", formule: {"une possibilité":["ko"]}, titre: "Truc", question:"?"},
|
|
{nom: "ko", espace: "sum . evt"}],
|
|
rules = rawRules.map(enrichRule),
|
|
situation = analyseSituation(rules,"startHere")(stateSelector),
|
|
result = getObjectives(situation)
|
|
|
|
expect(result).to.have.lengthOf(1)
|
|
expect(result[0]).to.have.property('name','deux')
|
|
});
|
|
|
|
it('should identify missing variables', function() {
|
|
let rawRules = [
|
|
{nom: "startHere", formule: {somme: [2, "deux"]}, espace: "sum"},
|
|
{nom: "deux", formule: 2, "non applicable si" : "sum . evt . ko", espace: "sum"},
|
|
{nom: "evt", espace: "sum", formule: {"une possibilité":["ko"]}, titre: "Truc", question:"?"},
|
|
{nom: "ko", espace: "sum . evt"}],
|
|
rules = rawRules.map(enrichRule),
|
|
situation = analyseSituation(rules,"startHere")(stateSelector),
|
|
result = collectMissingVariables()(situation)
|
|
|
|
expect(result).to.have.property('sum . evt . ko')
|
|
});
|
|
|
|
});
|
|
|
|
describe('findVariantsAndRecords', function() {
|
|
|
|
it('should classify rules as records by default', function() {
|
|
let rawRules = [
|
|
{nom: "startHere", formule: {somme: [3259, "dix"]}, espace: "top"},
|
|
{nom: "dix", formule: "cinq", espace: "top"},
|
|
{nom: "cinq", espace: "top", question:"?"}],
|
|
rules = rawRules.map(enrichRule),
|
|
situation = analyseSituation(rules,"startHere")(stateSelector),
|
|
result = findVariantsAndRecords2(rules, ['top . cinq'])
|
|
|
|
expect(result).to.have.deep.property('recordGroups', {top: ['top . cinq']})
|
|
});
|
|
|
|
it('should classify rules as variants if they are named in a "one of these" formula', function() {
|
|
let rawRules = [
|
|
{nom: "sum", formule: {somme: [2, "deux"]}, espace: "top"},
|
|
{nom: "deux", formule: 2, "non applicable si" : "top . sum . evt . ko", espace: "top"},
|
|
{nom: "evt", espace: "top . sum", formule: {"une possibilité":["ko"]}, titre: "Truc", question:"?"},
|
|
{nom: "ko", espace: "top . sum . evt"}],
|
|
rules = rawRules.map(enrichRule),
|
|
situation = analyseSituation(rules,"sum")(stateSelector),
|
|
result = findVariantsAndRecords2(rules, ['top . sum . evt . ko'])
|
|
|
|
expect(result).to.have.deep.property('variantGroups', {"top . sum . evt": ['top . sum . evt . ko']})
|
|
});
|
|
|
|
it('should find variants', function() {
|
|
let rawRules = [
|
|
{nom: "sum", formule: {somme: [2, "deux"]}, espace: "top"},
|
|
{nom: "deux", formule: 2, "non applicable si" : "top . sum . evt . ko", espace: "top"},
|
|
{nom: "evt", espace: "top . sum", formule: {"une possibilité":["ko"]}, titre: "Truc", question:"?"},
|
|
{nom: "ko", espace: "top . sum . evt"}],
|
|
rules = rawRules.map(enrichRule),
|
|
situation = analyseSituation(rules,"sum")(stateSelector),
|
|
result = findVariantsAndRecords2(rules, ['top . sum . evt . ko'])
|
|
|
|
expect(result['variantGroups']).to.deep.equal({"top . sum . evt": ['top . sum . evt . ko']})
|
|
});
|
|
|
|
it('should provide equivalent function to findVAR with findVariants', function() {
|
|
let situation = analyseSituation(rules,"surcoût CDD")(stateSelector),
|
|
findVAR = (memo,name) => findVariantsAndRecords(rules, memo, name, null),
|
|
missing = collectMissingVariables()(situation),
|
|
newResult = R.pipe(R.keys,R.reduce(findVAR, {variantGroups: {}, recordGroups: {}}))(missing),
|
|
oldResult = findVariantsAndRecords2(rules, R.keys(missing))
|
|
|
|
expect(newResult).to.deep.equal(oldResult)
|
|
});
|
|
|
|
});
|