Files
xiaomihome/miot/miot_mdns_test.go
T
4566704 3a44cb9e6a feat: 初始化小米 IoT (MIoT) 智能家居 Go 库
- 实现 MIoT 客户端核心功能(MQTT 连接、设备管理、属性读写)
- 支持云端 API 调用与局域网设备发现(mDNS)
- 集成国际化(i18n)多语言支持
- 添加 MIoT 设备规约解析器(spec_parser)
- 包含单元测试与使用示例
2026-06-28 22:05:49 +08:00

140 lines
3.7 KiB
Go

// Package miot provides white-box tests for miot_mdns.go mDNS service.
package miot
import "testing"
func TestMipsServiceState_String(t *testing.T) {
tests := []struct {
state MipsServiceState
want string
}{
{MipsServiceAdded, "added"},
{MipsServiceRemoved, "removed"},
{MipsServiceUpdated, "updated"},
{MipsServiceState(99), "unknown"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.state.String(); got != tt.want {
t.Errorf("MipsServiceState(%d).String() = %q, want %q", tt.state, got, tt.want)
}
})
}
}
func TestMipsServiceData_ValidService(t *testing.T) {
svc := &MipsServiceData{Name: "test", Role: 1, SuiteMQTT: true}
if !svc.validService() {
t.Error("validService should be true for role=1, suiteMQTT=true")
}
svc2 := &MipsServiceData{Name: "test", Role: 0, SuiteMQTT: true}
if svc2.validService() {
t.Error("validService should be false for role=0")
}
svc3 := &MipsServiceData{Name: "test", Role: 1, SuiteMQTT: false}
if svc3.validService() {
t.Error("validService should be false for suiteMQTT=false")
}
}
func TestMipsServiceData_ToDict(t *testing.T) {
svc := &MipsServiceData{
Name: "test_service", Addresses: []string{"192.168.1.1"},
Port: 8080, Type: "_miot-central._tcp.local.",
Server: "server1", DID: "12345", GroupID: "group_abc",
Role: 1, SuiteMQTT: true,
}
dict := svc.ToDict()
if dict["name"] != "test_service" {
t.Errorf("name = %v", dict["name"])
}
if dict["did"] != "12345" {
t.Errorf("did = %v", dict["did"])
}
if dict["suite_mqtt"] != true {
t.Errorf("suite_mqtt = %v", dict["suite_mqtt"])
}
}
func TestMipsServiceData_String(t *testing.T) {
svc := &MipsServiceData{Name: "test", Port: 8080}
s := svc.String()
if s == "" {
t.Error("MipsServiceData.String() should not be empty")
}
}
func TestReverseBytes(t *testing.T) {
data := []byte{1, 2, 3, 4, 5}
reverseBytes(data)
if data[0] != 5 || data[1] != 4 || data[2] != 3 || data[3] != 2 || data[4] != 1 {
t.Errorf("reverseBytes result = %v, want [5 4 3 2 1]", data)
}
}
func TestReverseBytes_SingleByte(t *testing.T) {
data := []byte{42}
reverseBytes(data)
if data[0] != 42 {
t.Errorf("single byte should be unchanged")
}
}
func TestReverseBytes_Empty(t *testing.T) {
data := []byte{}
reverseBytes(data)
}
func TestStrSlicesEqual(t *testing.T) {
if !strSlicesEqual([]string{"a", "b"}, []string{"a", "b"}) {
t.Error("identical slices should be equal")
}
if strSlicesEqual([]string{"a", "b"}, []string{"a", "c"}) {
t.Error("different slices should not be equal")
}
if strSlicesEqual([]string{"a", "b"}, []string{"a", "b", "c"}) {
t.Error("different length slices should not be equal")
}
if !strSlicesEqual(nil, nil) {
t.Error("nil slices should be equal")
}
if strSlicesEqual([]string{"a"}, nil) {
t.Error("nil vs non-nil should not be equal")
}
}
func TestNewMipsService(t *testing.T) {
s := NewMipsService()
if s == nil {
t.Fatal("NewMipsService returned nil")
}
services := s.GetServices("")
if len(services) != 0 {
t.Errorf("GetServices should be empty, got %d", len(services))
}
}
func TestSubUnsubServiceChange(t *testing.T) {
s := NewMipsService()
called := false
s.SubServiceChange("mykey", "*", func(groupID string, state MipsServiceState, data map[string]interface{}) {
called = true
})
s.UnsubServiceChange("mykey")
_ = called
}
func TestMipsServiceState_IotaValues(t *testing.T) {
if int(MipsServiceAdded) != 1 {
t.Errorf("MipsServiceAdded = %d, want 1", MipsServiceAdded)
}
if int(MipsServiceRemoved) != 2 {
t.Errorf("MipsServiceRemoved = %d, want 2", MipsServiceRemoved)
}
if int(MipsServiceUpdated) != 3 {
t.Errorf("MipsServiceUpdated = %d, want 3", MipsServiceUpdated)
}
}