package api import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "time" "droboticflow/internal/domain" "droboticflow/internal/store" "github.com/go-chi/chi/v5" ) func missingRequest(device string) *http.Request { req := httptest.NewRequest(http.MethodPost, "/api/external/object-sync/items/missing/skip-missing", nil) route := chi.NewRouteContext() route.URLParams.Add("itemId", "missing") ctx := context.WithValue(req.Context(), chi.RouteCtxKey, route) ctx = context.WithValue(ctx, objectSyncCredentialContextKey{}, &objectSyncCredentialPrincipal{Credential: domain.ObjectSyncCredentialRecord{ID: "credential", OrganizationID: "org"}, Device: domain.ObjectSyncDeviceRecord{ID: device}}) return req.WithContext(ctx) } func missingFixture(t *testing.T, status domain.ObjectSyncItemStatus) (*server, *store.MemoryStore, *resumableObjectSyncBrowser) { t.Helper() data := store.NewMemoryStore() fake := &resumableObjectSyncBrowser{fakeObjectStorageBrowser: newFakeObjectStorageBrowser()} prior := storageConnectionBrowserResolver storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) { return fake, func() {}, nil } t.Cleanup(func() { storageConnectionBrowserResolver = prior }) for _, err := range []error{ data.UpsertStorageConnection(t.Context(), domain.StorageConnectionRecord{ID: "connection", OrganizationID: "org"}), data.UpsertObjectSyncSpace(t.Context(), domain.ObjectSyncSpaceRecord{ID: "space", OrganizationID: "org", ConnectionID: "connection", Bucket: "bucket", Enabled: true}), data.UpsertObjectSyncSession(t.Context(), domain.ObjectSyncSessionRecord{ID: "session", OrganizationID: "org", CredentialID: "credential", DeviceID: "device", SyncSpaceID: "space", Status: domain.ObjectSyncSessionRunning, Counts: domain.ObjectSyncSessionCounts{Manifest: 1}}), data.UpsertObjectSyncItems(t.Context(), []domain.ObjectSyncItemRecord{{ID: "missing", OrganizationID: "org", SessionID: "session", RelativePath: "missing.bin", TargetKey: "final/missing.bin", SizeBytes: 12, Status: status, Action: "create"}}), } { if err != nil { t.Fatal(err) } } return &server{store: data, objectSyncEnabled: true}, data, fake } func TestMissingSourceSkipReleasesLeaseAndCompletesSession(t *testing.T) { s, data, fake := missingFixture(t, domain.ObjectSyncItemUploading) lease, ok, err := data.AcquireObjectSyncLease(t.Context(), domain.ObjectSyncLeaseRecord{OrganizationID: "org", ResourceHash: "resource", OwnerSessionID: "session", OwnerItemID: "missing"}, time.Minute) if err != nil || !ok { t.Fatal(err) } item, _ := data.GetObjectSyncItem(t.Context(), "missing") item.LeaseID = lease.ID item.FencingToken = lease.FencingToken item.UploadMode = "multipart" item.UploadID = "multipart" item.StagingKey = "staging/missing" data.UpsertObjectSyncItems(t.Context(), []domain.ObjectSyncItemRecord{*item}) fake.objects["bucket|final/missing.bin"] = domain.ObjectStorageObjectSummary{Key: "final/missing.bin", SizeBytes: 12} for range 2 { w := httptest.NewRecorder() s.handleExternalObjectSyncItemSkipMissing(w, missingRequest("device")) if w.Code != 200 { t.Fatalf("skip %d %s", w.Code, w.Body.String()) } var got domain.ObjectSyncItemRecord if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { t.Fatal(err) } if got.Status != domain.ObjectSyncItemSkipped || got.LastError != "source_missing" { t.Fatalf("not explicit missing skip: %#v", got) } } renewed, err := data.RenewObjectSyncLease(t.Context(), lease.ID, "session", "missing", lease.FencingToken, time.Minute) if err != nil || renewed { t.Fatal("lease retained", err) } if !fake.abortedUploads["bucket|staging/missing|multipart"] { t.Fatal("multipart not aborted") } if _, ok := fake.objects["bucket|final/missing.bin"]; !ok { t.Fatal("final object removed") } summary, err := data.SummarizeObjectSyncItems(t.Context(), []string{"session"}) if err != nil || summary["session"].Counts.Skipped != 1 || summary["session"].Counts.Failed != 0 { t.Fatalf("bad progress %#v %v", summary, err) } req := progressRequest(http.MethodPost, "complete", "device") principal, _ := currentObjectSyncCredential(req) principal.Credential.OrganizationID = "org" w := httptest.NewRecorder() s.handleExternalObjectSyncSessionFinish(w, req) if w.Code != 200 || !strings.Contains(w.Body.String(), `"status":"completed"`) { t.Fatalf("skip prevented session completion: %d %s", w.Code, w.Body.String()) } } func TestMissingSourceCannotDiscardUploadedOrTerminalData(t *testing.T) { for _, status := range []domain.ObjectSyncItemStatus{domain.ObjectSyncItemUploaded, domain.ObjectSyncItemCommitting, domain.ObjectSyncItemCommitted, domain.ObjectSyncItemKeptBoth, domain.ObjectSyncItemUnchanged, domain.ObjectSyncItemDenied, domain.ObjectSyncItemFailed, domain.ObjectSyncItemAborted, domain.ObjectSyncItemExpired} { t.Run(string(status), func(t *testing.T) { s, data, fake := missingFixture(t, status) w := httptest.NewRecorder() s.handleExternalObjectSyncItemSkipMissing(w, missingRequest("device")) got, _ := data.GetObjectSyncItem(t.Context(), "missing") if w.Code != 200 || got.Status != status || len(fake.abortedUploads) != 0 { t.Fatalf("uploaded/terminal changed %d %#v", w.Code, got) } }) } } func TestMissingSourceRequiresOwnedActiveSession(t *testing.T) { for _, variant := range []string{"foreign", "closed", "preview"} { t.Run(variant, func(t *testing.T) { s, data, _ := missingFixture(t, domain.ObjectSyncItemPlanned) session, _ := data.GetObjectSyncSession(t.Context(), "session") device := "device" if variant == "foreign" { device = "foreign" } if variant == "closed" { session.Status = domain.ObjectSyncSessionCompleted } if variant == "preview" { session.Preview = true } data.UpsertObjectSyncSession(t.Context(), *session) w := httptest.NewRecorder() s.handleExternalObjectSyncItemSkipMissing(w, missingRequest(device)) if w.Code != 404 && w.Code != 409 { t.Fatalf("unsafe missing skip %d", w.Code) } item, _ := data.GetObjectSyncItem(t.Context(), "missing") if item.Status != domain.ObjectSyncItemPlanned { t.Fatal("denied call mutated item") } }) } } type missingDeadlineBrowser struct { *resumableObjectSyncBrowser deadline time.Time } func (browser *missingDeadlineBrowser) AbortMultipartUpload(ctx context.Context, _, _, _ string) error { browser.deadline, _ = ctx.Deadline() return context.DeadlineExceeded } func TestMissingSourceStorageCleanupIsBoundedAndDoesNotBlockCompletion(t *testing.T) { s, data, base := missingFixture(t, domain.ObjectSyncItemUploading) browser := &missingDeadlineBrowser{resumableObjectSyncBrowser: base} storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) { return browser, func() {}, nil } item, _ := data.GetObjectSyncItem(t.Context(), "missing") item.UploadMode = "multipart" item.UploadID = "pending" item.StagingKey = "staging/missing" data.UpsertObjectSyncItems(t.Context(), []domain.ObjectSyncItemRecord{*item}) before := time.Now() w := httptest.NewRecorder() s.handleExternalObjectSyncItemSkipMissing(w, missingRequest("device")) if w.Code != 200 || w.Header().Get("X-Drosync-Cleanup") != "deferred" { t.Fatalf("cleanup failed foreground sync: %d %s", w.Code, w.Body.String()) } if browser.deadline.IsZero() || browser.deadline.Sub(before) > 5*time.Second+100*time.Millisecond { t.Fatalf("unbounded cleanup: %v", browser.deadline) } stored, _ := data.GetObjectSyncItem(t.Context(), "missing") if stored.Status != domain.ObjectSyncItemSkipped || stored.UploadID != "pending" { t.Fatal("lost deferred cleanup receipt") } } func TestMissingSourceConcurrentUploadAcknowledgmentWins(t *testing.T) { s, data, _ := missingFixture(t, domain.ObjectSyncItemUploading) wrapper := &cancellationRaceStore{MemoryStore: data} wrapper.beforeItemTransition = func(ctx context.Context) error { current, _ := data.GetObjectSyncItem(ctx, "missing") current.Status = domain.ObjectSyncItemUploaded return data.UpsertObjectSyncItems(ctx, []domain.ObjectSyncItemRecord{*current}) } s.store = wrapper w := httptest.NewRecorder() s.handleExternalObjectSyncItemSkipMissing(w, missingRequest("device")) got, _ := data.GetObjectSyncItem(t.Context(), "missing") if w.Code != 200 || got.Status != domain.ObjectSyncItemUploaded || got.LastError != "" { t.Fatalf("concurrent acknowledged upload was discarded: %d %#v", w.Code, got) } }