Micro_component_dataset / tests /test_purity_assay.py
FaustDerLegende's picture
Upload folder using huggingface_hub (part 100)
4c01f30 verified
Raw History Blame Contribute Delete
5.04 kB
"""Unit tests for Edge Component Purity Engine & Cannibalization Index."""
import pytest
from purity_engine.model_client import Detection, MockModelClient
from purity_engine.purity_assay import (
calculate_board_integrity_score,
calculate_adjusted_scrap_price,
assess_bis_discrepancy,
assay_pcb,
)
from purity_engine.vernacular_voice import generate_vernacular_voice_alert, SUPPORTED_LANGUAGES
from purity_engine.pcb_analyzer import PCBAnalyzer
def test_pristine_board_integrity_score():
detections = [
Detection("cpu_socket", 0.95, [0.1, 0.1, 0.4, 0.4]),
Detection("gold_finger", 0.90, [0.1, 0.8, 0.9, 0.9]),
Detection("ic_chip", 0.88, [0.5, 0.5, 0.6, 0.6]),
Detection("ic_chip", 0.85, [0.6, 0.5, 0.7, 0.6]),
Detection("ic_chip", 0.82, [0.7, 0.5, 0.8, 0.6]),
Detection("ic_chip", 0.80, [0.8, 0.5, 0.9, 0.6]),
Detection("tantalum_capacitor", 0.85, [0.2, 0.5, 0.3, 0.6]),
Detection("tantalum_capacitor", 0.84, [0.3, 0.5, 0.4, 0.6]),
Detection("tantalum_capacitor", 0.83, [0.4, 0.5, 0.5, 0.6]),
Detection("tantalum_capacitor", 0.82, [0.5, 0.5, 0.6, 0.6]),
Detection("tantalum_capacitor", 0.81, [0.6, 0.5, 0.7, 0.6]),
Detection("tantalum_capacitor", 0.80, [0.7, 0.5, 0.8, 0.6]),
]
bis, det_counts, exp_counts, fraud_pads, factory_pads, occluded = calculate_board_integrity_score(
detections, form_factor="desktop_atx"
)
assert bis == 100.0
assert fraud_pads == 0
assert factory_pads == 0
test_desoldered_fraud_penalty_data = [
(1, 75.0),
(2, 50.0),
(4, 0.0),
]
@pytest.mark.parametrize("fraud_count,expected_max_bis", test_desoldered_fraud_penalty_data)
def test_desoldered_fraud_penalty(fraud_count, expected_max_bis):
detections = [
Detection("cpu_socket", 0.95, [0.1, 0.1, 0.4, 0.4]),
] + [Detection("forcibly_desoldered_pad", 0.90, [0.5, 0.5, 0.6, 0.6]) for _ in range(fraud_count)]
bis, _, _, fraud_pads, _, _ = calculate_board_integrity_score(detections, form_factor="desktop_atx")
assert fraud_pads == fraud_count
assert bis <= expected_max_bis
def test_factory_unpopulated_pad_no_penalty():
detections = [
Detection("cpu_socket", 0.95, [0.1, 0.1, 0.4, 0.4]),
Detection("factory_unpopulated_pad", 0.90, [0.5, 0.5, 0.6, 0.6]),
Detection("factory_unpopulated_pad", 0.88, [0.6, 0.5, 0.7, 0.6]),
]
bis, _, _, fraud_pads, factory_pads, _ = calculate_board_integrity_score(detections, form_factor="desktop_atx")
assert fraud_pads == 0
assert factory_pads == 2
# Base score for CPU socket alone (0.40 weight out of 1.0) = 40.0
assert bis == 40.0
def test_category_scrap_grade_floors():
# Pristine board (BIS = 100) -> price = base_price (100.0)
adj_price_pristine, floor = calculate_adjusted_scrap_price(bis=100.0, base_price=100.0, form_factor="server_1u_2u")
assert adj_price_pristine == 100.0
assert floor == 0.15
# Completely stripped board (BIS = 0) -> price = base_price * floor
adj_price_stripped, _ = calculate_adjusted_scrap_price(bis=0.0, base_price=100.0, form_factor="server_1u_2u")
assert adj_price_stripped == 15.0
# Desktop ATX board (Floor = 0.30)
adj_price_desktop_stripped, _ = calculate_adjusted_scrap_price(bis=0.0, base_price=100.0, form_factor="desktop_atx")
assert adj_price_desktop_stripped == 30.0
# Power PCB (Floor = 0.60)
adj_price_power_stripped, _ = calculate_adjusted_scrap_price(bis=0.0, base_price=100.0, form_factor="low_grade_power_pcb")
assert adj_price_power_stripped == 60.0
def test_bis_discrepancy_bands():
# < 12% drop -> pass
assert assess_bis_discrepancy(collector_bis=90.0, recycler_bis=82.0) == "pass"
# 12% - 25% drop -> advisory_flag
assert assess_bis_discrepancy(collector_bis=90.0, recycler_bis=75.0) == "advisory_flag"
# > 25% drop -> critical_fraud_alert
assert assess_bis_discrepancy(collector_bis=90.0, recycler_bis=60.0) == "critical_fraud_alert"
def test_vernacular_voice_alerts():
for lang in SUPPORTED_LANGUAGES:
alert = generate_vernacular_voice_alert(bis_band="pristine", adjusted_price=100.0, lang=lang)
assert alert.lang == lang
assert f"{lang}_pristine.opus" in alert.audio_fallback_path
assert len(alert.transcript) > 0
def test_pcb_analyzer_end_to_end():
analyzer = PCBAnalyzer(model_client=MockModelClient())
result = analyzer.analyze_board("front.jpg", "back.jpg", base_price=100.0, form_factor="desktop_atx", lang="hi")
assert "board_integrity_score" in result
assert "adjusted_price" in result
assert "voice_alert" in result
assert result["voice_alert"]["lang"] == "hi"
# Test Recycler Intake Discrepancy Verification
verification = analyzer.verify_intake_discrepancy(
collector_bis=95.0, recycler_front_image="intake_front.jpg", form_factor="desktop_atx"
)
assert "discrepancy_status" in verification
assert verification["collector_bis"] == 95.0