Files
QuoteForge/internal/services/sync/service_orphaned_pending_test.go
T
Mikhail ChusavitinandClaude Sonnet 5 cccb8890a4 fix: requeue orphaned pending configurations; independent background sync streams
- Configurations stuck in sync_status=pending without a pending_changes row are
  re-enqueued as an update with the current snapshot on each push cycle.
- Background cycle no longer aborts pricelist/partbook/settings sync when push or
  readiness fails; each stream logs its own error.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 11:53:44 +03:00

67 lines
2.3 KiB
Go

package sync_test
import (
"testing"
"git.mchus.pro/mchus/quoteforge/internal/localdb"
"git.mchus.pro/mchus/quoteforge/internal/models"
"git.mchus.pro/mchus/quoteforge/internal/services"
syncsvc "git.mchus.pro/mchus/quoteforge/internal/services/sync"
)
// Reproduces the state seen in the field bundle: configuration stays sync_status=pending
// while its pending_changes rows are gone. Push must not leave it pending forever.
func TestPushPendingChangesRequeuesOrphanedPendingConfiguration(t *testing.T) {
local := newLocalDBForSyncTest(t)
serverDB := newServerDBForSyncTest(t)
localSync := syncsvc.NewService(nil, local)
configService := services.NewLocalConfigurationService(local, localSync, &services.QuoteService{}, func() bool { return false })
pushService := syncsvc.NewServiceWithDB(serverDB, local)
created, err := configService.Create("tester", &services.CreateConfigRequest{
Name: "Orphan v1",
Items: models.ConfigItems{{LotName: "CPU_O", Quantity: 1, UnitPrice: 500}},
ServerCount: 1,
})
if err != nil {
t.Fatalf("create config: %v", err)
}
if _, err := configService.UpdateNoAuth(created.UUID, &services.CreateConfigRequest{
Name: "Orphan v2",
Items: models.ConfigItems{{LotName: "CPU_O", Quantity: 2, UnitPrice: 500}},
ServerCount: 1,
ProjectUUID: created.ProjectUUID,
}); err != nil {
t.Fatalf("update config: %v", err)
}
// Simulate the dropped queue: rows are gone, local config still marked pending.
if err := local.DB().Where("1 = 1").Delete(&localdb.PendingChange{}).Error; err != nil {
t.Fatalf("clear pending changes: %v", err)
}
if _, err := pushService.PushPendingChanges(); err != nil {
t.Fatalf("push: %v", err)
}
var serverCfg models.Configuration
if err := serverDB.Where("uuid = ?", created.UUID).First(&serverCfg).Error; err != nil {
t.Fatalf("configuration not on server: %v", err)
}
if serverCfg.Name != "Orphan v2" {
t.Fatalf("expected latest version on server, got %q", serverCfg.Name)
}
localCfg, err := local.GetConfigurationByUUID(created.UUID)
if err != nil {
t.Fatalf("get local config: %v", err)
}
if localCfg.SyncStatus != "synced" {
t.Fatalf("expected sync_status synced, got %q", localCfg.SyncStatus)
}
if got := local.CountPendingChanges(); got != 0 {
t.Fatalf("expected empty pending queue, got %d", got)
}
}