"""Server batch job MySQL persistence."""

import json

from server.domain.models import ServerBatchJob
from server.infrastructure.database.server_batch_job_repo import job_row_to_api


def test_job_row_to_api_orders_results():
    row = ServerBatchJob(
        id=7,
        op="install-agent",
        label="批量安装 Agent",
        server_ids=json.dumps([1, 2]),
        status="partial",
        results=json.dumps([
            {"server_id": 1, "server_name": "A", "success": True, "stdout": "ok", "error": ""},
            {"server_id": 2, "server_name": "B", "success": False, "stdout": "", "error": "SSH fail"},
        ]),
        operator="admin",
    )
    api = job_row_to_api(row)
    assert api["job_id"] == 7
    assert api["success"] == 1
    assert api["failed"] == 1
    assert len(api["results"]) == 2
    assert api["results"][1]["error"] == "SSH fail"
