"""Tests for AI attribution detection module."""
from __future__ import annotations
import pytest
from mergeguard.analysis.attribution import detect_attribution
from mergeguard.models import PRInfo, AIAttribution, ChangedFile, FileChangeStatus
from datetime import datetime


def make_pr(**kwargs):
    defaults = dict(
        number=1, title="Test PR", author="dev",
        base_branch="main", head_branch="feature",
        head_sha="abc", created_at=datetime(2026, 1, 1),
        updated_at=datetime(2026, 1, 1),
    )
    defaults.update(kwargs)
    return PRInfo(**defaults)


class TestDetectAttribution:
    def test_human_pr(self):
        pr = make_pr(title="Fix bug in login", head_branch="fix/login")
        assert detect_attribution(pr) == AIAttribution.UNKNOWN

    def test_ai_branch_pattern(self):
        pr = make_pr(head_branch="cursor-fix-auth")
        result = detect_attribution(pr)
        assert result in (AIAttribution.AI_SUSPECTED, AIAttribution.AI_CONFIRMED)

    def test_ai_label(self):
        pr = make_pr(labels=["ai-generated"])
        result = detect_attribution(pr)
        assert result == AIAttribution.AI_CONFIRMED

    def test_ai_title_pattern(self):
        pr = make_pr(title="Generated by Claude: Add auth")
        result = detect_attribution(pr)
        assert result in (AIAttribution.AI_SUSPECTED, AIAttribution.AI_CONFIRMED)

    def test_agent_trace_file(self):
        pr = make_pr()
        pr.changed_files = [
            ChangedFile(path=".agent-trace/trace.json", status=FileChangeStatus.ADDED)
        ]
        result = detect_attribution(pr)
        assert result == AIAttribution.AI_CONFIRMED
