File size: 15,926 Bytes
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89031c2
6b5f876
89031c2
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89031c2
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89031c2
6b5f876
89031c2
6b5f876
 
 
 
89031c2
6b5f876
89031c2
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89031c2
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89031c2
6b5f876
89031c2
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89031c2
 
6b5f876
 
 
 
 
89031c2
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89031c2
 
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
89031c2
 
6b5f876
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1668d0
6b5f876
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-

"""
Unit tests for main.py CLI functions.
Tests for parse_cli_args(), resolve_server_config(), and print_startup_banner().
"""

import pytest
import argparse
import sys
from unittest.mock import patch, MagicMock
from io import StringIO


class TestParseCliArgs:
    """Tests for parse_cli_args() function."""
    
    def test_default_values_are_none(self):
        """
        What it does: Verifies that default values for host and port are None.
        Purpose: Ensure that None indicates "use env or default" in priority resolution.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with no arguments...")
        with patch.object(sys, 'argv', ['main.py']):
            args = parse_cli_args()
        
        print(f"args.host: {args.host}")
        print(f"args.port: {args.port}")
        print(f"Comparing: Expected host=None, port=None")
        assert args.host is None
        assert args.port is None
    
    def test_port_argument_long_form(self):
        """
        What it does: Verifies that --port argument is parsed correctly.
        Purpose: Ensure long form --port works.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with --port 9000...")
        with patch.object(sys, 'argv', ['main.py', '--port', '9000']):
            args = parse_cli_args()
        
        print(f"args.port: {args.port}")
        print(f"Comparing: Expected 9000, Got {args.port}")
        assert args.port == 9000
    
    def test_port_argument_short_form(self):
        """
        What it does: Verifies that -p argument is parsed correctly.
        Purpose: Ensure short form -p works.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with -p 8080...")
        with patch.object(sys, 'argv', ['main.py', '-p', '8080']):
            args = parse_cli_args()
        
        print(f"args.port: {args.port}")
        print(f"Comparing: Expected 8080, Got {args.port}")
        assert args.port == 8080
    
    def test_host_argument_long_form(self):
        """
        What it does: Verifies that --host argument is parsed correctly.
        Purpose: Ensure long form --host works.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with --host 127.0.0.1...")
        with patch.object(sys, 'argv', ['main.py', '--host', '127.0.0.1']):
            args = parse_cli_args()
        
        print(f"args.host: {args.host}")
        print(f"Comparing: Expected '127.0.0.1', Got '{args.host}'")
        assert args.host == "127.0.0.1"
    
    def test_host_argument_short_form(self):
        """
        What it does: Verifies that -H argument is parsed correctly.
        Purpose: Ensure short form -H works.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with -H 192.168.1.1...")
        with patch.object(sys, 'argv', ['main.py', '-H', '192.168.1.1']):
            args = parse_cli_args()
        
        print(f"args.host: {args.host}")
        print(f"Comparing: Expected '192.168.1.1', Got '{args.host}'")
        assert args.host == "192.168.1.1"
    
    def test_both_arguments_together(self):
        """
        What it does: Verifies that both --host and --port can be used together.
        Purpose: Ensure both arguments work simultaneously.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with --host 0.0.0.0 --port 3000...")
        with patch.object(sys, 'argv', ['main.py', '--host', '0.0.0.0', '--port', '3000']):
            args = parse_cli_args()
        
        print(f"args.host: {args.host}")
        print(f"args.port: {args.port}")
        assert args.host == "0.0.0.0"
        assert args.port == 3000
    
    def test_short_forms_together(self):
        """
        What it does: Verifies that both -H and -p can be used together.
        Purpose: Ensure short forms work simultaneously.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with -H 127.0.0.1 -p 5000...")
        with patch.object(sys, 'argv', ['main.py', '-H', '127.0.0.1', '-p', '5000']):
            args = parse_cli_args()
        
        print(f"args.host: {args.host}")
        print(f"args.port: {args.port}")
        assert args.host == "127.0.0.1"
        assert args.port == 5000


class TestResolveServerConfig:
    """Tests for resolve_server_config() function - priority hierarchy."""
    
    def test_cli_args_take_priority_over_env(self):
        """
        What it does: Verifies that CLI arguments have highest priority.
        Purpose: Ensure CLI args override environment variables.
        """
        print("Setup: Importing resolve_server_config...")
        from main import resolve_server_config
        
        print("Setup: Creating args with host=127.0.0.1, port=9000...")
        args = argparse.Namespace(host="127.0.0.1", port=9000)
        
        print("Action: Calling resolve_server_config with CLI args...")
        # Even if env vars are set, CLI should win
        with patch('main.SERVER_HOST', '0.0.0.0'), \
             patch('main.SERVER_PORT', 7860), \
             patch('main.DEFAULT_SERVER_HOST', '0.0.0.0'), \
             patch('main.DEFAULT_SERVER_PORT', 7860):
            host, port = resolve_server_config(args)
        
        print(f"Resolved host: {host}")
        print(f"Resolved port: {port}")
        print(f"Comparing: Expected ('127.0.0.1', 9000)")
        assert host == "127.0.0.1"
        assert port == 9000
    
    def test_env_vars_take_priority_over_defaults(self):
        """
        What it does: Verifies that env vars have priority over defaults.
        Purpose: Ensure env vars are used when CLI args are not provided.
        """
        print("Setup: Importing resolve_server_config...")
        from main import resolve_server_config
        
        print("Setup: Creating args with host=None, port=None (no CLI args)...")
        args = argparse.Namespace(host=None, port=None)
        
        print("Action: Calling resolve_server_config with env vars set...")
        # SERVER_HOST and SERVER_PORT are different from defaults
        with patch('main.SERVER_HOST', '192.168.1.100'), \
             patch('main.SERVER_PORT', 3000), \
             patch('main.DEFAULT_SERVER_HOST', '0.0.0.0'), \
             patch('main.DEFAULT_SERVER_PORT', 7860):
            host, port = resolve_server_config(args)
        
        print(f"Resolved host: {host}")
        print(f"Resolved port: {port}")
        print(f"Comparing: Expected ('192.168.1.100', 3000)")
        assert host == "192.168.1.100"
        assert port == 3000
    
    def test_defaults_used_when_nothing_set(self):
        """
        What it does: Verifies that defaults are used when nothing else is set.
        Purpose: Ensure default values work correctly.
        """
        print("Setup: Importing resolve_server_config...")
        from main import resolve_server_config
        
        print("Setup: Creating args with host=None, port=None...")
        args = argparse.Namespace(host=None, port=None)
        
        print("Action: Calling resolve_server_config with defaults...")
        # SERVER_HOST and SERVER_PORT equal to defaults (no env override)
        with patch('main.SERVER_HOST', '0.0.0.0'), \
             patch('main.SERVER_PORT', 7860), \
             patch('main.DEFAULT_SERVER_HOST', '0.0.0.0'), \
             patch('main.DEFAULT_SERVER_PORT', 7860):
            host, port = resolve_server_config(args)
        
        print(f"Resolved host: {host}")
        print(f"Resolved port: {port}")
        print(f"Comparing: Expected ('0.0.0.0', 7860)")
        assert host == "0.0.0.0"
        assert port == 7860
    
    def test_cli_host_only_env_port(self):
        """
        What it does: Verifies mixed priority - CLI host with env port.
        Purpose: Ensure each argument is resolved independently.
        """
        print("Setup: Importing resolve_server_config...")
        from main import resolve_server_config
        
        print("Setup: Creating args with host='127.0.0.1', port=None...")
        args = argparse.Namespace(host="127.0.0.1", port=None)
        
        print("Action: Calling resolve_server_config...")
        with patch('main.SERVER_HOST', '0.0.0.0'), \
             patch('main.SERVER_PORT', 9000), \
             patch('main.DEFAULT_SERVER_HOST', '0.0.0.0'), \
             patch('main.DEFAULT_SERVER_PORT', 7860):
            host, port = resolve_server_config(args)
        
        print(f"Resolved host: {host}")
        print(f"Resolved port: {port}")
        print(f"Comparing: Expected ('127.0.0.1', 9000)")
        assert host == "127.0.0.1"  # From CLI
        assert port == 9000  # From env (different from default)
    
    def test_cli_port_only_env_host(self):
        """
        What it does: Verifies mixed priority - CLI port with env host.
        Purpose: Ensure each argument is resolved independently.
        """
        print("Setup: Importing resolve_server_config...")
        from main import resolve_server_config
        
        print("Setup: Creating args with host=None, port=5000...")
        args = argparse.Namespace(host=None, port=5000)
        
        print("Action: Calling resolve_server_config...")
        with patch('main.SERVER_HOST', '192.168.1.1'), \
             patch('main.SERVER_PORT', 7860), \
             patch('main.DEFAULT_SERVER_HOST', '0.0.0.0'), \
             patch('main.DEFAULT_SERVER_PORT', 7860):
            host, port = resolve_server_config(args)
        
        print(f"Resolved host: {host}")
        print(f"Resolved port: {port}")
        print(f"Comparing: Expected ('192.168.1.1', 5000)")
        assert host == "192.168.1.1"  # From env (different from default)
        assert port == 5000  # From CLI


class TestPrintStartupBanner:
    """Tests for print_startup_banner() function."""
    
    def test_banner_contains_url(self, capsys):
        """
        What it does: Verifies that banner contains the server URL.
        Purpose: Ensure URL is displayed to user.
        """
        print("Setup: Importing print_startup_banner...")
        from main import print_startup_banner
        
        print("Action: Calling print_startup_banner('0.0.0.0', 7860)...")
        print_startup_banner("0.0.0.0", 7860)
        
        captured = capsys.readouterr()
        print(f"Captured output length: {len(captured.out)}")
        
        # When host is 0.0.0.0, display should show localhost
        assert "localhost:7860" in captured.out or "7860" in captured.out
    
    def test_banner_contains_custom_port(self, capsys):
        """
        What it does: Verifies that banner shows custom port.
        Purpose: Ensure custom port is displayed correctly.
        """
        print("Setup: Importing print_startup_banner...")
        from main import print_startup_banner
        
        print("Action: Calling print_startup_banner('127.0.0.1', 9000)...")
        print_startup_banner("127.0.0.1", 9000)
        
        captured = capsys.readouterr()
        print(f"Captured output contains '9000': {'9000' in captured.out}")
        
        assert "9000" in captured.out
    
    def test_banner_contains_docs_url(self, capsys):
        """
        What it does: Verifies that banner contains API docs URL.
        Purpose: Ensure /docs endpoint is mentioned.
        """
        print("Setup: Importing print_startup_banner...")
        from main import print_startup_banner
        
        print("Action: Calling print_startup_banner('0.0.0.0', 7860)...")
        print_startup_banner("0.0.0.0", 7860)
        
        captured = capsys.readouterr()
        print(f"Captured output contains '/docs': {'/docs' in captured.out}")
        
        assert "/docs" in captured.out
    
    def test_banner_contains_health_url(self, capsys):
        """
        What it does: Verifies that banner contains health check URL.
        Purpose: Ensure /health endpoint is mentioned.
        """
        print("Setup: Importing print_startup_banner...")
        from main import print_startup_banner
        
        print("Action: Calling print_startup_banner('0.0.0.0', 7860)...")
        print_startup_banner("0.0.0.0", 7860)
        
        captured = capsys.readouterr()
        print(f"Captured output contains '/health': {'/health' in captured.out}")
        
        assert "/health" in captured.out


class TestCliHelp:
    """Tests for CLI help output."""
    
    def test_help_shows_port_option(self):
        """
        What it does: Verifies that --help shows port option.
        Purpose: Ensure help is informative.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with --help...")
        with patch.object(sys, 'argv', ['main.py', '--help']):
            with pytest.raises(SystemExit) as exc_info:
                parse_cli_args()
        
        print(f"Exit code: {exc_info.value.code}")
        # --help exits with code 0
        assert exc_info.value.code == 0
    
    def test_help_shows_host_option(self, capsys):
        """
        What it does: Verifies that --help output contains host option.
        Purpose: Ensure host option is documented.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with --help...")
        with patch.object(sys, 'argv', ['main.py', '--help']):
            with pytest.raises(SystemExit):
                parse_cli_args()
        
        captured = capsys.readouterr()
        print(f"Help output contains '--host': {'--host' in captured.out}")
        print(f"Help output contains '-H': {'-H' in captured.out}")
        
        assert "--host" in captured.out
        assert "-H" in captured.out


class TestCliVersion:
    """Tests for CLI version output."""
    
    def test_version_flag_exits_with_zero(self):
        """
        What it does: Verifies that --version exits with code 0.
        Purpose: Ensure version flag works correctly.
        """
        print("Setup: Importing parse_cli_args...")
        from main import parse_cli_args
        
        print("Action: Calling parse_cli_args with --version...")
        with patch.object(sys, 'argv', ['main.py', '--version']):
            with pytest.raises(SystemExit) as exc_info:
                parse_cli_args()
        
        print(f"Exit code: {exc_info.value.code}")
        assert exc_info.value.code == 0
    
    def test_version_shows_app_version(self, capsys):
        """
        What it does: Verifies that --version shows application version.
        Purpose: Ensure version is displayed.
        """
        print("Setup: Importing parse_cli_args and APP_VERSION...")
        from main import parse_cli_args
        from kiro.config import APP_VERSION
        
        print("Action: Calling parse_cli_args with --version...")
        with patch.object(sys, 'argv', ['main.py', '--version']):
            with pytest.raises(SystemExit):
                parse_cli_args()
        
        captured = capsys.readouterr()
        print(f"Version output: {captured.out}")
        print(f"APP_VERSION: {APP_VERSION}")
        
        assert APP_VERSION in captured.out