feat(qa): consolidate eleven financial report tables pending private visual acceptance
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\VirtualDesk;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class InvestorIntegralReportTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
public function test_private_report_snapshots_are_preserved_in_the_exact_result_version_without_exposing_them_in_events(): void
|
||||
{
|
||||
$identity = DB::selectOne('select current_database() as name, inet_server_port() as port');
|
||||
$this->assertStringEndsWith('_test', $identity->name);
|
||||
$this->assertSame(55441, (int) $identity->port);
|
||||
$this->withoutVite();
|
||||
Http::fake();
|
||||
$owner = User::factory()->create();
|
||||
config(['investor-path.enabled' => true, 'portal.managers' => [$owner->email], 'suitecrm.enabled' => false, 'investmoz_ai.enabled' => false]);
|
||||
$this->actingAs($owner);
|
||||
if (! DB::table('desk_service_versions')->where('code', 'project-preparation')->exists()) {
|
||||
$this->postJson('/management/balcao/servicos', ['code' => 'project-preparation', 'confirm' => true])->assertCreated();
|
||||
}
|
||||
$case = $this->postJson('/investimentos', ['title' => 'QA synthetic report transport', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true])->assertCreated()->json('id');
|
||||
// Transport contract only: no invented financial amounts or calculated results.
|
||||
$economic = [
|
||||
'purpose' => 'simulate_only',
|
||||
'financial_tools_snapshot' => json_encode(['version' => 1, 'data' => []], JSON_THROW_ON_ERROR),
|
||||
'business_plan_snapshot' => json_encode(['version' => 17, 'plan' => ['fixture' => 'synthetic transport only']], JSON_THROW_ON_ERROR),
|
||||
'project_team_snapshot' => json_encode([['competency' => 'QA role', 'state' => 'unknown', 'action' => '', 'estimated_cost' => '', 'due_date' => '']], JSON_THROW_ON_ERROR),
|
||||
'project_execution_snapshot' => json_encode([['title' => 'QA milestone', 'due_date' => '', 'status' => 'planned', 'planned_amount' => '', 'actual_amount' => '']], JSON_THROW_ON_ERROR),
|
||||
];
|
||||
$draft = $this->postJson('/api/v1/simulation-drafts', ['context' => $case, 'personal_snapshot' => [], 'economic_snapshot' => $economic], ['Idempotency-Key' => (string) Str::uuid()])->assertCreated()->json('data.id');
|
||||
$view = $this->getJson('/investimentos/'.$case)->assertOk()->json();
|
||||
$request = ['version' => $view['case']['version'], 'draft_id' => $draft, 'draft_version' => 1];
|
||||
$result = $this->postJson('/investimentos/'.$case.'/resultados', $request)->assertOk()->json('result');
|
||||
foreach (['business_plan_snapshot', 'project_team_snapshot', 'project_execution_snapshot'] as $field) {
|
||||
$this->assertSame($economic[$field], $result['preparation_snapshot'][$field]);
|
||||
}
|
||||
$this->postJson('/investimentos/'.$case.'/resultados', $request)->assertOk()->assertJsonPath('result.id', $result['id']);
|
||||
$this->flushSession();
|
||||
$this->actingAs($owner)->getJson('/investimentos/'.$case)->assertOk()->assertJsonPath('results.0.preparation_snapshot', $result['preparation_snapshot']);
|
||||
$event = app(VirtualDesk::class)->decode(DB::table('desk_events')->where('case_id', $case)->where('action', 'tool_evidence_returned')->value('payload'));
|
||||
foreach (['business_plan_snapshot', 'project_team_snapshot', 'project_execution_snapshot'] as $field) {
|
||||
$this->assertArrayNotHasKey($field, $event['preparation_snapshot']);
|
||||
}
|
||||
$this->actingAs(User::factory()->create())->getJson('/investimentos/'.$case)->assertForbidden();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import JSZip from 'jszip';
|
||||
import { complementaryFixture } from './fixtures/complementary-plan.mjs';
|
||||
import { buildIntegralFinancialTables } from '../../resources/js/lib/integral-financial-report.ts';
|
||||
import { serializeBusinessPlanSnapshot } from '../../resources/js/lib/business-financial-plan.ts';
|
||||
import {
|
||||
createIntegralFinancialReport,
|
||||
createProjectDossierReport,
|
||||
integralFinancialTableIds,
|
||||
generateProjectDossierDocx,
|
||||
createProjectDossierPrintHtml,
|
||||
} from '../../resources/js/lib/project-dossier-report.ts';
|
||||
import {
|
||||
createInvestorDossierReport,
|
||||
createInvestorStageReport,
|
||||
investorFinancialReportChoices,
|
||||
} from '../../resources/js/lib/investor-dossier-report.ts';
|
||||
|
||||
export function consolidatedFixture() {
|
||||
const { plan, draft } = complementaryFixture(),
|
||||
balance = buildIntegralFinancialTables(plan).find(
|
||||
(t) => t.id === 'balance',
|
||||
);
|
||||
plan.ratioCurrentBalances = {
|
||||
assetsByMonth: balance.rows
|
||||
.find((r) => r[0] === 'Caixa líquida')
|
||||
.slice(1, 14),
|
||||
liabilitiesByMonth: balance.rows
|
||||
.find((r) => r[0] === 'Total do passivo')
|
||||
.slice(1, 14),
|
||||
};
|
||||
draft.business_plan_snapshot = serializeBusinessPlanSnapshot(plan);
|
||||
return { plan, draft };
|
||||
}
|
||||
const dossier = () => {
|
||||
const { draft } = consolidatedFixture();
|
||||
return {
|
||||
case: { id: 'synthetic-case', title: 'Hotel QA sintético', version: 3 },
|
||||
context_opportunity: null,
|
||||
opportunity_options: [],
|
||||
investment_context: { revision: 1, fields: {} },
|
||||
phases: {},
|
||||
steps: [
|
||||
{
|
||||
code: 'S03',
|
||||
title: 'Viabilidade',
|
||||
state: 'preparing',
|
||||
reason: '',
|
||||
phase: 1,
|
||||
detail: {},
|
||||
},
|
||||
],
|
||||
documents: [],
|
||||
results: [
|
||||
{
|
||||
id: 'saved-one',
|
||||
draft_id: 'synthetic-draft',
|
||||
draft_version: 1,
|
||||
recorded_at: '2026-11-20T12:00:00Z',
|
||||
currency: 'MZN',
|
||||
engine: 'financial-calculators:v1',
|
||||
financial_snapshot: '',
|
||||
preparation_snapshot: draft,
|
||||
context_revision: 1,
|
||||
context_state: 'current',
|
||||
},
|
||||
],
|
||||
};
|
||||
};
|
||||
void test('eleven tables and summary come from one immutable saved scenario and appear in both export formats', async () => {
|
||||
const { draft } = consolidatedFixture(),
|
||||
before = JSON.stringify(draft),
|
||||
report = createIntegralFinancialReport(draft, {
|
||||
version: 'QA v1',
|
||||
generatedAt: '2026-11-20T12:00:00Z',
|
||||
});
|
||||
assert.equal(report.sections.length, 1);
|
||||
assert.deepEqual(
|
||||
report.sections[0].tables.map((t) => t.id),
|
||||
[...integralFinancialTableIds],
|
||||
);
|
||||
assert.equal(new Set(report.sections[0].tables.map((t) => t.id)).size, 11);
|
||||
const original = createProjectDossierReport(draft, {
|
||||
selectedSections: new Set(['finance', 'market', 'team', 'execution']),
|
||||
});
|
||||
for (const t of report.sections[0].tables)
|
||||
assert.deepEqual(
|
||||
t,
|
||||
original.sections
|
||||
.flatMap((s) => s.tables ?? [])
|
||||
.find((r) => r.id === t.id),
|
||||
);
|
||||
const cash = report.sections[0].rows.find(
|
||||
(r) => r.field === 'Caixa final projectada',
|
||||
);
|
||||
assert.match(cash.value, /4 540,00/);
|
||||
assert.equal(JSON.stringify(draft), before);
|
||||
const html = await createProjectDossierPrintHtml(report, []),
|
||||
doc = await generateProjectDossierDocx(report, []),
|
||||
zip = await JSZip.loadAsync(
|
||||
doc instanceof Blob ? await doc.arrayBuffer() : doc,
|
||||
),
|
||||
xml = await zip.file('word/document.xml').async('string');
|
||||
for (const t of report.sections[0].tables) {
|
||||
assert.ok(html.includes(t.title));
|
||||
assert.ok(xml.includes(t.title));
|
||||
}
|
||||
});
|
||||
void test('dossier uses only the chosen preserved scenario, requires choice between versions and never leaks another opportunity', () => {
|
||||
const input = dossier();
|
||||
let report = createInvestorDossierReport(input);
|
||||
assert.equal(
|
||||
report.sections.find((s) => s.id === 'financial-report').tables.length,
|
||||
11,
|
||||
);
|
||||
const other = structuredClone(input.results[0]);
|
||||
other.id = 'other-opportunity';
|
||||
other.context_opportunity = 'foreign';
|
||||
other.preparation_snapshot.project_team_snapshot = 'FOREIGN PRIVATE DATA';
|
||||
input.results.push(other);
|
||||
assert.equal(investorFinancialReportChoices(input).length, 1);
|
||||
assert.ok(
|
||||
!JSON.stringify(createInvestorDossierReport(input)).includes(
|
||||
'FOREIGN PRIVATE DATA',
|
||||
),
|
||||
);
|
||||
const second = structuredClone(input.results[0]);
|
||||
second.id = 'saved-two';
|
||||
second.draft_version = 2;
|
||||
const snapshot = JSON.parse(
|
||||
second.preparation_snapshot.business_plan_snapshot,
|
||||
);
|
||||
snapshot.plan.reportAssumptions.startupExpenses[0].amount = '100.25';
|
||||
second.preparation_snapshot.business_plan_snapshot =
|
||||
JSON.stringify(snapshot);
|
||||
input.results.push(second);
|
||||
assert.throws(() => createInvestorDossierReport(input), /Escolha/);
|
||||
const chosen = createInvestorDossierReport(input, undefined, {
|
||||
financialResultId: 'saved-two',
|
||||
}).sections.find((s) => s.id === 'financial-report');
|
||||
assert.match(
|
||||
chosen.rows.find((r) => r.field === 'Caixa final projectada').value,
|
||||
/4 539,75/,
|
||||
);
|
||||
assert.equal(
|
||||
chosen.tables
|
||||
.find((t) => t.id === 'funding')
|
||||
.rows.find((r) => r[0] === 'Diferença por financiar')[1],
|
||||
'100.25',
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
createInvestorDossierReport(input, undefined, {
|
||||
financialResultId: other.id,
|
||||
}),
|
||||
/contexto/,
|
||||
);
|
||||
report = createInvestorDossierReport(input, undefined, {
|
||||
financialResultId: second.id,
|
||||
});
|
||||
assert.match(
|
||||
report.sections
|
||||
.find((s) => s.id === 'financial-report')
|
||||
.rows.find((r) => r.field === 'Caixa final projectada').value,
|
||||
/4 539,75/,
|
||||
);
|
||||
assert.equal(
|
||||
createInvestorStageReport(
|
||||
input,
|
||||
'S03',
|
||||
undefined,
|
||||
second.id,
|
||||
).sections.find((s) => s.id === 'financial-report').tables.length,
|
||||
11,
|
||||
);
|
||||
});
|
||||
void test('historical or absent snapshots are not silently reconstructed from another version', () => {
|
||||
const input = dossier();
|
||||
input.results[0].context_revision = 0;
|
||||
let report = createInvestorDossierReport(input);
|
||||
assert.match(
|
||||
report.sections
|
||||
.find((s) => s.id === 'financial-report')
|
||||
.paragraphs.join(' '),
|
||||
/histórica/,
|
||||
);
|
||||
delete input.results[0].preparation_snapshot.business_plan_snapshot;
|
||||
report = createInvestorDossierReport(input);
|
||||
assert.ok(!report.sections.some((s) => s.id === 'financial-report'));
|
||||
});
|
||||
|
||||
void test('currency differences remain explicit without relabelling or converting the saved plan', () => {
|
||||
const { draft } = consolidatedFixture();
|
||||
const report = createIntegralFinancialReport({ ...draft, currency: 'USD' });
|
||||
assert.match(report.sections[0].paragraphs.join(' '), /MZN.*USD/);
|
||||
assert.equal(
|
||||
report.sections[0].rows.find((r) => r.field === 'Moeda do plano').value,
|
||||
'MZN',
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user