github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/telegram/telegram_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 telegram 20 21 import ( 22 "context" 23 "testing" 24 "time" 25 26 . "github.com/smartystreets/goconvey/convey" 27 28 "github.com/e154/smart-home/adaptors" 29 "github.com/e154/smart-home/common" 30 "github.com/e154/smart-home/common/events" 31 m "github.com/e154/smart-home/models" 32 "github.com/e154/smart-home/plugins/notify" 33 notifyCommon "github.com/e154/smart-home/plugins/notify/common" 34 "github.com/e154/smart-home/plugins/telegram" 35 "github.com/e154/smart-home/system/bus" 36 "github.com/e154/smart-home/system/scripts" 37 "github.com/e154/smart-home/system/supervisor" 38 . "github.com/e154/smart-home/tests/plugins" 39 ) 40 41 func TestTelegram(t *testing.T) { 42 43 const sourceScript = ` 44 checkStatus =-> 45 #print '----------------1' 46 47 telegramAction = (entityId, actionName)-> 48 switch actionName 49 when 'CHECK' then checkStatus() 50 ` 51 52 Convey("telegram", t, func(ctx C) { 53 _ = container.Invoke(func(adaptors *adaptors.Adaptors, 54 scriptService scripts.ScriptService, 55 supervisor supervisor.Supervisor, 56 eventBus bus.Bus) { 57 58 // register plugins 59 AddPlugin(adaptors, "notify") 60 AddPlugin(adaptors, "telegram") 61 62 serviceCh := WaitService(eventBus, time.Second*5, "Supervisor") 63 pluginsCh := WaitPlugins(eventBus, time.Second*5, "notify", "telegram") 64 supervisor.Start(context.Background()) 65 defer supervisor.Shutdown(context.Background()) 66 So(<-serviceCh, ShouldBeTrue) 67 So(<-pluginsCh, ShouldBeTrue) 68 69 // add scripts 70 // ------------------------------------------------ 71 72 plugScript, err := AddScript("telegram script", sourceScript, adaptors, scriptService) 73 So(err, ShouldBeNil) 74 75 // add entity 76 // ------------------------------------------------ 77 tgEnt := GetNewTelegram("clavicus") 78 tgEnt.Actions = []*m.EntityAction{ 79 { 80 Name: "CHECK", 81 Description: "check status", 82 Script: plugScript, 83 }, 84 } 85 err = adaptors.Entity.Add(context.Background(), tgEnt) 86 ctx.So(err, ShouldBeNil) 87 _, err = adaptors.EntityStorage.Add(context.Background(), &m.EntityStorage{ 88 EntityId: tgEnt.Id, 89 Attributes: tgEnt.Attributes.Serialize(), 90 }) 91 So(err, ShouldBeNil) 92 93 eventBus.Publish("system/models/entities/"+tgEnt.Id.String(), events.EventCreatedEntityModel{ 94 EntityId: tgEnt.Id, 95 }) 96 97 time.Sleep(time.Second) 98 99 // add chat 100 tgChan := m.TelegramChat{ 101 EntityId: tgEnt.Id, 102 ChatId: 123, 103 Username: "user", 104 } 105 _ = adaptors.TelegramChat.Add(context.Background(), tgChan) 106 107 t.Run("succeed", func(t *testing.T) { 108 Convey("", t, func(ctx C) { 109 110 time.Sleep(time.Millisecond * 500) 111 112 eventBus.Publish(notify.TopicNotify, notifyCommon.Message{ 113 EntityId: common.NewEntityId("telegram.clavicus"), 114 Attributes: map[string]interface{}{ 115 "chat_id": 123, 116 "body": "body", 117 }, 118 }) 119 120 //todo: fix 121 time.Sleep(time.Millisecond * 500) 122 123 list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil) 124 ctx.So(err, ShouldBeNil) 125 ctx.So(total, ShouldEqual, 1) 126 127 ctx.So(list[0].Status, ShouldEqual, m.MessageStatusSucceed) 128 ctx.So(list[0].Address, ShouldEqual, "123") 129 ctx.So(list[0].ErrorMessageBody, ShouldBeNil) 130 ctx.So(list[0].ErrorMessageStatus, ShouldBeNil) 131 ctx.So(list[0].Message.Type, ShouldEqual, telegram.Name) 132 133 attr := telegram.NewMessageParams() 134 _, _ = attr.Deserialize(list[0].Message.Attributes) 135 ctx.So(attr[telegram.AttrBody].String(), ShouldEqual, "body") 136 137 }) 138 }) 139 140 t.Run("call actions", func(t *testing.T) { 141 Convey("call actions", t, func(ctx C) { 142 supervisor.CallAction(tgEnt.Id, "CHECK", nil) 143 time.Sleep(time.Second) 144 }) 145 }) 146 }) 147 }) 148 }