|
| 1 | +// Regression tests for CS-702: the automated daily cloud scan appeared to |
| 2 | +// return far fewer results than a manual scan. Root cause was in the |
| 3 | +// latest-run lookups here — they picked the newest IntegrationCheckRun per |
| 4 | +// connection with NO checkId scope, so the ~06:00 per-task evidence runs (a |
| 5 | +// handful of results each) shadowed the ~05:00 full cloud-security scan |
| 6 | +// (hundreds of results). These tests reproduce that exact ordering via a |
| 7 | +// Prisma mock that emulates where + orderBy + distinct. |
| 8 | + |
| 9 | +interface RunRow { |
| 10 | + id: string; |
| 11 | + connectionId: string; |
| 12 | + checkId: string; |
| 13 | + taskId: string | null; |
| 14 | + status: string; |
| 15 | + completedAt: Date; |
| 16 | + durationMs: number | null; |
| 17 | + totalChecked: number | null; |
| 18 | + passedCount: number | null; |
| 19 | + failedCount: number | null; |
| 20 | +} |
| 21 | + |
| 22 | +interface ResultRow { |
| 23 | + id: string; |
| 24 | + title: string; |
| 25 | + description: string | null; |
| 26 | + remediation: string | null; |
| 27 | + severity: string | null; |
| 28 | + collectedAt: Date; |
| 29 | + checkRunId: string; |
| 30 | + passed: boolean; |
| 31 | + evidence: Record<string, unknown> | null; |
| 32 | + resourceId: string | null; |
| 33 | + resourceType: string | null; |
| 34 | +} |
| 35 | + |
| 36 | +// In-memory fixtures the mocked Prisma layer reads from. Set per-test. |
| 37 | +let runRows: RunRow[] = []; |
| 38 | +let resultRows: ResultRow[] = []; |
| 39 | + |
| 40 | +// Emulate the slice of Prisma semantics these queries rely on: filter by the |
| 41 | +// provided `where`, order by completedAt desc, then distinct-by-connectionId |
| 42 | +// (Postgres DISTINCT ON keeps the first row after ordering). |
| 43 | +function checkRunFindMany(args: { |
| 44 | + where: { |
| 45 | + connectionId?: { in: string[] }; |
| 46 | + checkId?: { in: string[] }; |
| 47 | + status?: { in: string[] }; |
| 48 | + }; |
| 49 | + distinct?: string[]; |
| 50 | +}): Promise<RunRow[]> { |
| 51 | + const { where } = args; |
| 52 | + let rows = runRows.filter((r) => { |
| 53 | + if (where.connectionId && !where.connectionId.in.includes(r.connectionId)) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + if (where.checkId && !where.checkId.in.includes(r.checkId)) return false; |
| 57 | + if (where.status && !where.status.in.includes(r.status)) return false; |
| 58 | + return true; |
| 59 | + }); |
| 60 | + rows = [...rows].sort( |
| 61 | + (a, b) => b.completedAt.getTime() - a.completedAt.getTime(), |
| 62 | + ); |
| 63 | + if (args.distinct?.includes('connectionId')) { |
| 64 | + const seen = new Set<string>(); |
| 65 | + rows = rows.filter((r) => { |
| 66 | + if (seen.has(r.connectionId)) return false; |
| 67 | + seen.add(r.connectionId); |
| 68 | + return true; |
| 69 | + }); |
| 70 | + } |
| 71 | + return Promise.resolve(rows); |
| 72 | +} |
| 73 | + |
| 74 | +function checkResultFindMany(args: { |
| 75 | + where: { checkRunId: { in: string[] } }; |
| 76 | +}): Promise<ResultRow[]> { |
| 77 | + const ids = args.where.checkRunId.in; |
| 78 | + return Promise.resolve(resultRows.filter((r) => ids.includes(r.checkRunId))); |
| 79 | +} |
| 80 | + |
| 81 | +const dbMock = { |
| 82 | + integrationConnection: { findMany: jest.fn() }, |
| 83 | + integration: { findMany: jest.fn() }, |
| 84 | + integrationCheckRun: { findMany: jest.fn(checkRunFindMany) }, |
| 85 | + integrationCheckResult: { findMany: jest.fn(checkResultFindMany) }, |
| 86 | +}; |
| 87 | + |
| 88 | +jest.mock('@db', () => ({ db: dbMock })); |
| 89 | +jest.mock('@trycompai/integration-platform', () => ({ getManifest: jest.fn() })); |
| 90 | +jest.mock('./evidence-sanitizer', () => ({ |
| 91 | + sanitizeEvidence: (v: unknown) => v, |
| 92 | +})); |
| 93 | +jest.mock('./check-definition.utils', () => ({ |
| 94 | + resolveCheckKey: jest.fn(() => 'check-key'), |
| 95 | +})); |
| 96 | +jest.mock('./cloud-security-query.legacy', () => ({ |
| 97 | + getLegacyFindings: jest.fn().mockResolvedValue([]), |
| 98 | +})); |
| 99 | +jest.mock('./finding-exceptions', () => ({ |
| 100 | + loadActiveExceptionSet: jest.fn().mockResolvedValue({ size: 0 }), |
| 101 | +})); |
| 102 | + |
| 103 | +import { getManifest } from '@trycompai/integration-platform'; |
| 104 | +import { CloudSecurityQueryService } from './cloud-security-query.service'; |
| 105 | + |
| 106 | +const ORG_ID = 'org_test'; |
| 107 | +const CONNECTION_ID = 'icn_aws'; |
| 108 | +const SCAN_AT = new Date('2026-07-02T05:00:00Z'); // full scan |
| 109 | +const TASK_AT = new Date('2026-07-02T06:00:00Z'); // per-task run — NEWER |
| 110 | + |
| 111 | +const awsConnection = { |
| 112 | + id: CONNECTION_ID, |
| 113 | + organizationId: ORG_ID, |
| 114 | + provider: { slug: 'aws', name: 'AWS' }, |
| 115 | + metadata: {}, |
| 116 | + variables: {}, |
| 117 | + status: 'active', |
| 118 | + lastSyncAt: TASK_AT, |
| 119 | + createdAt: SCAN_AT, |
| 120 | + updatedAt: TASK_AT, |
| 121 | +}; |
| 122 | + |
| 123 | +// The 05:00 full scan: many results, taskId null, checkId 'aws-security-scan'. |
| 124 | +const scanRun: RunRow = { |
| 125 | + id: 'run_scan', |
| 126 | + connectionId: CONNECTION_ID, |
| 127 | + checkId: 'aws-security-scan', |
| 128 | + taskId: null, |
| 129 | + status: 'success', |
| 130 | + completedAt: SCAN_AT, |
| 131 | + durationMs: 5000, |
| 132 | + totalChecked: 300, |
| 133 | + passedCount: 250, |
| 134 | + failedCount: 50, |
| 135 | +}; |
| 136 | + |
| 137 | +// The 06:00 per-task evidence run: a handful of results, taskId set, checkId |
| 138 | +// is a manifest check id. This is the run that used to shadow the scan. |
| 139 | +const taskRun: RunRow = { |
| 140 | + id: 'run_task', |
| 141 | + connectionId: CONNECTION_ID, |
| 142 | + checkId: 'aws-iam-account-security', |
| 143 | + taskId: 'tsk_iam', |
| 144 | + status: 'failed', |
| 145 | + completedAt: TASK_AT, |
| 146 | + durationMs: 120, |
| 147 | + totalChecked: 3, |
| 148 | + passedCount: 1, |
| 149 | + failedCount: 2, |
| 150 | +}; |
| 151 | + |
| 152 | +function scanResult(id: string): ResultRow { |
| 153 | + return { |
| 154 | + id, |
| 155 | + title: `scan-${id}`, |
| 156 | + description: null, |
| 157 | + remediation: null, |
| 158 | + severity: 'medium', |
| 159 | + collectedAt: SCAN_AT, |
| 160 | + checkRunId: 'run_scan', |
| 161 | + passed: false, |
| 162 | + evidence: {}, |
| 163 | + resourceId: `res-${id}`, |
| 164 | + resourceType: 'AwsResource', |
| 165 | + }; |
| 166 | +} |
| 167 | + |
| 168 | +const taskResult: ResultRow = { |
| 169 | + id: 'task_only', |
| 170 | + title: 'task-only', |
| 171 | + description: null, |
| 172 | + remediation: null, |
| 173 | + severity: 'high', |
| 174 | + collectedAt: TASK_AT, |
| 175 | + checkRunId: 'run_task', |
| 176 | + passed: false, |
| 177 | + evidence: {}, |
| 178 | + resourceId: 'res-task', |
| 179 | + resourceType: 'AwsResource', |
| 180 | +}; |
| 181 | + |
| 182 | +describe('CloudSecurityQueryService — latest-run scoping (CS-702)', () => { |
| 183 | + let service: CloudSecurityQueryService; |
| 184 | + |
| 185 | + beforeEach(() => { |
| 186 | + jest.clearAllMocks(); |
| 187 | + service = new CloudSecurityQueryService(); |
| 188 | + |
| 189 | + // Both runs exist for the same connection; the per-task run is newer. |
| 190 | + runRows = [scanRun, taskRun]; |
| 191 | + resultRows = [ |
| 192 | + scanResult('a'), |
| 193 | + scanResult('b'), |
| 194 | + scanResult('c'), |
| 195 | + taskResult, |
| 196 | + ]; |
| 197 | + |
| 198 | + dbMock.integrationConnection.findMany.mockResolvedValue([awsConnection]); |
| 199 | + dbMock.integration.findMany.mockResolvedValue([]); |
| 200 | + dbMock.integrationCheckRun.findMany.mockImplementation(checkRunFindMany); |
| 201 | + dbMock.integrationCheckResult.findMany.mockImplementation( |
| 202 | + checkResultFindMany, |
| 203 | + ); |
| 204 | + (getManifest as jest.Mock).mockReturnValue({ |
| 205 | + supportsMultipleConnections: true, |
| 206 | + variables: [], |
| 207 | + checks: [], |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + it('getFindings returns the full-scan results, not the newer per-task run', async () => { |
| 212 | + const findings = await service.getFindings(ORG_ID); |
| 213 | + |
| 214 | + // The three scan findings — and none from the per-task run. |
| 215 | + expect(findings).toHaveLength(3); |
| 216 | + expect(findings.map((f) => f.id).sort()).toEqual(['a', 'b', 'c']); |
| 217 | + expect(findings.every((f) => f.checkId === 'aws-security-scan')).toBe(true); |
| 218 | + expect(findings.some((f) => f.id === 'task_only')).toBe(false); |
| 219 | + }); |
| 220 | + |
| 221 | + it('getProviders reports the full-scan summary, not the per-task run summary', async () => { |
| 222 | + const providers = await service.getProviders(ORG_ID); |
| 223 | + |
| 224 | + const aws = providers.find((p) => p.id === CONNECTION_ID); |
| 225 | + expect(aws?.latestRun).toEqual( |
| 226 | + expect.objectContaining({ |
| 227 | + totalChecked: 300, |
| 228 | + passedCount: 250, |
| 229 | + failedCount: 50, |
| 230 | + }), |
| 231 | + ); |
| 232 | + }); |
| 233 | + |
| 234 | + it('scopes the latest-run lookup to cloud-security scan checkIds', async () => { |
| 235 | + await service.getFindings(ORG_ID); |
| 236 | + |
| 237 | + expect(dbMock.integrationCheckRun.findMany).toHaveBeenCalledWith( |
| 238 | + expect.objectContaining({ |
| 239 | + where: expect.objectContaining({ |
| 240 | + checkId: { |
| 241 | + in: ['aws-security-scan', 'gcp-security-scan', 'azure-security-scan'], |
| 242 | + }, |
| 243 | + }), |
| 244 | + }), |
| 245 | + ); |
| 246 | + }); |
| 247 | +}); |
0 commit comments