package service import ( "testing" "git.apinb.com/heqiapp/platforms/backend/iot-server/internal/config" ) func TestDeviceIDFromTopic(t *testing.T) { cfg := config.Config{MQTT: config.MQTT{UpTopic: "devices/+/up", AckTopic: "devices/+/ack"}} for _, topic := range []string{"devices/0000000000000001/up", "devices/0000000000000001/ack"} { identity, ok := deviceIDFromTopic(cfg, topic) if !ok || identity != "0000000000000001" { t.Fatalf("topic %s: identity=%q ok=%v", topic, identity, ok) } } for _, topic := range []string{"devices/0001/up", "devices/0000000000000001/down", "other/0000000000000001/up"} { if identity, ok := deviceIDFromTopic(cfg, topic); ok || identity != "" { t.Fatalf("invalid topic %s accepted as %q", topic, identity) } } }