From 7c4e1ab8e3d2c58db97b0030d1e91f8d5a35ccd3 Mon Sep 17 00:00:00 2001 From: Jiri Luzny Date: Sun, 12 Nov 2023 21:23:00 +0100 Subject: [PATCH] Initial version of the sample live tests --- example/config.yaml | 7 +++ example/example_live_test.go | 90 ++++++++++++++++++++++++++++++++++++ example/go.mod | 23 +++++++++ 3 files changed, 120 insertions(+) create mode 100644 example/config.yaml create mode 100644 example/example_live_test.go create mode 100644 example/go.mod diff --git a/example/config.yaml b/example/config.yaml new file mode 100644 index 0000000..9e42dee --- /dev/null +++ b/example/config.yaml @@ -0,0 +1,7 @@ +app: + ip_address: 192.168.0.204 + port: 8123 + home_zone_entity_id: zone.home + +entities: + light_entity_id: light.office_bed_lamp diff --git a/example/example_live_test.go b/example/example_live_test.go new file mode 100644 index 0000000..d2bbaac --- /dev/null +++ b/example/example_live_test.go @@ -0,0 +1,90 @@ +package example + +import ( + "log/slog" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" + "gopkg.in/yaml.v3" + ga "saml.dev/gome-assistant" +) + +type ( + MySuite struct { + suite.Suite + app *ga.App + config *Config + } + + Config struct { + App struct { + HAAuthToken string `yaml:"ha_auth_token"` + IpAddress string `yaml:"ip_address"` + Port string `yaml:"port"` + HomeZoneEntityId string `yaml:"home_zone_entity_id"` + } + Entities struct { + LightEntityId string `yaml:"light_entity_id"` + } + } +) + +func (s *MySuite) SetupTest() { + configFile, err := os.ReadFile("./config.yaml") + if err != nil { + slog.Error("Error reading config file", err) + } + s.config = &Config{} + // either env var or config file can be used to set HA auth. token + s.config.App.HAAuthToken = os.Getenv("HA_AUTH_TOKEN") + if err := yaml.Unmarshal(configFile, s.config); err != nil { + slog.Error("Error unmarshalling config file:", err) + } + + s.app, err = ga.NewApp(ga.NewAppRequest{ + HAAuthToken: s.config.App.HAAuthToken, + IpAddress: s.config.App.IpAddress, + HomeZoneEntityId: s.config.App.HomeZoneEntityId, + }) + if err != nil { + slog.Error("Failed to createw new app:", err) + s.T().FailNow() + } +} + +func (s *MySuite) TearDownSuite() { + if s.app != nil { + s.app.Cleanup() + } +} + +// Basic test of ga app creation and light toggle service +func (s *MySuite) TestLightService() { + entityId := s.config.Entities.LightEntityId + + initialState, err := s.app.GetState().Get(entityId) + if err != nil { + slog.Error("Error getting entity state:", err) + } + slog.Info("Initial state of entity:", "state", initialState.State) + + s.app.GetService().Light.Toggle(entityId) + + time.Sleep(1 * time.Second) // wait for state to update + // <-time.After(1 * time.Second) + + newState, err := s.app.GetState().Get(entityId) + if err != nil { + slog.Error("Error getting entity state:", err) + } + slog.Info("New state of entity:", "state", newState.State) + assert.Equal(s.T(), initialState.State, newState.State) +} + +// Run the test suite +func TestMySuite(t *testing.T) { + suite.Run(t, new(MySuite)) +} diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..95813bf --- /dev/null +++ b/example/go.mod @@ -0,0 +1,23 @@ +module example + +go 1.21.0 + +require ( + github.com/stretchr/testify v1.8.4 + gopkg.in/yaml.v3 v3.0.1 + saml.dev/gome-assistant v0.2.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/gobuffalo/envy v1.10.2 // indirect + github.com/gobuffalo/packd v1.0.2 // indirect + github.com/gobuffalo/packr v1.30.1 // indirect + github.com/golang-module/carbon v1.7.3 // indirect + github.com/gorilla/websocket v1.5.0 // indirect + github.com/joho/godotenv v1.5.1 // indirect + github.com/nathan-osman/go-sunrise v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect + golang.org/x/mod v0.9.0 // indirect +)