|
| 1 | +import { registerFetch } from '../command'; |
| 2 | +import { Command } from '@commander-js/extra-typings'; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +const mockFetchGatewayToken = vi.fn(); |
| 6 | +const mockListGateways = vi.fn(); |
| 7 | +const mockRequireProject = vi.fn(); |
| 8 | +const mockRender = vi.fn(); |
| 9 | + |
| 10 | +vi.mock('../../../operations/fetch-access', () => ({ |
| 11 | + fetchGatewayToken: (...args: unknown[]) => mockFetchGatewayToken(...args), |
| 12 | + listGateways: (...args: unknown[]) => mockListGateways(...args), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('../../../tui/guards', () => ({ |
| 16 | + requireProject: (...args: unknown[]) => mockRequireProject(...args), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('ink', () => ({ |
| 20 | + render: (...args: unknown[]) => mockRender(...args), |
| 21 | + Box: 'Box', |
| 22 | + Text: 'Text', |
| 23 | +})); |
| 24 | + |
| 25 | +const jwtResult = { |
| 26 | + url: 'https://gw.example.com', |
| 27 | + authType: 'CUSTOM_JWT', |
| 28 | + token: 'test-token', |
| 29 | + expiresIn: 3600, |
| 30 | +}; |
| 31 | + |
| 32 | +const noneResult = { |
| 33 | + url: 'https://gw.example.com', |
| 34 | + authType: 'NONE', |
| 35 | + message: 'No authentication required.', |
| 36 | +}; |
| 37 | + |
| 38 | +describe('registerFetch', () => { |
| 39 | + let program: Command; |
| 40 | + let mockExit: ReturnType<typeof vi.spyOn>; |
| 41 | + let mockLog: ReturnType<typeof vi.spyOn>; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + program = new Command(); |
| 45 | + program.exitOverride(); |
| 46 | + registerFetch(program); |
| 47 | + |
| 48 | + mockExit = vi.spyOn(process, 'exit').mockImplementation(() => { |
| 49 | + throw new Error('process.exit'); |
| 50 | + }); |
| 51 | + mockLog = vi.spyOn(console, 'log').mockImplementation(() => undefined); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + mockExit.mockRestore(); |
| 56 | + mockLog.mockRestore(); |
| 57 | + vi.clearAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('registers fetch command with access subcommand', () => { |
| 61 | + const fetchCmd = program.commands.find(c => c.name() === 'fetch'); |
| 62 | + expect(fetchCmd).toBeDefined(); |
| 63 | + |
| 64 | + const accessCmd = fetchCmd!.commands.find(c => c.name() === 'access'); |
| 65 | + expect(accessCmd).toBeDefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('outputs valid JSON for CUSTOM_JWT result when --json flag is used', async () => { |
| 69 | + mockFetchGatewayToken.mockResolvedValue(jwtResult); |
| 70 | + |
| 71 | + await program.parseAsync(['fetch', 'access', '--name', 'myGateway', '--json'], { from: 'user' }); |
| 72 | + |
| 73 | + expect(mockLog).toHaveBeenCalledTimes(1); |
| 74 | + const output = JSON.parse(mockLog.mock.calls[0][0]); |
| 75 | + expect(output.success).toBe(true); |
| 76 | + expect(output.url).toBe('https://gw.example.com'); |
| 77 | + expect(output.authType).toBe('CUSTOM_JWT'); |
| 78 | + expect(output.token).toBe('test-token'); |
| 79 | + expect(output.expiresIn).toBe(3600); |
| 80 | + }); |
| 81 | + |
| 82 | + it('outputs valid JSON with no token field for NONE gateway when --json flag is used', async () => { |
| 83 | + mockFetchGatewayToken.mockResolvedValue(noneResult); |
| 84 | + |
| 85 | + await program.parseAsync(['fetch', 'access', '--name', 'myGateway', '--json'], { from: 'user' }); |
| 86 | + |
| 87 | + expect(mockLog).toHaveBeenCalledTimes(1); |
| 88 | + const output = JSON.parse(mockLog.mock.calls[0][0]); |
| 89 | + expect(output.success).toBe(true); |
| 90 | + expect(output.url).toBe('https://gw.example.com'); |
| 91 | + expect(output.authType).toBe('NONE'); |
| 92 | + expect(output.token).toBeUndefined(); |
| 93 | + expect(output.message).toBe('No authentication required.'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('shows error with available gateways when --name is missing and gateways exist', async () => { |
| 97 | + mockListGateways.mockResolvedValue([ |
| 98 | + { name: 'gateway-one', authType: 'CUSTOM_JWT' }, |
| 99 | + { name: 'gateway-two', authType: 'NONE' }, |
| 100 | + ]); |
| 101 | + |
| 102 | + await expect(program.parseAsync(['fetch', 'access'], { from: 'user' })).rejects.toThrow('process.exit'); |
| 103 | + |
| 104 | + expect(mockRender).toHaveBeenCalled(); |
| 105 | + const renderArg = mockRender.mock.calls[0]![0]; |
| 106 | + expect(JSON.stringify(renderArg)).toContain('Missing required option'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('shows deploy message when --name is missing and no gateways are deployed', async () => { |
| 110 | + mockListGateways.mockResolvedValue([]); |
| 111 | + |
| 112 | + await expect(program.parseAsync(['fetch', 'access'], { from: 'user' })).rejects.toThrow('process.exit'); |
| 113 | + |
| 114 | + expect(mockRender).toHaveBeenCalled(); |
| 115 | + const renderArg = mockRender.mock.calls[0]![0]; |
| 116 | + expect(JSON.stringify(renderArg)).toContain('agentcore deploy'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('outputs JSON error with available gateways when --name is missing and --json flag is used', async () => { |
| 120 | + mockListGateways.mockResolvedValue([ |
| 121 | + { name: 'gateway-one', authType: 'CUSTOM_JWT' }, |
| 122 | + { name: 'gateway-two', authType: 'NONE' }, |
| 123 | + ]); |
| 124 | + |
| 125 | + await expect(program.parseAsync(['fetch', 'access', '--json'], { from: 'user' })).rejects.toThrow('process.exit'); |
| 126 | + |
| 127 | + expect(mockLog).toHaveBeenCalledTimes(1); |
| 128 | + const output = JSON.parse(mockLog.mock.calls[0][0]); |
| 129 | + expect(output.success).toBe(false); |
| 130 | + expect(output.error).toBe('Missing required option: --name'); |
| 131 | + expect(output.availableGateways).toEqual([ |
| 132 | + { name: 'gateway-one', authType: 'CUSTOM_JWT' }, |
| 133 | + { name: 'gateway-two', authType: 'NONE' }, |
| 134 | + ]); |
| 135 | + expect(mockRender).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('outputs JSON deploy message when --name is missing, --json flag is used, and no gateways deployed', async () => { |
| 139 | + mockListGateways.mockResolvedValue([]); |
| 140 | + |
| 141 | + await expect(program.parseAsync(['fetch', 'access', '--json'], { from: 'user' })).rejects.toThrow('process.exit'); |
| 142 | + |
| 143 | + expect(mockLog).toHaveBeenCalledTimes(1); |
| 144 | + const output = JSON.parse(mockLog.mock.calls[0][0]); |
| 145 | + expect(output.success).toBe(false); |
| 146 | + expect(output.error).toContain('agentcore deploy'); |
| 147 | + expect(output.availableGateways).toBeUndefined(); |
| 148 | + expect(mockRender).not.toHaveBeenCalled(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('outputs JSON error when fetchGatewayToken throws and --json flag is used', async () => { |
| 152 | + mockFetchGatewayToken.mockRejectedValue(new Error('Token fetch failed')); |
| 153 | + |
| 154 | + await expect( |
| 155 | + program.parseAsync(['fetch', 'access', '--name', 'myGateway', '--json'], { from: 'user' }) |
| 156 | + ).rejects.toThrow('process.exit'); |
| 157 | + |
| 158 | + expect(mockLog).toHaveBeenCalledTimes(1); |
| 159 | + const output = JSON.parse(mockLog.mock.calls[0][0]); |
| 160 | + expect(output.success).toBe(false); |
| 161 | + expect(output.error).toBe('Token fetch failed'); |
| 162 | + expect(mockRender).not.toHaveBeenCalled(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('shows error message when fetchGatewayToken throws', async () => { |
| 166 | + mockFetchGatewayToken.mockRejectedValue(new Error('Token fetch failed')); |
| 167 | + |
| 168 | + await expect(program.parseAsync(['fetch', 'access', '--name', 'myGateway'], { from: 'user' })).rejects.toThrow( |
| 169 | + 'process.exit' |
| 170 | + ); |
| 171 | + |
| 172 | + expect(mockRender).toHaveBeenCalled(); |
| 173 | + const renderArg = mockRender.mock.calls[0]![0]; |
| 174 | + expect(JSON.stringify(renderArg)).toContain('Token fetch failed'); |
| 175 | + }); |
| 176 | +}); |
0 commit comments