fix(demography): verify D01-D06 in isolated QA with INE traceability

This commit is contained in:
2026-11-20 20:07:16 +02:00
parent 558c11ef17
commit 3f66201cbe
17 changed files with 570 additions and 89 deletions
+48 -7
View File
@@ -2,16 +2,20 @@
namespace Tests\Feature;
use App\Models\Sector;
use App\Models\User;
use App\Services\DemographicEvidence;
use App\Services\DemographicImport;
use App\Services\InvestmentPreparationWorkspace;
use App\Services\Malware\MalwareScanner;
use App\Services\SectorContentAnalysis;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\TestCase;
final class DemographyTest extends TestCase
@@ -77,6 +81,35 @@ final class DemographyTest extends TestCase
$this->assertSame([], app(DemographicEvidence::class)->analyse([$this->district], 2026, 2027)['territories']);
}
public function test_tourism_is_explicit_and_comparisons_include_intermediate_years(): void
{
$this->assertSame('Turismo', DemographicEvidence::sectors()['tourism']);
$response = $this->getJson('/api/v1/investment/demography?'.http_build_query(['territories' => [$this->district], 'start_year' => 2026, 'end_year' => 2028, 'sector' => 'tourism']))->assertOk();
$response->assertJsonPath('sector', 'tourism')->assertJsonPath('territories.0.series.0.values.0.original_value', '100.125');
$other = (array) DB::table('geo_locations')->where('id', $this->district)->first();
$other['id'] = (string) Str::uuid();
$other['code'] = $other['id'];
$other['external_id'] = '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:QA:'.$other['id'];
DB::table('geo_locations')->insert($other);
foreach (DB::table('demographic_observations')->where('geo_location_id', $this->district)->get() as $row) {
$row = (array) $row;
unset($row['id']);
DB::table('demographic_observations')->insert([...$row, 'geo_location_id' => $other['id']]);
}
$analysis = app(DemographicEvidence::class)->analyse([$this->district, $other['id']], 2026, 2028, 'commerce');
$this->assertSame([2026, 2027], array_column($analysis['comparisons'], 'year'));
$this->assertSame([], app(SectorContentAnalysis::class)->forTerritories([$this->district], 'commerce'));
}
public function test_commerce_and_territory_reach_the_normal_investor_entry(): void
{
$sector = Sector::firstOrCreate(['slug' => 'comercio-e-servicos'], ['name' => 'Comércio e serviços', 'is_active' => true]);
$this->get('/localizacoes/demografia?'.http_build_query(['territories' => [$this->district], 'sector' => 'commerce']))
->assertOk()->assertInertia(fn ($page) => $page->component('investment/demography')->where('entrySector', $sector->slug)->where('sectorReadings', []));
$this->actingAs(User::factory()->create())->get('/investimentos/iniciar?'.http_build_query(['source' => 'territory:'.$this->district, 'sector' => $sector->slug]))
->assertOk()->assertInertia(fn ($page) => $page->component('investor-path/context-start')->where('entryQuery.sector_id', (string) $sector->id)->where('source.id', $this->district)->etc());
}
public function test_new_editions_remain_separate_and_divergence_is_reported(): void
{
$other = $this->batch('approved');
@@ -202,8 +235,10 @@ final class DemographyTest extends TestCase
{
Storage::fake('local');
$disk = Storage::disk('local');
$original = 'SYNTHETIC prepared original'; $sha = hash('sha256', $original);
$directory = 'prepared/'.$sha; $disk->makeDirectory($directory);
$original = 'SYNTHETIC prepared original';
$sha = hash('sha256', $original);
$directory = 'prepared/'.$sha;
$disk->makeDirectory($directory);
$disk->put('synthetic.xlsx', $original);
$row = ['province' => 'SYNTHETIC province', 'territory' => 'SYNTHETIC province', 'level' => 'province',
'year' => 2026, 'indicator' => 'population', 'sex' => 'all', 'residence' => 'all', 'age_group' => 'all', 'unit' => 'pessoas',
@@ -214,10 +249,11 @@ final class DemographyTest extends TestCase
$preview = json_encode($report, JSON_THROW_ON_ERROR);
$receipt = ['sha256' => $sha, 'baseline_year' => null, 'parser_sha256' => hash_file('sha256', base_path('tools/demography_workbook.py')),
'preview_sha256' => hash('sha256', $preview), 'records_sha256' => hash('sha256', $records), 'record_count' => 1];
$disk->put($directory.'/preview.json', $preview); $disk->put($directory.'/records.ndjson', $records);
$disk->put($directory.'/preview.json', $preview);
$disk->put($directory.'/records.ndjson', $records);
$disk->put($directory.'/preparation.json', json_encode($receipt, JSON_THROW_ON_ERROR));
config(['demography.prepared_directory' => $disk->path('prepared'), 'demography.python' => '/missing-python-must-not-run']);
$this->mock(\App\Services\Malware\MalwareScanner::class)->shouldReceive('scan')->once()->andReturn([
$this->mock(MalwareScanner::class)->shouldReceive('scan')->once()->andReturn([
'result' => 'clean', 'engine' => 'synthetic', 'engine_version' => 'test']);
$service = app(DemographicImport::class);
$this->assertTrue($service->preparedSourcesReady(['synthetic.xlsx' => ['sha256' => $sha, 'records' => 1]]));
@@ -227,9 +263,14 @@ final class DemographyTest extends TestCase
$this->assertSame('100.125', DB::table('demographic_observations')->where('import_id', $result['id'])->value('numeric_value'));
$this->assertSame('imported', DB::table('demographic_imports')->where('id', $result['id'])->value('status'));
$disk->put($directory.'/records.ndjson', 'TAMPERED');
try { $service->preparedSource($sha, null); $this->fail('Changed prepared records must be refused.'); }
catch (\Symfony\Component\HttpKernel\Exception\HttpException $error) { $this->assertSame(409, $error->getStatusCode()); }
$receipt['parser_sha256'] = str_repeat('0', 64); $disk->put($directory.'/preparation.json', json_encode($receipt, JSON_THROW_ON_ERROR));
try {
$service->preparedSource($sha, null);
$this->fail('Changed prepared records must be refused.');
} catch (HttpException $error) {
$this->assertSame(409, $error->getStatusCode());
}
$receipt['parser_sha256'] = str_repeat('0', 64);
$disk->put($directory.'/preparation.json', json_encode($receipt, JSON_THROW_ON_ERROR));
$this->assertFalse($service->preparedSourcesReady(['synthetic.xlsx' => ['sha256' => $sha, 'records' => 1]]));
}
+1 -1
View File
@@ -131,7 +131,7 @@ final class SectorContentAnalysisTest extends TestCase
'location_type' => $type, 'source_system' => 'cartografia-workbook', 'data_classification' => 'public',
'external_id' => '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:QA:'.$id]);
}
$readings = app(SectorContentAnalysis::class)->forTerritories([$district]);
$readings = app(SectorContentAnalysis::class)->forTerritories([$district], 'tourism');
$this->assertCount(1, $readings);
$this->assertSame($article['href'], $readings[0]['href']);
$this->assertSame('Maputo Província', $readings[0]['analysis']['region']);
+113 -8
View File
@@ -24,10 +24,26 @@ function load(path, overrides = {}) {
compileFunction(code, ['require', 'module', 'exports'])(
(n) => {
if (n in overrides) return overrides[n];
const relative = n.startsWith('@/') ? n.slice(2) : n.startsWith('.') ? pathModule.posix.join(pathModule.posix.dirname(path), n) : null;
const relative = n.startsWith('@/')
? n.slice(2)
: n.startsWith('.')
? pathModule.posix.join(pathModule.posix.dirname(path), n)
: null;
if (relative) {
for (const candidate of [relative, relative + '.tsx', relative + '.ts']) {
if (fs.existsSync(new URL('../../resources/js/' + candidate, import.meta.url))) return load(candidate, overrides);
for (const candidate of [
relative,
relative + '.tsx',
relative + '.ts',
]) {
if (
fs.existsSync(
new URL(
'../../resources/js/' + candidate,
import.meta.url,
),
)
)
return load(candidate, overrides);
}
}
return require(n);
@@ -169,16 +185,23 @@ test('growth uses explicit comparable endpoints and does not fill missing interm
s.values[1].data_kind = 'observed';
assert.equal(helpers.demographicReading(s, 2026, 2028).growth, null);
});
test('technical source and full decimals remain available in closed details after a readable summary', () => {
test('public dimensions round population and hide technical references without modifying evidence', () => {
const original = base(),
before = JSON.stringify(original);
const html = renderToStaticMarkup(
React.createElement(Component, { evidence: evidence([base()]) }),
React.createElement(Component, { evidence: evidence([original]) }),
);
assert.ok(
html.indexOf('Retrato da população') <
html.indexOf('Valores originais por ano'),
html.indexOf('Valores de apresentação por ano'),
);
assert.match(html, /1000.1234567890123/);
assert.match(html, /Valores por dimensão, fonte e advertências/);
assert.doesNotMatch(
html,
/1000.1234567890123|SHA256|source.xlsx|Referência/,
);
assert.match(html, /1\s000/);
assert.match(html, /Valores por ano e dimensão/);
assert.equal(JSON.stringify(original), before);
assert.doesNotMatch(html, /<details[^>]*\bopen\b/);
assert.match(html, /um único ano/);
assert.match(html, /capacidade instalada/);
@@ -233,3 +256,85 @@ test('Barue 2026 provided evidence produces the readable proportions without cha
assert.equal(Number(r.age15to64Share.toFixed(1)), 51.3);
assert.equal(JSON.stringify(s), before);
});
test('annual comparisons format all populations and do not repeat technical explanations', () => {
const data = evidence([base()]);
data.comparisons = [2026, 2027, 2028].map((year) => ({
year,
left: 'Bilene',
right: 'Funhalouro',
left_value: '157982.9857834139',
right_value: '46656.505898723823',
difference: '111326.479884690077',
left_import: 'a',
right_import: 'b',
explanation: 'INTERNAL REPEATED EXPLANATION',
}));
const html = renderToStaticMarkup(
React.createElement(Component, { evidence: data }),
);
assert.match(html, /157\s983/);
assert.match(html, /46\s657/);
assert.match(html, /111\s326/);
assert.match(html, /<h3[^>]*>2027<\/h3>/);
assert.doesNotMatch(html, /111326\.479|INTERNAL REPEATED/);
assert.equal(
(
html.match(/Os indicadores demográficos não demonstram procura/g) ||
[]
).length,
1,
);
});
test('concurrent divergence remains visible without exposing reconciliation identifiers', () => {
const data = evidence([base()]);
data.differences = [
{
territory_id: 'INTERNAL-TERRITORY',
dimension: '2026|population|all|all|pessoas',
state: 'divergent_values',
values: [
{
import_id: 'INTERNAL-IMPORT',
value: '1000.1234567890123',
data_kind: 'projected',
},
],
},
];
const html = renderToStaticMarkup(
React.createElement(Component, { evidence: data }),
);
assert.match(html, /Há valores divergentes entre séries/);
assert.doesNotMatch(
html,
/INTERNAL-TERRITORY|INTERNAL-IMPORT|2026\|population|1000\.1234567890123/,
);
});
test('commerce preparation carries the explicit sector and territory', () => {
const Page = load('pages/investment/demography.tsx', {
'@inertiajs/react': { router: {} },
'./shared': {
Frame: ({ children }) =>
React.createElement('main', null, children),
button: '',
},
}).default;
const html = renderToStaticMarkup(
React.createElement(Page, {
evidence: evidence([base()]),
filters: { territories: ['district-one'], sector: 'commerce' },
territories: [],
sectors: { commerce: 'Comércio e serviços', tourism: 'Turismo' },
entrySector: 'comercio-e-servicos',
}),
);
assert.match(
html,
/source=territory%3Adistrict-one&amp;sector=comercio-e-servicos/,
);
assert.doesNotMatch(html, /Hotelaria e alojamento|sector=turismo/);
assert.match(html, /Não há ideias documentadas compatíveis/);
});