feat: expose action input specifications

This commit is contained in:
2026-07-21 10:09:05 +08:00
parent 8fea0c3c11
commit 4ffd1b7eac
4 changed files with 64 additions and 10 deletions
+25
View File
@@ -1,6 +1,7 @@
package mijia
import (
"bytes"
"context"
"encoding/json"
"errors"
@@ -123,6 +124,9 @@ func TestGetDeviceInfoParsesSpecAndCaches(t *testing.T) {
if info.Properties[7].Name != "outlet-power" || info.Actions[1].Name != "outlet-toggle" {
t.Fatalf("duplicates = %#v / %#v", info.Properties[7], info.Actions[1])
}
if got := info.Actions[0].Inputs; len(got) != 2 || got[0].PIID != 3 || got[1].PIID != 1 || got[0].Description != "Mode / 模式" || got[0].Type != "uint" || len(got[0].ValueList) != 2 {
t.Fatalf("action inputs = %#v", got)
}
if testServer.specCalls != 1 || testServer.specPaths[0] != "/spec/test.light.v1" {
t.Fatalf("spec requests = %v, want GET /spec/test.light.v1", testServer.specPaths)
}
@@ -151,6 +155,27 @@ func TestGetDeviceInfoParsesSpecAndCaches(t *testing.T) {
if err != nil || cached.Name != info.Name || testServer.specCalls != 1 {
t.Fatalf("cached = %#v, %v, calls=%d", cached, err, testServer.specCalls)
}
if len(cached.Actions[0].Inputs) != 2 || cached.Actions[0].Inputs[0].PIID != 3 || cached.Actions[0].Inputs[1].PIID != 1 {
t.Fatalf("cached action inputs = %#v", cached.Actions[0].Inputs)
}
}
func TestParseDeviceInfoRejectsUnknownActionInput(t *testing.T) {
fixture := bytes.Replace(loadSpecFixture(t), []byte(`"in":[3,1]`), []byte(`"in":[99]`), 1)
if _, err := parseDeviceInfoHTML(fixture); err == nil || !strings.Contains(err.Error(), "unknown property IID 99") {
t.Fatalf("parse 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()
actions["toggle"].Inputs[0].Range[0] = "changed"
actions["toggle"].Inputs[0].ValueList[0].Value = "changed"
got := device.actions["toggle"].Inputs[0]
if got.Range[0] != "1" || got.ValueList[0].Value != "1" {
t.Fatalf("internal action input mutated: %#v", got)
}
}
func TestPythonDeviceInfoCacheSupportsOperations(t *testing.T) {