|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import os from 'node:os'; |
| 4 | +import {LogLevel, TelemetryData} from '@salesforce/code-analyzer-engine-api'; |
| 5 | +import {RuntimeSfgeWrapper, SfgeRuleInfo} from '../src/sfge-wrapper'; |
| 6 | + |
| 7 | +class FakeJavaExec { |
| 8 | + public async exec(_args: string[], _cp: string[], onStdOut?: (msg: string) => void): Promise<void> { |
| 9 | + if (onStdOut) { |
| 10 | + // Build a mixed message payload with LOG and PROGRESS entries |
| 11 | + const payload = JSON.stringify([ |
| 12 | + { messageKey: 'debug_sfgeInfoLog', args: ['hello'], internalLog: '', messageSeverity: 'DEBUG' }, |
| 13 | + { messageKey: 'progress_sfgeFinishedCompilingFiles', args: ['1'], internalLog: '', progressPercent: 50 } |
| 14 | + ]); |
| 15 | + onStdOut(`SFCA-REALTIME-START${payload}SFCA-REALTIME-END`); |
| 16 | + } |
| 17 | + return; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +describe('sfge-wrapper handleRunStdOut coverage', () => { |
| 22 | + it('processes realtime LOG and PROGRESS messages', async () => { |
| 23 | + const tmp = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'sfge-wrapper-')); |
| 24 | + const logDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'sfge-logs-')); |
| 25 | + const resultsFile = path.join(tmp, 'resultsFile.json'); |
| 26 | + await fs.promises.writeFile(resultsFile, '[]', 'utf-8'); |
| 27 | + |
| 28 | + const logs: { level: LogLevel; msg: string }[] = []; |
| 29 | + const telemetry: TelemetryData[] = []; |
| 30 | + const wrapper = new RuntimeSfgeWrapper( |
| 31 | + new FakeJavaExec() as any, |
| 32 | + { now: () => new Date(), formatToDateTimeString: () => '' } as any, |
| 33 | + (lvl, msg) => logs.push({ level: lvl, msg }), |
| 34 | + (_name, data) => telemetry.push(data) |
| 35 | + ); |
| 36 | + |
| 37 | + const rules: SfgeRuleInfo[] = [{ name: 'ApexFlsViolation', description: '', category: '', severity: 3, url: '', isPilot: false }]; |
| 38 | + const progress: number[] = []; |
| 39 | + const res = await wrapper.invokeRunCommand(rules, [], [], { logFolder: logDir, disableLimitReachedViolations: false, threadCount: 1, threadTimeout: 1000 }, tmp, p => progress.push(p)); |
| 40 | + |
| 41 | + expect(Array.isArray(res)).toBe(true); |
| 42 | + expect(logs.find(l => l.msg.includes('hello'))).toBeTruthy(); |
| 43 | + expect(progress.some(p => p > 10)).toBe(true); // progressed via PROGRESS message branch |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | + |
0 commit comments