|
| 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(handler) { |
| 12 | + return new Promise((resolve) => { |
| 13 | + const server = createServer((req, res) => { |
| 14 | + res.writeHead(200, { 'Content-Type': 'application/json' }); |
| 15 | + res.end(JSON.stringify(handler(req))); |
| 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('bot-traffic command rendering', () => { |
| 33 | + it('renders project bot traffic summaries', async () => { |
| 34 | + const { server, url } = await startMockServer(() => ({ |
| 35 | + scope: 'project', |
| 36 | + project: 'test-site', |
| 37 | + period: { label: '7d', from: '2026-03-07', to: '2026-03-13' }, |
| 38 | + summary: { |
| 39 | + automated_requests: { current: 5, previous: 2, change: 3, change_pct: 150 }, |
| 40 | + dropped_events: { current: 8, previous: 3, change: 5, change_pct: 167 }, |
| 41 | + last_seen_at: 1773370000000, |
| 42 | + }, |
| 43 | + categories: [ |
| 44 | + { category: 'ai_agent', requests: 3, dropped_events: 6, share_pct: 60 }, |
| 45 | + { category: 'search_crawler', requests: 2, dropped_events: 2, share_pct: 40 }, |
| 46 | + ], |
| 47 | + actors: [ |
| 48 | + { actor: 'ChatGPT-User', category: 'ai_agent', requests: 3, dropped_events: 6, last_seen_at: 1773370000000 }, |
| 49 | + ], |
| 50 | + time_series: [ |
| 51 | + { date: '2026-03-13', requests: 5, dropped_events: 8 }, |
| 52 | + ], |
| 53 | + })); |
| 54 | + |
| 55 | + try { |
| 56 | + const { code, stdout } = await runCli(['bot-traffic', 'test-site'], { |
| 57 | + AGENT_ANALYTICS_API_KEY: 'aak_test', |
| 58 | + AGENT_ANALYTICS_URL: url, |
| 59 | + }); |
| 60 | + |
| 61 | + assert.equal(code, 0); |
| 62 | + assert.ok(stdout.includes('Bot Traffic: test-site')); |
| 63 | + assert.ok(stdout.includes('Automated requests')); |
| 64 | + assert.ok(stdout.includes('Dropped events')); |
| 65 | + assert.ok(stdout.includes('ChatGPT-User')); |
| 66 | + assert.ok(stdout.includes('ai_agent')); |
| 67 | + } finally { |
| 68 | + server.close(); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('renders account bot traffic summaries with --all', async () => { |
| 73 | + const { server, url } = await startMockServer(() => ({ |
| 74 | + scope: 'account', |
| 75 | + period: { label: '7d', from: '2026-03-07', to: '2026-03-13' }, |
| 76 | + summary: { |
| 77 | + automated_requests: { current: 7, previous: 4, change: 3, change_pct: 75 }, |
| 78 | + dropped_events: { current: 10, previous: 5, change: 5, change_pct: 100 }, |
| 79 | + active_projects: 2, |
| 80 | + total_projects: 3, |
| 81 | + last_seen_at: 1773370000000, |
| 82 | + }, |
| 83 | + categories: [ |
| 84 | + { category: 'ai_agent', requests: 4, dropped_events: 7, share_pct: 57.1 }, |
| 85 | + ], |
| 86 | + projects: [ |
| 87 | + { name: 'test-site', requests: 4, dropped_events: 7, share_pct: 57.1, last_seen_at: '2026-03-13' }, |
| 88 | + ], |
| 89 | + remaining_projects: 0, |
| 90 | + time_series: [ |
| 91 | + { date: '2026-03-13', requests: 7, dropped_events: 10 }, |
| 92 | + ], |
| 93 | + })); |
| 94 | + |
| 95 | + try { |
| 96 | + const { code, stdout } = await runCli(['bot-traffic', '--all'], { |
| 97 | + AGENT_ANALYTICS_API_KEY: 'aak_test', |
| 98 | + AGENT_ANALYTICS_URL: url, |
| 99 | + }); |
| 100 | + |
| 101 | + assert.equal(code, 0); |
| 102 | + assert.ok(stdout.includes('All Sites Bot Traffic')); |
| 103 | + assert.ok(stdout.includes('2 active / 3 total')); |
| 104 | + assert.ok(stdout.includes('test-site')); |
| 105 | + } finally { |
| 106 | + server.close(); |
| 107 | + } |
| 108 | + }); |
| 109 | +}); |
0 commit comments