Backend : resource_links + scan_session_id (000021), réconciliation déterministe (same_scan outbox, doublons SHA-256 à l'upload + backfill, same_issuer/référence via faits effectifs, verdicts préservés, élagage auto), GET /files/:id/relations + verdict (owner-only), GET /duplicates (groupes + gain récupérable). Tests service + handlers + migrations. Mobile (Room v22) : links + collections, scanSessionId dans les creates de scan, LinksRepository (sync + verdicts REST), « Voir aussi » dans la fiche (verdict infirmer/re-confirmer), écran Doublons (garder le plus récent), collections locales (sauver/rejouer/supprimer). FR/EN. Contrat docs/api-v1.md (/relations, pas /links — pris par le partage), V3.md (S5 livré, version_of non matérialisé).
94 lines
3.7 KiB
Go
94 lines
3.7 KiB
Go
package handlers_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/kazier/backend/repository"
|
|
)
|
|
|
|
// TestRelationsEndpoints : GET /files/:id/relations (réconciliation à la
|
|
// lecture) + POST verdict (confirmé/infirmé, 400/404).
|
|
func TestRelationsEndpoints(t *testing.T) {
|
|
r, _, repo := setup(t)
|
|
device := repository.NewID()
|
|
token, userID := registerAndLogin(t, r, repo, testUserUsername(device, "rl"), "relations-test-password", device)
|
|
|
|
session := repository.NewID()
|
|
page1, page2 := repository.NewID(), repository.NewID()
|
|
for _, id := range []string{page1, page2} {
|
|
if err := repo.Resources.InsertFile(userID, id, id+".png", "", 0, nil, nil); err != nil {
|
|
t.Fatalf("insert: %v", err)
|
|
}
|
|
if err := repo.Resources.SetScanSession(userID, id, session); err != nil {
|
|
t.Fatalf("session: %v", err)
|
|
}
|
|
}
|
|
|
|
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", token, nil, "")
|
|
env := expectOK(t, rec, "relations-get")
|
|
var rels []struct {
|
|
ID string `json:"id"`
|
|
OtherID string `json:"otherId"`
|
|
Type string `json:"type"`
|
|
Confidence float64 `json:"confidence"`
|
|
CreatedBy string `json:"createdBy"`
|
|
OtherName string `json:"otherName"`
|
|
}
|
|
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if len(rels) != 1 || rels[0].Type != "same_scan" || rels[0].OtherID != page2 {
|
|
t.Fatalf("relations inattendues: %+v", rels)
|
|
}
|
|
|
|
// Verdict infirmé : arête conservée à 0.0, origine user.
|
|
body := fmt.Sprintf(`{"otherId":%q,"linkType":"same_scan","confirmed":false}`, page2)
|
|
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", token, []byte(body), "application/json")
|
|
env = expectOK(t, rec, "relations-verdict")
|
|
rels = nil
|
|
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
|
t.Fatalf("unmarshal verdict: %v", err)
|
|
}
|
|
if len(rels) != 1 || rels[0].Confidence != 0 || rels[0].CreatedBy != "user" {
|
|
t.Fatalf("verdict inattendu: %+v", rels)
|
|
}
|
|
|
|
// Type inconnu → 400 ; paire non-hex → 400 ; étranger → 404.
|
|
for _, tc := range []struct {
|
|
name, body string
|
|
status int
|
|
code string
|
|
}{
|
|
{"type", fmt.Sprintf(`{"otherId":%q,"linkType":"copains","confirmed":true}`, page2), http.StatusBadRequest, "INVALID_REQUEST"},
|
|
{"hex", `{"otherId":"nope","linkType":"same_scan","confirmed":true}`, http.StatusBadRequest, "INVALID_REQUEST"},
|
|
} {
|
|
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", token, []byte(tc.body), "application/json")
|
|
expectError(t, rec, tc.status, tc.code, "relations-verdict-400:"+tc.name)
|
|
}
|
|
|
|
deviceB := repository.NewID()
|
|
tokenB, _ := registerAndLogin(t, r, repo, testUserUsername(deviceB, "rb"), "relations-test-password", deviceB)
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", tokenB, nil, "")
|
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "relations-foreign")
|
|
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/files/"+page1+"/relations/verdict", tokenB,
|
|
[]byte(fmt.Sprintf(`{"otherId":%q,"linkType":"same_scan","confirmed":true}`, page2)), "application/json")
|
|
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "relations-verdict-foreign")
|
|
|
|
// Trashé → 404 (l'autre bout trashé disparaît aussi de la liste).
|
|
if err := repo.Resources.TrashResource(userID, page2); err != nil {
|
|
t.Fatalf("trash: %v", err)
|
|
}
|
|
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/"+page1+"/relations", token, nil, "")
|
|
env = expectOK(t, rec, "relations-trashed-other")
|
|
rels = nil
|
|
if err := json.Unmarshal(env.Data, &rels); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if len(rels) != 0 {
|
|
t.Fatalf("bout trashé encore listé: %+v", rels)
|
|
}
|
|
}
|