|
| 1 | +// Mock functions must be defined before imports |
| 2 | +const mockRunWizard = jest.fn(); |
| 3 | +const mockRunMCPInstall = jest.fn(); |
| 4 | +const mockRunMCPRemove = jest.fn(); |
| 5 | + |
| 6 | +jest.mock('../run', () => ({ runWizard: mockRunWizard })); |
| 7 | +jest.mock('../mcp', () => ({ |
| 8 | + runMCPInstall: mockRunMCPInstall, |
| 9 | + runMCPRemove: mockRunMCPRemove, |
| 10 | +})); |
| 11 | +jest.mock('semver', () => ({ satisfies: () => true })); |
| 12 | + |
| 13 | +describe('CLI argument parsing', () => { |
| 14 | + const originalArgv = process.argv; |
| 15 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 16 | + const originalExit = process.exit; |
| 17 | + const originalEnv = { ...process.env }; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + jest.clearAllMocks(); |
| 21 | + |
| 22 | + // Reset environment |
| 23 | + process.env = { ...originalEnv }; |
| 24 | + delete process.env.POSTHOG_WIZARD_REGION; |
| 25 | + delete process.env.POSTHOG_WIZARD_DEFAULT; |
| 26 | + |
| 27 | + // Mock process.exit to prevent test runner from exiting |
| 28 | + process.exit = jest.fn() as any; |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + process.argv = originalArgv; |
| 33 | + process.exit = originalExit; |
| 34 | + process.env = originalEnv; |
| 35 | + jest.resetModules(); |
| 36 | + }); |
| 37 | + |
| 38 | + /** |
| 39 | + * Helper to run the CLI with given arguments |
| 40 | + */ |
| 41 | + async function runCLI(args: string[]) { |
| 42 | + process.argv = ['node', 'bin.ts', ...args]; |
| 43 | + |
| 44 | + jest.isolateModules(() => { |
| 45 | + require('../../bin.ts'); |
| 46 | + }); |
| 47 | + |
| 48 | + // Allow yargs to process |
| 49 | + await new Promise((resolve) => setImmediate(resolve)); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Helper to get the arguments passed to a mock function |
| 54 | + */ |
| 55 | + function getLastCallArgs(mockFn: jest.Mock) { |
| 56 | + expect(mockFn).toHaveBeenCalled(); |
| 57 | + return mockFn.mock.calls[mockFn.mock.calls.length - 1][0]; |
| 58 | + } |
| 59 | + |
| 60 | + describe('--default flag', () => { |
| 61 | + test('defaults to true when not specified', async () => { |
| 62 | + await runCLI([]); |
| 63 | + |
| 64 | + const args = getLastCallArgs(mockRunWizard); |
| 65 | + expect(args.default).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + test('can be explicitly set to false with --no-default', async () => { |
| 69 | + await runCLI(['--no-default']); |
| 70 | + |
| 71 | + const args = getLastCallArgs(mockRunWizard); |
| 72 | + expect(args.default).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + test('can be explicitly set to true', async () => { |
| 76 | + await runCLI(['--default']); |
| 77 | + |
| 78 | + const args = getLastCallArgs(mockRunWizard); |
| 79 | + expect(args.default).toBe(true); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('--region flag', () => { |
| 84 | + test('defaults to "us" when not specified', async () => { |
| 85 | + await runCLI([]); |
| 86 | + |
| 87 | + const args = getLastCallArgs(mockRunWizard); |
| 88 | + expect(args.region).toBe('us'); |
| 89 | + }); |
| 90 | + |
| 91 | + test.each(['us', 'eu'])( |
| 92 | + 'accepts "%s" as a valid region', |
| 93 | + async (region) => { |
| 94 | + await runCLI(['--region', region]); |
| 95 | + |
| 96 | + const args = getLastCallArgs(mockRunWizard); |
| 97 | + expect(args.region).toBe(region); |
| 98 | + }, |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('--eu flag (shorthand for --region eu)', () => { |
| 103 | + test('sets region to "eu"', async () => { |
| 104 | + await runCLI(['--eu']); |
| 105 | + |
| 106 | + const args = getLastCallArgs(mockRunWizard); |
| 107 | + expect(args.region).toBe('eu'); |
| 108 | + expect(args.eu).toBe(true); |
| 109 | + }); |
| 110 | + |
| 111 | + test('overrides --region flag when both are specified', async () => { |
| 112 | + await runCLI(['--region', 'us', '--eu']); |
| 113 | + |
| 114 | + const args = getLastCallArgs(mockRunWizard); |
| 115 | + expect(args.region).toBe('eu'); |
| 116 | + }); |
| 117 | + |
| 118 | + test('overrides --region flag regardless of order', async () => { |
| 119 | + await runCLI(['--eu', '--region', 'us']); |
| 120 | + |
| 121 | + const args = getLastCallArgs(mockRunWizard); |
| 122 | + expect(args.region).toBe('eu'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('environment variables', () => { |
| 127 | + test('respects POSTHOG_WIZARD_REGION', async () => { |
| 128 | + process.env.POSTHOG_WIZARD_REGION = 'eu'; |
| 129 | + |
| 130 | + await runCLI([]); |
| 131 | + |
| 132 | + const args = getLastCallArgs(mockRunWizard); |
| 133 | + expect(args.region).toBe('eu'); |
| 134 | + }); |
| 135 | + |
| 136 | + test('respects POSTHOG_WIZARD_DEFAULT', async () => { |
| 137 | + process.env.POSTHOG_WIZARD_DEFAULT = 'false'; |
| 138 | + |
| 139 | + await runCLI([]); |
| 140 | + |
| 141 | + const args = getLastCallArgs(mockRunWizard); |
| 142 | + expect(args.default).toBe(false); |
| 143 | + }); |
| 144 | + |
| 145 | + test('CLI args override environment variables', async () => { |
| 146 | + process.env.POSTHOG_WIZARD_REGION = 'us'; |
| 147 | + process.env.POSTHOG_WIZARD_DEFAULT = 'false'; |
| 148 | + |
| 149 | + await runCLI(['--region', 'eu', '--default']); |
| 150 | + |
| 151 | + const args = getLastCallArgs(mockRunWizard); |
| 152 | + expect(args.region).toBe('eu'); |
| 153 | + expect(args.default).toBe(true); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('backward compatibility', () => { |
| 158 | + test('all existing flags continue to work', async () => { |
| 159 | + await runCLI([ |
| 160 | + '--debug', |
| 161 | + '--signup', |
| 162 | + '--force-install', |
| 163 | + '--install-dir', |
| 164 | + '/custom/path', |
| 165 | + '--integration', |
| 166 | + 'nextjs', |
| 167 | + ]); |
| 168 | + |
| 169 | + const args = getLastCallArgs(mockRunWizard); |
| 170 | + |
| 171 | + // Existing flags |
| 172 | + expect(args.debug).toBe(true); |
| 173 | + expect(args.signup).toBe(true); |
| 174 | + expect(args['force-install']).toBe(true); |
| 175 | + expect(args['install-dir']).toBe('/custom/path'); |
| 176 | + expect(args.integration).toBe('nextjs'); |
| 177 | + |
| 178 | + // New defaults |
| 179 | + expect(args.default).toBe(true); |
| 180 | + expect(args.region).toBe('us'); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('mcp commands', () => { |
| 185 | + test('mcp add respects --eu flag', async () => { |
| 186 | + await runCLI(['mcp', 'add', '--eu']); |
| 187 | + |
| 188 | + const args = getLastCallArgs(mockRunMCPInstall); |
| 189 | + expect(args.region).toBe('eu'); |
| 190 | + }); |
| 191 | + |
| 192 | + test('mcp add uses default region when not specified', async () => { |
| 193 | + await runCLI(['mcp', 'add']); |
| 194 | + |
| 195 | + const args = getLastCallArgs(mockRunMCPInstall); |
| 196 | + expect(args.region).toBe('us'); |
| 197 | + }); |
| 198 | + |
| 199 | + test('mcp add respects --region flag', async () => { |
| 200 | + await runCLI(['mcp', 'add', '--region', 'eu']); |
| 201 | + |
| 202 | + const args = getLastCallArgs(mockRunMCPInstall); |
| 203 | + expect(args.region).toBe('eu'); |
| 204 | + }); |
| 205 | + |
| 206 | + test('mcp commands inherit global flags', async () => { |
| 207 | + await runCLI(['mcp', 'add', '--no-default', '--debug']); |
| 208 | + |
| 209 | + const args = getLastCallArgs(mockRunMCPInstall); |
| 210 | + expect(args.default).toBe(false); |
| 211 | + expect(args.debug).toBe(true); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments