Skip to content

Commit

Permalink
refactor(optim): add the missing 'grille' mecanism for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Jan 17, 2024
1 parent fed1c1e commit a4bdc5a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
6 changes: 3 additions & 3 deletions source/serializeParsedRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ function serializeASTNode(node: ASTNode): SerializedRule {
}
}

case 'taux progressif':
case 'barème': {
case 'barème':
case 'grille':
case 'taux progressif': {
const serializedNode = {
assiette: serializeASTNode(node.explanation.assiette),
tranches: node.explanation.tranches.map((tranche) => {
Expand Down Expand Up @@ -350,7 +351,6 @@ export function serializeParsedRules(

for (const [rule, node] of Object.entries(parsedRules)) {
if (Object.keys(node.rawNode).length === 0) {
console.log(`[SERIALIZE_PARSED_RULES]: empty rule '${rule}' found`)
// Empty rule should be null not {}
rawRules[rule] = null
continue
Expand Down
53 changes: 53 additions & 0 deletions test/serializeParsedRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ describe('API > mecanisms list', () => {
expect(serializedRules).toStrictEqual(rules)
})

it('should serialize rule with constant [formule]', () => {
const rules = {
rule: {
titre: 'My rule',
formule: '10 * rule2',
},
rule2: {
valeur: 2,
},
}
const serializedRules = serializeParsedRules(
new Engine(rules).getParsedRules(),
)
expect(serializedRules).toStrictEqual({
rule: {
titre: 'My rule',
valeur: '10 * rule2',
},
rule2: {
valeur: 2,
},
})
})

it('should serialize rule with ref [applicable si]', () => {
const rules = {
rule: {
Expand Down Expand Up @@ -366,6 +390,35 @@ describe('API > mecanisms list', () => {
expect(serializedRules).toStrictEqual(rules)
})

it('should serialize rule with [grille]', () => {
const rules = {
'SMIC horaire': {
valeur: '10 €/heures',
},
'revenu cotisé': {
valeur: '1900 €/an',
},
'trimestres validés': {
unité: 'trimestres validés/an',
grille: {
assiette: 'revenu cotisé',
multiplicateur: 'SMIC horaire',
tranches: [
{ montant: 0, plafond: '150 heures/an' },
{ montant: 1, plafond: '300 heures/an' },
{ montant: 2, plafond: '450 heures/an' },
{ montant: 3, plafond: '600 heures/an' },
{ montant: 4 },
],
},
},
}
const serializedRules = serializeParsedRules(
new Engine(rules).getParsedRules(),
)
expect(serializedRules).toStrictEqual(rules)
})

it("should serialize rule with [barème] and a custom 'multiplicateur'", () => {
const rules = {
'revenu imposable': {
Expand Down

0 comments on commit a4bdc5a

Please sign in to comment.