import test from 'node:test'; import assert from 'node:assert/strict'; import { harness, props, text } from './helpers/education-review.mjs'; const initial = { actor_id: 1, kind: 'demands', record_id: props().declaration.id, version: 0, operation_key: null, snapshot: null, unavailable: false, }; const reason = 'Justificação e evidência da revisão'; const receipt = (request) => ({ actor_id: request.data.actor_id, kind: request.url.split('/')[3], record_id: request.url.split('/')[4], version: request.data.version + 1, operation_key: request.data.operation_key, snapshot: request.data.snapshot, unavailable: false, }); test('private review preparation is recovered after remount and never decides on save or restore', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC deliberate reviewer note for later recovery.', ); h.click('Guardar preparação da revisão'); assert.equal(h.sent.length, 0); const request = h.preparations[0]; await h.preparationResponse(receipt(request)); const reopened = harness(props({ preparation: receipt(request) })); assert.equal(reopened.field('education-review', reason).props.value, ''); reopened.click('Recuperar preparação guardada'); await reopened.preparationResponse(receipt(request)); assert.equal( reopened.field('education-review', reason).props.value, request.data.snapshot.values.review_reason, ); assert.equal(reopened.sent.length, 0); }); test('review waits for a durable intent; a lost preparation response retries exactly before one native decision', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC proof of durable intent before native decision.', ); h.submit('education-review'); assert.equal(h.sent.length, 0); const request = h.preparations[0]; assert.ok(request.data.snapshot.pending.values.client_key); await h.preparationFailure( new Error('SYNTHETIC lost persistence response'), ); assert.equal(h.sent.length, 0); h.submit('education-review'); assert.deepEqual(h.preparations[1].data, request.data); await h.preparationResponse(receipt(request)); assert.equal(h.sent.length, 1); assert.equal( h.sent[0].data.client_key, request.data.snapshot.pending.values.client_key, ); h.fail(h.sent[0]); const fresh = harness(props({ preparation: receipt(request) })); fresh.click('Recuperar preparação guardada'); await fresh.preparationResponse(receipt(request)); fresh.submit('education-review'); const resumed = fresh.preparations.at(-1); await fresh.preparationResponse(receipt(resumed)); assert.deepEqual(fresh.sent[0].data, h.sent[0].data); }); test('restoring an older declaration requires comparison and preserves deliberate edits during loading', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC stored reasoning against the first version.', ); h.click('Guardar preparação da revisão'); const saved = receipt(h.preparations[0]); await h.preparationResponse(saved); const fresh = harness( props({ preparation: saved, declaration: { ...h.props.declaration, version_no: 2, quantity: 40, }, }), ); fresh.click('Recuperar preparação guardada'); await fresh.preparationResponse(saved); assert.match(text(fresh.tree), /A declaração mudou/); fresh.submit('education-review'); assert.equal(fresh.sent.length, 0); const loading = harness(props({ preparation: saved })); loading.click('Recuperar preparação guardada'); loading.change( 'education-review', reason, 'SYNTHETIC later edit while recovery response is pending.', ); await loading.preparationResponse(saved); assert.match( loading.field('education-review', reason).props.value, /later edit/, ); assert.match(text(loading.tree), /não as substituiu/); }); test('late private preparation responses cannot populate a different actor', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC private content for the original reviewer only.', ); h.click('Guardar preparação da revisão'); const request = h.preparations[0]; h.render({ ...h.props, actorId: 3, declaration: { ...h.props.declaration, review_context: { actor_id: 3, commands: [] }, }, preparation: initial, }); await h.preparationResponse(receipt(request)); assert.equal(h.field('education-review', reason).props.value, ''); assert.equal(h.sent.length, 0); }); test('a newer saved preparation in another window never causes the older intent to be submitted', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC first window prepares a review.', ); h.submit('education-review'); const request = h.preparations[0]; await h.preparationResponse({ ...receipt(request), version: 4, operation_key: 'another-window', }); assert.equal(h.sent.length, 0); assert.match(text(h.tree), /noutra janela/); h.submit('education-review'); assert.deepEqual( h.preparations.at(-1).data, request.data, 'A newer server version must not be adopted silently', ); }); test('restoring a committed uncertain attempt acknowledges the native receipt and retains edits after sending', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC original submitted reason.', ); h.submit('education-review'); const request = h.preparations[0]; await h.preparationResponse(receipt(request)); h.fail(h.sent[0]); h.change( 'education-review', reason, 'SYNTHETIC later deliberate note after lost native response.', ); h.click('Guardar preparação da revisão'); const saved = receipt(h.preparations.at(-1)); await h.preparationResponse(saved); const command = request.data.snapshot.pending.values.client_key; const fresh = harness( props({ preparation: saved, declaration: { ...h.props.declaration, version_no: 2, status: 'reviewed', review_context: { actor_id: 1, commands: [{ key: command, version: 2 }], }, }, }), ); fresh.click('Recuperar preparação guardada'); await fresh.preparationResponse(saved); assert.equal(fresh.sent.length, 0); assert.match( fresh.field('education-review', reason).props.value, /later deliberate note/, ); assert.match(text(fresh.tree), /Revisão registada/); }); test('a private preparation response for another account or declaration never unlocks a review', async () => { for (const patch of [ { actor_id: 2 }, { record_id: 'another-record' }, { kind: 'capacities' }, ]) { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC note belongs to this reviewer and declaration only.', ); h.submit('education-review'); const request = h.preparations[0]; await h.preparationResponse({ ...receipt(request), ...patch }); assert.equal(h.sent.length, 0); assert.match(text(h.tree), /não pôde ser conferida/); } }); test('an unreadable persistence receipt cannot release the business decision', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC intent must remain recoverable before deciding.', ); h.submit('education-review'); await h.preparationResponse({ ...receipt(h.preparations[0]), snapshot: null, unavailable: true, }); assert.equal(h.sent.length, 0); assert.match(text(h.tree), /não pôde ser recuperada/); }); test('edits made while a native decision is in flight can be saved without a second decision', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC original native decision reason.', ); h.submit('education-review'); const first = h.preparations[0]; await h.preparationResponse(receipt(first)); assert.equal(h.sent[0].form.processing, true); h.change( 'education-review', reason, 'SYNTHETIC later note while native response is pending.', ); h.click('Guardar preparação da revisão'); const during = h.preparations.at(-1); assert.equal( during.data.snapshot.pending.values.client_key, h.sent[0].data.client_key, ); assert.match(during.data.snapshot.values.review_reason, /later note/); await h.preparationResponse(receipt(during)); assert.equal(h.sent.length, 1); }); test('a receipt observed during recovery acknowledges the current declaration instead of a captured old page', async () => { const h = harness(props({ preparation: initial })); h.change( 'education-review', reason, 'SYNTHETIC a review whose response was uncertain.', ); h.submit('education-review'); const request = h.preparations[0]; await h.preparationResponse(receipt(request)); const fresh = harness(props({ preparation: receipt(request) })); fresh.click('Recuperar preparação guardada'); fresh.render({ ...fresh.props, declaration: { ...fresh.props.declaration, version_no: 2, status: 'reviewed', review_context: { actor_id: 1, commands: [ { key: request.data.snapshot.pending.values.client_key, version: 2, }, ], }, }, }); await fresh.preparationResponse(receipt(request)); assert.equal(fresh.sent.length, 0); assert.match(text(fresh.tree), /Revisão registada/); });