Files
Kazier/backend/repository/operations_test.go
T
m a6b7f2002b
CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
fix backend audit findings (auth, upload, sync, data, ocr, contract)
2026-09-21 19:59:52 +02:00

201 lines
6.8 KiB
Go

package repository
import (
"errors"
"testing"
"github.com/kazier/backend/dbtest"
)
func newTestOperations(t *testing.T) (*Operations, *Devices) {
t.Helper()
conn := dbtest.OpenTestDatabase(t, repositoryTestURL)
return &Operations{DB: conn}, &Devices{DB: conn}
}
func TestOperationsRecordAndApplied(t *testing.T) {
o, devices := newTestOperations(t)
deviceID := NewID()
if err := devices.Upsert(deviceID); err != nil {
t.Fatalf("register device: %v", err)
}
opID := NewID()
applied, err := o.Applied(deviceID, opID)
if err != nil || applied {
t.Fatalf("avant record : applied=%v err=%v", applied, err)
}
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), []byte(`{"name":"x"}`)); err != nil {
t.Fatalf("record: %v", err)
}
applied, err = o.Applied(deviceID, opID)
if err != nil || !applied {
t.Fatalf("après record : applied=%v err=%v", applied, err)
}
// Rejeu → ON CONFLICT DO NOTHING, aucune erreur.
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), []byte(`null`)); err != nil {
t.Fatalf("rejeu: %v", err)
}
if applied, _ := o.Applied(deviceID, opID); !applied {
t.Error("rejeu : l'op doit rester appliquée")
}
// Idempotence par (device_id, operation_id) : un autre device peut réutiliser
// le même operation_id sans conflit (les deux rows sont distinctes).
device02 := NewID()
if err := devices.Upsert(device02); err != nil {
t.Fatalf("register device 2: %v", err)
}
if err := o.Record(device02, opID, "create_resource", "resource", nil, NewID(), []byte(`{}`)); err != nil {
t.Errorf("même op_id sur un autre device: %v", err)
}
}
func TestOperationsRecordRejectsInvalidOperationID(t *testing.T) {
o, devices := newTestOperations(t)
if err := devices.Upsert(NewID()); err != nil {
t.Fatalf("register device: %v", err)
}
err := o.Record(NewID(), "UPPERCASENOT32", "create_resource", "resource", nil, NewID(), []byte(`{}`))
if !errors.Is(err, ErrInvalidOperationID) {
t.Errorf("operation_id invalide : attendu ErrInvalidOperationID, got %v", err)
}
}
func TestOperationsRecordRejectsInvalidPayload(t *testing.T) {
o, devices := newTestOperations(t)
deviceID := NewID()
if err := devices.Upsert(deviceID); err != nil {
t.Fatalf("register device: %v", err)
}
opID := NewID()
// Un payload non-JSON est refusé (pas de normalisation silencieuse vers {}) :
// l'appelant (service) doit rejeter en amont en INVALID_REQUEST.
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), []byte(`not-json`)); err == nil {
t.Error("payload non-JSON : erreur attendue, got nil")
}
if applied, _ := o.Applied(deviceID, opID); applied {
t.Error("record refusé : l'op ne doit pas être tracée")
}
}
func TestOperationsRecordStoresPayloadHash(t *testing.T) {
o, devices := newTestOperations(t)
deviceID := NewID()
if err := devices.Upsert(deviceID); err != nil {
t.Fatalf("register device: %v", err)
}
opID := NewID()
payload := []byte(`{"name":"x"}`)
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, NewID(), payload); err != nil {
t.Fatalf("record: %v", err)
}
stored, err := o.Get(deviceID, opID)
if err != nil {
t.Fatalf("get: %v", err)
}
if stored.Status != OpStatusApplied {
t.Errorf("status = %q, attendu %q", stored.Status, OpStatusApplied)
}
want := CanonicalPayloadHash(payload)
if stored.PayloadHash != want {
t.Errorf("payload_hash = %q, attendu %q", stored.PayloadHash, want)
}
if len(stored.PayloadHash) != 64 {
t.Errorf("payload_hash attendu 64-hex, got %q", stored.PayloadHash)
}
}
func TestOperationsClaimAndMarkApplied(t *testing.T) {
o, devices := newTestOperations(t)
deviceID := NewID()
if err := devices.Upsert(deviceID); err != nil {
t.Fatalf("register device: %v", err)
}
opID := NewID()
payload := []byte(`{"name":"x"}`)
// Premier claim → pris.
claimed, err := o.Claim(deviceID, opID, "create_resource", "resource", nil, NewID(), payload)
if err != nil || !claimed {
t.Fatalf("claim 1: claimed=%v err=%v", claimed, err)
}
// La trace brouillon est en status failed (pas encore confirmée).
stored, err := o.Get(deviceID, opID)
if err != nil {
t.Fatalf("get: %v", err)
}
if stored.Status != OpStatusFailed {
t.Errorf("status après claim = %q, attendu %q", stored.Status, OpStatusFailed)
}
// Second claim (batch concurrent) → perdu, chemin rejeu.
claimed, err = o.Claim(deviceID, opID, "create_resource", "resource", nil, NewID(), payload)
if err != nil || claimed {
t.Fatalf("claim 2: claimed=%v err=%v", claimed, err)
}
// Promotion après apply réussi.
if err := o.MarkApplied(deviceID, opID); err != nil {
t.Fatalf("mark applied: %v", err)
}
stored, err = o.Get(deviceID, opID)
if err != nil {
t.Fatalf("get 2: %v", err)
}
if stored.Status != OpStatusApplied {
t.Errorf("status après promote = %q, attendu %q", stored.Status, OpStatusApplied)
}
// MarkApplied idempotent (re-promote sans effet).
if err := o.MarkApplied(deviceID, opID); err != nil {
t.Errorf("re-promote: %v", err)
}
}
func TestStoredOpMatchesReplay(t *testing.T) {
o, devices := newTestOperations(t)
deviceID := NewID()
if err := devices.Upsert(deviceID); err != nil {
t.Fatalf("register device: %v", err)
}
opID := NewID()
resID := NewID()
payload := []byte(`{"name":"x"}`)
if err := o.Record(deviceID, opID, "create_resource", "resource", nil, resID, payload); err != nil {
t.Fatalf("record: %v", err)
}
stored, err := o.Get(deviceID, opID)
if err != nil {
t.Fatalf("get: %v", err)
}
// Identique (espaces neutres via hash canonique) → match.
if err := stored.MatchesReplay("create_resource", resID, []byte("{ \"name\" : \"x\" }")); err != nil {
t.Errorf("rejeu identique: %v", err)
}
// Payload muté → ErrPayloadMismatch.
if err := stored.MatchesReplay("create_resource", resID, []byte(`{"name":"y"}`)); !errors.Is(err, ErrPayloadMismatch) {
t.Errorf("payload muté : attendu ErrPayloadMismatch, got %v", err)
}
// Type muté → ErrPayloadMismatch.
if err := stored.MatchesReplay("delete_resource", resID, payload); !errors.Is(err, ErrPayloadMismatch) {
t.Errorf("type muté : attendu ErrPayloadMismatch, got %v", err)
}
// Ressource mutée → ErrPayloadMismatch.
if err := stored.MatchesReplay("create_resource", NewID(), payload); !errors.Is(err, ErrPayloadMismatch) {
t.Errorf("ressource mutée : attendu ErrPayloadMismatch, got %v", err)
}
// Ligne héritée sans payload_hash (pré-000013) : hash recalculé depuis
// le payload stocké → match quand identique.
stored.PayloadHash = ""
if err := stored.MatchesReplay("create_resource", resID, payload); err != nil {
t.Errorf("ligne héritée identique: %v", err)
}
if err := stored.MatchesReplay("create_resource", resID, []byte(`{"name":"z"}`)); !errors.Is(err, ErrPayloadMismatch) {
t.Errorf("ligne héritée mutée : attendu ErrPayloadMismatch, got %v", err)
}
}