|
| 1 | +/** |
| 2 | + * Tests for CLI Command Trace Wrapper |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 6 | +import { Command } from 'commander'; |
| 7 | +import { |
| 8 | + wrapCommand, |
| 9 | + wrapProgram, |
| 10 | + traceStep, |
| 11 | + traceQuery, |
| 12 | + traceAPI, |
| 13 | +} from '../cli-trace-wrapper.js'; |
| 14 | + |
| 15 | +describe('cli-trace-wrapper', () => { |
| 16 | + beforeEach(() => { |
| 17 | + delete process.env['DEBUG_TRACE']; |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + vi.restoreAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('traceStep', () => { |
| 25 | + it('should execute and return the async function result', async () => { |
| 26 | + const result = await traceStep('my-step', async () => 42); |
| 27 | + expect(result).toBe(42); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should propagate errors from the function', async () => { |
| 31 | + await expect( |
| 32 | + traceStep('failing-step', async () => { |
| 33 | + throw new Error('step failed'); |
| 34 | + }) |
| 35 | + ).rejects.toThrow('step failed'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should handle async operations', async () => { |
| 39 | + const result = await traceStep('async-step', async () => { |
| 40 | + return new Promise<string>((resolve) => { |
| 41 | + setTimeout(() => resolve('delayed'), 10); |
| 42 | + }); |
| 43 | + }); |
| 44 | + expect(result).toBe('delayed'); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('traceQuery', () => { |
| 49 | + it('should execute and return the sync function result', () => { |
| 50 | + const result = traceQuery('SELECT 1', {}, () => [{ val: 1 }]); |
| 51 | + expect(result).toEqual([{ val: 1 }]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should propagate errors from the function', () => { |
| 55 | + expect(() => |
| 56 | + traceQuery('BAD QUERY', {}, () => { |
| 57 | + throw new Error('query failed'); |
| 58 | + }) |
| 59 | + ).toThrow('query failed'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle long SQL by truncating name', () => { |
| 63 | + const longSql = 'SELECT ' + 'a, '.repeat(100) + 'b FROM table'; |
| 64 | + const result = traceQuery(longSql, { param: 1 }, () => 'ok'); |
| 65 | + expect(result).toBe('ok'); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('traceAPI', () => { |
| 70 | + it('should execute and return the async function result', async () => { |
| 71 | + const result = await traceAPI('GET', '/api/users', null, async () => ({ |
| 72 | + users: [], |
| 73 | + })); |
| 74 | + expect(result).toEqual({ users: [] }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should propagate errors from the function', async () => { |
| 78 | + await expect( |
| 79 | + traceAPI('POST', '/api/fail', { data: 1 }, async () => { |
| 80 | + throw new Error('api failed'); |
| 81 | + }) |
| 82 | + ).rejects.toThrow('api failed'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should handle different HTTP methods', async () => { |
| 86 | + for (const method of ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']) { |
| 87 | + const result = await traceAPI( |
| 88 | + method, |
| 89 | + '/api/test', |
| 90 | + null, |
| 91 | + async () => method |
| 92 | + ); |
| 93 | + expect(result).toBe(method); |
| 94 | + } |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('wrapCommand', () => { |
| 99 | + it('should return the same command instance', () => { |
| 100 | + const cmd = new Command('test'); |
| 101 | + cmd.action(() => {}); |
| 102 | + const wrapped = wrapCommand(cmd); |
| 103 | + expect(wrapped).toBe(cmd); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should recursively wrap subcommands', () => { |
| 107 | + const parent = new Command('parent'); |
| 108 | + parent.action(() => {}); |
| 109 | + |
| 110 | + const child = new Command('child'); |
| 111 | + child.action(() => {}); |
| 112 | + parent.addCommand(child); |
| 113 | + |
| 114 | + const wrapped = wrapCommand(parent); |
| 115 | + expect(wrapped.commands).toHaveLength(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should preserve command name and properties', () => { |
| 119 | + const cmd = new Command('my-cmd'); |
| 120 | + cmd.description('A test command'); |
| 121 | + cmd.option('-v, --verbose', 'Verbose output'); |
| 122 | + cmd.action(() => {}); |
| 123 | + |
| 124 | + wrapCommand(cmd); |
| 125 | + expect(cmd.name()).toBe('my-cmd'); |
| 126 | + expect(cmd.description()).toBe('A test command'); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('wrapProgram', () => { |
| 131 | + it('should return the same program instance', () => { |
| 132 | + const program = new Command('stackmemory'); |
| 133 | + const wrapped = wrapProgram(program); |
| 134 | + expect(wrapped).toBe(program); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should wrap all existing commands', () => { |
| 138 | + const program = new Command('stackmemory'); |
| 139 | + const sub1 = new Command('capture'); |
| 140 | + sub1.action(() => {}); |
| 141 | + const sub2 = new Command('restore'); |
| 142 | + sub2.action(() => {}); |
| 143 | + |
| 144 | + program.addCommand(sub1); |
| 145 | + program.addCommand(sub2); |
| 146 | + |
| 147 | + wrapProgram(program); |
| 148 | + expect(program.commands).toHaveLength(2); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should add exit override', () => { |
| 152 | + const program = new Command('stackmemory'); |
| 153 | + wrapProgram(program); |
| 154 | + // exitOverride is configured internally; verify program is still functional |
| 155 | + expect(program.name()).toBe('stackmemory'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should add pre and post action hooks', () => { |
| 159 | + const program = new Command('stackmemory'); |
| 160 | + wrapProgram(program); |
| 161 | + // Hooks are registered but we can verify the program is configured |
| 162 | + expect(program).toBeDefined(); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments