Files
dh-inspeccion-v2/api-v3/test/unit/f5-1-clean-manual-inventory-contract.test.ts
T

93 lines
5.0 KiB
TypeScript

import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import test from 'node:test';
function source(path: string) {
return readFileSync(resolve(process.cwd(), path), 'utf8');
}
test('F5.1 clean start deletes domain instances and audit history but preserves master configuration by contract', () => {
const migration = source('src/database/migrations/1790087400000-f5-1-clean-manual-inventory.ts');
assert.match(migration, /TRUNCATE TABLE assets CASCADE/);
assert.match(migration, /TRUNCATE TABLE audit_events/);
assert.match(migration, /TRUNCATE TABLE administrative_departments CASCADE/);
assert.match(migration, /TRUNCATE TABLE finding_catalog_item_inventory_families/);
assert.doesNotMatch(migration, /TRUNCATE TABLE inventory_families/);
assert.doesNotMatch(migration, /TRUNCATE TABLE finding_catalog_items/);
assert.doesNotMatch(migration, /TRUNCATE TABLE users/);
});
test('F5.1 canonical hierarchy starts at Departamento and Area is no longer root', () => {
const migration = source('src/database/migrations/1790087400000-f5-1-clean-manual-inventory.ts');
const dto = source('src/asset-master/dto/create-inventory-structure.dto.ts');
const structure = source('src/asset-master/inventory-structure.service.ts');
assert.match(dto, /'DEPARTAMENTO'/);
assert.match(structure, /DEPARTAMENTO: 'departamento'/);
assert.match(structure, /AREA: 'departamento'/);
assert.match(structure, /Departamento → Área → Yacimiento → Instalación → Subinstalación/);
assert.match(migration, /child_code='area' AND parent_code<>'departamento'/);
assert.match(migration, /WHERE lower\(code\)='area'/);
assert.match(migration, /SET can_be_root=false/);
});
test('Authoritative manual creation keeps Area physical and stores Company plus concession on Yacimiento', () => {
const structure = source('src/asset-master/inventory-structure.service.ts');
const dto = source('src/asset-master/dto/create-inventory-structure.dto.ts');
assert.match(structure, /operator_company_id,concession_type_id,inventory_family_id/);
assert.match(structure, /dto\.kind === 'YACIMIENTO'/);
assert.match(structure, /INVENTORY_YACIMIENTO_COMPANY_REQUIRED/);
assert.match(structure, /INVENTORY_YACIMIENTO_CONCESSION_REQUIRED/);
assert.match(structure, /asset_context_history/);
assert.match(dto, /operatorCompanyId\?: string \| null/);
assert.match(dto, /concessionTypeId\?: string \| null/);
});
test('F5.1 Inventory browser exposes every canonical level instead of filtering by is_inventory_instance', () => {
const browser = source('src/asset-master/inventory-browser.service.ts');
const controller = source('src/asset-master/inventory-browser.controller.ts');
assert.match(browser, /'departamento','area','yacimiento','instalacion','subinstalacion'/);
assert.match(browser, /case 'departamento': return 'area'/);
assert.doesNotMatch(browser, /['"]asset\.is_inventory_instance=true['"]/);
assert.match(controller, /@Get\('departments'\)/);
assert.match(controller, /@Get\('companies'\)/);
});
test('F5.1 Finding Catalog defaults to associated findings and exposes all items only for linking', () => {
const panel = source('../web-v2/src/features/inspections/FindingCatalogTypeApplicabilityPanel.tsx');
assert.match(panel, /type ViewMode = 'ASSOCIATED' \| 'ALL'/);
assert.match(panel, /useState<ViewMode>\('ASSOCIATED'\)/);
assert.match(panel, /available\.filter\(\(item\) => savedIds\.has\(item\.id\)\)/);
assert.match(panel, /Buscar dentro de \{viewMode === 'ASSOCIATED' \? 'los asociados' : 'todo el Catálogo'\}/);
assert.match(panel, /Todos para vincular/);
assert.match(panel, /Esta clasificación todavía no tiene Hallazgos asociados/);
});
test('Authoritative Configuration presents exact physical relationships and technical families', () => {
const configPage = source('../web-v2/src/pages/AuthoritativeInventoryConfigPage.tsx');
assert.match(configPage, /El Área sólo tiene Nombre y pertenece obligatoriamente a un Departamento/);
assert.match(configPage, /Tipo de concesión/);
assert.match(configPage, /Empresa relacionada/);
assert.match(configPage, /Yacimiento → Instalación/);
assert.match(configPage, /Instalación → Subinstalación/);
assert.match(configPage, /installationFamilies\.map/);
assert.match(configPage, /subinstallationFamilies\.map/);
assert.match(configPage, /Hallazgos asociados/);
});
test('Authoritative Web creation exposes Departamento as root and Yacimiento as operational owner', () => {
const createPage = source('../web-v2/src/pages/InventoryCreatePage.tsx');
const configPage = source('../web-v2/src/pages/AuthoritativeInventoryConfigPage.tsx');
assert.match(createPage, /kind: 'DEPARTAMENTO'/);
assert.match(createPage, /AREA:'Departamento'/);
assert.match(createPage, /Empresa relacionada/);
assert.match(createPage, /Tipo de concesión/);
assert.match(createPage, /Datos opcionales/);
assert.match(configPage, /<strong>Departamento<\/strong>/);
assert.match(configPage, /<strong>Yacimiento<\/strong>/);
});