"""Tests for CSV/JSON post source."""

import json
import tempfile
from pathlib import Path

import pytest

from agent_samochodowy.ingestor.csv_source import CsvSource


class TestCsvSource:
    def test_read_json(self):
        source = CsvSource("data/samples/posts.json")
        posts = list(source.fetch_posts())
        assert len(posts) == 30
        assert posts[0].post_id == "post_001"
        assert posts[0].author_name == "Jan Kowalski"

    def test_read_csv(self, tmp_path: Path):
        csv_file = tmp_path / "posts.csv"
        csv_file.write_text(
            "post_id,group,author_name,author_profile_url,text,timestamp,post_url\n"
            'p1,grp,Author,https://fb.com/a,"Szukam pompy",2025-01-01T00:00:00,https://fb.com/p1\n'
        )
        source = CsvSource(str(csv_file))
        posts = list(source.fetch_posts())
        assert len(posts) == 1
        assert posts[0].text == "Szukam pompy"

    def test_file_not_found(self):
        with pytest.raises(FileNotFoundError):
            CsvSource("/nonexistent/file.json")

    def test_unsupported_format(self, tmp_path: Path):
        f = tmp_path / "data.xml"
        f.write_text("<data/>")
        source = CsvSource(str(f))
        with pytest.raises(ValueError, match="Unsupported"):
            list(source.fetch_posts())
