fix: accept non-controllable device properties

This commit is contained in:
2026-07-22 17:19:02 +08:00
parent 93799df53c
commit 3200089fa4
2 changed files with 50 additions and 0 deletions
+44
View File
@@ -104,6 +104,15 @@ func loadSpecFixture(t *testing.T) []byte {
return fixture
}
func loadNonControllableSpecFixture(t *testing.T) []byte {
t.Helper()
fixture, err := os.ReadFile("testdata/miot-spec-non-controllable.html")
if err != nil {
t.Fatal(err)
}
return fixture
}
func TestGetDeviceInfoParsesSpecAndCaches(t *testing.T) {
testServer := newDeviceTestServer(t, loadSpecFixture(t), nil)
httpClient := testServer.server.Client()
@@ -171,6 +180,41 @@ func TestParseDeviceInfoRejectsUnknownActionInput(t *testing.T) {
}
}
func TestGetDeviceInfoPreservesNonControllableProperties(t *testing.T) {
testServer := newDeviceTestServer(t, loadNonControllableSpecFixture(t), nil)
cacheDir := t.TempDir()
info, err := GetDeviceInfo(context.Background(), testServer.server.Client(), "test.sensor.v1", cacheDir)
if err != nil {
t.Fatal(err)
}
if len(info.Properties) != 2 || info.Properties[0].Name != "event" || info.Properties[0].RW != "" || info.Properties[1].Name != "command" || info.Properties[1].RW != "" {
t.Fatalf("properties = %#v", info.Properties)
}
if len(info.Actions) != 1 || len(info.Actions[0].Inputs) != 1 || !reflect.DeepEqual(info.Actions[0].Inputs[0], info.Properties[1]) {
t.Fatalf("actions = %#v", info.Actions)
}
testServer.fixture = nil
cached, err := GetDeviceInfo(context.Background(), testServer.server.Client(), "test.sensor.v1", cacheDir)
if err != nil {
t.Fatal(err)
}
if len(cached.Properties) != 2 || cached.Properties[0].RW != "" || cached.Properties[1].RW != "" ||
len(cached.Actions) != 1 || len(cached.Actions[0].Inputs) != 1 || !reflect.DeepEqual(cached.Actions[0].Inputs[0], cached.Properties[1]) || testServer.specCalls != 1 {
t.Fatalf("cached = %#v, spec calls = %d", cached, testServer.specCalls)
}
}
func TestDeviceRejectsGetSetForNonControllableProperty(t *testing.T) {
device := Device{properties: map[string]PropertySpec{"event": {Name: "event", Type: "string", SIID: 2, PIID: 1}}}
if _, err := device.Get(context.Background(), "event"); err == nil || !strings.Contains(err.Error(), "不可读取") {
t.Fatalf("Get() error = %v", err)
}
if err := device.Set(context.Background(), "event", "value"); err == nil || !strings.Contains(err.Error(), "不可写入") {
t.Fatalf("Set() error = %v", err)
}
}
func TestDeviceActionsDeepCopyInputs(t *testing.T) {
device := Device{actions: map[string]ActionSpec{"toggle": {Inputs: []PropertySpec{{Range: []json.Number{"1", "2"}, ValueList: []ValueListItem{{Value: "1"}}}}}}}
actions := device.Actions()