* Run the analytics script with a custom JSONL file by overriding the path. * We test the exported functions directly for unit tests, and use this * helper for integration-style checks.
(jsonlPath: string | null, extraArgs: string = '')
| 22 | * helper for integration-style checks. |
| 23 | */ |
| 24 | function runScript(jsonlPath: string | null, extraArgs: string = ''): string { |
| 25 | // We test via the exported functions; for CLI integration we read the file |
| 26 | // and run the pipeline manually to avoid needing to override the hardcoded path. |
| 27 | if (jsonlPath === null) { |
| 28 | return 'No analytics data found.'; |
| 29 | } |
| 30 | if (!fs.existsSync(jsonlPath)) { |
| 31 | return 'No analytics data found.'; |
| 32 | } |
| 33 | const content = fs.readFileSync(jsonlPath, 'utf-8').trim(); |
| 34 | if (!content) { |
| 35 | return 'No analytics data found.'; |
| 36 | } |
| 37 | const events = parseJSONL(content); |
| 38 | if (events.length === 0) { |
| 39 | return 'No analytics data found.'; |
| 40 | } |
| 41 | // Parse period from extraArgs |
| 42 | let period = 'all'; |
| 43 | const match = extraArgs.match(/--period\s+(\S+)/); |
| 44 | if (match) period = match[1]; |
| 45 | const filtered = filterByPeriod(events, period); |
| 46 | return formatReport(filtered, period); |
| 47 | } |
| 48 | |
| 49 | beforeEach(() => { |
| 50 | fs.mkdirSync(TMP_DIR, { recursive: true }); |
no test coverage detected