|
| 1 | +import { spawnAndCollect } from '../src/test-utils/cli-runner.js'; |
1 | 2 | import { runCLI } from '../src/test-utils/index.js'; |
2 | | -import { describe, expect, it } from 'vitest'; |
| 3 | +import { readdirSync } from 'node:fs'; |
| 4 | +import { mkdir, readFile, rm } from 'node:fs/promises'; |
| 5 | +import { tmpdir } from 'node:os'; |
| 6 | +import { join } from 'node:path'; |
| 7 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
3 | 8 |
|
4 | 9 | const COMMANDS = [ |
5 | 10 | 'create', |
@@ -38,3 +43,55 @@ describe('CLI help', () => { |
38 | 43 | } |
39 | 44 | }); |
40 | 45 | }); |
| 46 | + |
| 47 | +describe('help modes telemetry', () => { |
| 48 | + let testConfigDir: string; |
| 49 | + const cliPath = join(__dirname, '..', 'dist', 'cli', 'index.mjs'); |
| 50 | + |
| 51 | + beforeAll(async () => { |
| 52 | + testConfigDir = join(tmpdir(), `agentcore-help-telemetry-${Date.now()}`); |
| 53 | + await mkdir(testConfigDir, { recursive: true }); |
| 54 | + }); |
| 55 | + afterAll(() => rm(testConfigDir, { recursive: true, force: true })); |
| 56 | + |
| 57 | + function run(args: string[], extraEnv: Record<string, string> = {}) { |
| 58 | + return spawnAndCollect('node', [cliPath, ...args], tmpdir(), { |
| 59 | + AGENTCORE_SKIP_INSTALL: '1', |
| 60 | + AGENTCORE_CONFIG_DIR: testConfigDir, |
| 61 | + ...extraEnv, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + it('writes JSONL audit file when audit is enabled via env var', async () => { |
| 66 | + const result = await run(['help', 'modes'], { AGENTCORE_TELEMETRY_AUDIT: '1' }); |
| 67 | + expect(result.exitCode).toBe(0); |
| 68 | + |
| 69 | + const telemetryDir = join(testConfigDir, 'telemetry'); |
| 70 | + const files = readdirSync(telemetryDir).filter(f => f.startsWith('help-')); |
| 71 | + expect(files).toHaveLength(1); |
| 72 | + |
| 73 | + const content = await readFile(join(telemetryDir, files[0]!), 'utf-8'); |
| 74 | + const entry = JSON.parse(content.trim()); |
| 75 | + expect(entry.attrs).toMatchObject({ |
| 76 | + command_group: 'help', |
| 77 | + command: 'help.modes', |
| 78 | + exit_reason: 'success', |
| 79 | + }); |
| 80 | + expect(entry.value).toBeGreaterThanOrEqual(0); |
| 81 | + }); |
| 82 | + |
| 83 | + it('does not write audit file when audit is not enabled', async () => { |
| 84 | + const telemetryDir = join(testConfigDir, 'telemetry'); |
| 85 | + await rm(telemetryDir, { recursive: true, force: true }); |
| 86 | + |
| 87 | + const result = await run(['help', 'modes']); |
| 88 | + expect(result.exitCode).toBe(0); |
| 89 | + |
| 90 | + try { |
| 91 | + const files = readdirSync(telemetryDir); |
| 92 | + expect(files).toHaveLength(0); |
| 93 | + } catch { |
| 94 | + // telemetry dir doesn't exist — correct |
| 95 | + } |
| 96 | + }); |
| 97 | +}); |
0 commit comments