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): string { return readFileSync(resolve(process.cwd(), path), 'utf8'); } test('Authoritative model stores Company on Yacimiento and propagates it to physical descendants', () => { const migration = source('src/database/migrations/1790106600000-authoritative-inventory-relationships.ts'); const structure = source('src/asset-master/inventory-structure.service.ts'); assert.match(migration, /Yacimiento requiere Empresa relacionada/); assert.match(migration, /NEW\.operational_area_id:=NEW\.parent_id/); assert.match(migration, /NEW\.operator_company_id:=parent_company/); assert.match(structure, /dto\.kind === 'YACIMIENTO'/); assert.match(structure, /parent\?\.operatorCompanyId/); assert.match(structure, /ensureCompatibilityProjection/); }); test('Authoritative Yacimiento relationships cannot be mutated through generic Inventory edits', () => { const updateDto = source('src/asset-master/dto/update-asset.dto.ts'); assert.match(updateDto, /@IsEmpty/); assert.match(updateDto, /La Empresa relacionada se administra en el Yacimiento/); assert.match(updateDto, /El Tipo de concesión se administra en el Yacimiento/); }); test('F6.1 rejects legacy Inventory searches that filter by operator snapshot', () => { const listDto = source('src/asset-master/dto/list-assets-query.dto.ts'); const treeDto = source('src/asset-master/dto/list-asset-tree-query.dto.ts'); for (const dto of [listDto, treeDto]) { assert.match(dto, /operatorCompanyId\?: string/); assert.match(dto, /@IsEmpty/); } }); test('Compatibility Area relation history remains available for legacy temporal queries', () => { const temporal = source('src/asset-master/asset-temporal.service.ts'); const migration = source('src/database/migrations/1790106600000-authoritative-inventory-relationships.ts'); assert.match(temporal, /FROM area_company_relations relation/); assert.match(migration, /Derivado automáticamente de Yacimientos del modelo autoritativo/); assert.match(migration, /TRUNCATE TABLE area_company_relations CASCADE/); }); test('F6.1 verification planning can consume the Area compatibility projection', () => { const verification = source('src/inspection-verifications/inspection-verifications.service.ts'); const migration = source('src/database/migrations/1790106600000-authoritative-inventory-relationships.ts'); assert.match(verification, /FROM area_company_relations relation/); assert.match(verification, /VERIFICATION_OPERATOR_NOT_ACTIVE/); assert.match(migration, /relation_role,source_document_id,valid_from,start_reason/); }); test('F6.1 historical operational lists read Company and Area from the parent Inspection', () => { const acts = source('src/inspection-acts/inspection-acts.service.ts'); const findings = source('src/inspection-findings/inspection-finding-worklist.service.ts'); const reports = source('src/inspection-reports/inspection-reports.service.ts'); assert.match(acts, /visit\.operator_company_id/); assert.match(acts, /visit\.operational_area_id/); assert.doesNotMatch(acts, /context_asset\.operator_company_id/); assert.match(findings, /company\.id = visit\.operator_company_id/); assert.match(findings, /area\.id = visit\.operational_area_id/); assert.match(reports, /visit\.\$\{column\} = \$\{parameter\}::uuid/); assert.match(reports, /company\.id=context_visit\.operator_company_id/); assert.match(reports, /area\.id=context_visit\.operational_area_id/); }); test('F6.1 WEB Inventory searches use physical scope and never the operator snapshot filter', () => { const inspectionEditor = source('../web-v2/src/pages/InspectionVisitEditorF4Page.tsx'); const preventiveApi = source('../web-v2/src/features/inspections/preventiveCandidatesApi.ts'); const preventiveService = source('src/inspection-visits/inspection-preventive-candidates.service.ts'); const fieldDiscoveries = source('../web-v2/src/pages/FieldDiscoveriesPage.tsx'); const inventoryMerge = source('../web-v2/src/lib/inventoryMergeApi.ts'); assert.match(inspectionEditor, /listInspectionPreventiveCandidates\(id, assetSearch\)/); assert.match(preventiveApi, /inspection-visits\/\$\{visitId\}\/preventive-candidates/); assert.match(preventiveService, /COALESCE\(visit\.scope_asset_id, visit\.operational_area_id\) AS "scopeAssetId"/); assert.doesNotMatch(inspectionEditor, /listAssets\(\{[\s\S]{0,300}operatorCompanyId:/); for (const page of [fieldDiscoveries, inventoryMerge]) { assert.match(page, /listAssets\(\{[\s\S]{0,300}operationalAreaId:/); assert.doesNotMatch(page, /listAssets\(\{[\s\S]{0,300}operatorCompanyId:/); } }); test('F7 WEB creation selects one Area-linked Company for Yacimiento and retires concession from the simple flow', () => { const page = source('../web-v2/src/pages/InventoryCreatePage.tsx'); const api = source('../web-v2/src/lib/inventoryStructureApi.ts'); const simpleStructure = source('src/asset-master/simple-inventory-structure.service.ts'); const migration = source('src/database/migrations/1790110200000-f7-simple-inventory-context.ts'); assert.match(page, /const requiresCompany = kind === 'YACIMIENTO'/); assert.match(page, /listCompaniesForInventoryArea\(parentId\)/); assert.match(page, /Empresa operadora/); assert.match(page, /operatorCompanyId: operatorCompanyId \|\| null/); assert.doesNotMatch(page, /Tipo de concesión/); assert.doesNotMatch(page, /concessionTypeId/); assert.match(api, /inventory-structure\/simple/); assert.match(simpleStructure, /requireAreaCompanyRelation/); assert.match(simpleStructure, /INVENTORY_YACIMIENTO_COMPANY_NOT_IN_AREA/); assert.match(migration, /La Empresa del Yacimiento debe estar vinculada como explotadora del Área/); assert.match(migration, /NEW\.operational_area_id:=NEW\.parent_id/); });