@@ -23,6 +23,9 @@ describe('CLI argument parsing', () => {
2323 process . env = { ...originalEnv } ;
2424 delete process . env . POSTHOG_WIZARD_REGION ;
2525 delete process . env . POSTHOG_WIZARD_DEFAULT ;
26+ delete process . env . POSTHOG_WIZARD_CI ;
27+ delete process . env . POSTHOG_WIZARD_API_KEY ;
28+ delete process . env . POSTHOG_WIZARD_INSTALL_DIR ;
2629
2730 // Mock process.exit to prevent test runner from exiting
2831 process . exit = jest . fn ( ) as any ;
@@ -187,4 +190,113 @@ describe('CLI argument parsing', () => {
187190 expect ( args . debug ) . toBe ( true ) ;
188191 } ) ;
189192 } ) ;
193+
194+ describe ( '--ci flag' , ( ) => {
195+ test ( 'defaults to false when not specified' , async ( ) => {
196+ await runCLI ( [ ] ) ;
197+
198+ const args = getLastCallArgs ( mockRunWizard ) ;
199+ expect ( args . ci ) . toBe ( false ) ;
200+ } ) ;
201+
202+ test ( 'can be set to true' , async ( ) => {
203+ await runCLI ( [
204+ '--ci' ,
205+ '--region' ,
206+ 'us' ,
207+ '--api-key' ,
208+ 'phx_test' ,
209+ '--install-dir' ,
210+ '/tmp/test' ,
211+ ] ) ;
212+
213+ const args = getLastCallArgs ( mockRunWizard ) ;
214+ expect ( args . ci ) . toBe ( true ) ;
215+ } ) ;
216+
217+ test ( 'requires --region when --ci is set' , async ( ) => {
218+ await runCLI ( [
219+ '--ci' ,
220+ '--api-key' ,
221+ 'phx_test' ,
222+ '--install-dir' ,
223+ '/tmp/test' ,
224+ ] ) ;
225+
226+ expect ( process . exit ) . toHaveBeenCalledWith ( 1 ) ;
227+ } ) ;
228+
229+ test ( 'requires --api-key when --ci is set' , async ( ) => {
230+ await runCLI ( [ '--ci' , '--region' , 'us' , '--install-dir' , '/tmp/test' ] ) ;
231+
232+ expect ( process . exit ) . toHaveBeenCalledWith ( 1 ) ;
233+ } ) ;
234+
235+ test ( 'requires --install-dir when --ci is set' , async ( ) => {
236+ await runCLI ( [ '--ci' , '--region' , 'us' , '--api-key' , 'phx_test' ] ) ;
237+
238+ expect ( process . exit ) . toHaveBeenCalledWith ( 1 ) ;
239+ } ) ;
240+
241+ test ( 'passes --api-key to runWizard' , async ( ) => {
242+ await runCLI ( [
243+ '--ci' ,
244+ '--region' ,
245+ 'us' ,
246+ '--api-key' ,
247+ 'phx_test_key' ,
248+ '--install-dir' ,
249+ '/tmp/test' ,
250+ ] ) ;
251+
252+ const args = getLastCallArgs ( mockRunWizard ) ;
253+ expect ( args . apiKey ) . toBe ( 'phx_test_key' ) ;
254+ } ) ;
255+ } ) ;
256+
257+ describe ( 'CI environment variables' , ( ) => {
258+ test ( 'respects POSTHOG_WIZARD_CI' , async ( ) => {
259+ process . env . POSTHOG_WIZARD_CI = 'true' ;
260+ process . env . POSTHOG_WIZARD_REGION = 'us' ;
261+ process . env . POSTHOG_WIZARD_API_KEY = 'phx_env_key' ;
262+ process . env . POSTHOG_WIZARD_INSTALL_DIR = '/tmp/test' ;
263+
264+ await runCLI ( [ ] ) ;
265+
266+ const args = getLastCallArgs ( mockRunWizard ) ;
267+ expect ( args . ci ) . toBe ( true ) ;
268+ } ) ;
269+
270+ test ( 'respects POSTHOG_WIZARD_API_KEY' , async ( ) => {
271+ process . env . POSTHOG_WIZARD_CI = 'true' ;
272+ process . env . POSTHOG_WIZARD_REGION = 'eu' ;
273+ process . env . POSTHOG_WIZARD_API_KEY = 'phx_env_key' ;
274+ process . env . POSTHOG_WIZARD_INSTALL_DIR = '/tmp/test' ;
275+
276+ await runCLI ( [ ] ) ;
277+
278+ const args = getLastCallArgs ( mockRunWizard ) ;
279+ expect ( args . apiKey ) . toBe ( 'phx_env_key' ) ;
280+ } ) ;
281+
282+ test ( 'CLI args override CI environment variables' , async ( ) => {
283+ process . env . POSTHOG_WIZARD_CI = 'true' ;
284+ process . env . POSTHOG_WIZARD_REGION = 'us' ;
285+ process . env . POSTHOG_WIZARD_API_KEY = 'phx_env_key' ;
286+ process . env . POSTHOG_WIZARD_INSTALL_DIR = '/tmp/test' ;
287+
288+ await runCLI ( [
289+ '--region' ,
290+ 'eu' ,
291+ '--api-key' ,
292+ 'phx_cli_key' ,
293+ '--install-dir' ,
294+ '/other/path' ,
295+ ] ) ;
296+
297+ const args = getLastCallArgs ( mockRunWizard ) ;
298+ expect ( args . region ) . toBe ( 'eu' ) ;
299+ expect ( args . apiKey ) . toBe ( 'phx_cli_key' ) ;
300+ } ) ;
301+ } ) ;
190302} ) ;
0 commit comments