import assert from 'node:assert/strict'; import test from 'node:test'; import { inheritCompatibleFields } from '../../resources/js/lib/context-inheritance.ts'; import { buildProjectDossierRows } from '../../resources/js/lib/project-dossier.ts'; void test('changing only the treated opportunity refreshes inherited geography and preserves deliberate data', () => { const scope = 'dossier:QA:land'; const first = { project_reference: 'Investimento conjunto', purpose: 'Segunda oportunidade', parcels: [{ province_id: 'gaza' }], }; const initial = inheritCompatibleFields(scope, {}, first, null); const values = { ...initial.patch, context_reuse: initial.metadata, purpose: 'Edição deliberada', parcels: [ { province_id: 'gaza', area: '23', place_description: 'Local declarado', }, ], }; const next = inheritCompatibleFields( scope, values, { ...first, purpose: 'Novo resumo da segunda', parcels: [{ province_id: 'manica' }], }, initial.metadata, ); assert.equal(next.patch.parcels[0].province_id, 'manica'); assert.equal(next.patch.parcels[0].district_id, ''); assert.equal(next.patch.parcels[0].area, '23'); assert.equal(next.patch.parcels[0].place_description, 'Local declarado'); assert.equal('purpose' in next.patch, false); assert.ok(next.conflicts.includes('purpose')); // The land form merges this geographic proposal into the existing parcel; // area and the declared place are not part of the geographic replacement. const merged = { ...values.parcels[0], ...next.patch.parcels[0] }; assert.equal(merged.area, '23'); assert.equal(merged.place_description, 'Local declarado'); }); void test('a deliberate site in a second-opportunity request remains a conflict instead of being replaced', () => { const scope = 'dossier:QA:desk'; const source = { territory_id: 'gaza', description: 'Resumo específico' }; const initial = inheritCompatibleFields(scope, {}, source, null); const next = inheritCompatibleFields( scope, { ...initial.patch, territory_id: 'local-deliberado' }, { ...source, territory_id: 'manica' }, initial.metadata, ); assert.equal('territory_id' in next.patch, false); assert.ok(next.conflicts.includes('territory_id')); }); void test('report keeps each park attached to its own opportunity after only the second changes', () => { const choices = [ { opportunity: { key: 'opportunity:1', title: 'Primeira' }, territory: { title: 'Sofala' }, park: { title: 'Parque Sofala' }, }, { opportunity: { key: 'opportunity:2', title: 'Segunda' }, territory: { title: 'Manica' }, park: { title: 'Parque Manica' }, }, ]; const rows = buildProjectDossierRows({ context_applicability_snapshot: JSON.stringify({ version: 1, opportunity_choices: choices, }), }); const parks = rows.filter((r) => r[1] === 'Zona/parque da oportunidade'); assert.deepEqual( parks.map((r) => [r[2], r[3]]), [ ['Parque Sofala', 'Primeira; opportunity:1'], ['Parque Manica', 'Segunda; opportunity:2'], ], ); });