# -*- coding: utf-8 -*-
"""
回归测试：todo_write 工具应能容忍 todos 被作为 JSON 字符串传入的场景。

Bug 复现：LLM 偶尔将 todos 序列化为字符串（而非数组），
导致 sorted(todos) 迭代字符串字符，t.get() 抛出 AttributeError。
"""
import json

from abyss.tools.todo_write import TodoWriteTool


def _sample_todos():
    """构造一组合法的任务列表（dict 形式）。"""
    return [
        {"id": "1", "content": "任务A", "status": "pending", "priority": "high"},
        {"id": "2", "content": "任务B", "status": "in_progress", "priority": "medium"},
    ]


def test_execute_accepts_list_of_dicts():
    """基线：传入合法 list[dict] 应正常工作。"""
    tool = TodoWriteTool()

    out = tool.execute(todos=_sample_todos(), merge=False)

    assert out["success"] is True


def test_execute_accepts_json_string_todos():
    """Bug 回归：todos 为 JSON 字符串时应自动解析。

    复现报错：
      AttributeError: 'str' object has no attribute 'get'
    """
    tool = TodoWriteTool()
    todos_as_str = json.dumps(_sample_todos(), ensure_ascii=False)

    out = tool.execute(todos=todos_as_str, merge=False)

    assert out["success"] is True


def test_execute_rejects_empty_after_parse():
    """边界：解析后为空列表应返回失败。"""
    tool = TodoWriteTool()

    out = tool.execute(todos="[]", merge=False)

    assert out["success"] is False
