V2-4 : placements multi-device (report_bytes, tables 000015, copies UI)

This commit is contained in:
venture
2026-09-25 19:45:33 +02:00
parent 0f90aad938
commit a91ddf7aa8
42 changed files with 714 additions and 25 deletions
+73
View File
@@ -1485,3 +1485,76 @@ func TestDeleteAbsentIsIdempotentNoop(t *testing.T) {
t.Errorf("delete absent: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
}
}
func TestApplyBatchReportBytes(t *testing.T) {
s := newServiceStore(t)
ownerID := mustCreateUser(t, s.Repository, "report-owner")
granteeID := mustCreateUser(t, s.Repository, "report-grantee")
outsiderID := mustCreateUser(t, s.Repository, "report-outsider")
deviceID := mustRegisterDevice(t, s.Repository, shareTestDeviceID)
otherDevice := mustRegisterDevice(t, s.Repository, "b0000000000000000000000000000001")
fileID := repository.NewID()
if _, err := s.ApplyBatch(ownerID, deviceID, []SyncOperation{
syncOp(repository.NewID(), fileID, OpCreateResource, "file", map[string]any{"name": "r.txt"}),
syncOp(repository.NewID(), fileID, OpShare, "file", map[string]any{"granteeUserId": granteeID, "access": "viewer"}),
}); err != nil {
t.Fatalf("setup: %v", err)
}
report := func(user, device, res string, hasBytes bool) (SyncResult, error) {
return s.ApplyBatch(user, device, []SyncOperation{
syncOp(repository.NewID(), res, OpReportBytes, "file", map[string]any{"hasBytes": hasBytes}),
})
}
// Owner + grantee (viewer) peuvent reporter ; l'owner lit serveur + device.
if result, err := report(ownerID, deviceID, fileID, true); err != nil || result.Applied != 1 || result.Failed != nil {
t.Fatalf("report owner: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
}
if result, err := report(granteeID, otherDevice, fileID, true); err != nil || result.Applied != 1 || result.Failed != nil {
t.Fatalf("report grantee: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
}
placements, err := s.ListPlacements(ownerID, fileID)
if err != nil {
t.Fatalf("ListPlacements: %v", err)
}
if len(placements) != 3 {
t.Fatalf("attendu serveur + 2 devices, got %+v", placements)
}
if placements[0].LocationType != "server" || placements[0].HasBytes {
t.Errorf("entrée serveur = %+v, attendu hasBytes=false", placements[0])
}
// Changement d'état (drop) avec un NOUVEL operation_id ; hasBytes manquant → halt.
if result, err := report(ownerID, deviceID, fileID, false); err != nil || result.Applied != 1 || result.Failed != nil {
t.Fatalf("report drop: applied=%d failed=%+v err=%v", result.Applied, result.Failed, err)
}
bad, err := s.ApplyBatch(ownerID, deviceID, []SyncOperation{
syncOp(repository.NewID(), fileID, OpReportBytes, "file", map[string]any{}),
})
if err != nil || bad.Applied != 0 || bad.Failed == nil || bad.Failed.Code != "INVALID_REQUEST" {
t.Errorf("hasBytes manquant: attendu halt INVALID_REQUEST, got %+v err=%v", bad, err)
}
// Outsider (invisible) → halt NOT_FOUND, sans énumération.
denied, err := report(outsiderID, otherDevice, fileID, true)
if err != nil || denied.Applied != 0 || denied.Failed == nil || denied.Failed.Code != "NOT_FOUND" {
t.Errorf("report outsider: attendu halt NOT_FOUND, got %+v err=%v", denied, err)
}
// Rejeu à l'identique → idempotent (skip, pas d'erreur).
replayID := repository.NewID()
first, err := s.ApplyBatch(ownerID, deviceID, []SyncOperation{
syncOp(replayID, fileID, OpReportBytes, "file", map[string]any{"hasBytes": true}),
})
if err != nil || first.Applied != 1 || first.Failed != nil {
t.Fatalf("report initial: %+v err=%v", first, err)
}
second, err := s.ApplyBatch(ownerID, deviceID, []SyncOperation{
syncOp(replayID, fileID, OpReportBytes, "file", map[string]any{"hasBytes": true}),
})
if err != nil || second.Applied != 1 || second.Failed != nil {
t.Errorf("rejeu identique: attendu skip idempotent, got %+v err=%v", second, err)
}
}