"""Tests for native pi session parsing in the web UI."""

from __future__ import annotations

from abreka.webui.app import _discover_sessions, parse_pi_session_entries


class TestParsePiSessionEntries:
    def test_parses_user_assistant_and_tool_blocks(self):
        entries = [
            {"type": "session", "version": 3, "id": "sess", "timestamp": "2026-01-01T00:00:00Z", "cwd": "/tmp"},
            {
                "type": "message",
                "id": "u1",
                "parentId": None,
                "timestamp": "2026-01-01T00:00:01Z",
                "message": {"role": "user", "content": "Implement the experiment."},
            },
            {
                "type": "message",
                "id": "a1",
                "parentId": "u1",
                "timestamp": "2026-01-01T00:00:02Z",
                "message": {
                    "role": "assistant",
                    "provider": "openai",
                    "model": "gpt-test",
                    "usage": {"totalTokens": 12},
                    "content": [
                        {"type": "thinking", "thinking": "Need to inspect files."},
                        {"type": "text", "text": "I'll read the code."},
                        {
                            "type": "toolCall",
                            "id": "call-1",
                            "name": "bash",
                            "arguments": {"command": "ls"},
                        },
                    ],
                },
            },
            {
                "type": "message",
                "id": "t1",
                "parentId": "a1",
                "timestamp": "2026-01-01T00:00:03Z",
                "message": {
                    "role": "toolResult",
                    "toolCallId": "call-1",
                    "toolName": "bash",
                    "content": [{"type": "text", "text": "run.py\n"}],
                    "isError": False,
                },
            },
        ]

        blocks = parse_pi_session_entries(entries)

        assert blocks[0] == {"type": "user", "text": "Implement the experiment."}
        assert blocks[1]["type"] == "assistant"
        assert blocks[1]["thinking"] == "Need to inspect files."
        assert blocks[1]["text"] == "I'll read the code."
        assert blocks[1]["model"] == "gpt-test"
        assert blocks[2] == {
            "type": "tool",
            "name": "bash",
            "args": {"command": "ls"},
            "result": "run.py\n",
            "is_error": False,
        }


class TestDiscoverSessions:
    def test_discovers_native_jsonl_stage_files(self, abreka_project):
        exp_path = abreka_project / "abreka" / "experiments" / "0001"
        artifacts = exp_path / "artifacts"
        artifacts.mkdir(parents=True)
        (artifacts / "pi_session_propose.jsonl").write_text('{"type":"session","version":3}\n')
        (artifacts / "pi_session_implement.jsonl").write_text('{"type":"session","version":3}\n')
        (artifacts / "pi_session_run_repair_1.jsonl").write_text('{"type":"session","version":3}\n')

        sessions = _discover_sessions(exp_path)

        assert [(s["stage"], s["filename"]) for s in sessions] == [
            ("propose", "pi_session_propose.jsonl"),
            ("implement", "pi_session_implement.jsonl"),
            ("run_repair_1", "pi_session_run_repair_1.jsonl"),
        ]
        assert all(s["size_kb"] > 0 for s in sessions)
