File size: 1,288 Bytes
3a330e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6a19a7
3a330e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import csv
from pathlib import Path

import imagehash
import pytest
from PIL import Image

import pipeline.layer4_provenance as l4


def make_img(color):
    return Image.new("RGB", (64, 64), color=color)


def test_layer4_flagged_and_clean(tmp_path, monkeypatch):
    # Build a tiny temp CSV with one known pHash.
    flagged = make_img((255, 0, 0))
    flagged_hash = str(imagehash.colorhash(flagged))

    csv_path = tmp_path / "scam_database.csv"
    with open(csv_path, "w", newline="", encoding="utf-8") as f:
        w = csv.DictWriter(
            f,
            fieldnames=["phash_value", "platform", "scam_type", "reported_date", "note"],
        )
        w.writeheader()
        w.writerow(
            {
                "phash_value": flagged_hash,
                "platform": "Test",
                "scam_type": "counterfeit",
                "reported_date": "2026-01-01",
                "note": "unit",
            }
        )

    monkeypatch.setattr(l4, "CSV_PATH", csv_path)

    r1 = l4.check_provenance(flagged)
    assert r1["provenance_status"] == "Flagged"
    assert r1["match_source"] == "Test"

    clean = make_img((0, 255, 0))
    r2 = l4.check_provenance(clean)
    assert r2["provenance_status"] == "Clean"
    assert "demo database" in r2["note"].lower()