from __future__ import annotations

import json

import pytest

from sync_state_machine.common.collection import DataCollection
from sync_state_machine.datasource.jsonl.datasource import JsonlDataSource
from sync_state_machine.domain.units.jsonl_handler import UnitsJsonlHandler


@pytest.mark.asyncio
async def test_units_jsonl_handler_loads_units_filename(tmp_path):
    data_file = tmp_path / "units_1.jsonl"
    data_file.write_text(
        json.dumps(
            {
                "id": "u-1",
                "project_id": "p-1",
                "serial_number": 1,
                "actual_production_date": "2025-01-01",
            },
            ensure_ascii=False,
        )
        + "\n",
        encoding="utf-8",
    )

    datasource = JsonlDataSource(tmp_path)
    datasource.add_handler(UnitsJsonlHandler(datasource))
    datasource.set_collection(DataCollection("local"))

    await datasource.load_all(order=["units"])

    loaded = datasource._collection.filter(node_type="units")  # type: ignore[union-attr]
    assert {node.data_id for node in loaded} == {"u-1"}
