2018-01-03 15:54:19 +00:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import dedent from 'dedent-js'
|
2018-11-20 14:39:00 +00:00
|
|
|
import { safeLoad } from 'js-yaml'
|
2018-06-06 08:22:46 +00:00
|
|
|
import { collectMissingVariables } from '../source/engine/generateQuestions'
|
2018-09-04 14:23:05 +00:00
|
|
|
import { enrichRule, rules as realRules } from '../source/engine/rules'
|
2018-06-06 08:22:46 +00:00
|
|
|
import { analyse, analyseMany, parseAll } from '../source/engine/traverse'
|
2018-01-03 15:54:19 +00:00
|
|
|
|
|
|
|
describe('inversions', () => {
|
|
|
|
it('should handle non inverted example', () => {
|
|
|
|
let fakeState = { brut: 2300 }
|
|
|
|
let stateSelector = name => fakeState[name]
|
|
|
|
|
|
|
|
let rawRules = dedent`
|
2017-11-20 12:16:18 +00:00
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: brut
|
|
|
|
taux: 77%
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
`,
|
2018-11-20 14:39:00 +00:00
|
|
|
rules = parseAll(safeLoad(rawRules).map(enrichRule)),
|
2018-01-03 15:54:19 +00:00
|
|
|
analysis = analyse(rules, 'net')(stateSelector)
|
2017-11-06 17:05:45 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
expect(analysis.targets[0].nodeValue).to.be.closeTo(1771, 0.001)
|
|
|
|
})
|
2017-11-14 15:01:16 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
it('should handle simple inversion', () => {
|
|
|
|
let fakeState = { net: 2000 }
|
|
|
|
let stateSelector = name => fakeState[name]
|
2017-11-06 17:05:45 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
let rawRules = dedent`
|
2017-11-20 12:16:18 +00:00
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: brut
|
|
|
|
taux: 77%
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
formule:
|
|
|
|
inversion:
|
|
|
|
avec:
|
|
|
|
- net
|
|
|
|
`,
|
2018-11-20 14:39:00 +00:00
|
|
|
rules = parseAll(safeLoad(rawRules).map(enrichRule)),
|
2018-01-03 15:54:19 +00:00
|
|
|
analysis = analyse(rules, 'brut')(stateSelector)
|
2017-11-07 08:48:13 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
expect(analysis.targets[0].nodeValue).to.be.closeTo(
|
|
|
|
2000 / (77 / 100),
|
|
|
|
0.0001 * 2000
|
|
|
|
)
|
|
|
|
})
|
2017-11-07 08:48:13 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
it('should ask the input of one of the possible inversions', () => {
|
|
|
|
let rawRules = dedent`
|
2017-11-20 12:16:18 +00:00
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: assiette
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
2018-08-07 19:20:25 +00:00
|
|
|
alors:
|
|
|
|
taux: 80%
|
|
|
|
- sinon:
|
|
|
|
taux: 70%
|
2017-11-20 12:16:18 +00:00
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
formule:
|
|
|
|
inversion:
|
|
|
|
avec:
|
|
|
|
- net
|
|
|
|
- nom: cadre
|
|
|
|
- nom: assiette
|
|
|
|
formule: 67 + brut
|
|
|
|
|
|
|
|
`,
|
2018-11-20 14:39:00 +00:00
|
|
|
rules = parseAll(safeLoad(rawRules).map(enrichRule)),
|
2018-06-06 08:22:46 +00:00
|
|
|
stateSelector = () => null,
|
2018-01-03 15:54:19 +00:00
|
|
|
analysis = analyse(rules, 'brut')(stateSelector),
|
|
|
|
missing = collectMissingVariables(analysis.targets)
|
2017-11-16 13:04:51 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
expect(analysis.targets[0].nodeValue).to.be.null
|
2018-04-23 09:22:09 +00:00
|
|
|
expect(missing).to.include('brut')
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|
2017-11-16 13:04:51 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
it('should handle inversions with missing variables', () => {
|
|
|
|
let rawRules = dedent`
|
2017-11-20 12:16:18 +00:00
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: assiette
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
2018-08-07 19:20:25 +00:00
|
|
|
alors:
|
|
|
|
taux: 80%
|
|
|
|
- sinon:
|
|
|
|
taux: 70%
|
2017-11-20 12:16:18 +00:00
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
formule:
|
|
|
|
inversion:
|
|
|
|
avec:
|
|
|
|
- net
|
|
|
|
- nom: cadre
|
|
|
|
- nom: assiette
|
2018-04-10 15:16:27 +00:00
|
|
|
formule:
|
|
|
|
somme:
|
|
|
|
- 1200
|
|
|
|
- brut
|
|
|
|
- taxeOne
|
|
|
|
- nom: taxeOne
|
|
|
|
non applicable si: cadre
|
|
|
|
formule: taxe + taxe
|
|
|
|
- nom: taxe
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: 1200
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
2018-08-07 19:20:25 +00:00
|
|
|
alors:
|
|
|
|
taux: 80%
|
|
|
|
- sinon:
|
|
|
|
taux: 70%
|
2017-11-20 12:16:18 +00:00
|
|
|
`,
|
2018-11-20 14:39:00 +00:00
|
|
|
rules = parseAll(safeLoad(rawRules).map(enrichRule)),
|
2018-01-03 15:54:19 +00:00
|
|
|
stateSelector = name => ({ net: 2000 }[name]),
|
|
|
|
analysis = analyse(rules, 'brut')(stateSelector),
|
|
|
|
missing = collectMissingVariables(analysis.targets)
|
2017-11-06 17:05:45 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
expect(analysis.targets[0].nodeValue).to.be.null
|
2018-04-23 09:22:09 +00:00
|
|
|
expect(missing).to.include('cadre')
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|
2017-11-14 15:01:16 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
it("shouldn't report a missing salary if another salary was input", () => {
|
|
|
|
let rawRules = dedent`
|
2017-11-14 15:01:16 +00:00
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: assiette
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
2018-08-07 19:20:25 +00:00
|
|
|
alors:
|
|
|
|
taux: 80%
|
2017-11-14 15:01:16 +00:00
|
|
|
- si: ≠ cadre
|
2018-08-07 19:20:25 +00:00
|
|
|
alors:
|
|
|
|
taux: 70%
|
2017-11-14 15:01:16 +00:00
|
|
|
|
|
|
|
- nom: total
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: assiette
|
|
|
|
taux: 150%
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
|
|
|
inversion:
|
|
|
|
avec:
|
|
|
|
- net
|
|
|
|
- total
|
2017-11-14 15:01:16 +00:00
|
|
|
|
|
|
|
- nom: cadre
|
|
|
|
|
|
|
|
- nom: assiette
|
|
|
|
formule: 67 + brut
|
|
|
|
|
|
|
|
`,
|
2018-11-20 14:39:00 +00:00
|
|
|
rules = parseAll(safeLoad(rawRules).map(enrichRule)),
|
2018-01-03 15:54:19 +00:00
|
|
|
stateSelector = name => ({ net: 2000, cadre: 'oui' }[name]),
|
|
|
|
analysis = analyse(rules, 'total')(stateSelector),
|
|
|
|
missing = collectMissingVariables(analysis.targets)
|
|
|
|
|
|
|
|
expect(analysis.targets[0].nodeValue).to.equal(3750)
|
|
|
|
expect(missing).to.be.empty
|
|
|
|
})
|
2018-09-19 13:16:04 +00:00
|
|
|
it('complex inversion with composantes', () => {
|
|
|
|
let rawRules = dedent`
|
2017-11-23 13:48:42 +00:00
|
|
|
- nom: net
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: 67 + brut
|
|
|
|
taux: 80%
|
|
|
|
|
|
|
|
- nom: cotisation
|
|
|
|
formule:
|
|
|
|
multiplication:
|
|
|
|
assiette: 67 + brut
|
|
|
|
composantes:
|
|
|
|
- attributs:
|
|
|
|
dû par: employeur
|
|
|
|
taux: 100%
|
|
|
|
- attributs:
|
|
|
|
dû par: salarié
|
|
|
|
taux: 50%
|
|
|
|
|
|
|
|
- nom: total
|
|
|
|
formule: cotisation (employeur) + cotisation (salarié)
|
|
|
|
|
|
|
|
- nom: brut
|
|
|
|
format: euro
|
|
|
|
formule:
|
|
|
|
inversion:
|
|
|
|
avec:
|
|
|
|
- net
|
|
|
|
- total
|
|
|
|
`,
|
2018-09-19 13:16:04 +00:00
|
|
|
rules = parseAll(safeLoad(rawRules).map(enrichRule)),
|
|
|
|
stateSelector = name => ({ net: 2000 }[name]),
|
|
|
|
analysis = analyse(rules, 'total')(stateSelector),
|
|
|
|
missing = collectMissingVariables(analysis.targets)
|
2018-01-03 15:54:19 +00:00
|
|
|
|
2018-09-19 13:16:04 +00:00
|
|
|
expect(analysis.targets[0].nodeValue).to.equal(3750)
|
|
|
|
expect(missing).to.be.empty
|
|
|
|
})
|
|
|
|
it('should collect missing variables not too slowly', function() {
|
|
|
|
let stateSelector = name =>
|
|
|
|
({ 'contrat salarié . salaire . net': '2300' }[name])
|
|
|
|
|
|
|
|
let rules = parseAll(realRules.map(enrichRule)),
|
|
|
|
analysis = analyseMany(rules, [
|
|
|
|
'contrat salarié . salaire . brut',
|
|
|
|
'contrat salarié . rémunération . total'
|
|
|
|
])(stateSelector)
|
|
|
|
|
|
|
|
let start = Date.now()
|
|
|
|
collectMissingVariables(analysis.targets)
|
|
|
|
let elapsed = Date.now() - start
|
|
|
|
expect(elapsed).to.be.below(1500)
|
|
|
|
})
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|