|
| 1 | +import List from '../../../src/subCommands/list'; |
| 2 | +import FC from '../../../src/resources/fc'; |
| 3 | +import { IInputs } from '../../../src/interface'; |
| 4 | +import { tableShow } from '../../../src/utils'; |
| 5 | + |
| 6 | +// Mock dependencies |
| 7 | +jest.mock('../../../src/resources/fc'); |
| 8 | +jest.mock('../../../src/logger', () => ({ |
| 9 | + _set: jest.fn(), |
| 10 | + debug: jest.fn(), |
| 11 | + info: jest.fn(), |
| 12 | + error: jest.fn(), |
| 13 | + warn: jest.fn(), |
| 14 | + log: jest.fn(), |
| 15 | +})); |
| 16 | +jest.mock('../../../src/utils', () => ({ |
| 17 | + tableShow: jest.fn(), |
| 18 | + isAppCenter: jest.fn(), |
| 19 | + getUserAgent: jest.fn((userAgent, command) => { |
| 20 | + return ( |
| 21 | + userAgent || |
| 22 | + `Component:fc3;Nodejs:${process.version};OS:${process.platform}-${process.arch};command:${command}` |
| 23 | + ); |
| 24 | + }), |
| 25 | +})); |
| 26 | + |
| 27 | +describe('List', () => { |
| 28 | + let list: List; |
| 29 | + let mockInputs: IInputs; |
| 30 | + let mockFcSdk: jest.Mocked<FC>; |
| 31 | + |
| 32 | + const mockFunctionsArray = [ |
| 33 | + { |
| 34 | + functionName: 'test-func-1', |
| 35 | + runtime: 'nodejs18', |
| 36 | + handler: 'index.handler', |
| 37 | + memorySize: 128, |
| 38 | + state: 'Active', |
| 39 | + lastModifiedTime: '2024-01-01T00:00:00Z', |
| 40 | + }, |
| 41 | + { |
| 42 | + functionName: 'test-func-2', |
| 43 | + runtime: 'python3.10', |
| 44 | + handler: 'main.handler', |
| 45 | + memorySize: 256, |
| 46 | + state: 'Active', |
| 47 | + lastModifiedTime: '2024-01-02T00:00:00Z', |
| 48 | + }, |
| 49 | + ]; |
| 50 | + |
| 51 | + const mockFunctionsPageBody = { |
| 52 | + functions: mockFunctionsArray, |
| 53 | + nextToken: 'next-token-123', |
| 54 | + }; |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + mockInputs = { |
| 58 | + props: { |
| 59 | + region: 'cn-hangzhou', |
| 60 | + }, |
| 61 | + credential: { |
| 62 | + AccountID: 'test-account', |
| 63 | + AccessKeyID: 'test-access-key-id', |
| 64 | + AccessKeySecret: 'test-access-key-secret', |
| 65 | + }, |
| 66 | + args: [], |
| 67 | + } as any; |
| 68 | + |
| 69 | + mockFcSdk = new FC(mockInputs.props.region, mockInputs.credential, {}) as jest.Mocked<FC>; |
| 70 | + (FC as unknown as jest.Mock).mockImplementation(() => mockFcSdk); |
| 71 | + }); |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + jest.clearAllMocks(); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('constructor', () => { |
| 78 | + it('should create List instance with valid inputs', () => { |
| 79 | + mockInputs.args = []; |
| 80 | + list = new List(mockInputs); |
| 81 | + expect(list).toBeInstanceOf(List); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should throw error when region is not specified', () => { |
| 85 | + delete mockInputs.props.region; |
| 86 | + mockInputs.args = []; |
| 87 | + expect(() => new List(mockInputs)).toThrow('Region not specified, please specify --region'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should use region from command line args over props', () => { |
| 91 | + mockInputs.args = ['--region', 'cn-beijing']; |
| 92 | + list = new List(mockInputs); |
| 93 | + expect(list).toBeInstanceOf(List); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('run - without limit (auto-pagination)', () => { |
| 98 | + beforeEach(() => { |
| 99 | + mockInputs.args = []; |
| 100 | + list = new List(mockInputs); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should list all functions via auto-pagination when no limit specified', async () => { |
| 104 | + mockFcSdk.listFunctions = jest.fn().mockResolvedValue(mockFunctionsArray); |
| 105 | + |
| 106 | + const result = await list.run(); |
| 107 | + expect(mockFcSdk.listFunctions).toHaveBeenCalledWith(undefined); |
| 108 | + expect(mockFcSdk.listFunctionsPage).not.toHaveBeenCalled(); |
| 109 | + expect(result).toEqual({ functions: mockFunctionsArray }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should list all functions with prefix filter via auto-pagination', async () => { |
| 113 | + mockInputs.args = ['--prefix', 'test']; |
| 114 | + list = new List(mockInputs); |
| 115 | + mockFcSdk.listFunctions = jest.fn().mockResolvedValue(mockFunctionsArray); |
| 116 | + |
| 117 | + const result = await list.run(); |
| 118 | + expect(mockFcSdk.listFunctions).toHaveBeenCalledWith('test'); |
| 119 | + expect(result).toEqual({ functions: mockFunctionsArray }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should show table output without limit', async () => { |
| 123 | + mockInputs.args = ['--table']; |
| 124 | + list = new List(mockInputs); |
| 125 | + mockFcSdk.listFunctions = jest.fn().mockResolvedValue(mockFunctionsArray); |
| 126 | + |
| 127 | + await list.run(); |
| 128 | + expect(tableShow).toHaveBeenCalledWith(mockFunctionsArray, [ |
| 129 | + 'functionName', |
| 130 | + 'runtime', |
| 131 | + 'handler', |
| 132 | + 'memorySize', |
| 133 | + 'state', |
| 134 | + 'lastModifiedTime', |
| 135 | + ]); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should handle empty functions list via auto-pagination', async () => { |
| 139 | + mockFcSdk.listFunctions = jest.fn().mockResolvedValue([]); |
| 140 | + |
| 141 | + const result = await list.run(); |
| 142 | + expect(result).toEqual({ functions: [] }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should not call tableShow when --table is not specified', async () => { |
| 146 | + mockFcSdk.listFunctions = jest.fn().mockResolvedValue(mockFunctionsArray); |
| 147 | + |
| 148 | + await list.run(); |
| 149 | + expect(tableShow).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe('run - with limit (single page)', () => { |
| 154 | + it('should list functions with custom limit via single page', async () => { |
| 155 | + mockInputs.args = ['--limit', '20']; |
| 156 | + list = new List(mockInputs); |
| 157 | + mockFcSdk.listFunctionsPage = jest.fn().mockResolvedValue(mockFunctionsPageBody); |
| 158 | + |
| 159 | + const result = await list.run(); |
| 160 | + expect(mockFcSdk.listFunctionsPage).toHaveBeenCalledWith(20, undefined, undefined); |
| 161 | + expect(mockFcSdk.listFunctions).not.toHaveBeenCalled(); |
| 162 | + expect(result).toEqual(mockFunctionsPageBody); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should reject --limit with value 0', async () => { |
| 166 | + mockInputs.args = ['--limit', '0']; |
| 167 | + list = new List(mockInputs); |
| 168 | + |
| 169 | + await expect(list.run()).rejects.toThrow('--limit must be a positive integer'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should reject --limit with non-integer value', async () => { |
| 173 | + mockInputs.args = ['--limit', 'abc']; |
| 174 | + list = new List(mockInputs); |
| 175 | + |
| 176 | + await expect(list.run()).rejects.toThrow('--limit must be a positive integer'); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should list functions with prefix and limit', async () => { |
| 180 | + mockInputs.args = ['--prefix', 'test', '--limit', '20']; |
| 181 | + list = new List(mockInputs); |
| 182 | + mockFcSdk.listFunctionsPage = jest.fn().mockResolvedValue(mockFunctionsPageBody); |
| 183 | + |
| 184 | + const result = await list.run(); |
| 185 | + expect(mockFcSdk.listFunctionsPage).toHaveBeenCalledWith(20, 'test', undefined); |
| 186 | + expect(result).toEqual(mockFunctionsPageBody); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should list functions with nextToken for pagination', async () => { |
| 190 | + mockInputs.args = ['--limit', '20', '--next-token', 'next-token-123']; |
| 191 | + list = new List(mockInputs); |
| 192 | + mockFcSdk.listFunctionsPage = jest.fn().mockResolvedValue(mockFunctionsPageBody); |
| 193 | + |
| 194 | + const result = await list.run(); |
| 195 | + expect(mockFcSdk.listFunctionsPage).toHaveBeenCalledWith(20, undefined, 'next-token-123'); |
| 196 | + expect(result).toEqual(mockFunctionsPageBody); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should list functions with all query parameters', async () => { |
| 200 | + mockInputs.args = ['--prefix', 'test', '--limit', '10', '--next-token', 'token-abc']; |
| 201 | + list = new List(mockInputs); |
| 202 | + mockFcSdk.listFunctionsPage = jest.fn().mockResolvedValue(mockFunctionsPageBody); |
| 203 | + |
| 204 | + const result = await list.run(); |
| 205 | + expect(mockFcSdk.listFunctionsPage).toHaveBeenCalledWith(10, 'test', 'token-abc'); |
| 206 | + expect(result).toEqual(mockFunctionsPageBody); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should show table output with limit', async () => { |
| 210 | + mockInputs.args = ['--limit', '20', '--table']; |
| 211 | + list = new List(mockInputs); |
| 212 | + mockFcSdk.listFunctionsPage = jest.fn().mockResolvedValue(mockFunctionsPageBody); |
| 213 | + |
| 214 | + await list.run(); |
| 215 | + expect(tableShow).toHaveBeenCalledWith(mockFunctionsArray, [ |
| 216 | + 'functionName', |
| 217 | + 'runtime', |
| 218 | + 'handler', |
| 219 | + 'memorySize', |
| 220 | + 'state', |
| 221 | + 'lastModifiedTime', |
| 222 | + ]); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should handle table output with empty functions via single page', async () => { |
| 226 | + mockInputs.args = ['--limit', '20', '--table']; |
| 227 | + list = new List(mockInputs); |
| 228 | + mockFcSdk.listFunctionsPage = jest.fn().mockResolvedValue({ |
| 229 | + functions: [], |
| 230 | + }); |
| 231 | + |
| 232 | + await list.run(); |
| 233 | + expect(tableShow).toHaveBeenCalledWith( |
| 234 | + [], |
| 235 | + ['functionName', 'runtime', 'handler', 'memorySize', 'state', 'lastModifiedTime'], |
| 236 | + ); |
| 237 | + }); |
| 238 | + }); |
| 239 | + |
| 240 | + describe('run - error handling', () => { |
| 241 | + it('should propagate auto-pagination API errors', async () => { |
| 242 | + mockInputs.args = []; |
| 243 | + list = new List(mockInputs); |
| 244 | + mockFcSdk.listFunctions = jest.fn().mockRejectedValue(new Error('API error')); |
| 245 | + |
| 246 | + await expect(list.run()).rejects.toThrow('API error'); |
| 247 | + }); |
| 248 | + |
| 249 | + it('should propagate single page API errors', async () => { |
| 250 | + mockInputs.args = ['--limit', '20']; |
| 251 | + list = new List(mockInputs); |
| 252 | + mockFcSdk.listFunctionsPage = jest.fn().mockRejectedValue(new Error('API error')); |
| 253 | + |
| 254 | + await expect(list.run()).rejects.toThrow('API error'); |
| 255 | + }); |
| 256 | + }); |
| 257 | +}); |
0 commit comments