|
| 1 | +import { spawnAndCollect } from '../src/test-utils/cli-runner.js'; |
| 2 | +import { mkdtempSync, readdirSync } from 'node:fs'; |
| 3 | +import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { afterAll, describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +const testConfigDir = mkdtempSync(join(tmpdir(), 'agentcore-audit-')); |
| 9 | +const telemetryDir = join(testConfigDir, 'telemetry'); |
| 10 | +const cliPath = join(__dirname, '..', 'dist', 'cli', 'index.mjs'); |
| 11 | + |
| 12 | +function run(args: string[]) { |
| 13 | + return spawnAndCollect('node', [cliPath, ...args], tmpdir(), { |
| 14 | + AGENTCORE_SKIP_INSTALL: '1', |
| 15 | + AGENTCORE_CONFIG_DIR: testConfigDir, |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +describe('telemetry audit', () => { |
| 20 | + afterAll(() => rm(testConfigDir, { recursive: true, force: true })); |
| 21 | + |
| 22 | + it('help modes writes JSONL audit file when audit is enabled', async () => { |
| 23 | + await mkdir(testConfigDir, { recursive: true }); |
| 24 | + await writeFile(join(testConfigDir, 'config.json'), JSON.stringify({ telemetry: { audit: true } })); |
| 25 | + |
| 26 | + const result = await run(['help', 'modes']); |
| 27 | + expect(result.exitCode).toBe(0); |
| 28 | + |
| 29 | + const files = readdirSync(telemetryDir).filter(f => f.startsWith('help-')); |
| 30 | + expect(files).toHaveLength(1); |
| 31 | + |
| 32 | + const content = await readFile(join(telemetryDir, files[0]!), 'utf-8'); |
| 33 | + const entry = JSON.parse(content.trim()); |
| 34 | + expect(entry.attrs).toMatchObject({ |
| 35 | + command_group: 'help', |
| 36 | + command: 'help.modes', |
| 37 | + exit_reason: 'success', |
| 38 | + }); |
| 39 | + expect(entry.value).toBeGreaterThanOrEqual(0); |
| 40 | + }); |
| 41 | + |
| 42 | + it('help modes does not write audit file when audit is disabled', async () => { |
| 43 | + await mkdir(testConfigDir, { recursive: true }); |
| 44 | + await writeFile(join(testConfigDir, 'config.json'), JSON.stringify({ telemetry: { audit: false } })); |
| 45 | + await rm(telemetryDir, { recursive: true, force: true }); |
| 46 | + |
| 47 | + const result = await run(['help', 'modes']); |
| 48 | + expect(result.exitCode).toBe(0); |
| 49 | + |
| 50 | + try { |
| 51 | + const files = readdirSync(telemetryDir); |
| 52 | + expect(files).toHaveLength(0); |
| 53 | + } catch { |
| 54 | + // telemetry dir doesn't exist — that's correct |
| 55 | + } |
| 56 | + }); |
| 57 | +}); |
0 commit comments