github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/trigger_empty/trigger_empty_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_empty
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/e154/smart-home/adaptors"
    27  	"github.com/e154/smart-home/common"
    28  	m "github.com/e154/smart-home/models"
    29  	"github.com/e154/smart-home/plugins/triggers"
    30  	"github.com/e154/smart-home/system/automation"
    31  	"github.com/e154/smart-home/system/bus"
    32  	"github.com/e154/smart-home/system/scheduler"
    33  	"github.com/e154/smart-home/system/scripts"
    34  	"github.com/e154/smart-home/system/supervisor"
    35  	. "github.com/e154/smart-home/tests/plugins"
    36  	. "github.com/smartystreets/goconvey/convey"
    37  )
    38  
    39  func TestTriggerEmpty(t *testing.T) {
    40  
    41  	Convey("trigger empty", t, func(ctx C) {
    42  		_ = container.Invoke(func(adaptors *adaptors.Adaptors,
    43  			scriptService scripts.ScriptService,
    44  			supervisor supervisor.Supervisor,
    45  			automation automation.Automation,
    46  			eventBus bus.Bus,
    47  			scheduler *scheduler.Scheduler,
    48  		) {
    49  
    50  			// register plugins
    51  			AddPlugin(adaptors, "triggers")
    52  
    53  			serviceCh := WaitService(eventBus, time.Second*5, "Supervisor", "Automation", "Scheduler")
    54  			pluginsCh := WaitPlugins(eventBus, time.Second*5, "triggers")
    55  			scheduler.Start(context.Background())
    56  			automation.Start()
    57  			supervisor.Start(context.Background())
    58  			defer scheduler.Shutdown(context.Background())
    59  			defer automation.Shutdown()
    60  			defer supervisor.Shutdown(context.Background())
    61  			So(<-serviceCh, ShouldBeTrue)
    62  			So(<-pluginsCh, ShouldBeTrue)
    63  
    64  			// automation
    65  			// ------------------------------------------------
    66  			trigger := &m.NewTrigger{
    67  				Enabled:    true,
    68  				Name:       "trigger1",
    69  				PluginName: "time",
    70  				Payload: m.Attributes{
    71  					triggers.CronOptionTrigger: {
    72  						Name:  triggers.CronOptionTrigger,
    73  						Type:  common.AttributeString,
    74  						Value: "* * * * * *", //every seconds
    75  					},
    76  				},
    77  			}
    78  			triggerId, err := AddTrigger(trigger, adaptors, eventBus)
    79  			So(err, ShouldBeNil)
    80  
    81  			//TASK3
    82  			newTask := &m.NewTask{
    83  				Name:       "Toggle plug OFF",
    84  				Enabled:    true,
    85  				Condition:  common.ConditionAnd,
    86  				TriggerIds: []int64{triggerId},
    87  			}
    88  			_, err = AddTask(newTask, adaptors, eventBus)
    89  			So(err, ShouldBeNil)
    90  
    91  			time.Sleep(time.Millisecond * 500)
    92  
    93  		})
    94  	})
    95  }