|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { execa } from 'execa'; |
| 3 | +import { writeFileSync, mkdtempSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { fixtures } from '@ocpp-debugkit/core'; |
| 7 | + |
| 8 | +const CLI_PATH = join(process.cwd(), 'packages/cli/dist/index.js'); |
| 9 | + |
| 10 | +// Helper: run the CLI with given args |
| 11 | +async function runCli( |
| 12 | + ...args: string[] |
| 13 | +): Promise<{ stdout: string; stderr: string; exitCode: number }> { |
| 14 | + try { |
| 15 | + const result = await execa('node', [CLI_PATH, ...args]); |
| 16 | + return { stdout: result.stdout, stderr: result.stderr, exitCode: result.exitCode ?? 0 }; |
| 17 | + } catch (e) { |
| 18 | + const error = e as { stdout?: string; stderr?: string; exitCode?: number }; |
| 19 | + return { |
| 20 | + stdout: error.stdout ?? '', |
| 21 | + stderr: error.stderr ?? '', |
| 22 | + exitCode: error.exitCode ?? 1, |
| 23 | + }; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// Helper: write a trace to a temp file |
| 28 | +function writeTempTrace(trace: unknown): string { |
| 29 | + const dir = mkdtempSync(join(tmpdir(), 'ocpp-test-')); |
| 30 | + const filePath = join(dir, 'trace.json'); |
| 31 | + writeFileSync(filePath, JSON.stringify(trace), 'utf8'); |
| 32 | + return filePath; |
| 33 | +} |
| 34 | + |
| 35 | +describe('CLI integration', () => { |
| 36 | + describe('--version', () => { |
| 37 | + it('outputs version', async () => { |
| 38 | + const result = await runCli('--version'); |
| 39 | + expect(result.exitCode).toBe(0); |
| 40 | + expect(result.stdout).toContain('0.0.0'); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('--help', () => { |
| 45 | + it('shows help with all commands', async () => { |
| 46 | + const result = await runCli('--help'); |
| 47 | + expect(result.exitCode).toBe(0); |
| 48 | + expect(result.stdout).toContain('inspect'); |
| 49 | + expect(result.stdout).toContain('report'); |
| 50 | + expect(result.stdout).toContain('scenario'); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('inspect', () => { |
| 55 | + it('inspects a normal session trace', async () => { |
| 56 | + const filePath = writeTempTrace(fixtures.normalSession); |
| 57 | + const result = await runCli('inspect', filePath); |
| 58 | + expect(result.exitCode).toBe(0); |
| 59 | + expect(result.stdout).toContain('Trace Inspection'); |
| 60 | + expect(result.stdout).toContain('Sessions: 1'); |
| 61 | + expect(result.stdout).toContain('No failures detected'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('inspects a failed-auth trace and shows failures', async () => { |
| 65 | + const filePath = writeTempTrace(fixtures.failedAuth); |
| 66 | + const result = await runCli('inspect', filePath); |
| 67 | + expect(result.exitCode).toBe(0); |
| 68 | + expect(result.stdout).toContain('FAILED_AUTHORIZATION'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('inspects a connector-fault trace and shows failures', async () => { |
| 72 | + const filePath = writeTempTrace(fixtures.connectorFault); |
| 73 | + const result = await runCli('inspect', filePath); |
| 74 | + expect(result.exitCode).toBe(0); |
| 75 | + expect(result.stdout).toContain('CONNECTOR_FAULT'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('handles missing file gracefully', async () => { |
| 79 | + const result = await runCli('inspect', '/nonexistent/file.json'); |
| 80 | + expect(result.exitCode).not.toBe(0); |
| 81 | + }); |
| 82 | + |
| 83 | + it('handles invalid JSON gracefully', async () => { |
| 84 | + const dir = mkdtempSync(join(tmpdir(), 'ocpp-test-')); |
| 85 | + const filePath = join(dir, 'bad.json'); |
| 86 | + writeFileSync(filePath, '{ invalid json }', 'utf8'); |
| 87 | + const result = await runCli('inspect', filePath); |
| 88 | + expect(result.exitCode).not.toBe(0); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('report', () => { |
| 93 | + it('generates a Markdown report to stdout', async () => { |
| 94 | + const filePath = writeTempTrace(fixtures.normalSession); |
| 95 | + const result = await runCli('report', filePath); |
| 96 | + expect(result.exitCode).toBe(0); |
| 97 | + expect(result.stdout).toContain('OCPP DebugKit — Trace Analysis Report'); |
| 98 | + expect(result.stdout).toContain('## Session Overview'); |
| 99 | + expect(result.stdout).toContain('## Failures'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('generates a report with failures', async () => { |
| 103 | + const filePath = writeTempTrace(fixtures.failedAuth); |
| 104 | + const result = await runCli('report', filePath); |
| 105 | + expect(result.exitCode).toBe(0); |
| 106 | + expect(result.stdout).toContain('FAILED_AUTHORIZATION'); |
| 107 | + expect(result.stdout).toContain('## Suggested Next Steps'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('writes report to a file with --output', async () => { |
| 111 | + const filePath = writeTempTrace(fixtures.normalSession); |
| 112 | + const dir = mkdtempSync(join(tmpdir(), 'ocpp-test-')); |
| 113 | + const outputPath = join(dir, 'report.md'); |
| 114 | + const result = await runCli('report', filePath, '--output', outputPath); |
| 115 | + expect(result.exitCode).toBe(0); |
| 116 | + expect(result.stdout).toContain('Report written to'); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('scenario list', () => { |
| 121 | + it('lists all available scenarios', async () => { |
| 122 | + const result = await runCli('scenario', 'list'); |
| 123 | + expect(result.exitCode).toBe(0); |
| 124 | + expect(result.stdout).toContain('normal-session'); |
| 125 | + expect(result.stdout).toContain('failed-auth'); |
| 126 | + expect(result.stdout).toContain('connector-fault'); |
| 127 | + expect(result.stdout).toContain('station-offline'); |
| 128 | + expect(result.stdout).toContain('unexpected-stop-reason'); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('scenario run', () => { |
| 133 | + it('runs normal-session scenario and passes', async () => { |
| 134 | + const result = await runCli('scenario', 'run', 'normal-session'); |
| 135 | + expect(result.exitCode).toBe(0); |
| 136 | + expect(result.stdout).toContain('Running Scenario: normal-session'); |
| 137 | + expect(result.stdout).toContain('No failures expected, none detected'); |
| 138 | + expect(result.stdout).toContain('PASS'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('runs failed-auth scenario and detects FAILED_AUTHORIZATION', async () => { |
| 142 | + const result = await runCli('scenario', 'run', 'failed-auth'); |
| 143 | + expect(result.exitCode).toBe(0); |
| 144 | + expect(result.stdout).toContain('FAILED_AUTHORIZATION'); |
| 145 | + expect(result.stdout).toContain('PASS'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('runs connector-fault scenario and detects CONNECTOR_FAULT', async () => { |
| 149 | + const result = await runCli('scenario', 'run', 'connector-fault'); |
| 150 | + expect(result.exitCode).toBe(0); |
| 151 | + expect(result.stdout).toContain('CONNECTOR_FAULT'); |
| 152 | + expect(result.stdout).toContain('PASS'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('runs station-offline scenario and detects STATION_OFFLINE_DURING_SESSION', async () => { |
| 156 | + const result = await runCli('scenario', 'run', 'station-offline'); |
| 157 | + expect(result.exitCode).toBe(0); |
| 158 | + expect(result.stdout).toContain('STATION_OFFLINE_DURING_SESSION'); |
| 159 | + expect(result.stdout).toContain('PASS'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('runs unexpected-stop-reason scenario (no failures)', async () => { |
| 163 | + const result = await runCli('scenario', 'run', 'unexpected-stop-reason'); |
| 164 | + expect(result.exitCode).toBe(0); |
| 165 | + expect(result.stdout).toContain('No failures expected, none detected'); |
| 166 | + expect(result.stdout).toContain('PASS'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('handles unknown scenario name', async () => { |
| 170 | + const result = await runCli('scenario', 'run', 'nonexistent'); |
| 171 | + expect(result.exitCode).not.toBe(0); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments