import time from pipeline.dedupe import Deduper def test_exact_duplicate_detected(tmp_path): d = Deduper(tmp_path / "idx.txt") code = "def add(a, b):\n return a + b\n" r1 = d.check_and_add(code, "id-1") assert r1["duplicate"] is False r2 = d.check_and_add(code, "id-2") assert r2["duplicate"] is True assert r2["kind"] == "exact" assert r2["match_id"] == "id-1" def test_structural_duplicate_detected_despite_renamed_vars(tmp_path): d = Deduper(tmp_path / "idx.txt") d.check_and_add("def add(a, b):\n return a + b\n", "id-1") r = d.check_and_add("def add(x, y):\n return x + y\n", "id-2") assert r["duplicate"] is True assert r["kind"] == "structural" def test_distinct_code_is_not_a_duplicate(tmp_path): d = Deduper(tmp_path / "idx.txt") d.check_and_add("def add(a, b):\n return a + b\n", "id-1") r = d.check_and_add( "class Stack:\n def __init__(self):\n self.items = []\n" " def push(self, x):\n self.items.append(x)\n", "id-2", ) assert r["duplicate"] is False def test_near_duplicate_detected_with_small_edit(tmp_path): d = Deduper(tmp_path / "idx.txt", similarity=0.8) base = "def compute(values):\n total = 0\n for v in values:\n total += v\n return total\n" d.check_and_add(base, "id-1") # Same logic, one extra trivial statement -> should still land above 0.8 Jaccard. near = "def compute(values):\n total = 0\n for v in values:\n total += v\n return total # done\n" r = d.check_and_add(near, "id-2") assert r["duplicate"] is True assert r["kind"] in ("near", "structural") def test_persists_and_reloads_index(tmp_path): path = tmp_path / "idx.txt" d1 = Deduper(path) d1.check_and_add("def add(a, b):\n return a + b\n", "id-1") d2 = Deduper(path) # simulate a fresh process reloading the index r = d2.check_and_add("def add(a, b):\n return a + b\n", "id-2") assert r["duplicate"] is True assert r["match_id"] == "id-1" def test_scales_past_naive_on_two_thousand_records(tmp_path): """Not a strict perf assertion (CI machines vary), just a sanity check that adding ~2k distinct records doesn't take an unreasonable amount of time the way an O(n^2) full Jaccard scan would.""" d = Deduper(tmp_path / "idx.txt") start = time.time() for i in range(2000): code = f"def f_{i}(a, b):\n return a + b + {i}\n" d.check_and_add(code, f"id-{i}") elapsed = time.time() - start assert elapsed < 20 # generous ceiling; O(n^2) shingle Jaccard would be far slower