222 lines
7.6 KiB
JavaScript
222 lines
7.6 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,
|
|
importExemptionReportRows,
|
|
} 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-g5b-inputs.json', import.meta.url),
|
|
'utf8',
|
|
),
|
|
);
|
|
const row = (treatment = 'reimport_same_state') => ({
|
|
...newImportExemption('g6a-' + treatment, 'MZN'),
|
|
treatment,
|
|
description:
|
|
treatment === 'reimport_same_state'
|
|
? 'SYNTHETIC QA equipamento reimportado'
|
|
: 'SYNTHETIC QA ouro importado',
|
|
period: '2026-12',
|
|
declaredValue: '1000',
|
|
importer:
|
|
treatment === 'reimport_same_state'
|
|
? 'SYNTHETIC QA exportador original'
|
|
: 'SYNTHETIC QA Banco de Moçambique',
|
|
documentRef: 'SYNTHETIC QA G6A ' + treatment,
|
|
documentDate: '2026-12-11',
|
|
evidence: 'SYNTHETIC QA pressupostos expressos; não é documento real',
|
|
conditions: {
|
|
sameExporter: 'yes',
|
|
sameState: 'yes',
|
|
customsExempt: 'yes',
|
|
gold: 'yes',
|
|
centralBank: 'yes',
|
|
},
|
|
});
|
|
const draft = () => ({
|
|
...structuredClone(frozen),
|
|
context_opportunity: 'opportunity:330',
|
|
project_title: 'SYNTHETIC QA — G6A / Importações',
|
|
});
|
|
|
|
void test('P049 requires all three own conditions;unknown differs from failed and neither produces normal tax or exempt zero', () => {
|
|
const input = row();
|
|
assert.equal(evaluateImportExemption(input, 'MZN').importVat, '0.00');
|
|
for (const key of ['sameExporter', 'sameState', 'customsExempt'])
|
|
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' },
|
|
{ documentDate: '2026-02-30' },
|
|
{ documentRef: '' },
|
|
{ evidence: '' },
|
|
{ declaredValue: '-1' },
|
|
{ origin: 'unknown' },
|
|
{ treatment: 'unknown' },
|
|
])
|
|
assert.equal(
|
|
evaluateImportExemption({ ...input, ...patch }, 'MZN').importVat,
|
|
null,
|
|
);
|
|
assert.equal(
|
|
evaluateImportExemption(
|
|
{ ...input, documentDate: '2026-11-11', declaredValue: '' },
|
|
'MZN',
|
|
).importVat,
|
|
'0.00',
|
|
);
|
|
assert.deepEqual(input, row());
|
|
assert.equal(evaluateImportExemption(input, 'USD').importVat, null);
|
|
});
|
|
|
|
void test('P051 uses gold and actual central-bank importer;does not import reimport conditions or unworked-form restriction from domestic sales', () => {
|
|
const input = {
|
|
...row('central_bank_gold'),
|
|
description:
|
|
'SYNTHETIC QA ouro trabalhado; classificação de importação',
|
|
conditions: { gold: 'yes', centralBank: 'yes' },
|
|
};
|
|
assert.equal(evaluateImportExemption(input, 'MZN').status, 'exempt');
|
|
for (const key of ['gold', 'centralBank']) {
|
|
assert.equal(
|
|
evaluateImportExemption(
|
|
{ ...input, conditions: { ...input.conditions, [key]: '' } },
|
|
'MZN',
|
|
).status,
|
|
'indeterminate',
|
|
);
|
|
assert.equal(
|
|
evaluateImportExemption(
|
|
{ ...input, conditions: { ...input.conditions, [key]: 'no' } },
|
|
'MZN',
|
|
).status,
|
|
'not_applicable',
|
|
);
|
|
}
|
|
assert.equal(
|
|
evaluateImportExemption(
|
|
{
|
|
...input,
|
|
conditions: { ...input.conditions, customsExempt: 'no' },
|
|
},
|
|
'MZN',
|
|
).importVat,
|
|
'0.00',
|
|
);
|
|
});
|
|
|
|
void test('explicit classification archives prior inputs/results;editing currency,opportunity,location or conditions marks result historical', () => {
|
|
const d = draft();
|
|
let book = recordImportExemptions(d, {
|
|
rows: [
|
|
{ ...row(), conditions: { sameExporter: 'yes', sameState: 'yes' } },
|
|
],
|
|
});
|
|
assert.equal(book.result.rows[0].importVat, null);
|
|
const original = structuredClone(book);
|
|
book = recordImportExemptions(d, { ...book, rows: [row()] });
|
|
assert.deepEqual(book.history, [original.result]);
|
|
assert.ok(importExemptionCurrent(d, book));
|
|
for (const patch of [
|
|
{ currency: 'USD' },
|
|
{ context_opportunity: 'opportunity:331' },
|
|
{ selection_labels: { province_id: 'Maputo' } },
|
|
])
|
|
assert.ok(!importExemptionCurrent({ ...d, ...patch }, book));
|
|
assert.ok(
|
|
!importExemptionCurrent(d, {
|
|
...book,
|
|
rows: [
|
|
{
|
|
...row(),
|
|
conditions: { ...row().conditions, sameState: 'no' },
|
|
},
|
|
],
|
|
}),
|
|
);
|
|
assert.throws(
|
|
() => recordImportExemptions(d, { rows: [row(), row()] }),
|
|
/distintos/,
|
|
);
|
|
});
|
|
|
|
void test('save/recovery/report preserve20sales and acceptedP086 purchase;classifying import leaves all ModeloA fields untouched', () => {
|
|
const d = draft(),
|
|
prior = JSON.parse(d.modelo_a_snapshot);
|
|
let book = recordImportExemptions(d, {
|
|
rows: [row(), row('central_bank_gold')],
|
|
});
|
|
d.modelo_a_snapshot = saveImportExemptionBook(d, book);
|
|
assert.deepEqual(importExemptionBook(JSON.parse(JSON.stringify(d))), book);
|
|
const next = JSON.parse(d.modelo_a_snapshot);
|
|
assert.deepEqual(next.salesOperations, prior.salesOperations);
|
|
assert.deepEqual(next.books, prior.books);
|
|
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'],
|
|
),
|
|
);
|
|
const report = JSON.stringify(
|
|
createProjectDossierReport(d, {
|
|
selectedSections: new Set(['finance']),
|
|
version: 'QA G6A',
|
|
}),
|
|
);
|
|
for (const token of [
|
|
'P049',
|
|
'P051',
|
|
'2026-12',
|
|
'opportunity:330',
|
|
'Manica',
|
|
'SYNTHETIC QA G6A',
|
|
'0.00',
|
|
'12(1)(d/f)',
|
|
'campo11',
|
|
'sem fixar base aduaneira',
|
|
])
|
|
assert.ok(report.includes(token), token);
|
|
assert.equal(
|
|
importExemptionReportRows({
|
|
...d,
|
|
context_opportunity: 'opportunity:331',
|
|
}).length,
|
|
0,
|
|
);
|
|
if (process.env.IVA_G6A_CALCULATED)
|
|
fs.writeFileSync(process.env.IVA_G6A_CALCULATED, JSON.stringify(d));
|
|
});
|
|
|
|
void test('old snapshots need no import book;corrupt import data cannot erase originals', () => {
|
|
assert.deepEqual(importExemptionBook(draft()), { rows: [] });
|
|
const d = draft(),
|
|
data = JSON.parse(d.modelo_a_snapshot);
|
|
data.importExemptions = [];
|
|
d.modelo_a_snapshot = JSON.stringify(data);
|
|
const original = d.modelo_a_snapshot;
|
|
assert.throws(() => importExemptionBook(d), /preservados/);
|
|
assert.equal(d.modelo_a_snapshot, original);
|
|
});
|