|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { execFile } from 'node:child_process'; |
| 4 | +import { createServer } from 'node:http'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | +import { dirname, join } from 'node:path'; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | +const CLI = join(__dirname, '..', 'bin', 'cli.mjs'); |
| 10 | + |
| 11 | +function startMockServer(responseData) { |
| 12 | + return new Promise((resolve) => { |
| 13 | + const server = createServer((req, res) => { |
| 14 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 15 | + res.end(JSON.stringify(responseData)); |
| 16 | + }); |
| 17 | + server.listen(0, '127.0.0.1', () => { |
| 18 | + const port = server.address().port; |
| 19 | + resolve({ server, url: `http://127.0.0.1:${port}` }); |
| 20 | + }); |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +function runCli(args, env) { |
| 25 | + return new Promise((resolve) => { |
| 26 | + execFile('node', [CLI, ...args], { timeout: 5000, env: { ...process.env, ...env } }, (err, stdout, stderr) => { |
| 27 | + resolve({ code: err ? err.code : 0, stdout, stderr }); |
| 28 | + }); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +describe('stats command rendering', () => { |
| 33 | + it('renders timeSeries as daily chart', async () => { |
| 34 | + const { server, url } = await startMockServer({ |
| 35 | + totals: { total_events: 100, unique_users: 10 }, |
| 36 | + events: [{ event: 'page_view', count: 100, unique_users: 10 }], |
| 37 | + timeSeries: [ |
| 38 | + { date: '2026-02-25', total_events: 40, unique_users: 5 }, |
| 39 | + { date: '2026-02-26', total_events: 60, unique_users: 8 }, |
| 40 | + ], |
| 41 | + }); |
| 42 | + |
| 43 | + try { |
| 44 | + const { stdout } = await runCli(['stats', 'test-project'], { |
| 45 | + AGENT_ANALYTICS_API_KEY: 'aak_test', |
| 46 | + AGENT_ANALYTICS_URL: url, |
| 47 | + }); |
| 48 | + |
| 49 | + // Should render the daily chart section |
| 50 | + assert.ok(stdout.includes('Daily:'), 'should show Daily heading'); |
| 51 | + assert.ok(stdout.includes('2026-02-25'), 'should show first date'); |
| 52 | + assert.ok(stdout.includes('2026-02-26'), 'should show second date'); |
| 53 | + assert.ok(stdout.includes('40 events'), 'should show first day count'); |
| 54 | + assert.ok(stdout.includes('60 events'), 'should show second day count'); |
| 55 | + } finally { |
| 56 | + server.close(); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + it('does not crash when timeSeries is absent', async () => { |
| 61 | + const { server, url } = await startMockServer({ |
| 62 | + totals: { total_events: 5, unique_users: 1 }, |
| 63 | + events: [], |
| 64 | + }); |
| 65 | + |
| 66 | + try { |
| 67 | + const { code, stdout } = await runCli(['stats', 'test-project'], { |
| 68 | + AGENT_ANALYTICS_API_KEY: 'aak_test', |
| 69 | + AGENT_ANALYTICS_URL: url, |
| 70 | + }); |
| 71 | + |
| 72 | + assert.equal(code, 0); |
| 73 | + assert.ok(!stdout.includes('Daily:'), 'should not show Daily when no timeSeries'); |
| 74 | + } finally { |
| 75 | + server.close(); |
| 76 | + } |
| 77 | + }); |
| 78 | +}); |
0 commit comments