from krrkt.engine.dedup import merge_findings


class TestMergeFindings:
    def test_no_overlap(self):
        fast = [{"start": 0, "end": 5, "weight": 100, "source": "fast"}]
        deep = [{"start": 10, "end": 15, "weight": 80, "source": "deep"}]
        result = merge_findings(fast, deep)
        assert len(result) == 2

    def test_exact_overlap_keeps_higher_weight(self):
        fast = [{"start": 0, "end": 5, "weight": 50, "source": "fast"}]
        deep = [{"start": 0, "end": 5, "weight": 100, "source": "deep"}]
        result = merge_findings(fast, deep)
        assert len(result) == 1
        assert result[0]["weight"] == 100

    def test_partial_overlap_keeps_higher_weight(self):
        fast = [{"start": 0, "end": 8, "weight": 100, "source": "fast"}]
        deep = [{"start": 3, "end": 10, "weight": 50, "source": "deep"}]
        result = merge_findings(fast, deep)
        assert len(result) == 1
        assert result[0]["weight"] == 100

    def test_empty_inputs(self):
        assert merge_findings([], []) == []

    def test_one_side_empty(self):
        fast = [{"start": 0, "end": 5, "weight": 100, "source": "fast"}]
        assert len(merge_findings(fast, [])) == 1
        assert len(merge_findings([], fast)) == 1

    def test_sorted_by_position(self):
        fast = [{"start": 20, "end": 25, "weight": 100, "source": "fast"}]
        deep = [{"start": 5, "end": 10, "weight": 80, "source": "deep"}]
        result = merge_findings(fast, deep)
        assert result[0]["start"] < result[1]["start"]
