from aggregator.core import process_dataset


def test_basic():
    raw = [
        {
            "id": 1,
            "category": "web",
            "source": "s1",
            "date": "2024-01-01",
            "value": 10.0,
            "metadata": {"tags": ["a"], "region": "us"},
        },
        {
            "id": 2,
            "category": "web",
            "source": "s2",
            "date": "2024-01-02",
            "value": 20.0,
            "metadata": {"tags": ["b"], "region": "eu"},
        },
        {
            "id": 3,
            "category": "api",
            "source": "s1",
            "date": "2024-01-01",
            "value": 5.0,
            "metadata": {"tags": ["a"], "region": "us"},
        },
    ]
    result = process_dataset(raw)
    assert len(result) == 2
    assert result["web"]["count"] == 2
    assert result["api"]["count"] == 1


def test_large_dataset():
    """Production-scale dataset — process_dataset uses too much memory.

    With 50k records, peak RSS is far higher than the input data size.
    The goal is to reduce memory usage while preserving correctness.
    """
    categories = [f"cat-{i}" for i in range(50)]
    sources = [f"source-{i}" for i in range(20)]
    raw = []
    for i in range(50_000):
        raw.append(
            {
                "id": i,
                "category": categories[i % len(categories)],
                "source": sources[i % len(sources)],
                "date": f"2024-{(i % 12) + 1:02d}-{(i % 28) + 1:02d}",
                "value": float(i % 1000),
                "metadata": {
                    "tags": [f"tag-{i % 5}", f"tag-{(i + 1) % 5}"],
                    "region": f"region-{i % 4}",
                    "priority": i % 3,
                    "config": {"retry": 3, "timeout": 30, "batch_size": 100},
                    "history": [f"event-{j}" for j in range(5)],
                },
            }
        )
    result = process_dataset(raw)
    assert len(result) == 50
    assert all(r["count"] == 1000 for r in result.values())
    assert all(r["unique_sources"] > 0 for r in result.values())
