GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
fix(device): sort device list by created_at for stable UI ordering (#528)
Aldino Kemal6609da28f962
diff --git a/src/infrastructure/whatsapp/device_manager.go b/src/infrastructure/whatsapp/device_manager.goindex 8ef9e772..2234978f 100644--- a/src/infrastructure/whatsapp/device_manager.go+++ b/src/infrastructure/whatsapp/device_manager.go@@ -4,6 +4,7 @@ import ("context""errors""fmt"+ "slices""strings""sync""time"@@ -235,10 +236,20 @@ func (m *DeviceManager) ListDevices() []*DeviceInstance {m.mu.RLock()defer m.mu.RUnlock()- var result []*DeviceInstance+ result := make([]*DeviceInstance, 0, len(m.devices))for _, instance := range m.devices {result = append(result, instance)}++ // Sort by CreatedAt ascending (oldest first) for stable UI ordering.+ // Use ID as tie-breaker when CreatedAt is equal.+ slices.SortFunc(result, func(a, b *DeviceInstance) int {+ if cmp := a.CreatedAt().Compare(b.CreatedAt()); cmp != 0 {+ return cmp+ }+ return strings.Compare(a.ID(), b.ID())+ })+return result}diff --git a/src/infrastructure/whatsapp/device_manager_test.go b/src/infrastructure/whatsapp/device_manager_test.gonew file mode 100644index 00000000..c5112eef--- /dev/null+++ b/src/infrastructure/whatsapp/device_manager_test.go@@ -0,0 +1,111 @@+package whatsapp++import (+ "testing"+ "time"+)++func TestListDevices_SortsByCreatedAtAscending(t *testing.T) {+ manager := &DeviceManager{+ devices: make(map[string]*DeviceInstance),+ }++ // Create devices with different creation times (in random order)+ now := time.Now()+ devices := []*DeviceInstance{+ {id: "device-c", createdAt: now.Add(2 * time.Hour)},+ {id: "device-a", createdAt: now},+ {id: "device-b", createdAt: now.Add(1 * time.Hour)},+ }++ // Add in the given order (which is not sorted by createdAt)+ for _, d := range devices {+ manager.devices[d.id] = d+ }++ // Get list multiple times to verify consistent sorting+ for i := 0; i < 10; i++ {+ result := manager.ListDevices()++ // Verify order: device-a, device-b, device-c (oldest to newest)+ if len(result) != 3 {+ t.Fatalf("iteration %d: expected 3 devices, got %d", i, len(result))+ }+ if result[0].ID() != "device-a" {+ t.Errorf("iteration %d: expected first device to be device-a, got %s", i, result[0].ID())+ }+ if result[1].ID() != "device-b" {+ t.Errorf("iteration %d: expected second device to be device-b, got %s", i, result[1].ID())+ }+ if result[2].ID() != "device-c" {+ t.Errorf("iteration %d: expected third device to be device-c, got %s", i, result[2].ID())+ }+ }+}++func TestListDevices_EmptyList(t *testing.T) {+ manager := &DeviceManager{+ devices: make(map[string]*DeviceInstance),+ }++ result := manager.ListDevices()++ if len(result) != 0 {+ t.Errorf("expected empty slice, got %d devices", len(result))+ }+}++func TestListDevices_SingleDevice(t *testing.T) {+ manager := &DeviceManager{+ devices: make(map[string]*DeviceInstance),+ }++ device := &DeviceInstance{id: "only-device", createdAt: time.Now()}+ manager.devices[device.id] = device++ result := manager.ListDevices()++ if len(result) != 1 {+ t.Fatalf("expected 1 device, got %d", len(result))+ }+ if result[0].ID() != "only-device" {+ t.Errorf("expected device id to be only-device, got %s", result[0].ID())+ }+}++func TestListDevices_SameCreatedAt(t *testing.T) {+ manager := &DeviceManager{+ devices: make(map[string]*DeviceInstance),+ }++ // Devices with same creation time should be sorted by ID as tie-breaker+ sameTime := time.Now()+ devices := []*DeviceInstance{+ {id: "device-3", createdAt: sameTime},+ {id: "device-1", createdAt: sameTime},+ {id: "device-2", createdAt: sameTime},+ }++ for _, d := range devices {+ manager.devices[d.id] = d+ }++ expectedOrder := []string{"device-1", "device-2", "device-3"}++ // Call ListDevices multiple times to verify consistent ordering+ for i := 0; i < 10; i++ {+ result := manager.ListDevices()++ if len(result) != 3 {+ t.Fatalf("iteration %d: expected 3 devices, got %d", i, len(result))+ }++ // Verify order: devices should be sorted by ID when createdAt is equal+ for j, expected := range expectedOrder {+ if result[j].ID() != expected {+ t.Errorf("iteration %d: expected device at index %d to be %s, got %s",+ i, j, expected, result[j].ID())+ }+ }+ }+}