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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for cli/ module."""

import asyncio
import json
import os
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

from messaging.event_parser import parse_cli_event

# --- Existing Parser Tests ---


class TestCLIParser:
    """Test CLI event parsing."""

    def test_parse_text_content(self):
        """Test parsing text content from assistant message."""
        event = {
            "type": "assistant",
            "message": {"content": [{"type": "text", "text": "Hello, world!"}]},
        }
        result = parse_cli_event(event)
        assert len(result) == 1
        assert result[0]["type"] == "text_chunk"
        assert result[0]["text"] == "Hello, world!"

    def test_parse_thinking_content(self):
        """Test parsing thinking content."""
        event = {
            "type": "assistant",
            "message": {
                "content": [{"type": "thinking", "thinking": "Let me think..."}]
            },
        }
        result = parse_cli_event(event)
        assert len(result) == 1
        assert result[0]["type"] == "thinking_chunk"
        assert (
            result[0]["text"] == "Let me think...\n"
            or result[0]["text"] == "Let me think..."
        )

    def test_parse_multiple_content(self):
        """Test parsing mixed content (thinking + tools)."""
        event = {
            "type": "assistant",
            "message": {
                "content": [
                    {"type": "thinking", "thinking": "Thinking..."},
                    {"type": "tool_use", "name": "ls", "input": {}},
                ]
            },
        }
        result = parse_cli_event(event)
        assert len(result) == 2
        assert result[0]["type"] == "thinking_chunk"
        assert result[0]["text"] == "Thinking..."
        assert result[1]["type"] == "tool_use"

    def test_parse_tool_use(self):
        """Test parsing tool use content."""
        event = {
            "type": "assistant",
            "message": {
                "content": [
                    {
                        "type": "tool_use",
                        "name": "read_file",
                        "input": {"path": "/test"},
                    }
                ]
            },
        }
        result = parse_cli_event(event)
        assert len(result) == 1
        assert result[0]["type"] == "tool_use"
        assert result[0]["name"] == "read_file"
        assert result[0]["input"] == {"path": "/test"}

    def test_parse_text_delta(self):
        """Test parsing streaming text delta."""
        event = {
            "type": "content_block_delta",
            "index": 0,
            "delta": {"type": "text_delta", "text": "streaming text"},
        }
        result = parse_cli_event(event)
        assert len(result) == 1
        assert result[0]["type"] == "text_delta"
        assert result[0]["text"] == "streaming text"

    def test_parse_thinking_delta(self):
        """Test parsing streaming thinking delta."""
        event = {
            "type": "content_block_delta",
            "index": 1,
            "delta": {"type": "thinking_delta", "thinking": "thinking..."},
        }
        result = parse_cli_event(event)
        assert len(result) == 1
        assert result[0]["type"] == "thinking_delta"
        assert result[0]["text"] == "thinking..."

    def test_parse_error(self):
        """Test parsing error event."""
        event = {"type": "error", "error": {"message": "Something went wrong"}}
        result = parse_cli_event(event)
        assert result[0]["type"] == "error"
        assert result[0]["message"] == "Something went wrong"

    def test_parse_exit_success(self):
        """Test parsing exit event with success."""
        event = {"type": "exit", "code": 0}
        result = parse_cli_event(event)
        assert result[0]["type"] == "complete"
        assert result[0]["status"] == "success"

    def test_parse_exit_failure(self):
        """Test parsing exit event with failure returns error then complete."""
        event = {"type": "exit", "code": 1}
        result = parse_cli_event(event)
        # Non-zero exit now returns error first, then complete
        assert len(result) == 2
        assert result[0]["type"] == "error"
        assert (
            "exit" in result[0]["message"].lower()
            or "code" in result[0]["message"].lower()
        )
        assert result[1]["type"] == "complete"
        assert result[1]["status"] == "failed"

    def test_parse_invalid_event(self):
        """Test parsing returns empty list for unrecognized event."""
        result = parse_cli_event({"type": "unknown"})
        assert result == []

    def test_parse_non_dict(self):
        """Test parsing returns empty list for non-dict input."""
        result = parse_cli_event("not a dict")
        assert result == []


# --- CLI Session Tests ---


class TestCLISession:
    """Test CLISession."""

    def test_session_init(self):
        """Test CLISession initialization."""
        from cli.session import CLISession

        session = CLISession(
            workspace_path="/tmp/test",
            api_url="http://localhost:8082/v1",
            allowed_dirs=["/home/user/projects"],
        )
        assert session.workspace == os.path.normpath(os.path.abspath("/tmp/test"))
        assert session.api_url == "http://localhost:8082/v1"
        assert not session.is_busy

    def test_session_extract_session_id(self):
        """Test session ID extraction from various event formats."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        # Direct session_id field
        assert session._extract_session_id({"session_id": "abc123"}) == "abc123"
        assert session._extract_session_id({"sessionId": "abc123"}) == "abc123"

        # Nested in init
        assert (
            session._extract_session_id({"init": {"session_id": "nested123"}})
            == "nested123"
        )

        # Nested in result
        assert (
            session._extract_session_id({"result": {"session_id": "res123"}})
            == "res123"
        )

        # Conversation id
        assert (
            session._extract_session_id({"conversation": {"id": "conv123"}})
            == "conv123"
        )

        # No session ID
        assert session._extract_session_id({"type": "message"}) is None
        assert session._extract_session_id("not a dict") is None

    @pytest.mark.asyncio
    async def test_start_task_basic_flow(self):
        """Test start_task running a basic command flow."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        # Mock subprocess
        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [
            b'{"type": "message", "content": "Hello"}\n',
            b'{"session_id": "sess_1"}\n',
            b"",  # EOF
        ]
        mock_process.stderr.read.return_value = b""  # No error
        mock_process.wait.return_value = 0
        mock_process.returncode = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process

            events = [e async for e in session.start_task("Hello")]

            # Verify command construction
            # Arg 1 is subprocess command
            args = mock_exec.call_args[0]
            assert args[0] == "claude"
            assert "-p" in args
            assert "Hello" in args

            # Verify events
            assert (
                len(events) == 4
            )  # message, session_id, session_info (synthesized), exit
            assert events[0] == {"type": "message", "content": "Hello"}
            assert events[1] == {"type": "session_info", "session_id": "sess_1"}
            # The session_info event is yielded by _handle_line_gen right after extracting ID
            assert events[2] == {"session_id": "sess_1"}  # The original event
            assert events[3] == {"type": "exit", "code": 0, "stderr": None}

            assert session.current_session_id == "sess_1"

    @pytest.mark.asyncio
    async def test_start_task_with_session_resume(self):
        """Test resuming an existing session."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [
            b"",
        ]  # Immediate EOF
        mock_process.stderr.read.return_value = b""
        mock_process.wait.return_value = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process

            async for _ in session.start_task("Hello", session_id="sess_abc"):
                pass

            args = mock_exec.call_args[0]
            assert "--resume" in args
            assert "sess_abc" in args
            assert "--fork-session" not in args

    @pytest.mark.asyncio
    async def test_start_task_with_session_resume_and_fork(self):
        """Test resuming an existing session and forking."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [b""]  # Immediate EOF
        mock_process.stderr.read.return_value = b""
        mock_process.wait.return_value = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process

            async for _ in session.start_task(
                "Hello", session_id="sess_abc", fork_session=True
            ):
                pass

            args = mock_exec.call_args[0]
            assert "--resume" in args
            assert "sess_abc" in args
            assert "--fork-session" in args

    @pytest.mark.asyncio
    async def test_start_task_process_failure_with_stderr(self):
        """Test process exit with error code and stderr output."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [b""]  # No stdout
        mock_process.stderr.read.return_value = b"Fatal error"
        mock_process.wait.return_value = 1

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process

            events = [e async for e in session.start_task("Hello")]

            # Should have error event from stderr, then exit event
            assert len(events) == 2
            assert events[0]["type"] == "error"
            assert events[0]["error"]["message"] == "Fatal error"

            assert events[1]["type"] == "exit"
            assert events[1]["code"] == 1
            assert events[1]["stderr"] == "Fatal error"

    @pytest.mark.asyncio
    async def test_stop_session(self):
        """Test stopping the session process."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = MagicMock()
        mock_process.returncode = None  # Running
        # Mock wait to simulate async finish
        mock_process.wait = AsyncMock(return_value=0)

        session.process = mock_process

        stopped = await session.stop()

        assert stopped is True
        mock_process.terminate.assert_called_once()
        mock_process.wait.assert_called()

    @pytest.mark.asyncio
    async def test_stop_session_timeout_force_kill(self):
        """Test force kill if terminate times out."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = MagicMock()
        mock_process.returncode = None

        # First wait times out
        async def wait_side_effect():
            if not mock_process.kill.called:
                await asyncio.sleep(6)  # Should be > 5.0 timeout
            return 0

        # We can simulate timeout by raising TimeoutError directly on first call
        mock_process.wait = AsyncMock(side_effect=[asyncio.TimeoutError, 0])

        session.process = mock_process

        stopped = await session.stop()

        assert stopped is True
        mock_process.terminate.assert_called()
        mock_process.kill.assert_called()

    @pytest.mark.asyncio
    async def test_start_task_split_buffer(self):
        """Test handling of JSON split across chunks."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = AsyncMock()
        # Split json: {"type": "mess... age"}
        mock_process.stdout.read.side_effect = [
            b'{"type": "mess',
            b'age", "content": "Split"}\n',
            b"",
        ]
        mock_process.stderr.read.return_value = b""
        mock_process.wait.return_value = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process

            events = [
                e async for e in session.start_task("test") if e["type"] == "message"
            ]

            assert len(events) == 1
            assert events[0]["content"] == "Split"

    @pytest.mark.asyncio
    async def test_start_task_remnant_buffer(self):
        """Test handling of buffer remnant at EOF (no newline at end)."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [
            b'{"type": "message", "content": "Remnant"}',  # No newline
            b"",
        ]
        mock_process.stderr.read.return_value = b""
        mock_process.wait.return_value = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process

            events = [
                e async for e in session.start_task("test") if e["type"] == "message"
            ]

            assert len(events) == 1
            assert events[0]["content"] == "Remnant"

    @pytest.mark.asyncio
    async def test_start_task_non_v1_url(self):
        """Test start_task with a non-v1 URL."""
        from cli.session import CLISession

        # URL not ending in /v1
        session = CLISession("/tmp", "http://localhost:8082")

        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [b""]
        mock_process.stderr.read.return_value = b""
        mock_process.wait.return_value = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process
            async for _ in session.start_task("test"):
                pass

            # Check env var
            kwargs = mock_exec.call_args[1]
            env = kwargs["env"]
            assert env["ANTHROPIC_BASE_URL"] == "http://localhost:8082"

    @pytest.mark.asyncio
    async def test_start_task_allowed_dirs(self):
        """Test start_task includes allowed dirs in command."""
        from cli.session import CLISession

        session = CLISession(
            "/tmp", "http://localhost:8082/v1", allowed_dirs=["/dir1", "/dir2"]
        )

        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [b""]
        mock_process.stderr.read.return_value = b""
        mock_process.wait.return_value = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process
            async for _ in session.start_task("test"):
                pass

            cmd = mock_exec.call_args[0]
            assert "--add-dir" in cmd
            assert os.path.normpath("/dir1") in cmd
            assert os.path.normpath("/dir2") in cmd

    @pytest.mark.asyncio
    async def test_start_task_plans_directory(self):
        """Test start_task includes --settings plansDirectory when plans_directory set."""
        from cli.session import CLISession

        session = CLISession(
            "/tmp",
            "http://localhost:8082/v1",
            plans_directory="./agent_workspace/plans",
        )

        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [b""]
        mock_process.stderr.read.return_value = b""
        mock_process.wait.return_value = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process
            async for _ in session.start_task("test"):
                pass

            cmd = mock_exec.call_args[0]
            assert "--settings" in cmd
            settings_idx = cmd.index("--settings")
            assert settings_idx + 1 < len(cmd)
            settings = json.loads(cmd[settings_idx + 1])
            assert settings["plansDirectory"] == "./agent_workspace/plans"

    @pytest.mark.asyncio
    async def test_start_task_json_error(self):
        """Test handling of non-JSON output from CLI."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = AsyncMock()
        mock_process.stdout.read.side_effect = [b"Not valid json\n", b""]
        mock_process.stderr.read.return_value = b""
        mock_process.wait.return_value = 0

        with patch(
            "asyncio.create_subprocess_exec", new_callable=AsyncMock
        ) as mock_exec:
            mock_exec.return_value = mock_process

            events = [e async for e in session.start_task("test") if e["type"] == "raw"]

            assert len(events) == 1
            assert events[0]["content"] == "Not valid json"

    @pytest.mark.asyncio
    async def test_stop_exception(self):
        """Test exception handling during stop."""
        from cli.session import CLISession

        session = CLISession("/tmp", "http://localhost:8082/v1")

        mock_process = MagicMock()
        mock_process.returncode = None
        # Raise exception on terminate
        mock_process.terminate.side_effect = RuntimeError("Permission denied")

        session.process = mock_process

        stopped = await session.stop()
        assert stopped is False


class TestCLISessionManager:
    """Test CLISessionManager."""

    @pytest.mark.asyncio
    async def test_manager_create_session(self):
        """Test creating a new session."""
        from cli.manager import CLISessionManager

        manager = CLISessionManager(
            workspace_path="/tmp/test",
            api_url="http://localhost:8082/v1",
        )

        session, sid, is_new = await manager.get_or_create_session()
        assert session is not None
        assert sid.startswith("pending_")
        assert is_new is True

    @pytest.mark.asyncio
    async def test_manager_reuse_session(self):
        """Test reusing an existing session."""
        from cli.manager import CLISessionManager

        manager = CLISessionManager(
            workspace_path="/tmp/test",
            api_url="http://localhost:8082/v1",
        )

        # Create first session
        s1, sid1, _is_new1 = await manager.get_or_create_session()

        # Request same session
        s2, _sid2, is_new2 = await manager.get_or_create_session(session_id=sid1)

        assert s1 is s2
        assert is_new2 is False

    @pytest.mark.asyncio
    async def test_manager_stats(self):
        """Test manager stats."""
        from cli.manager import CLISessionManager

        manager = CLISessionManager(
            workspace_path="/tmp/test",
            api_url="http://localhost:8082/v1",
        )

        stats = manager.get_stats()
        assert stats["active_sessions"] == 0
        assert stats["pending_sessions"] == 0