import test from 'node:test'; import assert from 'node:assert/strict'; import { harness, props, companyId, text, } from './helpers/investment-profile-editor.mjs'; const sourceId = '30000000-0000-4000-8000-000000000001', form = 'Declaração de empresa', country = 'País de constituição da entidade'; const source = (patch = {}) => ({ id: sourceId, name: 'SYNTHETIC reuse source', version: 3, country: 'FR', canEdit: false, provenance: { kind: 'representative_declaration', reference: 'SYNTHETIC stored reference', }, ...patch, }); const state = (patch = {}) => props({ organizations: [source()], ...patch }); async function preview(h, response = state()) { const read = h.click('Consultar país de SYNTHETIC reuse source'); assert.equal(read.method, 'GET'); await h.reply(response, read); } test('the actual reuse starts with a fresh preview and writes only after explicit confirmation', async () => { const h = harness(state()); await preview(h); assert.equal(h.sent.length, 1); assert.equal(h.value(country, form), 'MZ'); assert.match(text(h.tree), /SYNTHETIC stored reference/); const request = h.click('Confirmar reutilização do país'); assert.equal(request.method, 'POST'); assert.equal( request.url, '/api/v1/investment/companies/' + companyId + '/organization-country', ); assert.equal(request.data.organization_id, sourceId); assert.equal(request.data.organization_version, 3); assert.equal(request.data.version, 1); assert.equal(request.data.payload, undefined); await h.reply({ version: 2, country: 'FR' }); assert.equal(h.value(country, form), 'FR'); assert.equal(h.value('Nome da empresa', form), 'SYNTHETIC company'); assert.match(text(h.tree), /gravação confirmada/); assert.deepEqual(h.reloads[0].only, ['companies', 'organizations']); }); test('an uncertain reuse locks the company and repeats exactly the frozen source command', async () => { const h = harness(state()); await preview(h); const first = h.click('Confirmar reutilização do país'); await h.fail(); assert.equal(h.field(country, form).props.disabled, true); const count = h.sent.length; h.submit(form); assert.equal(h.sent.length, count); h.render(state({ organizations: [source({ version: 4, country: 'ZA' })] })); const second = h.click('Repetir a mesma reutilização'); assert.deepEqual(second.data, first.data); await h.reply({ version: 2, country: 'FR' }); assert.equal(h.value(country, form), 'FR'); assert.equal(h.field(country, form).props.disabled, false); }); test('unsaved company edits prevent preview and a later edit also prevents confirming a prepared reuse', async () => { const h = harness(state()); h.change('Nome da empresa', 'SYNTHETIC dirty company', form); h.click('Consultar país de SYNTHETIC reuse source'); assert.equal(h.sent.length, 0); h.change('Nome da empresa', 'SYNTHETIC company', form); await preview(h); h.change('Nome da empresa', 'SYNTHETIC later edit', form); const count = h.sent.length; h.click('Confirmar reutilização do país'); assert.equal(h.sent.length, count); assert.equal(h.value('Nome da empresa', form), 'SYNTHETIC later edit'); }); test('a conflicting copy preserves the target and requires a new preview and key', async () => { const h = harness(state()); await preview(h); const first = h.click('Confirmar reutilização do país'); await h.fail(409); assert.equal(h.value(country, form), 'MZ'); assert.equal(h.field(country, form).props.disabled, false); assert.equal(h.button('Confirmar reutilização do país'), undefined); await preview( h, state({ organizations: [source({ version: 4, country: 'ZA' })] }), ); const next = h.click('Confirmar reutilização do país'); assert.notEqual(next.data.operation_key, first.data.operation_key); assert.equal(next.data.organization_version, 4); }); test('a known newer source on the page blocks a stale preview before any write', async () => { const h = harness(state()); await preview(h); h.render(state({ organizations: [source({ version: 4, country: 'ZA' })] })); const count = h.sent.length; h.click('Confirmar reutilização do país'); assert.equal(h.sent.length, count); assert.match(text(h.tree), /organização.*alterada|organização mudou/); }); test('an invalid acknowledgement retains the original copy and never clears the company', async () => { const h = harness(state()); await preview(h); const first = h.click('Confirmar reutilização do país'); await h.reply({ version: 2, country: 'ZA' }); assert.equal(h.value(country, form), 'MZ'); assert.equal(h.field(country, form).props.disabled, true); assert.equal(h.reloads.length, 0); const retry = h.click('Repetir a mesma reutilização'); assert.deepEqual(retry.data, first.data); }); test('restored read representation can confirm an original copy while consulting never changes its command', async () => { const h = harness(state()); await preview(h); const first = h.click('Confirmar reutilização do país'); await h.fail(403); const count = h.sent.length; h.click('Repetir a mesma reutilização'); assert.equal(h.sent.length, count); h.click('Consultar estado da reutilização'); await h.reply( state({ organizations: [source({ version: 5, country: null })] }), ); assert.equal(h.field(country, form).props.disabled, true); const retry = h.click('Repetir a mesma reutilização'); assert.deepEqual(retry.data, first.data); }); test('a receipt from a prior actor cannot update the new actor or reload their page', async () => { const h = harness(state()); await preview(h); const request = h.click('Confirmar reutilização do país'); h.render(state({ actorId: 2, companies: [], organizations: [] })); await h.reply({ version: 2, country: 'FR' }, request); assert.equal(h.reloads.length, 0); assert.doesNotMatch(text(h.tree), /País reutilizado e gravação confirmada/); }); test('a changed target discovered by preview directs comparison without applying a write', async () => { const h = harness(state()); const company = { ...h.props.companies[0], version: 2 }; await preview(h, state({ companies: [company] })); assert.equal(h.sent.length, 1); assert.equal(h.button('Confirmar reutilização do país'), undefined); assert.match(text(h.tree), /Consulte e compare/); assert.equal(h.value(country, form), 'MZ'); }); test('reuse and another dirty form share one page leave guard', async () => { const h = harness(state()); await preview(h); h.change('Telefone', 'SYNTHETIC dirty profile', 'Perfil de investimento'); assert.equal(h.leave(), true); assert.equal(h.confirmCalls, 1); });