fix: complete InvesteMoz validation and error corrections
This commit is contained in:
@@ -1,52 +1,234 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { newImportExemption,evaluateImportExemption,recordImportExemptions,saveImportExemptionBook,importExemptionBook,importExemptionCurrent,importExemptionRules,importCustomsSource } from '../../resources/js/lib/iva-import-exemptions.ts';
|
||||
import {
|
||||
newImportExemption,
|
||||
evaluateImportExemption,
|
||||
recordImportExemptions,
|
||||
saveImportExemptionBook,
|
||||
importExemptionBook,
|
||||
importExemptionCurrent,
|
||||
importExemptionRules,
|
||||
importCustomsSource,
|
||||
} from '../../resources/js/lib/iva-import-exemptions.ts';
|
||||
import { salesCurrent } from '../../resources/js/lib/iva-operations.ts';
|
||||
import { acquisitionCurrent } from '../../resources/js/lib/iva-acquisitions.ts';
|
||||
import { createProjectDossierReport } from '../../resources/js/lib/project-dossier-report.ts';
|
||||
const frozen=JSON.parse(fs.readFileSync(new URL('./fixtures/iva-g6c-inputs.json',import.meta.url),'utf8'));
|
||||
const ids=['customs_transit_import','customs_temporary_import','customs_drawback_import'];
|
||||
const row=(treatment=ids[0])=>({...newImportExemption('g6d-'+treatment,'MZN'),treatment,description:'SYNTHETIC QA '+treatment,period:'2026-12',declaredValue:'5000',importer:'SYNTHETIC QA importador',documentRef:'SYNTHETIC QA G6D '+treatment,documentDate:'2026-11-14',evidence:'SYNTHETIC QA bens, regime e isenção total de direitos expressamente assumidos; não é documento real',conditions:Object.fromEntries(importExemptionRules.find(r=>r.id===treatment).conditions.map(([key])=>[key,'yes']))});
|
||||
const draft=()=>({...structuredClone(frozen),context_opportunity:'opportunity:333',project_title:'SYNTHETIC QA — G6D / Regimes aduaneiros'});
|
||||
|
||||
test('P048 has three treatments,one branch;regime and total customs exemption both required,unknown is not zero',()=>{
|
||||
for(const id of ids){
|
||||
const input=row(id),rule=importExemptionRules.find(r=>r.id===id);assert.equal(rule.branch,'P048');assert.equal(evaluateImportExemption(input,'MZN').importVat,'0.00');
|
||||
for(const [key] of rule.conditions)for(const value of ['','no','unknown']){
|
||||
const result=evaluateImportExemption({...input,conditions:{...input.conditions,[key]:value}},'MZN');assert.equal(result.status,value==='no'?'not_applicable':'indeterminate');assert.equal(result.importVat,null);
|
||||
}
|
||||
for(const patch of [{period:'2025-12'},{currency:'USD'},{documentRef:''},{documentDate:'2026-02-30'},{evidence:''}])assert.equal(evaluateImportExemption({...input,...patch},'MZN').importVat,null);
|
||||
assert.deepEqual(input,row(id));
|
||||
}
|
||||
assert.match(importCustomsSource,/isentas totalmente de direitos aduaneiros/);
|
||||
const frozen = JSON.parse(
|
||||
fs.readFileSync(
|
||||
new URL('./fixtures/iva-g6c-inputs.json', import.meta.url),
|
||||
'utf8',
|
||||
),
|
||||
);
|
||||
const ids = [
|
||||
'customs_transit_import',
|
||||
'customs_temporary_import',
|
||||
'customs_drawback_import',
|
||||
];
|
||||
const row = (treatment = ids[0]) => ({
|
||||
...newImportExemption('g6d-' + treatment, 'MZN'),
|
||||
treatment,
|
||||
description: 'SYNTHETIC QA ' + treatment,
|
||||
period: '2026-12',
|
||||
declaredValue: '5000',
|
||||
importer: 'SYNTHETIC QA importador',
|
||||
documentRef: 'SYNTHETIC QA G6D ' + treatment,
|
||||
documentDate: '2026-11-14',
|
||||
evidence:
|
||||
'SYNTHETIC QA bens, regime e isenção total de direitos expressamente assumidos; não é documento real',
|
||||
conditions: Object.fromEntries(
|
||||
importExemptionRules
|
||||
.find((r) => r.id === treatment)
|
||||
.conditions.map(([key]) => [key, 'yes']),
|
||||
),
|
||||
});
|
||||
const draft = () => ({
|
||||
...structuredClone(frozen),
|
||||
context_opportunity: 'opportunity:333',
|
||||
project_title: 'SYNTHETIC QA — G6D / Regimes aduaneiros',
|
||||
});
|
||||
|
||||
test('partial relief,suspension or former reimport confirmation never substitute for full exemption;regimes keep distinct confirmations',()=>{
|
||||
const input=row();
|
||||
for(const evidence of ['SYNTHETIC QA redução parcial50%','SYNTHETIC QA mera suspensão de direitos']){
|
||||
const result=evaluateImportExemption({...input,evidence,conditions:{...input.conditions,fullCustomsExemption:'no'}},'MZN');assert.equal(result.status,'not_applicable');assert.equal(result.importVat,null);assert.match(result.reason,/sem taxa presumida/);
|
||||
}
|
||||
assert.equal(evaluateImportExemption({...input,conditions:{customsTransit:'yes',customsExempt:'yes'}},'MZN').status,'indeterminate');
|
||||
const switched={...input,treatment:ids[1]};assert.equal(evaluateImportExemption(switched,'MZN').status,'indeterminate');assert.equal(switched.documentRef,input.documentRef);assert.equal(switched.conditions.customsTransit,'yes');
|
||||
assert.equal(evaluateImportExemption({...switched,conditions:{...switched.conditions,customsTemporary:'yes'}},'MZN').status,'exempt');
|
||||
assert.equal(evaluateImportExemption({...input,treatment:ids[2]},'MZN').status,'indeterminate');
|
||||
void test('P048 has three treatments,one branch;regime and total customs exemption both required,unknown is not zero', () => {
|
||||
for (const id of ids) {
|
||||
const input = row(id),
|
||||
rule = importExemptionRules.find((r) => r.id === id);
|
||||
assert.equal(rule.branch, 'P048');
|
||||
assert.equal(evaluateImportExemption(input, 'MZN').importVat, '0.00');
|
||||
for (const [key] of rule.conditions)
|
||||
for (const value of ['', 'no', 'unknown']) {
|
||||
const result = evaluateImportExemption(
|
||||
{
|
||||
...input,
|
||||
conditions: { ...input.conditions, [key]: value },
|
||||
},
|
||||
'MZN',
|
||||
);
|
||||
assert.equal(
|
||||
result.status,
|
||||
value === 'no' ? 'not_applicable' : 'indeterminate',
|
||||
);
|
||||
assert.equal(result.importVat, null);
|
||||
}
|
||||
for (const patch of [
|
||||
{ period: '2025-12' },
|
||||
{ currency: 'USD' },
|
||||
{ documentRef: '' },
|
||||
{ documentDate: '2026-02-30' },
|
||||
{ evidence: '' },
|
||||
])
|
||||
assert.equal(
|
||||
evaluateImportExemption({ ...input, ...patch }, 'MZN')
|
||||
.importVat,
|
||||
null,
|
||||
);
|
||||
assert.deepEqual(input, row(id));
|
||||
}
|
||||
assert.match(
|
||||
importCustomsSource,
|
||||
/isentas totalmente de direitos aduaneiros/,
|
||||
);
|
||||
});
|
||||
|
||||
test('explicit classification archives indeterminate/nonapplicable inputs;currency and context changes mark earlier result historical',()=>{
|
||||
const d=draft(),input=row();let book=recordImportExemptions(d,{rows:[{...input,conditions:{customsTransit:'yes'}}]});assert.equal(book.result.rows[0].status,'indeterminate');
|
||||
book=recordImportExemptions(d,{...book,rows:[{...input,conditions:{...input.conditions,fullCustomsExemption:'no'}}]});assert.equal(book.result.rows[0].status,'not_applicable');
|
||||
book=recordImportExemptions(d,{...book,rows:ids.map(row)});assert.equal(book.history.length,2);assert.equal(book.history[0].rows[0].importVat,null);assert.equal(book.history[1].rows[0].importVat,null);assert.ok(importExemptionCurrent(d,book));
|
||||
for(const patch of [{currency:'USD'},{context_opportunity:'opportunity:332'},{selection_labels:{province_id:'Maputo'}}])assert.ok(!importExemptionCurrent({...d,...patch},book));
|
||||
void test('partial relief,suspension or former reimport confirmation never substitute for full exemption;regimes keep distinct confirmations', () => {
|
||||
const input = row();
|
||||
for (const evidence of [
|
||||
'SYNTHETIC QA redução parcial50%',
|
||||
'SYNTHETIC QA mera suspensão de direitos',
|
||||
]) {
|
||||
const result = evaluateImportExemption(
|
||||
{
|
||||
...input,
|
||||
evidence,
|
||||
conditions: { ...input.conditions, fullCustomsExemption: 'no' },
|
||||
},
|
||||
'MZN',
|
||||
);
|
||||
assert.equal(result.status, 'not_applicable');
|
||||
assert.equal(result.importVat, null);
|
||||
assert.match(result.reason, /sem taxa presumida/);
|
||||
}
|
||||
assert.equal(
|
||||
evaluateImportExemption(
|
||||
{
|
||||
...input,
|
||||
conditions: { customsTransit: 'yes', customsExempt: 'yes' },
|
||||
},
|
||||
'MZN',
|
||||
).status,
|
||||
'indeterminate',
|
||||
);
|
||||
const switched = { ...input, treatment: ids[1] };
|
||||
assert.equal(
|
||||
evaluateImportExemption(switched, 'MZN').status,
|
||||
'indeterminate',
|
||||
);
|
||||
assert.equal(switched.documentRef, input.documentRef);
|
||||
assert.equal(switched.conditions.customsTransit, 'yes');
|
||||
assert.equal(
|
||||
evaluateImportExemption(
|
||||
{
|
||||
...switched,
|
||||
conditions: { ...switched.conditions, customsTemporary: 'yes' },
|
||||
},
|
||||
'MZN',
|
||||
).status,
|
||||
'exempt',
|
||||
);
|
||||
assert.equal(
|
||||
evaluateImportExemption({ ...input, treatment: ids[2] }, 'MZN').status,
|
||||
'indeterminate',
|
||||
);
|
||||
});
|
||||
|
||||
test('save/recover/report preserve24acceptedbooks current;P048 has three report rows and never populates deduction field11',()=>{
|
||||
const d=draft(),prior=JSON.parse(frozen.modelo_a_snapshot);let book=recordImportExemptions(d,{rows:ids.map(id=>({...row(id),conditions:{}}))});book=recordImportExemptions(d,{...book,rows:ids.map(row)});d.modelo_a_snapshot=saveImportExemptionBook(d,book);
|
||||
assert.deepEqual(importExemptionBook(JSON.parse(JSON.stringify(d))),book);assert.match(book.result.sourceKey,/import-customs-regimes\/1/);assert.doesNotMatch(book.result.sourceKey,/beneficiaryVersion|assetVersion/);assert.match(book.result.source,/12\(1\)\(c\)/);
|
||||
const next=JSON.parse(d.modelo_a_snapshot);assert.deepEqual(next.salesOperations,prior.salesOperations);assert.deepEqual(next.books,prior.books);assert.equal(Object.keys(prior.importExemptions).length,3);
|
||||
for(const [scope,imports] of Object.entries(prior.importExemptions)){assert.deepEqual(next.importExemptions[scope],imports);assert.ok(importExemptionCurrent({...d,context_opportunity:scope},imports));}
|
||||
for(const [scope,sales] of Object.entries(next.salesOperations))assert.ok(salesCurrent({...d,context_opportunity:scope},sales));
|
||||
assert.ok(acquisitionCurrent({...d,context_opportunity:'opportunity:329'},next.books['opportunity:329']));for(const field of ['11','17','18'])assert.equal(next.books['opportunity:329'].result.periods[0].fields[field],null);
|
||||
const report=JSON.stringify(createProjectDossierReport(d,{selectedSections:new Set(['finance']),version:'QA G6D'}));for(const token of ['P048','opportunity:333','Manica','2026-12','SYNTHETIC QA G6D','0.00','12(1)(c)','campo11',...ids])assert.ok(report.includes(token),token);
|
||||
if(process.env.IVA_G6D_CALCULATED)fs.writeFileSync(process.env.IVA_G6D_CALCULATED,JSON.stringify(d));
|
||||
void test('explicit classification archives indeterminate/nonapplicable inputs;currency and context changes mark earlier result historical', () => {
|
||||
const d = draft(),
|
||||
input = row();
|
||||
let book = recordImportExemptions(d, {
|
||||
rows: [{ ...input, conditions: { customsTransit: 'yes' } }],
|
||||
});
|
||||
assert.equal(book.result.rows[0].status, 'indeterminate');
|
||||
book = recordImportExemptions(d, {
|
||||
...book,
|
||||
rows: [
|
||||
{
|
||||
...input,
|
||||
conditions: { ...input.conditions, fullCustomsExemption: 'no' },
|
||||
},
|
||||
],
|
||||
});
|
||||
assert.equal(book.result.rows[0].status, 'not_applicable');
|
||||
book = recordImportExemptions(d, { ...book, rows: ids.map(row) });
|
||||
assert.equal(book.history.length, 2);
|
||||
assert.equal(book.history[0].rows[0].importVat, null);
|
||||
assert.equal(book.history[1].rows[0].importVat, null);
|
||||
assert.ok(importExemptionCurrent(d, book));
|
||||
for (const patch of [
|
||||
{ currency: 'USD' },
|
||||
{ context_opportunity: 'opportunity:332' },
|
||||
{ selection_labels: { province_id: 'Maputo' } },
|
||||
])
|
||||
assert.ok(!importExemptionCurrent({ ...d, ...patch }, book));
|
||||
});
|
||||
|
||||
void test('save/recover/report preserve24acceptedbooks current;P048 has three report rows and never populates deduction field11', () => {
|
||||
const d = draft(),
|
||||
prior = JSON.parse(frozen.modelo_a_snapshot);
|
||||
let book = recordImportExemptions(d, {
|
||||
rows: ids.map((id) => ({ ...row(id), conditions: {} })),
|
||||
});
|
||||
book = recordImportExemptions(d, { ...book, rows: ids.map(row) });
|
||||
d.modelo_a_snapshot = saveImportExemptionBook(d, book);
|
||||
assert.deepEqual(importExemptionBook(JSON.parse(JSON.stringify(d))), book);
|
||||
assert.match(book.result.sourceKey, /import-customs-regimes\/1/);
|
||||
assert.doesNotMatch(
|
||||
book.result.sourceKey,
|
||||
/beneficiaryVersion|assetVersion/,
|
||||
);
|
||||
assert.match(book.result.source, /12\(1\)\(c\)/);
|
||||
const next = JSON.parse(d.modelo_a_snapshot);
|
||||
assert.deepEqual(next.salesOperations, prior.salesOperations);
|
||||
assert.deepEqual(next.books, prior.books);
|
||||
assert.equal(Object.keys(prior.importExemptions).length, 3);
|
||||
for (const [scope, imports] of Object.entries(prior.importExemptions)) {
|
||||
assert.deepEqual(next.importExemptions[scope], imports);
|
||||
assert.ok(
|
||||
importExemptionCurrent(
|
||||
{ ...d, context_opportunity: scope },
|
||||
imports,
|
||||
),
|
||||
);
|
||||
}
|
||||
for (const [scope, sales] of Object.entries(next.salesOperations))
|
||||
assert.ok(salesCurrent({ ...d, context_opportunity: scope }, sales));
|
||||
assert.ok(
|
||||
acquisitionCurrent(
|
||||
{ ...d, context_opportunity: 'opportunity:329' },
|
||||
next.books['opportunity:329'],
|
||||
),
|
||||
);
|
||||
for (const field of ['11', '17', '18'])
|
||||
assert.equal(
|
||||
next.books['opportunity:329'].result.periods[0].fields[field],
|
||||
null,
|
||||
);
|
||||
const report = JSON.stringify(
|
||||
createProjectDossierReport(d, {
|
||||
selectedSections: new Set(['finance']),
|
||||
version: 'QA G6D',
|
||||
}),
|
||||
);
|
||||
for (const token of [
|
||||
'P048',
|
||||
'opportunity:333',
|
||||
'Manica',
|
||||
'2026-12',
|
||||
'SYNTHETIC QA G6D',
|
||||
'0.00',
|
||||
'12(1)(c)',
|
||||
'campo11',
|
||||
...ids,
|
||||
])
|
||||
assert.ok(report.includes(token), token);
|
||||
if (process.env.IVA_G6D_CALCULATED)
|
||||
fs.writeFileSync(process.env.IVA_G6D_CALCULATED, JSON.stringify(d));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user