Backend : CorrectFacts (editor+, validation Go -> 400 jamais 500), op outbox correct_facts (full-replace jeu user), PATCH /files/:id/facts (renvoie jeu complet). Tests service + sync + handlers. Mobile (Room v20) : table resource_facts + FactsDao, FactsRepository (sync remplace-en-bloc, correction optimiste + outbox atomique), fiche « Ce qu'on a compris » sur le viewer (chips, suggéré grisé, édition par type, reset auto), bouton viewer si processed, FR/EN. Contrat docs/api-v1.md, V3.md (S1-S3 livrés, FTS -> v21).
169 lines
6.1 KiB
Go
169 lines
6.1 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/kazier/backend/repository"
|
|
"github.com/kazier/backend/service"
|
|
)
|
|
|
|
// TestFactsEndpoint : GET /files/:id/facts — faits auto visibles (viewer+),
|
|
// [] si rien d'extractible, 404 si trashé/absent/étranger.
|
|
func TestFactsEndpoint(t *testing.T) {
|
|
r, store, repo := setup(t)
|
|
device := repository.NewID()
|
|
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "fa"), "facts-test-password", device)
|
|
|
|
fileID := repository.NewID()
|
|
if err := repo.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
|
t.Fatalf("insert: %v", err)
|
|
}
|
|
if err := repo.Resources.UpdateOcrText(userID, fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
|
t.Fatalf("ocr text: %v", err)
|
|
}
|
|
extractSvc := service.NewExtract(repo)
|
|
if err := extractSvc.ProcessText(fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
|
t.Fatalf("extract: %v", err)
|
|
}
|
|
_ = store
|
|
|
|
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/facts", token, nil, "")
|
|
env := expectOK(t, rec, "facts-get")
|
|
var facts []struct {
|
|
FactType string `json:"factType"`
|
|
ValueText string `json:"valueText"`
|
|
ValueNum *float64 `json:"valueNum"`
|
|
Confidence float64 `json:"confidence"`
|
|
Origin string `json:"origin"`
|
|
}
|
|
if err := json.Unmarshal(env.Data, &facts); err != nil {
|
|
t.Fatalf("facts unmarshal: %v", err)
|
|
}
|
|
if len(facts) == 0 {
|
|
t.Fatal("faits attendus, got []")
|
|
}
|
|
types := map[string]bool{}
|
|
for _, f := range facts {
|
|
types[f.FactType] = true
|
|
if f.Origin != "auto" {
|
|
t.Errorf("origine = %q, attendu auto", f.Origin)
|
|
}
|
|
}
|
|
for _, want := range []string{"reference", "amount", "date", "doc_type"} {
|
|
if !types[want] {
|
|
t.Errorf("fait %q manquant: %+v", want, facts)
|
|
}
|
|
}
|
|
|
|
// Fichier visible sans faits → [] (200, jamais 404).
|
|
emptyID := repository.NewID()
|
|
if err := repo.Resources.InsertFile(userID, emptyID, "vide.txt", "", 0, nil, nil); err != nil {
|
|
t.Fatalf("insert empty: %v", err)
|
|
}
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+emptyID+"/facts", token, nil, "")
|
|
env = expectOK(t, rec, "facts-empty")
|
|
if string(env.Data) != "[]" {
|
|
t.Fatalf("sans faits : attendu [], got %s", env.Data)
|
|
}
|
|
|
|
// Trashé → 404 (comme l'OCR).
|
|
if _, err := store.Trash(userID, fileID); err != nil {
|
|
t.Fatalf("trash: %v", err)
|
|
}
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+fileID+"/facts", token, nil, "")
|
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-trashed")
|
|
|
|
// Étranger → 404.
|
|
deviceB := repository.NewID()
|
|
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "fb"), "facts-test-password", deviceB)
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+emptyID+"/facts", tokenB, nil, "")
|
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-foreign")
|
|
}
|
|
|
|
// TestCorrectFactsEndpoint : PATCH /files/:id/facts — remplace le jeu user
|
|
// (editor+ = owner ici), renvoie le jeu complet ; 400 sur corps invalide ;
|
|
// 404 si trashé/étranger.
|
|
func TestCorrectFactsEndpoint(t *testing.T) {
|
|
r, store, repo := setup(t)
|
|
device := repository.NewID()
|
|
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "fc"), "facts-test-password", device)
|
|
|
|
fileID := repository.NewID()
|
|
if err := repo.Resources.InsertFile(userID, fileID, "facture.pdf", "", 0, nil, nil); err != nil {
|
|
t.Fatalf("insert: %v", err)
|
|
}
|
|
extractSvc := service.NewExtract(repo)
|
|
if err := extractSvc.ProcessText(fileID, "EDF\nFacture n° F-1 du 01/02/2026\nTotal : 10,00 €\n"); err != nil {
|
|
t.Fatalf("extract: %v", err)
|
|
}
|
|
_ = store
|
|
|
|
patch := func(body string) (int, envelope) {
|
|
t.Helper()
|
|
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token, []byte(body), "application/json")
|
|
var env envelope
|
|
if rec.Code == http.StatusOK {
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
|
|
t.Fatalf("patch unmarshal: %v body=%s", err, rec.Body.String())
|
|
}
|
|
}
|
|
return rec.Code, env
|
|
}
|
|
|
|
// Correction : émetteur fixé par l'utilisateur.
|
|
code, env := patch(`{"facts":[{"factType":"issuer","valueText":"Vrai Fournisseur","confidence":1}]}`)
|
|
if code != http.StatusOK {
|
|
t.Fatalf("patch: attendu 200, got %d", code)
|
|
}
|
|
var facts []struct {
|
|
FactType string `json:"factType"`
|
|
ValueText string `json:"valueText"`
|
|
Origin string `json:"origin"`
|
|
}
|
|
if err := json.Unmarshal(env.Data, &facts); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
foundUser, foundAuto := false, false
|
|
for _, f := range facts {
|
|
if f.FactType == "issuer" && f.Origin == "user" && f.ValueText == "Vrai Fournisseur" {
|
|
foundUser = true
|
|
}
|
|
if f.Origin == "auto" {
|
|
foundAuto = true
|
|
}
|
|
}
|
|
if !foundUser || !foundAuto {
|
|
t.Fatalf("jeu après correction inattendu: %+v", facts)
|
|
}
|
|
|
|
// Corps invalides → 400.
|
|
for _, body := range []string{
|
|
`{}`,
|
|
`{"facts":[{"factType":"couleur","valueText":"bleu"}]}`,
|
|
`{"facts":[{"factType":"date","valueDate":"12/03/2026"}]}`,
|
|
`{"facts":[{"factType":"issuer"}]}`,
|
|
`not-json`,
|
|
} {
|
|
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token, []byte(body), "application/json")
|
|
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "facts-patch-400:"+body)
|
|
}
|
|
|
|
// Étranger (viewer simulé : partage + snapshot hors scope ici, un autre
|
|
// user sans grant suffit — CorrectFacts exige editor+) → 404.
|
|
deviceB := repository.NewID()
|
|
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "fd"), "facts-test-password", deviceB)
|
|
rec, _ := doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", tokenB,
|
|
[]byte(`{"facts":[{"factType":"issuer","valueText":"X"}]}`), "application/json")
|
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-patch-foreign")
|
|
|
|
// Trashé → 404.
|
|
if _, err := store.Trash(userID, fileID); err != nil {
|
|
t.Fatalf("trash: %v", err)
|
|
}
|
|
rec, _ = doRequest(t, r, http.MethodPatch, "/api/v1/files/"+fileID+"/facts", token,
|
|
[]byte(`{"facts":[{"factType":"issuer","valueText":"X"}]}`), "application/json")
|
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "facts-patch-trashed")
|
|
}
|