61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestParserControllerDetection(t *testing.T) {
|
|
sampleConfig := `user = "root"
|
|
group = "plugdev"
|
|
device {
|
|
id = 5
|
|
name = "TF_OFFICE_DESK"
|
|
controller = 0
|
|
protocol = "arctech"
|
|
model = "selflearning-switch"
|
|
parameters {
|
|
house = "12168864"
|
|
unit = "11"
|
|
}
|
|
}
|
|
controller {
|
|
id = 1
|
|
type = 2
|
|
serial = "AE01DHRN"
|
|
}
|
|
`
|
|
|
|
dir := t.TempDir()
|
|
cfgPath := filepath.Join(dir, "tellstick.conf")
|
|
if err := os.WriteFile(cfgPath, []byte(sampleConfig), 0o600); err != nil {
|
|
t.Fatalf("failed writing sample config: %v", err)
|
|
}
|
|
|
|
parser := NewParser(cfgPath)
|
|
cfg, err := parser.Parse()
|
|
if err != nil {
|
|
t.Fatalf("parse failed: %v", err)
|
|
}
|
|
|
|
if got, want := len(cfg.Controllers), 1; got != want {
|
|
t.Fatalf("expected %d controller, got %d", want, got)
|
|
}
|
|
ctrl := cfg.Controllers[0]
|
|
if ctrl.ID != 1 || ctrl.Serial != "AE01DHRN" {
|
|
t.Fatalf("unexpected controller parsed: %+v", ctrl)
|
|
}
|
|
|
|
if got, want := len(cfg.Devices), 1; got != want {
|
|
t.Fatalf("expected %d device, got %d", want, got)
|
|
}
|
|
dev := cfg.Devices[0]
|
|
if dev.Name != "TF_OFFICE_DESK" {
|
|
t.Fatalf("unexpected device parsed: %+v", dev)
|
|
}
|
|
if val := dev.Parameters["controller"]; val != "0" {
|
|
t.Fatalf("expected controller parameter stored, got %q", val)
|
|
}
|
|
}
|