184 lines
6.4 KiB
JavaScript
184 lines
6.4 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import {
|
|
newImportExemption,
|
|
evaluateImportExemption,
|
|
recordImportExemptions,
|
|
saveImportExemptionBook,
|
|
importExemptionBook,
|
|
importExemptionCurrent,
|
|
importExemptionRules,
|
|
importSpecialRegimeSource,
|
|
} from '../../resources/js/lib/iva-import-exemptions.ts';
|
|
import { createProjectDossierReport } from '../../resources/js/lib/project-dossier-report.ts';
|
|
const frozen = JSON.parse(
|
|
fs.readFileSync(
|
|
new URL('./fixtures/iva-g6d-inputs.json', import.meta.url),
|
|
'utf8',
|
|
),
|
|
);
|
|
const rules = importExemptionRules.filter((r) => r.branch === 'P080');
|
|
const row = (rule) => ({
|
|
...newImportExemption('g5c-' + rule.id, 'MZN'),
|
|
treatment: rule.id,
|
|
description: 'SYNTHETIC QA ' + rule.label,
|
|
period: '2026-12',
|
|
declaredValue: '5000',
|
|
importer: 'SYNTHETIC QA importador',
|
|
documentRef: 'SYNTHETIC QA G5C ' + rule.id,
|
|
documentDate: '2026-11-14',
|
|
evidence:
|
|
'SYNTHETIC QA regime e documento aplicável, controlo e permanência; sem uso/consumo finais. Pressuposto expresso, não é documento real.',
|
|
conditions: Object.fromEntries(
|
|
rule.conditions.map(([key]) => [key, 'yes']),
|
|
),
|
|
});
|
|
const draft = () => ({
|
|
...structuredClone(frozen),
|
|
context_opportunity: 'opportunity:334',
|
|
project_title: 'SYNTHETIC QA — G5C / Importações em regimes especiais',
|
|
});
|
|
|
|
void test('P080 covers six enumerated regimes; each legal condition independently prevents an unfounded zero', () => {
|
|
assert.equal(rules.length, 6);
|
|
assert.match(importSpecialRegimeSource, /14\(1\)\(a\)/);
|
|
for (const rule of rules) {
|
|
const input = row(rule);
|
|
assert.equal(evaluateImportExemption(input, 'MZN').importVat, '0.00');
|
|
for (const [key] of rule.conditions)
|
|
for (const value of ['', 'unknown', 'no']) {
|
|
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,
|
|
);
|
|
}
|
|
});
|
|
void test('geography or regime selection alone cannot exempt; switching regimes preserves data but requires own confirmation', () => {
|
|
const input = row(rules[0]);
|
|
assert.equal(
|
|
evaluateImportExemption({ ...input, conditions: {} }, 'MZN').status,
|
|
'indeterminate',
|
|
);
|
|
const switched = { ...input, treatment: rules[1].id };
|
|
assert.equal(
|
|
evaluateImportExemption(switched, 'MZN').status,
|
|
'indeterminate',
|
|
);
|
|
assert.equal(switched.documentRef, input.documentRef);
|
|
assert.equal(switched.conditions.specialEconomicZone, 'yes');
|
|
assert.equal(
|
|
evaluateImportExemption(
|
|
{
|
|
...switched,
|
|
conditions: { ...switched.conditions, freeZone: 'yes' },
|
|
},
|
|
'MZN',
|
|
).status,
|
|
'exempt',
|
|
);
|
|
for (const key of [
|
|
'specialRegimePermanence',
|
|
'specialNoFinalUseConsumption',
|
|
])
|
|
assert.equal(
|
|
evaluateImportExemption(
|
|
{ ...input, conditions: { ...input.conditions, [key]: 'no' } },
|
|
'MZN',
|
|
).status,
|
|
'not_applicable',
|
|
);
|
|
});
|
|
void test('P080 save/recover/report archives negative/unknown history and preserves all25 prior books, source keys and field11', () => {
|
|
const d = draft(),
|
|
prior = JSON.parse(frozen.modelo_a_snapshot);
|
|
let book = recordImportExemptions(d, {
|
|
rows: rules.map((rule) => ({ ...row(rule), conditions: {} })),
|
|
});
|
|
book = recordImportExemptions(d, {
|
|
...book,
|
|
rows: rules.map((rule) => ({
|
|
...row(rule),
|
|
conditions: {
|
|
...row(rule).conditions,
|
|
specialNoFinalUseConsumption: 'no',
|
|
},
|
|
})),
|
|
});
|
|
book = recordImportExemptions(d, { ...book, rows: rules.map(row) });
|
|
assert.equal(book.history.length, 2);
|
|
assert.equal(book.history[0].rows[0].status, 'indeterminate');
|
|
assert.equal(book.history[1].rows[0].status, 'not_applicable');
|
|
assert.ok(
|
|
book.history.every((h) => h.rows.every((r) => r.importVat === null)),
|
|
);
|
|
d.modelo_a_snapshot = saveImportExemptionBook(d, book);
|
|
assert.deepEqual(importExemptionBook(JSON.parse(JSON.stringify(d))), book);
|
|
assert.ok(importExemptionCurrent(d, book));
|
|
assert.ok(
|
|
!importExemptionCurrent(
|
|
{ ...d, context_opportunity: 'opportunity:333' },
|
|
book,
|
|
),
|
|
);
|
|
assert.match(book.result.sourceKey, /import-special-regimes\/1/);
|
|
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, 4);
|
|
for (const [scope, previous] of Object.entries(prior.importExemptions)) {
|
|
assert.deepEqual(next.importExemptions[scope], previous);
|
|
assert.ok(
|
|
importExemptionCurrent(
|
|
{ ...d, context_opportunity: scope },
|
|
previous,
|
|
),
|
|
);
|
|
}
|
|
assert.equal(
|
|
next.books['opportunity:329'].result.periods[0].fields['11'],
|
|
null,
|
|
);
|
|
const report = JSON.stringify(
|
|
createProjectDossierReport(d, {
|
|
selectedSections: new Set(['finance']),
|
|
version: 'QA G5C',
|
|
}),
|
|
);
|
|
for (const token of [
|
|
'P080',
|
|
'opportunity:334',
|
|
'Manica',
|
|
'2026-12',
|
|
'SYNTHETIC QA G5C',
|
|
'0.00',
|
|
'14(1)(a)',
|
|
'campo11',
|
|
...rules.map((r) => r.id),
|
|
])
|
|
assert.ok(report.includes(token), token);
|
|
if (process.env.IVA_G5C_CALCULATED)
|
|
fs.writeFileSync(process.env.IVA_G5C_CALCULATED, JSON.stringify(d));
|
|
});
|