|
| 1 | +// loadFrameworkSources operates purely on the injected `tx`; its only @db |
| 2 | +// imports are `import type`, which are erased at runtime, so a no-op mock keeps |
| 3 | +// jest from initialising a real PrismaClient. |
| 4 | +jest.mock('@db', () => ({})); |
| 5 | + |
| 6 | +import { loadFrameworkSources } from './frameworks-source-loader.helper'; |
| 7 | +import type { FrameworkManifest } from './framework-versioning/manifest.types'; |
| 8 | + |
| 9 | +type LoaderTx = Parameters<typeof loadFrameworkSources>[0]['tx']; |
| 10 | + |
| 11 | +function manifest(overrides: Partial<FrameworkManifest> = {}): FrameworkManifest { |
| 12 | + return { |
| 13 | + framework: { id: 'frk_pci', name: 'PCI DSS', catalogVersion: '1', description: null }, |
| 14 | + requirements: [], |
| 15 | + controls: [], |
| 16 | + policies: [], |
| 17 | + tasks: [], |
| 18 | + ...overrides, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * A manifest with one control wiring up one requirement, one policy and one |
| 24 | + * task. Callers extend it to introduce ids that no longer exist live. |
| 25 | + */ |
| 26 | +function fullManifest(): FrameworkManifest { |
| 27 | + return manifest({ |
| 28 | + requirements: [{ id: 'req_live', identifier: 'R1', name: 'Req', description: null }], |
| 29 | + controls: [ |
| 30 | + { |
| 31 | + id: 'ct_live', |
| 32 | + name: 'Control', |
| 33 | + description: 'd', |
| 34 | + requirementIds: ['req_live'], |
| 35 | + policyIds: ['pt_live'], |
| 36 | + taskIds: ['tt_live'], |
| 37 | + documentTypes: [], |
| 38 | + }, |
| 39 | + ], |
| 40 | + policies: [ |
| 41 | + { id: 'pt_live', name: 'Policy', description: null, content: [], frequency: null, department: null }, |
| 42 | + ], |
| 43 | + tasks: [{ id: 'tt_live', name: 'Task', description: 'd', frequency: null, department: null }], |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function mockTx({ |
| 48 | + versions, |
| 49 | + liveControlIds, |
| 50 | + livePolicyIds, |
| 51 | + liveTasks, |
| 52 | + liveRequirementIds, |
| 53 | +}: { |
| 54 | + versions: Array<{ id: string; frameworkId: string; manifest: FrameworkManifest }>; |
| 55 | + liveControlIds: string[]; |
| 56 | + livePolicyIds: string[]; |
| 57 | + liveTasks: Array<{ id: string; automationStatus: string }>; |
| 58 | + liveRequirementIds: string[]; |
| 59 | +}): LoaderTx { |
| 60 | + return { |
| 61 | + frameworkVersion: { findMany: jest.fn().mockResolvedValue(versions) }, |
| 62 | + frameworkEditorControlTemplate: { |
| 63 | + findMany: jest.fn().mockResolvedValue(liveControlIds.map((id) => ({ id }))), |
| 64 | + }, |
| 65 | + frameworkEditorPolicyTemplate: { |
| 66 | + findMany: jest.fn().mockResolvedValue(livePolicyIds.map((id) => ({ id }))), |
| 67 | + }, |
| 68 | + frameworkEditorTaskTemplate: { |
| 69 | + findMany: jest.fn().mockResolvedValue(liveTasks), |
| 70 | + }, |
| 71 | + frameworkEditorRequirement: { |
| 72 | + findMany: jest.fn().mockResolvedValue(liveRequirementIds.map((id) => ({ id }))), |
| 73 | + }, |
| 74 | + } as unknown as LoaderTx; |
| 75 | +} |
| 76 | + |
| 77 | +function ids<T extends { id: string }>(rows: T[]): string[] { |
| 78 | + return rows.map((r) => r.id); |
| 79 | +} |
| 80 | + |
| 81 | +describe('loadFrameworkSources — stale-manifest reconciliation', () => { |
| 82 | + const frameworkEditorIds = ['frk_pci']; |
| 83 | + |
| 84 | + it('drops a manifest TASK whose live template was hard-deleted (the reported Task_taskTemplateId_fkey bug)', async () => { |
| 85 | + const m = fullManifest(); |
| 86 | + m.controls[0].taskIds = ['tt_live', 'tt_dead']; |
| 87 | + m.tasks.push({ id: 'tt_dead', name: 'Deleted Task', description: 'd', frequency: null, department: null }); |
| 88 | + |
| 89 | + const tx = mockTx({ |
| 90 | + versions: [{ id: 'fv_1', frameworkId: 'frk_pci', manifest: m }], |
| 91 | + liveControlIds: ['ct_live'], |
| 92 | + livePolicyIds: ['pt_live'], |
| 93 | + liveTasks: [{ id: 'tt_live', automationStatus: 'AUTOMATED' }], // tt_dead absent |
| 94 | + liveRequirementIds: ['req_live'], |
| 95 | + }); |
| 96 | + |
| 97 | + const result = await loadFrameworkSources({ frameworkEditorIds, frameworkEditorFrameworks: [], tx }); |
| 98 | + |
| 99 | + // tt_dead must never reach task.createMany — it would FK-fail on insert. |
| 100 | + expect(ids(result.taskTemplates)).toEqual(['tt_live']); |
| 101 | + }); |
| 102 | + |
| 103 | + it('drops a manifest CONTROL whose live template was hard-deleted', async () => { |
| 104 | + const m = fullManifest(); |
| 105 | + m.controls.push({ |
| 106 | + id: 'ct_dead', |
| 107 | + name: 'Deleted Control', |
| 108 | + description: 'd', |
| 109 | + requirementIds: [], |
| 110 | + policyIds: [], |
| 111 | + taskIds: [], |
| 112 | + documentTypes: [], |
| 113 | + }); |
| 114 | + |
| 115 | + const tx = mockTx({ |
| 116 | + versions: [{ id: 'fv_1', frameworkId: 'frk_pci', manifest: m }], |
| 117 | + liveControlIds: ['ct_live'], // ct_dead absent |
| 118 | + livePolicyIds: ['pt_live'], |
| 119 | + liveTasks: [{ id: 'tt_live', automationStatus: 'AUTOMATED' }], |
| 120 | + liveRequirementIds: ['req_live'], |
| 121 | + }); |
| 122 | + |
| 123 | + const result = await loadFrameworkSources({ frameworkEditorIds, frameworkEditorFrameworks: [], tx }); |
| 124 | + |
| 125 | + expect(ids(result.controlTemplates)).toEqual(['ct_live']); |
| 126 | + }); |
| 127 | + |
| 128 | + it('drops a manifest POLICY whose live template was hard-deleted', async () => { |
| 129 | + const m = fullManifest(); |
| 130 | + m.controls[0].policyIds = ['pt_live', 'pt_dead']; |
| 131 | + m.policies.push({ |
| 132 | + id: 'pt_dead', |
| 133 | + name: 'Deleted Policy', |
| 134 | + description: null, |
| 135 | + content: [], |
| 136 | + frequency: null, |
| 137 | + department: null, |
| 138 | + }); |
| 139 | + |
| 140 | + const tx = mockTx({ |
| 141 | + versions: [{ id: 'fv_1', frameworkId: 'frk_pci', manifest: m }], |
| 142 | + liveControlIds: ['ct_live'], |
| 143 | + livePolicyIds: ['pt_live'], // pt_dead absent |
| 144 | + liveTasks: [{ id: 'tt_live', automationStatus: 'AUTOMATED' }], |
| 145 | + liveRequirementIds: ['req_live'], |
| 146 | + }); |
| 147 | + |
| 148 | + const result = await loadFrameworkSources({ frameworkEditorIds, frameworkEditorFrameworks: [], tx }); |
| 149 | + |
| 150 | + expect(ids(result.policyTemplates)).toEqual(['pt_live']); |
| 151 | + }); |
| 152 | + |
| 153 | + it('drops a dead REQUIREMENT from groupedRelations (RequirementMap.requirementId has no downstream guard)', async () => { |
| 154 | + const m = fullManifest(); |
| 155 | + m.controls[0].requirementIds = ['req_live', 'req_dead']; |
| 156 | + m.requirements.push({ id: 'req_dead', identifier: 'R2', name: 'Deleted Req', description: null }); |
| 157 | + |
| 158 | + const tx = mockTx({ |
| 159 | + versions: [{ id: 'fv_1', frameworkId: 'frk_pci', manifest: m }], |
| 160 | + liveControlIds: ['ct_live'], |
| 161 | + livePolicyIds: ['pt_live'], |
| 162 | + liveTasks: [{ id: 'tt_live', automationStatus: 'AUTOMATED' }], |
| 163 | + liveRequirementIds: ['req_live'], // req_dead absent |
| 164 | + }); |
| 165 | + |
| 166 | + const result = await loadFrameworkSources({ frameworkEditorIds, frameworkEditorFrameworks: [], tx }); |
| 167 | + |
| 168 | + const rel = result.groupedRelations.find((r) => r.controlTemplateId === 'ct_live'); |
| 169 | + expect(rel?.requirementTemplateIds).toEqual(['req_live']); |
| 170 | + }); |
| 171 | + |
| 172 | + it('passes everything through unchanged and resolves automationStatus when all templates are live', async () => { |
| 173 | + const tx = mockTx({ |
| 174 | + versions: [{ id: 'fv_1', frameworkId: 'frk_pci', manifest: fullManifest() }], |
| 175 | + liveControlIds: ['ct_live'], |
| 176 | + livePolicyIds: ['pt_live'], |
| 177 | + liveTasks: [{ id: 'tt_live', automationStatus: 'MANUAL' }], |
| 178 | + liveRequirementIds: ['req_live'], |
| 179 | + }); |
| 180 | + |
| 181 | + const result = await loadFrameworkSources({ frameworkEditorIds, frameworkEditorFrameworks: [], tx }); |
| 182 | + |
| 183 | + expect(ids(result.controlTemplates)).toEqual(['ct_live']); |
| 184 | + expect(ids(result.policyTemplates)).toEqual(['pt_live']); |
| 185 | + expect(ids(result.taskTemplates)).toEqual(['tt_live']); |
| 186 | + // automationStatus is not in the manifest — it must come from the live row. |
| 187 | + expect(result.taskTemplates[0].automationStatus).toBe('MANUAL'); |
| 188 | + const rel = result.groupedRelations.find((r) => r.controlTemplateId === 'ct_live'); |
| 189 | + expect(rel?.requirementTemplateIds).toEqual(['req_live']); |
| 190 | + expect(rel?.policyTemplateIds).toEqual(['pt_live']); |
| 191 | + expect(rel?.taskTemplateIds).toEqual(['tt_live']); |
| 192 | + }); |
| 193 | +}); |
0 commit comments