droboticflow/internal/api/object_sync_progress_test.go

154 lines
7.5 KiB
Go

package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"github.com/go-chi/chi/v5"
)
type progressCountingStore struct {
*store.MemoryStore
itemLists, summaries int
}
func (dataStore *progressCountingStore) ListObjectSyncItems(ctx context.Context, query domain.ObjectSyncListQuery) (domain.ObjectSyncItemPage, error) {
dataStore.itemLists++
return dataStore.MemoryStore.ListObjectSyncItems(ctx, query)
}
func (dataStore *progressCountingStore) SummarizeObjectSyncItems(ctx context.Context, ids []string) (map[string]domain.ObjectSyncItemSummary, error) {
dataStore.summaries++
return dataStore.MemoryStore.SummarizeObjectSyncItems(ctx, ids)
}
func progressRequest(method, action, deviceID string) *http.Request {
request := httptest.NewRequest(method, "/api/external/object-sync/sessions/session/"+action, nil)
routes := chi.NewRouteContext()
routes.URLParams.Add("sessionId", "session")
routes.URLParams.Add("action", action)
ctx := context.WithValue(request.Context(), chi.RouteCtxKey, routes)
ctx = context.WithValue(ctx, objectSyncCredentialContextKey{}, &objectSyncCredentialPrincipal{
Credential: domain.ObjectSyncCredentialRecord{ID: "credential"}, Device: domain.ObjectSyncDeviceRecord{ID: deviceID},
})
return request.WithContext(ctx)
}
func TestObjectSyncProgressAndFinishAvoidItemEnumeration(t *testing.T) {
dataStore := &progressCountingStore{MemoryStore: store.NewMemoryStore()}
instance := &server{store: dataStore, objectSyncEnabled: true}
session := domain.ObjectSyncSessionRecord{ID: "session", CredentialID: "credential", DeviceID: "device", Status: domain.ObjectSyncSessionRunning, Counts: domain.ObjectSyncSessionCounts{Manifest: 900}}
if err := dataStore.UpsertObjectSyncSession(t.Context(), session); err != nil {
t.Fatal(err)
}
statuses := []domain.ObjectSyncItemStatus{domain.ObjectSyncItemCommitted, domain.ObjectSyncItemKeptBoth, domain.ObjectSyncItemUnchanged, domain.ObjectSyncItemSkipped, domain.ObjectSyncItemDenied, domain.ObjectSyncItemFailed, domain.ObjectSyncItemAborted, domain.ObjectSyncItemExpired, domain.ObjectSyncItemCommitting}
items := make([]domain.ObjectSyncItemRecord, 0, 900)
for _, status := range statuses {
for index := 0; index < 100; index++ {
items = append(items, domain.ObjectSyncItemRecord{ID: fmt.Sprintf("%s-%d", status, index), SessionID: session.ID, Status: status, Action: "create", RelativePath: strings.Repeat("long-file-path/", 100)})
}
}
if err := dataStore.UpsertObjectSyncItems(t.Context(), items); err != nil {
t.Fatal(err)
}
writer := httptest.NewRecorder()
instance.handleExternalObjectSyncSessionStatus(writer, progressRequest(http.MethodGet, "status", "device"))
var response struct {
domain.ObjectSyncSessionRecord
Progress struct{ TotalItems, TerminalItems int64 }
}
if err := json.Unmarshal(writer.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if writer.Code != http.StatusOK || response.ID != session.ID || response.Progress.TotalItems != 900 || response.Progress.TerminalItems != 800 || response.Counts.Committed != 200 || response.Counts.Failed != 300 {
t.Fatalf("wrong live progress: %d %s", writer.Code, writer.Body.String())
}
if writer.Body.Len() > 4096 || strings.Contains(writer.Body.String(), "long-file-path") || dataStore.itemLists != 0 {
t.Fatalf("progress leaked item details: bytes=%d enumerations=%d", writer.Body.Len(), dataStore.itemLists)
}
t.Logf("900 item records: progress response %d bytes, full item enumerations %d", writer.Body.Len(), dataStore.itemLists)
writer = httptest.NewRecorder()
instance.handleExternalObjectSyncSessionStatus(writer, progressRequest(http.MethodGet, "status", "other-device"))
if writer.Code != http.StatusNotFound || dataStore.summaries != 1 {
t.Fatal("progress bypassed device isolation")
}
writer = httptest.NewRecorder()
instance.handleExternalObjectSyncSessionFinish(writer, progressRequest(http.MethodPost, "complete", "device"))
if writer.Code != http.StatusConflict {
t.Fatalf("active items finalized: %d %s", writer.Code, writer.Body.String())
}
for index := range items {
if items[index].Status == domain.ObjectSyncItemCommitting {
items[index].Status = domain.ObjectSyncItemCommitted
}
}
if err := dataStore.UpsertObjectSyncItems(t.Context(), items); err != nil {
t.Fatal(err)
}
writer = httptest.NewRecorder()
instance.handleExternalObjectSyncSessionFinish(writer, progressRequest(http.MethodPost, "complete", "device"))
var final domain.ObjectSyncSessionRecord
if err := json.Unmarshal(writer.Body.Bytes(), &final); err != nil {
t.Fatal(err)
}
if writer.Code != http.StatusOK || final.Status != domain.ObjectSyncSessionPartial || final.Counts.Committed != 300 || final.Counts.KeptBoth != 100 || final.Counts.Failed != 300 || dataStore.itemLists != 0 {
t.Fatalf("wrong finalization: %d %s enumerations=%d", writer.Code, writer.Body.String(), dataStore.itemLists)
}
// A management page gets one batched query, not stale persisted counters.
page := []domain.ObjectSyncSessionRecord{session, {ID: "empty", Status: domain.ObjectSyncSessionRunning}, final}
before := dataStore.summaries
if err := refreshObjectSyncSessionCounts(t.Context(), dataStore, page); err != nil {
t.Fatal(err)
}
if dataStore.summaries != before+1 || page[0].Counts.Committed != 300 || page[0].Counts.Manifest != 900 || page[2].Counts != final.Counts {
t.Fatalf("wrong batched management counts: %#v", page)
}
}
func TestObjectSyncCompletionPreservesManifestWithDerivedItems(t *testing.T) {
dataStore := store.NewMemoryStore()
instance := &server{store: dataStore, objectSyncEnabled: true}
session := domain.ObjectSyncSessionRecord{ID: "session", CredentialID: "credential", DeviceID: "device", Status: domain.ObjectSyncSessionRunning, Counts: domain.ObjectSyncSessionCounts{Manifest: 1}}
if err := dataStore.UpsertObjectSyncSession(t.Context(), session); err != nil {
t.Fatal(err)
}
if err := dataStore.UpsertObjectSyncItems(t.Context(), []domain.ObjectSyncItemRecord{
{ID: "original", SessionID: session.ID, Status: domain.ObjectSyncItemKeptBoth, Action: "create"},
{ID: "resolution", SessionID: session.ID, Status: domain.ObjectSyncItemCommitted, Action: "force_overwrite"},
}); err != nil {
t.Fatal(err)
}
writer := httptest.NewRecorder()
instance.handleExternalObjectSyncSessionFinish(writer, progressRequest(http.MethodPost, "complete", "device"))
var final domain.ObjectSyncSessionRecord
if err := json.Unmarshal(writer.Body.Bytes(), &final); err != nil {
t.Fatal(err)
}
if writer.Code != http.StatusOK || final.Counts.Manifest != 1 || final.Counts.Committed != 2 || final.Counts.KeptBoth != 1 {
t.Fatalf("derived operation changed manifest count: %d %s", writer.Code, writer.Body.String())
}
page := []domain.ObjectSyncSessionRecord{session}
if err := refreshObjectSyncSessionCounts(t.Context(), dataStore, page); err != nil {
t.Fatal(err)
}
if page[0].Counts.Manifest != 1 || page[0].Counts.Committed != 2 {
t.Fatalf("wrong live derived counts: %#v", page[0].Counts)
}
session.Counts.Manifest = 3
if err := dataStore.UpsertObjectSyncSession(t.Context(), session); err != nil {
t.Fatal(err)
}
writer = httptest.NewRecorder()
instance.handleExternalObjectSyncSessionFinish(writer, progressRequest(http.MethodPost, "complete", "device"))
if writer.Code != http.StatusConflict || !strings.Contains(writer.Body.String(), "session_manifest_incomplete") {
t.Fatalf("missing manifest items finalized: %d %s", writer.Code, writer.Body.String())
}
}