55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
deviceentity "spark-server/internal/module/device/entity"
|
|
)
|
|
|
|
type memReg struct {
|
|
mu sync.Mutex
|
|
m map[string]*RegistryRow
|
|
}
|
|
|
|
func (m *memReg) Upsert(_ context.Context, row *RegistryRow) error {
|
|
if row == nil {
|
|
return nil
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.m == nil {
|
|
m.m = make(map[string]*RegistryRow)
|
|
}
|
|
cp := *row
|
|
if cp.UpdatedAt.IsZero() {
|
|
cp.UpdatedAt = time.Now()
|
|
}
|
|
m.m[row.FingerprintHash] = &cp
|
|
return nil
|
|
}
|
|
|
|
func TestRegisterUpsertsDevice(t *testing.T) {
|
|
svc := NewService(&memReg{})
|
|
ctx := context.Background()
|
|
id1, err := svc.Register(ctx, RegisterInput{
|
|
DeviceType: deviceentity.TypeIOS,
|
|
IDFV: "00000000-0000-0000-0000-000000000007",
|
|
DeviceName: "Phone",
|
|
ClientIP: "203.0.113.1",
|
|
})
|
|
if err != nil || id1 == 0 {
|
|
t.Fatalf("register %d %v", id1, err)
|
|
}
|
|
id2, err := svc.Register(ctx, RegisterInput{
|
|
DeviceType: deviceentity.TypeIOS,
|
|
IDFV: "00000000-0000-0000-0000-000000000007",
|
|
DeviceName: "Phone 2",
|
|
})
|
|
if err != nil || id2 != id1 {
|
|
t.Fatalf("same device %d %d %v", id1, id2, err)
|
|
}
|
|
}
|