File size: 25,369 Bytes
f201cf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import re
import subprocess
import sys
from unittest.mock import Mock, patch

import pytest

from minisweagent.run.mini import DEFAULT_CONFIG, app, main


def strip_ansi_codes(text: str) -> str:
    """Remove ANSI escape sequences from text."""
    ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
    return ansi_escape.sub("", text)


def test_configure_if_first_time_called():
    """Test that configure_if_first_time is called when running mini main."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time") as mock_configure,
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test"}, "env": {}, "model": {}}

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        mock_interactive_agent_class.return_value = mock_agent

        # Call main function
        main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task="Test task",
            yolo=False,
            output=None,
            visual=False,
            model_class=None,
        )

        # Verify configure_if_first_time was called
        mock_configure.assert_called_once()


def test_mini_command_calls_run_interactive():
    """Test that mini command creates InteractiveAgent when visual=False."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test", "mode": "confirm"}, "env": {}, "model": {}}

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        mock_interactive_agent_class.return_value = mock_agent

        # Call main function with task provided (so prompt is not called)
        main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task="Test task",
            yolo=False,
            output=None,
            visual=False,
            model_class=None,
        )

        # Verify InteractiveAgent was instantiated
        mock_interactive_agent_class.assert_called_once()
        args, kwargs = mock_interactive_agent_class.call_args
        assert args[0] == mock_model  # model
        assert args[1] == mock_environment  # env
        # Verify agent.run was called with the task
        mock_agent.run.assert_called_once_with("Test task")


def test_mini_v_command_calls_run_textual():
    """Test that mini -v command creates TextualAgent when visual=True."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.TextualAgent") as mock_textual_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test", "mode": "confirm"}, "env": {}, "model": {}}

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        mock_textual_agent_class.return_value = mock_agent

        # Call main function with visual=True and task provided
        main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task="Test task",
            yolo=False,
            output=None,
            visual=True,
            model_class=None,
        )

        # Verify TextualAgent was instantiated
        mock_textual_agent_class.assert_called_once()
        args, kwargs = mock_textual_agent_class.call_args
        assert args[0] == mock_model  # model
        assert args[1] == mock_environment  # env
        # Verify agent.run was called with the task
        mock_agent.run.assert_called_once_with("Test task")


def test_mini_calls_prompt_when_no_task_provided():
    """Test that mini calls prompt when no task is provided."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.prompt_session.prompt") as mock_prompt,
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_prompt.return_value = "User provided task"
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test", "mode": "confirm"}, "env": {}, "model": {}}

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        mock_interactive_agent_class.return_value = mock_agent

        # Call main function without task
        main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task=None,  # No task provided
            yolo=False,
            output=None,
            visual=False,
            model_class=None,
        )

        # Verify prompt was called
        mock_prompt.assert_called_once()

        # Verify InteractiveAgent was instantiated
        mock_interactive_agent_class.assert_called_once()
        # Verify agent.run was called with the task from prompt
        mock_agent.run.assert_called_once_with("User provided task")


def test_mini_v_calls_prompt_when_no_task_provided():
    """Test that mini -v calls prompt when no task is provided."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.prompt_session.prompt") as mock_prompt,
        patch("minisweagent.run.mini.TextualAgent") as mock_textual_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_prompt.return_value = "User provided visual task"
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test", "mode": "confirm"}, "env": {}, "model": {}}

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        mock_textual_agent_class.return_value = mock_agent

        # Call main function with visual=True but no task
        main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task=None,  # No task provided
            yolo=False,
            output=None,
            visual=True,
            model_class=None,
        )

        # Verify prompt was called
        mock_prompt.assert_called_once()

        # Verify TextualAgent was instantiated
        mock_textual_agent_class.assert_called_once()
        # Verify agent.run was called with the task from prompt
        mock_agent.run.assert_called_once_with("User provided visual task")


def test_mini_with_explicit_model():
    """Test that mini works with explicitly provided model."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {
            "agent": {"system_template": "test", "mode": "yolo"},
            "env": {},
            "model": {"default_config": "test"},
        }

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        mock_interactive_agent_class.return_value = mock_agent

        # Call main function with explicit model
        main(
            config_spec=DEFAULT_CONFIG,
            model_name="gpt-4",
            task="Test task with explicit model",
            yolo=True,
            output=None,
            visual=False,
            model_class=None,
        )

        # Verify get_model was called with the explicit model
        mock_get_model.assert_called_once_with("gpt-4", {"default_config": "test"})

        # Verify InteractiveAgent was instantiated
        mock_interactive_agent_class.assert_called_once()
        # Verify agent.run was called
        mock_agent.run.assert_called_once_with("Test task with explicit model")


def test_yolo_mode_sets_correct_agent_config():
    """Test that yolo mode sets the correct agent configuration."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test"}, "env": {}, "model": {}}

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        mock_interactive_agent_class.return_value = mock_agent

        # Call main function with yolo=True
        main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task="Test yolo task",
            yolo=True,
            output=None,
            visual=False,
            model_class=None,
        )

        # Verify InteractiveAgent was called with yolo mode
        mock_interactive_agent_class.assert_called_once()
        args, kwargs = mock_interactive_agent_class.call_args
        # The agent_config should contain the mode as a keyword argument
        assert kwargs.get("mode") == "yolo"
        # Verify agent.run was called
        mock_agent.run.assert_called_once_with("Test yolo task")


def test_confirm_mode_sets_correct_agent_config():
    """Test that when yolo=False, no explicit mode is set (defaults to None)."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test"}, "env": {}, "model": {}}

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        mock_interactive_agent_class.return_value = mock_agent

        # Call main function with yolo=False (default)
        main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task="Test confirm task",
            yolo=False,
            output=None,
            visual=False,
            model_class=None,
        )

        # Verify InteractiveAgent was called with no explicit mode (defaults to None)
        mock_interactive_agent_class.assert_called_once()
        args, kwargs = mock_interactive_agent_class.call_args
        # The agent_config should not contain mode when yolo=False (defaults to None)
        assert kwargs.get("mode") is None
        # Verify agent.run was called
        mock_agent.run.assert_called_once_with("Test confirm task")


def test_mini_help():
    """Test that mini --help works correctly."""
    result = subprocess.run(
        [sys.executable, "-m", "minisweagent", "--help"],
        capture_output=True,
        text=True,
        timeout=10,
    )

    assert result.returncode == 0
    # Strip ANSI color codes for reliable text matching
    clean_output = strip_ansi_codes(result.stdout)
    assert "Run mini-SWE-agent in your local environment." in clean_output
    assert "--help" in clean_output
    assert "--config" in clean_output
    assert "--model" in clean_output
    assert "--task" in clean_output
    assert "--yolo" in clean_output
    assert "--output" in clean_output
    assert "--visual" in clean_output


def test_mini_help_with_typer_runner():
    """Test help functionality using typer's test runner."""
    from typer.testing import CliRunner

    runner = CliRunner()
    result = runner.invoke(app, ["--help"])

    assert result.exit_code == 0
    # Strip ANSI color codes for reliable text matching
    clean_output = strip_ansi_codes(result.stdout)
    assert "Run mini-SWE-agent in your local environment." in clean_output
    assert "--help" in clean_output
    assert "--config" in clean_output
    assert "--model" in clean_output
    assert "--task" in clean_output
    assert "--yolo" in clean_output
    assert "--output" in clean_output
    assert "--visual" in clean_output


def test_python_m_minisweagent_help():
    """Test that python -m minisweagent --help works correctly."""
    result = subprocess.run(
        [sys.executable, "-m", "minisweagent", "--help"],
        capture_output=True,
        text=True,
        timeout=10,
    )

    assert result.returncode == 0
    assert "mini-SWE-agent" in result.stdout


def test_mini_script_help():
    """Test that the mini script entry point help works."""
    result = subprocess.run(
        ["mini", "--help"],
        capture_output=True,
        text=True,
        timeout=10,
    )

    assert result.returncode == 0
    assert "mini-SWE-agent" in result.stdout


def test_mini_swe_agent_help():
    """Test that mini-swe-agent --help works correctly."""
    result = subprocess.run(
        ["mini-swe-agent", "--help"],
        capture_output=True,
        text=True,
        timeout=10,
    )

    assert result.returncode == 0
    clean_output = strip_ansi_codes(result.stdout)
    assert "mini-SWE-agent" in clean_output


def test_mini_extra_help():
    """Test that mini-extra --help works correctly."""
    result = subprocess.run(
        ["mini-extra", "--help"],
        capture_output=True,
        text=True,
        timeout=10,
    )

    assert result.returncode == 0
    clean_output = strip_ansi_codes(result.stdout)
    assert "central entry point for all extra commands" in clean_output
    assert "config" in clean_output
    assert "inspect" in clean_output
    assert "github-issue" in clean_output
    assert "swebench" in clean_output


def test_mini_e_help():
    """Test that mini-e --help works correctly."""
    result = subprocess.run(
        ["mini-e", "--help"],
        capture_output=True,
        text=True,
        timeout=10,
    )

    assert result.returncode == 0
    clean_output = strip_ansi_codes(result.stdout)
    assert "central entry point for all extra commands" in clean_output


@pytest.mark.parametrize(
    ("subcommand", "aliases"),
    [
        ("config", ["config"]),
        ("inspect", ["inspect", "i", "inspector"]),
        ("github-issue", ["github-issue", "gh"]),
        ("swebench", ["swebench"]),
        ("swebench-single", ["swebench-single"]),
    ],
)
def test_mini_extra_subcommand_help(subcommand: str, aliases: list[str]):
    """Test that mini-extra subcommands --help work correctly."""
    for alias in aliases:
        result = subprocess.run(
            ["mini-extra", alias, "--help"],
            capture_output=True,
            text=True,
            timeout=10,
        )

        assert result.returncode == 0
        # Just verify that help output is returned (content varies by subcommand)
        assert len(result.stdout) > 0


def test_mini_extra_config_help():
    """Test that mini-extra config --help works correctly."""
    result = subprocess.run(
        ["mini-extra", "config", "--help"],
        capture_output=True,
        text=True,
        timeout=10,
    )

    assert result.returncode == 0
    assert len(result.stdout) > 0
    # Config command should have help output
    clean_output = strip_ansi_codes(result.stdout)
    assert "--help" in clean_output


def test_exit_immediately_flag_sets_confirm_exit_false():
    """Test that --exit-immediately flag sets confirm_exit to False in agent config."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test"}, "env": {}, "model": {}}

        # Create mock agent with config
        mock_agent = Mock()
        mock_agent.config.confirm_exit = False
        mock_agent.run.return_value = ("Success", "Result")
        mock_interactive_agent_class.return_value = mock_agent

        # Call main function with --exit-immediately flag
        agent = main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task="Test task",
            yolo=False,
            output=None,
            visual=False,
            exit_immediately=True,  # This should set confirm_exit=False
            model_class=None,
        )

        # Verify the agent's config has confirm_exit set to False
        assert agent.config.confirm_exit is False


def test_no_exit_immediately_flag_sets_confirm_exit_true():
    """Test that when --exit-immediately flag is not used, confirm_exit defaults to True."""
    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test"}, "env": {}, "model": {}}

        # Create mock agent with config
        mock_agent = Mock()
        mock_agent.config.confirm_exit = True
        mock_agent.run.return_value = ("Success", "Result")
        mock_interactive_agent_class.return_value = mock_agent

        # Call main function without --exit-immediately flag (defaults to False)
        agent = main(
            config_spec=DEFAULT_CONFIG,
            model_name="test-model",
            task="Test task",
            yolo=False,
            output=None,
            visual=False,
            model_class=None,
        )

        # Verify the agent's config has confirm_exit set to True
        assert agent.config.confirm_exit is True


def test_exit_immediately_flag_with_typer_runner():
    """Test --exit-immediately flag using typer's test runner."""
    from typer.testing import CliRunner

    with (
        patch("minisweagent.run.mini.configure_if_first_time"),
        patch("minisweagent.run.mini.InteractiveAgent") as mock_interactive_agent_class,
        patch("minisweagent.run.mini.get_model") as mock_get_model,
        patch("minisweagent.run.mini.LocalEnvironment") as mock_env,
        patch("minisweagent.run.mini.get_config_path") as mock_get_config_path,
        patch("minisweagent.run.mini.yaml.safe_load") as mock_yaml_load,
        patch("minisweagent.run.mini.save_traj"),
    ):
        # Setup mocks
        mock_model = Mock()
        mock_get_model.return_value = mock_model
        mock_environment = Mock()
        mock_env.return_value = mock_environment
        mock_config_path = Mock()
        mock_config_path.read_text.return_value = ""
        mock_get_config_path.return_value = mock_config_path
        mock_yaml_load.return_value = {"agent": {"system_template": "test"}, "env": {}, "model": {}}

        # Setup mock agent instance
        mock_agent = Mock()
        mock_agent.run.return_value = ("Success", "Result")
        # Mock the attributes needed for save_traj
        mock_agent.model.cost = 0.0
        mock_agent.model.n_calls = 0
        mock_agent.messages = []
        mock_interactive_agent_class.return_value = mock_agent

        runner = CliRunner()
        result = runner.invoke(app, ["--task", "Test task", "--exit-immediately", "--model", "test-model"])

        assert result.exit_code == 0
        mock_interactive_agent_class.assert_called_once()
        args, kwargs = mock_interactive_agent_class.call_args
        # The agent_config should contain confirm_exit as a keyword argument
        assert kwargs.get("confirm_exit") is False