github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/trigger_system/trigger_system_test.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package trigger_system
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"testing"
    25  	"time"
    26  
    27  	"go.uber.org/atomic"
    28  
    29  	"github.com/e154/smart-home/adaptors"
    30  	"github.com/e154/smart-home/common"
    31  	m "github.com/e154/smart-home/models"
    32  	"github.com/e154/smart-home/plugins/triggers"
    33  	"github.com/e154/smart-home/system/automation"
    34  	"github.com/e154/smart-home/system/bus"
    35  	"github.com/e154/smart-home/system/mqtt"
    36  	"github.com/e154/smart-home/system/scripts"
    37  	"github.com/e154/smart-home/system/supervisor"
    38  	"github.com/e154/smart-home/system/zigbee2mqtt"
    39  	. "github.com/e154/smart-home/tests/plugins"
    40  	. "github.com/smartystreets/goconvey/convey"
    41  )
    42  
    43  func TestTriggerSystem(t *testing.T) {
    44  
    45  	const (
    46  		task3SourceScript = `
    47  automationTriggerSystem = (msg)->
    48      #print '---trigger---'
    49      p = msg.payload
    50      Done p.event
    51      return false
    52  `
    53  	)
    54  
    55  	Convey("trigger system", t, func(ctx C) {
    56  		_ = container.Invoke(func(adaptors *adaptors.Adaptors,
    57  			scriptService scripts.ScriptService,
    58  			supervisor supervisor.Supervisor,
    59  			zigbee2mqtt zigbee2mqtt.Zigbee2mqtt,
    60  			mqttServer mqtt.MqttServ,
    61  			automation automation.Automation,
    62  			eventBus bus.Bus) {
    63  
    64  			// register plugins
    65  			_ = AddPlugin(adaptors, "triggers")
    66  
    67  			serviceCh := WaitService(eventBus, time.Second*5, "Supervisor", "Automation", "Zigbee2mqtt", "Mqtt")
    68  			pluginsCh := WaitPlugins(eventBus, time.Second*5, "triggers")
    69  			go mqttServer.Start()
    70  			go zigbee2mqtt.Start(context.Background())
    71  			automation.Start()
    72  			supervisor.Start(context.Background())
    73  			defer mqttServer.Shutdown()
    74  			defer zigbee2mqtt.Shutdown(context.Background())
    75  			defer automation.Shutdown()
    76  			defer supervisor.Shutdown(context.Background())
    77  			So(<-serviceCh, ShouldBeTrue)
    78  			So(<-pluginsCh, ShouldBeTrue)
    79  
    80  			var counter atomic.Int32
    81  			var lastEvent atomic.String
    82  			scriptService.PushFunctions("Done", func(systemEvent string) {
    83  				lastEvent.Store(systemEvent)
    84  				counter.Inc()
    85  			})
    86  
    87  			// add scripts
    88  			// ------------------------------------------------
    89  
    90  			task3Script, err := AddScript("task3", task3SourceScript, adaptors, scriptService)
    91  			So(err, ShouldBeNil)
    92  
    93  			// automation
    94  			// ------------------------------------------------
    95  			trigger := &m.NewTrigger{
    96  				Enabled:    true,
    97  				Name:       "tr1",
    98  				ScriptId:   common.Int64(task3Script.Id),
    99  				PluginName: "system",
   100  			}
   101  			triggerId, err := AddTrigger(trigger, adaptors, eventBus)
   102  			So(err, ShouldBeNil)
   103  
   104  			//TASK3
   105  			newTask := &m.NewTask{
   106  				Name:       "Toggle plug OFF",
   107  				Enabled:    true,
   108  				Condition:  common.ConditionAnd,
   109  				TriggerIds: []int64{triggerId},
   110  			}
   111  			_, err = AddTask(newTask, adaptors, eventBus)
   112  			So(err, ShouldBeNil)
   113  
   114  			// ------------------------------------------------
   115  
   116  			time.Sleep(time.Second)
   117  
   118  			eventBus.Publish(triggers.TopicSystemStart, "START")
   119  
   120  			time.Sleep(time.Second)
   121  
   122  			fmt.Println(counter.Load())
   123  
   124  			So(counter.Load(), ShouldBeGreaterThanOrEqualTo, 1)
   125  			So(lastEvent.Load(), ShouldEqual, "START")
   126  
   127  		})
   128  	})
   129  }