V3 S3 : correction des faits (fiche document + correct_facts)
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).
This commit is contained in:
@@ -81,3 +81,88 @@ func TestFactsEndpoint(t *testing.T) {
|
||||
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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user