go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/botdata/bot_data_test.go (about) 1 // Copyright 2020 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package botdata 16 17 import ( 18 "testing" 19 "time" 20 21 gerritpb "go.chromium.org/luci/common/proto/gerrit" 22 23 . "github.com/smartystreets/goconvey/convey" 24 . "go.chromium.org/luci/common/testing/assertions" 25 ) 26 27 func TestParse(t *testing.T) { 28 t.Parallel() 29 30 Convey("Parse", t, func() { 31 32 Convey("Nil ChangeMessageInfo", func() { 33 _, ok := Parse(nil) 34 So(ok, ShouldBeFalse) 35 }) 36 37 Convey("Success", func() { 38 cmi := &gerritpb.ChangeMessageInfo{ 39 Message: `human message 40 41 Bot data: {"action":"start","triggered_at":"2013-03-23T21:36:52.332Z","revision":"abcd","cls":["chromium-review.googlesource.com:1111","chromium-review.googlesource.com:2222"]}`, 42 } 43 ret, ok := Parse(cmi) 44 So(ok, ShouldBeTrue) 45 So(ret, ShouldResemble, BotData{ 46 Action: Start, 47 TriggeredAt: time.Date(2013, 03, 23, 21, 36, 52, 332000000, time.UTC), 48 Revision: "abcd", 49 }) 50 }) 51 52 Convey("BotDataPrefix Missing", func() { 53 cmi := &gerritpb.ChangeMessageInfo{ 54 Message: `{"action": "start", "triggered_at": "2013-03-23T21:36:52.332Z", "revision": "abcdef"}`, 55 } 56 _, ok := Parse(cmi) 57 So(ok, ShouldBeFalse) 58 }) 59 60 Convey("Invalid BotData JSON string", func() { 61 cmi := &gerritpb.ChangeMessageInfo{ 62 Message: `Bot data: I'm a plain string`, 63 } 64 _, ok := Parse(cmi) 65 So(ok, ShouldBeFalse) 66 }) 67 }) 68 } 69 70 func TestAppend(t *testing.T) { 71 t.Parallel() 72 73 Convey("Append", t, func() { 74 Convey("Bot Message too long", func() { 75 bd := BotData{ 76 Action: Cancel, 77 TriggeredAt: time.Date(2013, 03, 23, 21, 36, 52, 332000000, time.UTC), 78 Revision: "abcd", 79 } 80 _, err := append("", bd, 10) 81 So(err, ShouldErrLike, "bot data too long; max length: 10") 82 }) 83 84 Convey("Empty human message", func() { 85 bd := BotData{ 86 Action: Cancel, 87 TriggeredAt: time.Date(2013, 03, 23, 21, 36, 52, 332000000, time.UTC), 88 Revision: "abcd", 89 } 90 ret, err := append("", bd, 1000) 91 So(err, ShouldBeNil) 92 So(ret, ShouldEqual, `Bot data: {"action":"cancel","triggered_at":"2013-03-23T21:36:52.332Z","revision":"abcd"}`) 93 }) 94 95 Convey("Full human message", func() { 96 bd := BotData{ 97 Action: Cancel, 98 TriggeredAt: time.Date(2013, 03, 23, 21, 36, 52, 332000000, time.UTC), 99 Revision: "abcd", 100 } 101 ret, err := append("Message for human", bd, 1000) 102 So(err, ShouldBeNil) 103 So(ret, ShouldEqual, `Message for human 104 105 Bot data: {"action":"cancel","triggered_at":"2013-03-23T21:36:52.332Z","revision":"abcd"}`) 106 }) 107 108 Convey("Truncated human message", func() { 109 bd := BotData{ 110 Action: Cancel, 111 TriggeredAt: time.Date(2013, 03, 23, 21, 36, 52, 332000000, time.UTC), 112 Revision: "abcd", 113 } 114 ret, err := append("Message for human. But it's way tooooooooooooooooooooooo long", bd, 150) 115 So(err, ShouldBeNil) 116 So(ret, ShouldEqual, `Message for human. But it's 117 ...[truncated too long message] 118 119 Bot data: {"action":"cancel","triggered_at":"2013-03-23T21:36:52.332Z","revision":"abcd"}`) 120 }) 121 122 Convey("Bot Data too long to fit human message", func() { 123 bd := BotData{ 124 Action: Cancel, 125 TriggeredAt: time.Date(2013, 03, 23, 21, 36, 52, 332000000, time.UTC), 126 Revision: "abcd", 127 } 128 // Bot data itself is 89 characters long already. 129 // Placeholder is 31 characters long. 130 _, err := append("Message for human", bd, 100) 131 So(err, ShouldErrLike, "bot data too long to display human message; max length: 100") 132 }) 133 }) 134 }