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('Historical authoritative endpoint keeps the former Yacimiento Company plus concession contract for compatibility', () => { 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\('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('F7 Inventory Configuration exposes only simple types, containment and fields', () => { const configPage = source('../web-v2/src/pages/AuthoritativeInventoryConfigPage.tsx'); assert.match(configPage, /Tipos y campos/); assert.match(configPage, /Departamento → Área → Yacimiento → Instalación → Subinstalación/); assert.match(configPage, /Un Área puede estar vinculada a varias Empresas/); assert.match(configPage, /Cada Yacimiento elige una sola Empresa operadora/); assert.match(configPage, /Tipos de Instalación/); assert.match(configPage, /Tipos de Subinstalación/); assert.match(configPage, /Puede estar dentro de/); assert.match(configPage, /\{ value: 'TEXT', label: 'Texto' \}/); assert.match(configPage, /\{ value: 'NUMBER', label: 'Número' \}/); assert.match(configPage, /\{ value: 'DATE', label: 'Fecha' \}/); assert.match(configPage, /\{ value: 'BOOLEAN', label: 'Sí \/ No' \}/); assert.doesNotMatch(configPage, /Hallazgos asociados/); }); test('F7 Web creation keeps the fixed hierarchy and asks only the required simple context', () => { 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, /YACIMIENTO: 'Área'/); assert.match(createPage, /INSTALACION: 'Yacimiento'/); assert.match(createPage, /SUBINSTALACION: 'Instalación'/); assert.match(createPage, /listCompaniesForInventoryArea/); assert.match(createPage, /Empresa operadora/); assert.match(createPage, /ALTA RÁPIDA/); assert.doesNotMatch(createPage, /Tipo de concesión/); assert.match(configPage, /Departamento → Área → Yacimiento → Instalación → Subinstalación/); });