|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { detectInteractiveSession, initConfigs } from '../config/init'; |
| 4 | +import { mergeConfigs } from '../config/merge'; |
| 5 | + |
| 6 | +describe('interactive config', () => { |
| 7 | + it('should use detectInteractiveSession when not provided', async () => { |
| 8 | + const result = await initConfigs({ |
| 9 | + input: 'test.json', |
| 10 | + output: './test', |
| 11 | + }); |
| 12 | + |
| 13 | + // In test environment, TTY is typically not available, so it should be false |
| 14 | + expect(result.results[0]?.config.interactive).toBe(false); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should respect user config when set to true', async () => { |
| 18 | + const result = await initConfigs({ |
| 19 | + input: 'test.json', |
| 20 | + interactive: true, |
| 21 | + output: './test', |
| 22 | + }); |
| 23 | + |
| 24 | + expect(result.results[0]?.config.interactive).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should respect user config when set to false', async () => { |
| 28 | + const result = await initConfigs({ |
| 29 | + input: 'test.json', |
| 30 | + interactive: false, |
| 31 | + output: './test', |
| 32 | + }); |
| 33 | + |
| 34 | + expect(result.results[0]?.config.interactive).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should allow file config to set interactive when CLI does not provide it', () => { |
| 38 | + // This simulates what happens when: |
| 39 | + // 1. User has a config file with interactive: false |
| 40 | + // 2. CLI doesn't provide interactive (undefined) |
| 41 | + // 3. The bug was: bin script would set interactive = isInteractive (true in TTY) |
| 42 | + |
| 43 | + const fileConfig = { |
| 44 | + input: 'test.json', |
| 45 | + interactive: false, |
| 46 | + output: './test', |
| 47 | + }; |
| 48 | + |
| 49 | + // CLI config without interactive (correct behavior after fix) |
| 50 | + const cliConfigWithoutInteractive = { |
| 51 | + input: 'test.json', |
| 52 | + output: './test', |
| 53 | + }; |
| 54 | + |
| 55 | + // CLI config with interactive set to true (simulating the bug) |
| 56 | + const cliConfigWithInteractiveBug = { |
| 57 | + input: 'test.json', |
| 58 | + interactive: true, |
| 59 | + output: './test', // Bug: bin script was setting this even when user didn't provide it |
| 60 | + }; |
| 61 | + |
| 62 | + // After fix: file config's interactive should be preserved |
| 63 | + const mergedCorrect = mergeConfigs(fileConfig, cliConfigWithoutInteractive); |
| 64 | + expect(mergedCorrect.interactive).toBe(false); |
| 65 | + |
| 66 | + // Before fix: CLI's auto-detected interactive would override file config |
| 67 | + const mergedWithBug = mergeConfigs(fileConfig, cliConfigWithInteractiveBug); |
| 68 | + expect(mergedWithBug.interactive).toBe(true); // This was the bug - it overrode the file config |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('detectInteractiveSession', () => { |
| 73 | + const originalEnv = process.env; |
| 74 | + const originalStdin = process.stdin; |
| 75 | + const originalStdout = process.stdout; |
| 76 | + |
| 77 | + afterEach(() => { |
| 78 | + process.env = originalEnv; |
| 79 | + Object.defineProperty(process, 'stdin', { value: originalStdin }); |
| 80 | + Object.defineProperty(process, 'stdout', { value: originalStdout }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should return false when CI environment variable is set', () => { |
| 84 | + process.env = { ...originalEnv, CI: 'true' }; |
| 85 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 86 | + configurable: true, |
| 87 | + value: true, |
| 88 | + }); |
| 89 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 90 | + configurable: true, |
| 91 | + value: true, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(detectInteractiveSession()).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should return false when NO_INTERACTIVE environment variable is set', () => { |
| 98 | + process.env = { ...originalEnv, NO_INTERACTIVE: '1' }; |
| 99 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 100 | + configurable: true, |
| 101 | + value: true, |
| 102 | + }); |
| 103 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 104 | + configurable: true, |
| 105 | + value: true, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(detectInteractiveSession()).toBe(false); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should return false when NO_INTERACTION environment variable is set', () => { |
| 112 | + process.env = { ...originalEnv, NO_INTERACTION: '1' }; |
| 113 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 114 | + configurable: true, |
| 115 | + value: true, |
| 116 | + }); |
| 117 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 118 | + configurable: true, |
| 119 | + value: true, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(detectInteractiveSession()).toBe(false); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should return false when stdin is not TTY', () => { |
| 126 | + process.env = { ...originalEnv }; |
| 127 | + delete process.env.CI; |
| 128 | + delete process.env.NO_INTERACTIVE; |
| 129 | + delete process.env.NO_INTERACTION; |
| 130 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 131 | + configurable: true, |
| 132 | + value: false, |
| 133 | + }); |
| 134 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 135 | + configurable: true, |
| 136 | + value: true, |
| 137 | + }); |
| 138 | + |
| 139 | + expect(detectInteractiveSession()).toBe(false); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should return false when stdout is not TTY', () => { |
| 143 | + process.env = { ...originalEnv }; |
| 144 | + delete process.env.CI; |
| 145 | + delete process.env.NO_INTERACTIVE; |
| 146 | + delete process.env.NO_INTERACTION; |
| 147 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 148 | + configurable: true, |
| 149 | + value: true, |
| 150 | + }); |
| 151 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 152 | + configurable: true, |
| 153 | + value: false, |
| 154 | + }); |
| 155 | + |
| 156 | + expect(detectInteractiveSession()).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should return true when TTY is available and no blocking env vars are set', () => { |
| 160 | + process.env = { ...originalEnv }; |
| 161 | + delete process.env.CI; |
| 162 | + delete process.env.NO_INTERACTIVE; |
| 163 | + delete process.env.NO_INTERACTION; |
| 164 | + Object.defineProperty(process.stdin, 'isTTY', { |
| 165 | + configurable: true, |
| 166 | + value: true, |
| 167 | + }); |
| 168 | + Object.defineProperty(process.stdout, 'isTTY', { |
| 169 | + configurable: true, |
| 170 | + value: true, |
| 171 | + }); |
| 172 | + |
| 173 | + expect(detectInteractiveSession()).toBe(true); |
| 174 | + }); |
| 175 | +}); |
0 commit comments