V3 S4 : recherche FTS + facettes (serveur + mobile)
Backend : search_tsv french + GIN (000020), SearchDocumentsVisible (nom + TS + faits, rang nom > TS > fait sûr > fait douteux, jamais d'exclusion), facettes issuer/type/montant/période sur GET /files/search (400 si malformée). Tests repo + handlers + migrations. Mobile (Room v21) : FTS4 files_fts + triggers partagés (fresh-install/migration, entité déclarée pour KSP, SQL aligné 21.json), searchDocuments offline (même rang, accents préservés) + UI filtres (émetteur, types chips, montants, période ISO, compteur, effacer). Contrat docs/api-v1.md, V3.md (S4 livré, migrations renumérotées links->000021, token->000022).
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/dbtest"
|
||||
)
|
||||
|
||||
const searchTestURL = "postgres://kazier:kazier@localhost:5432/kazier_search_test?sslmode=disable"
|
||||
|
||||
func newSearchRepo(t *testing.T) *Repository {
|
||||
t.Helper()
|
||||
return NewRepository(dbtest.OpenTestDatabase(t, searchTestURL))
|
||||
}
|
||||
|
||||
func mustSearchUser(t *testing.T, repo *Repository, username string) string {
|
||||
t.Helper()
|
||||
id, err := repo.Users.Create(username, username, "test-hash", false)
|
||||
if err != nil {
|
||||
t.Fatalf("create user: %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func seedDoc(t *testing.T, repo *Repository, userID, name, ocr string, facts []FactParam) string {
|
||||
t.Helper()
|
||||
id := NewID()
|
||||
if err := repo.Resources.InsertFile(userID, id, name, "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert %q: %v", name, err)
|
||||
}
|
||||
if ocr != "" {
|
||||
if err := repo.Resources.UpdateOcrText(userID, id, ocr); err != nil {
|
||||
t.Fatalf("ocr %q: %v", name, err)
|
||||
}
|
||||
}
|
||||
if len(facts) > 0 {
|
||||
if err := repo.Resources.UpsertAutoFacts(id, facts); err != nil {
|
||||
t.Fatalf("facts %q: %v", name, err)
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func f64(v float64) *float64 { return &v }
|
||||
func fstr(v string) *string { return &v }
|
||||
|
||||
func searchIDs(t *testing.T, repo *Repository, userID, q string, facets SearchFacets) []string {
|
||||
t.Helper()
|
||||
rows, _, err := repo.Resources.SearchDocumentsVisible(userID, q, facets, 50, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("search %q: %v", q, err)
|
||||
}
|
||||
ids := make([]string, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
ids = append(ids, r.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func containsID(ids []string, want string) bool {
|
||||
for _, id := range ids {
|
||||
if id == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestSearchDocumentsFTS(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-fts")
|
||||
|
||||
// Nom opaque : trouvable uniquement via l'OCR (TS français).
|
||||
scan := seedDoc(t, repo, userID, "scan_042.pdf",
|
||||
"Attestation d'assurance MMA pour l'année 2026", nil)
|
||||
plain := seedDoc(t, repo, userID, "assurance-vie.txt", "", nil)
|
||||
|
||||
ids := searchIDs(t, repo, userID, "assurance", SearchFacets{})
|
||||
if !containsID(ids, scan) {
|
||||
t.Errorf("scan_042.pdf introuvable via OCR: %v", ids)
|
||||
}
|
||||
if !containsID(ids, plain) {
|
||||
t.Errorf("assurance-vie.txt introuvable via nom: %v", ids)
|
||||
}
|
||||
|
||||
// Stemming français : "factures" matche "facture".
|
||||
fact := seedDoc(t, repo, userID, "doc.pdf", "Votre facture de mars", nil)
|
||||
ids = searchIDs(t, repo, userID, "factures", SearchFacets{})
|
||||
if !containsID(ids, fact) {
|
||||
t.Errorf("stemming FR manquant: %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsFacets(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-facets")
|
||||
|
||||
edf := seedDoc(t, repo, userID, "edf.pdf", "Facture EDF", []FactParam{
|
||||
{Type: "issuer", ValueText: "EDF", Confidence: 0.9},
|
||||
{Type: "amount", ValueText: "84.20 €", ValueNum: f64(84.20), Confidence: 0.9},
|
||||
{Type: "due_date", ValueText: "2026-04-12", ValueDate: fstr("2026-04-12"), Confidence: 0.8},
|
||||
{Type: "doc_type", ValueText: "facture", Confidence: 0.8},
|
||||
})
|
||||
engie := seedDoc(t, repo, userID, "engie.pdf", "Facture Engie", []FactParam{
|
||||
{Type: "issuer", ValueText: "ENGIE", Confidence: 0.9},
|
||||
{Type: "amount", ValueText: "120.00 €", ValueNum: f64(120), Confidence: 0.9},
|
||||
{Type: "doc_type", ValueText: "facture", Confidence: 0.8},
|
||||
})
|
||||
|
||||
ids := searchIDs(t, repo, userID, "facture", SearchFacets{Issuer: "edf"})
|
||||
if !containsID(ids, edf) || containsID(ids, engie) {
|
||||
t.Errorf("facette issuer: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{MinAmount: f64(100)})
|
||||
if !containsID(ids, engie) || containsID(ids, edf) {
|
||||
t.Errorf("facette min_amount: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{DocType: "facture"})
|
||||
if !containsID(ids, edf) || !containsID(ids, engie) {
|
||||
t.Errorf("facette doc_type: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{From: "2026-04-01", To: "2026-04-30"})
|
||||
if !containsID(ids, edf) || containsID(ids, engie) {
|
||||
t.Errorf("facette période: %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsLowConfidenceDemotes(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-humble")
|
||||
|
||||
// Fait douteux (0.3) : trouvé quand même (jamais exclu), mais après le
|
||||
// match franc (nom) — humble par design.
|
||||
sure := seedDoc(t, repo, userID, "EDF-contrat.pdf", "", nil)
|
||||
doubt := seedDoc(t, repo, userID, "scan_x.pdf", "courrier", []FactParam{
|
||||
{Type: "issuer", ValueText: "EDF", Confidence: 0.3},
|
||||
})
|
||||
|
||||
ids := searchIDs(t, repo, userID, "EDF", SearchFacets{})
|
||||
if !containsID(ids, doubt) {
|
||||
t.Fatalf("fait douteux exclu (interdit): %v", ids)
|
||||
}
|
||||
if len(ids) < 2 || ids[0] != sure || ids[1] != doubt {
|
||||
t.Errorf("ordre de rang inattendu (franc avant douteux): %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsTrashedExcluded(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-trash")
|
||||
id := seedDoc(t, repo, userID, "poubelle.pdf", "contenu unique xyz", nil)
|
||||
|
||||
if ids := searchIDs(t, repo, userID, "xyz", SearchFacets{}); !containsID(ids, id) {
|
||||
t.Fatalf("introuvable avant trash: %v", ids)
|
||||
}
|
||||
if err := repo.Resources.TrashResource(userID, id); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
if ids := searchIDs(t, repo, userID, "xyz", SearchFacets{}); containsID(ids, id) {
|
||||
t.Errorf("trashé encore visible: %v", ids)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user