import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import test from 'node:test'; const root = process.cwd(); const read = (relative: string) => fs.readFileSync(path.join(root, relative), 'utf8'); const legacyMigration = read('src/database/migrations/1788631200000-phase-d5-3-22-director-report-review.ts'); const f4Removal = read('src/database/migrations/1790000400000-f4-remove-director-report-legacy.ts'); const workflow = read('src/inspection-reports/inspection-report-workflow.service.ts'); const word = read('src/inspection-reports/inspection-report-word.service.ts'); const officializeDto = read('src/inspection-reports/dto/officialize-inspection-report.dto.ts'); const delivery = read('src/inspection-reports/inspection-document-delivery.service.ts'); const acts = read('src/inspection-acts/inspection-acts.service.ts'); const findings = read('src/inspection-findings/inspection-findings.service.ts'); test('D5.3.22 revision ledger is retained as historical INF versioning', () => { assert.match(legacyMigration, /inspection_report_revisions/); assert.match(legacyMigration, /UNIQUE \(report_id, revision_number\)/); assert.match(legacyMigration, /current_revision_number/); assert.match(word, /inspection_report_revisions/); assert.match(word, /current_revision_number/); assert.match(word, /'AUTO'/); }); test('F4 explicitly retires Director review approval and final-signature state', () => { assert.match(f4Removal, /DELETE FROM inspection_document_deliveries/); assert.match(f4Removal, /recipient_kind = 'DIRECTOR'/); assert.match(f4Removal, /DELETE FROM role_permissions/); assert.match(f4Removal, /DELETE FROM permissions/); assert.match(f4Removal, /inspection_reports\.revise/); assert.match(f4Removal, /inspection_reports\.review/); assert.match(f4Removal, /inspection_reports\.sign_final/); assert.match(f4Removal, /DROP COLUMN IF EXISTS approved_revision_id/); assert.match(f4Removal, /DROP COLUMN IF EXISTS approved_by/); assert.match(f4Removal, /DROP COLUMN IF EXISTS approved_at/); assert.match(f4Removal, /DROP COLUMN IF EXISTS review_note/); assert.match(f4Removal, /DROP COLUMN IF EXISTS signed_at/); assert.match(f4Removal, /DROP COLUMN IF EXISTS review_status/); assert.doesNotMatch(f4Removal, /DROP COLUMN IF EXISTS current_revision_number/); }); test('F4 removes active Director endpoints but preserves historical signature evidence', () => { assert.equal(fs.existsSync(path.join(root, 'src/inspection-reports/inspection-report-review.service.ts')), false); assert.equal(fs.existsSync(path.join(root, 'src/inspection-reports/inspection-report-review.controller.ts')), false); assert.doesNotMatch(f4Removal, /DROP TABLE IF EXISTS inspection_report_signatures/); assert.match(f4Removal, /inspection_report_signatures tambiƩn se conserva como archivo/); }); test('F4 replaces Director approval with editable INF then immutable GEDO IF officialization', () => { assert.match(workflow, /InspectionReportStatus\.WORKING/); assert.match(workflow, /status='OFFICIALIZED'/); assert.match(workflow, /gedo_if_identifier/); assert.match(workflow, /gedo_officialized_at/); assert.match(workflow, /INSPECTION_REPORT_ALREADY_OFFICIALIZED/); assert.match(officializeDto, /gedoIfIdentifier!/); assert.match(officializeDto, /gedoOfficializedAt!/); assert.match(officializeDto, /@MinLength\(2\)/); assert.match(officializeDto, /@IsISO8601/); }); test('F4 delivery has no Director recipient while preserving company office and inspector copies', () => { assert.match(delivery, /'COMPANY' \| 'OFFICE' \| 'INSPECTOR'/); assert.match(delivery, /recipient_kind<>'DIRECTOR'/); assert.doesNotMatch(delivery, /recipientKind:'DIRECTOR'/); }); test('F4 Director-retirement rollback reconstructs schema and RBAC contract without inventing deleted data', () => { assert.match(f4Removal, /ADD COLUMN IF NOT EXISTS review_status/); assert.match(f4Removal, /chk_inspection_reports_review_status/); assert.match(f4Removal, /fk_inspection_reports_approved_by/); assert.match(f4Removal, /fk_inspection_reports_approved_revision/); assert.match(f4Removal, /INSERT INTO permissions/); assert.match(f4Removal, /INSERT INTO role_permissions/); }); test('F4 document workflow changes do not reopen acts or mutate finding history', () => { assert.match(acts, /assertVisitHasNoDraftAct/); assert.match(acts, /INSPECTION_VISIT_DRAFT_ACT_ALREADY_EXISTS/); assert.match(findings, /INSPECTION_FINDING_RESPONSE_IMMUTABLE/); assert.doesNotMatch(f4Removal, /ALTER TABLE inspection_acts/); assert.doesNotMatch(f4Removal, /ALTER TABLE inspection_findings/); });