from __future__ import annotations import math import pytest from app.config import Settings from app.db import add_catalog_item, init_schema, upsert_vector, VecLoadError, connect from app.match import band_for, distance_to_similarity, match_line_item from app.schemas import LineItem, MatchBand, ReceiptExtract from backends.base import InputType class FakeEmbed: name = "fake" dim = 8 def embed(self, texts: list[str], *, input_type: InputType) -> list[list[float]]: del input_type out = [] for text in texts: vec = [(b / 255.0) for b in (text.encode("utf-8") + b"\x00" * 8)[:8]] if "milk" in text.lower() or "MILK-1" in text: vec[0] = 1.0 vec[1] = 0.95 n = math.sqrt(sum(x * x for x in vec)) or 1.0 out.append([x / n for x in vec]) return out def health(self) -> bool: return True def test_bands() -> None: assert band_for(0.91, auto=0.88, review=0.72) is MatchBand.auto assert band_for(0.80, auto=0.88, review=0.72) is MatchBand.review assert band_for(0.10, auto=0.88, review=0.72) is MatchBand.unmatched assert abs(distance_to_similarity(0.2) - 0.8) < 1e-9 def test_exact_sku(settings: Settings) -> None: try: con = connect(settings.db_path) init_schema(con, settings) except VecLoadError: pytest.skip("sqlite-vec not loadable") cid = add_catalog_item(con, description="Milk 2%", sku="MILK-1", vendor="HEB") embed = FakeEmbed() from app.match import embed_catalog_row embed_catalog_row(con, settings, embed, cid) extract = ReceiptExtract(vendor="HEB", line_items=[]) item = LineItem(description="2% milk", sku="MILK-1") hit = match_line_item(con, settings, embed, extract, item) assert hit.band is MatchBand.exact assert hit.catalog_id == cid con.close() def test_knn_auto(settings: Settings) -> None: try: con = connect(settings.db_path) init_schema(con, settings) except VecLoadError: pytest.skip("sqlite-vec not loadable") embed = FakeEmbed() milk = add_catalog_item(con, description="organic milk", sku="X", vendor="HEB") other = add_catalog_item(con, description="bolts", sku="Y", vendor="Ace") from app.match import embed_catalog_row embed_catalog_row(con, settings, embed, milk) embed_catalog_row(con, settings, embed, other) extract = ReceiptExtract(vendor="HEB", line_items=[]) item = LineItem(description="milk") hit = match_line_item(con, settings, embed, extract, item) assert hit.catalog_id == milk assert hit.band in {MatchBand.auto, MatchBand.review} con.close()