Files
Kazier/backend/extract/rules_test.go
T
venture 95b4cf79cf V3 S1-S2 : corbeille + versions + extract générique
S1 filet (backend + mobile) : corbeille 30j (deleted_at réutilisé,
trash/restore récursifs, purge auto serveur), versions N=3 à chaque
overwrite, ops outbox trash/restore/purge (delete legacy mappé), Room
v19 (trashed_at), Deleters -> trash, écran Corbeille, walk guards.

S2 extract (backend) : package extract (règles FR stdlib, corpus
testé), resource_facts 000019 (origin auto/user), hook worker OCR +
backfill boot, GET /files/:id/facts (viewer+).

Contrat docs/api-v1.md étendu, V3.md (vision + plan).
2026-09-26 10:14:07 +02:00

184 lines
5.0 KiB
Go

package extract
import (
"testing"
)
func factsByType(facts []Fact) map[FactType][]Fact {
out := map[FactType][]Fact{}
for _, f := range facts {
out[f.Type] = append(out[f.Type], f)
}
return out
}
func hasText(facts []Fact, want string) bool {
for _, f := range facts {
if f.ValueText == want {
return true
}
}
return false
}
const edfBill = `EDF - SERVICE CLIENTS
TSA 12345
Facture n° FA-2026-0031 du 12/03/2026
Période : mars 2026
Montant total TTC : 1 234,56 €
À payer avant le 12/04/2026
Référence client : 123456789
`
func TestFactureEDF(t *testing.T) {
by := factsByType(FromText(edfBill))
if !hasText(by[FactReference], "facture:FA-2026-0031") {
t.Errorf("référence facture manquante: %+v", by[FactReference])
}
if !hasText(by[FactDate], "2026-03-12") {
t.Errorf("date émission manquante: %+v", by[FactDate])
}
if !hasText(by[FactDueDate], "2026-04-12") {
t.Errorf("échéance manquante: %+v", by[FactDueDate])
}
if !hasText(by[FactPeriod], "2026-03") {
t.Errorf("période manquante: %+v", by[FactPeriod])
}
found := false
for _, f := range by[FactAmount] {
if f.HasNum && f.ValueNum == 1234.56 && f.Confidence >= 0.9 {
found = true
}
}
if !found {
t.Errorf("montant 1234.56 (conf 0.9) manquant: %+v", by[FactAmount])
}
if !hasText(by[FactIssuer], "EDF - SERVICE CLIENTS") {
t.Errorf("émetteur manquant: %+v", by[FactIssuer])
}
if !hasText(by[FactDocType], "facture") {
t.Errorf("doc_type facture manquant: %+v", by[FactDocType])
}
}
const bulletin = `BULLETIN DE SALAIRE
DUPONT Jean
Période : janvier 2026
Salaire brut : 2 845,00 €
Net social : 2 210,35 €
SIRET 123 456 789 00012
`
func TestBulletinSalaire(t *testing.T) {
by := factsByType(FromText(bulletin))
if !hasText(by[FactDocType], "bulletin") {
t.Errorf("doc_type bulletin manquant: %+v", by[FactDocType])
}
if !hasText(by[FactPeriod], "2026-01") {
t.Errorf("période manquante: %+v", by[FactPeriod])
}
if !hasText(by[FactReference], "siret:123 456 789 00012") {
t.Errorf("siret manquant: %+v", by[FactReference])
}
amounts := map[float64]bool{}
for _, f := range by[FactAmount] {
if f.HasNum {
amounts[f.ValueNum] = true
}
}
if !amounts[2845.00] || !amounts[2210.35] {
t.Errorf("montants bulletin manquants: %+v", by[FactAmount])
}
}
const ordonnance = `Dr MARTIN Sophie
Ordonnance du 3 mars 2026
Doliprane 1000mg — 1 matin et soir pendant 7 jours
`
func TestOrdonnance(t *testing.T) {
by := factsByType(FromText(ordonnance))
if !hasText(by[FactDocType], "ordonnance") {
t.Errorf("doc_type ordonnance manquant: %+v", by[FactDocType])
}
if !hasText(by[FactDate], "2026-03-03") {
t.Errorf("date manquante: %+v", by[FactDate])
}
if len(by[FactAmount]) != 0 {
t.Errorf("1000mg ne doit pas être un montant: %+v", by[FactAmount])
}
}
const rib = `RELEVÉ D'IDENTITÉ BANCAIRE
Titulaire : DUPONT Jean
IBAN : FR76 1234 5678 9012 3456 7890 123
BIC : AGRIFRPP
`
func TestRIBMasque(t *testing.T) {
by := factsByType(FromText(rib))
if !hasText(by[FactDocType], "rib") {
t.Errorf("doc_type rib manquant: %+v", by[FactDocType])
}
if !hasText(by[FactReference], "rib:••••0123") {
t.Errorf("IBAN masqué manquant: %+v", by[FactReference])
}
// L'IBAN complet ne doit jamais fuiter dans les faits.
for _, f := range by[FactReference] {
if len(f.ValueText) > 20 {
t.Errorf("fuite IBAN en clair: %q", f.ValueText)
}
}
}
func TestTexteVideEtBruit(t *testing.T) {
if got := FromText(""); len(got) != 0 {
t.Errorf("vide → %d faits, attendu 0", len(got))
}
// Bruit OCR sans structure : pas de doc_type, pas de montant.
by := factsByType(FromText("lorem ipsum\nbla bla 99\n///"))
if len(by[FactDocType]) != 0 {
t.Errorf("doc_type sur bruit: %+v", by[FactDocType])
}
if len(by[FactAmount]) != 0 {
t.Errorf("montant sur bruit: %+v", by[FactAmount])
}
}
func TestCourrierSimpleSansType(t *testing.T) {
// Tous les documents comptent : un courrier sans mots-clés métier donne
// des faits (date) mais aucun doc_type — jamais pénalisé.
by := factsByType(FromText("Bonjour,\nmerci de me recontacter.\nCordialement.\nParis, le 05/06/2026\n"))
if !hasText(by[FactDate], "2026-06-05") {
t.Errorf("date courrier manquante: %+v", by[FactDate])
}
if len(by[FactDocType]) != 0 {
t.Errorf("pas de doc_type attendu: %+v", by[FactDocType])
}
}
func TestAnneesCourtesEtFormats(t *testing.T) {
by := factsByType(FromText("Fait le 01.02.99\nÉchéance 15-08-26\n"))
if !hasText(by[FactDate], "1999-02-01") {
t.Errorf("année courte 99 → 1999: %+v", by[FactDate])
}
if !hasText(by[FactDueDate], "2026-08-15") {
t.Errorf("année courte 26 → 2026: %+v", by[FactDueDate])
}
}
func TestBornes(t *testing.T) {
// Dates invalides ignorées, pas de crash.
by := factsByType(FromText("le 45/13/2026 et le 00/00/0000"))
if len(by[FactDate])+len(by[FactDueDate]) != 0 {
t.Errorf("dates invalides extraites: %+v", by)
}
// Confiances toujours dans [0,1].
for _, f := range FromText(edfBill + bulletin + ordonnance + rib) {
if f.Confidence < 0 || f.Confidence > 1 {
t.Errorf("confiance hors bornes: %+v", f)
}
}
}