package fcm import ( "testing" "github.com/tinode/chat/server/store" "github.com/tinode/chat/server/store/types" ) type testProductPushTokens struct { devices map[types.Uid][]types.DeviceDef } func (s *testProductPushTokens) Upsert(types.TenantID, *types.DevicePushToken) (*types.DevicePushToken, error) { return nil, nil } func (s *testProductPushTokens) GetAll(_ types.TenantID, _ ...types.Uid) (map[types.Uid][]types.DeviceDef, int, error) { count := 0 for _, devices := range s.devices { count += len(devices) } return s.devices, count, nil } type testLegacyPushDevices struct { called bool } func (*testLegacyPushDevices) Update(types.TenantID, types.Uid, string, *types.DeviceDef) error { return nil } func (s *testLegacyPushDevices) GetAll(types.TenantID, ...types.Uid) (map[types.Uid][]types.DeviceDef, int, error) { s.called = true return nil, 0, nil } func (*testLegacyPushDevices) Delete(types.TenantID, types.Uid, string) error { return nil } func TestPushDevicesForUsersPrefersProductTokens(t *testing.T) { oldTokens := store.DevicePushTokens oldDevices := store.Devices defer func() { store.DevicePushTokens = oldTokens store.Devices = oldDevices }() uid := types.Uid(42) legacy := &testLegacyPushDevices{} store.DevicePushTokens = &testProductPushTokens{devices: map[types.Uid][]types.DeviceDef{ uid: {{TenantID: 7, DeviceId: "product-token", Platform: "android"}}, }} store.Devices = legacy devices, count, err := pushDevicesForUsers(7, uid) if err != nil { t.Fatal(err) } if count != 1 || devices[uid][0].DeviceId != "product-token" { t.Fatalf("unexpected devices: %#v count=%d", devices, count) } if legacy.called { t.Fatal("legacy device store must not be read when product tokens exist") } }