mirror of
https://github.com/betagouv/mon-entreprise
synced 2025-02-09 00:35:00 +00:00
* Définition à partir du nom complet en notation pointée (plutôt que comme deux attributs indépendants "name" et "espace") * Structure de données de premier niveau "dictionnaire" plutôt que liste, s'aligne mieux avec notre contrainte d'unicité des noms * Possibilité de définir les règles à partir d'une liste dans les tests, dans ce cas il ne faut plus utiliser l'attribut "espace" mais renseigner directement la notation pointée dans le "nom".
159 lines
4.1 KiB
JavaScript
159 lines
4.1 KiB
JavaScript
import { expect } from 'chai'
|
|
import { map } from 'ramda'
|
|
import {
|
|
disambiguateRuleReference,
|
|
enrichRule,
|
|
nestedSituationToPathMap,
|
|
ruleParents,
|
|
rules,
|
|
translateAll
|
|
} from '../source/engine/rules'
|
|
|
|
describe('enrichRule', function() {
|
|
it('should extract the type of the rule', function() {
|
|
let rule = { nom: 'retraite', cotisation: {} }
|
|
expect(enrichRule(rule)).to.have.property('type', 'cotisation')
|
|
})
|
|
|
|
it('should extract the dotted name of the rule', function() {
|
|
let rule = { nom: 'contrat salarié . CDD' }
|
|
expect(enrichRule(rule)).to.have.property('name', 'CDD')
|
|
expect(enrichRule(rule)).to.have.property(
|
|
'dottedName',
|
|
'contrat salarié . CDD'
|
|
)
|
|
})
|
|
})
|
|
|
|
describe('rule checks', function() {
|
|
it('most input rules should have defaults', function() {
|
|
let rulesNeedingDefault = rules.filter(
|
|
r =>
|
|
r.espace &&
|
|
!r.simulateur &&
|
|
(!r.formule || r.formule['une possibilité']) &&
|
|
r.defaultValue == null &&
|
|
r.question &&
|
|
!['impôt . taux personnalisé'].includes(r.dottedName)
|
|
)
|
|
|
|
rulesNeedingDefault.map(r =>
|
|
//eslint-disable-next-line
|
|
console.log(
|
|
'La règle suivante doit avoir une valeur par défaut : ',
|
|
r.dottedName
|
|
)
|
|
)
|
|
expect(rulesNeedingDefault).to.be.empty
|
|
})
|
|
it('rules with a period should not have a flexible period', function() {
|
|
let problems = rules.filter(
|
|
({ defaultValue, période }) => période === 'flexible' && defaultValue
|
|
)
|
|
|
|
problems.map(({ dottedName }) =>
|
|
console.log(
|
|
'La valeur règle ',
|
|
dottedName,
|
|
" a une période flexible et une valeur par défaut. C'est un problème, car on ne sait pas pour quelle période ce défaut est défini. "
|
|
)
|
|
)
|
|
expect(problems).to.be.empty
|
|
})
|
|
})
|
|
|
|
it('rules with a formula should not have defaults', function() {
|
|
let errors = rules.filter(
|
|
r =>
|
|
r.formule !== undefined &&
|
|
!r.formule['une possibilité'] &&
|
|
r.defaultValue !== undefined
|
|
)
|
|
|
|
// variant formulas are an exception, their implementation is to refactor TODO
|
|
expect(errors).to.be.empty
|
|
})
|
|
describe('translateAll', function() {
|
|
it('should translate flat rules', function() {
|
|
let rules = [
|
|
{
|
|
nom: 'foo . bar',
|
|
titre: 'Titre',
|
|
description: 'Description',
|
|
question: 'Question'
|
|
}
|
|
]
|
|
let translations = {
|
|
'foo . bar': {
|
|
'titre.en': 'TITRE',
|
|
'description.en': 'DESC',
|
|
'question.en': 'QUEST'
|
|
}
|
|
}
|
|
|
|
let result = translateAll(translations, map(enrichRule, rules))
|
|
|
|
expect(result[0]).to.have.property('titre', 'TITRE')
|
|
expect(result[0]).to.have.property('description', 'DESC')
|
|
expect(result[0]).to.have.property('question', 'QUEST')
|
|
})
|
|
})
|
|
describe('misc', function() {
|
|
it('should unnest nested form values', function() {
|
|
let values = {
|
|
'contrat salarié': { rémunération: { 'brut de base': '2300' } }
|
|
}
|
|
|
|
let pathMap = nestedSituationToPathMap(values)
|
|
|
|
expect(pathMap).to.have.property(
|
|
'contrat salarié . rémunération . brut de base',
|
|
'2300'
|
|
)
|
|
})
|
|
it('should procude an array of the parents of a rule', function() {
|
|
let rawRules = [
|
|
{ nom: 'CDD', question: 'CDD ?' },
|
|
{ nom: 'CDD . taxe', formule: 'montant annuel / 12' },
|
|
{
|
|
nom: 'CDD . taxe . montant annuel',
|
|
formule: '20 - exonération annuelle'
|
|
},
|
|
{
|
|
nom: 'CDD . taxe . montant annuel . exonération annuelle',
|
|
formule: 20
|
|
}
|
|
]
|
|
|
|
let parents = ruleParents(rawRules.map(enrichRule)[3].dottedName)
|
|
expect(parents).to.eql([
|
|
['CDD', 'taxe', 'montant annuel'],
|
|
['CDD', 'taxe'],
|
|
['CDD']
|
|
])
|
|
})
|
|
it("should disambiguate a reference to another rule in a rule, given the latter's namespace", function() {
|
|
let rawRules = [
|
|
{ nom: 'CDD', question: 'CDD ?' },
|
|
{ nom: 'CDD . taxe', formule: 'montant annuel / 12' },
|
|
{
|
|
nom: 'CDD . taxe . montant annuel',
|
|
formule: '20 - exonération annuelle'
|
|
},
|
|
{
|
|
nom: 'CDD . taxe . montant annuel . exonération annuelle',
|
|
formule: 20
|
|
}
|
|
]
|
|
|
|
let enrichedRules = rawRules.map(enrichRule),
|
|
resolved = disambiguateRuleReference(
|
|
enrichedRules,
|
|
enrichedRules[2],
|
|
'exonération annuelle'
|
|
)
|
|
expect(resolved).to.eql(
|
|
'CDD . taxe . montant annuel . exonération annuelle'
|
|
)
|
|
})
|
|
})
|