Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Apr 8, 2024
1 parent 2cb5eac commit fa12d1c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
11 changes: 9 additions & 2 deletions test/commplatform/teams_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ func (s *TeamsTester) InitUsers(t *testing.T) {
func (s *TeamsTester) InitChannels(t *testing.T) []func() {
t.Helper()

channels, _ := s.cli.GetChannels(context.Background(), s.cfg.OrganizationTeamID)

for _, c := range channels {
s.cli.DeleteChannel(context.Background(), s.cfg.OrganizationTeamID, c)
}

firstChannel, cleanupFirstChannelFn := s.CreateChannel(t, "first")
s.firstChannel = firstChannel

Expand Down Expand Up @@ -256,8 +262,7 @@ func (s *TeamsTester) WaitForMessagePosted(userID, channelID string, limitMessag
}

func (s *TeamsTester) WaitForMessagePostedRecentlyEqual(userID, channelID, expectedMsg string) error {
msg := api.NewPlaintextMessage(expectedMsg, false)
return s.waitForAdaptiveCardMessage(userID, channelID, s.cfg.RecentMessagesLimit, interactive.CoreMessage{Message: msg})
return s.WaitForInteractiveMessagePosted(userID, channelID, s.cfg.RecentMessagesLimit, s.AssertEquals(expectedMsg))
}

func (s *TeamsTester) WaitForInteractiveMessagePosted(userID, channelID string, limitMessages int, assertFn MessageAssertion) error {
Expand Down Expand Up @@ -378,6 +383,8 @@ func (s *TeamsTester) AssertEquals(expectedMsg string) MessageAssertion {
return func(gotMsg string) (bool, int, string) {
gotMsg, expectedMsg = NormalizeTeamsWhitespacesInMessages(gotMsg, expectedMsg)
expectedMsg = teamsx.ReplaceEmojiTagsWithActualOne(expectedMsg)
// * -> _
expectedMsg = strings.ReplaceAll(expectedMsg, "*", "_")
if !strings.EqualFold(expectedMsg, gotMsg) {
count := diff.CountMatchBlock(expectedMsg, gotMsg)
msgDiff := diff.Diff(expectedMsg, gotMsg)
Expand Down
68 changes: 47 additions & 21 deletions test/e2e/bots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func runBotTest(t *testing.T,
gqlCli.MustCreateAlias(t, alias[0], alias[1], alias[2], deployment.ID)
}
// Setting env is needed to instrument help msg with cloud sections, and proper links
os.Setenv("CONFIG_PROVIDER_IDENTIFIER", deployment.ID)
os.Setenv("CONFIG_PROVIDER_IDENTIFIER", deployment.ID)
t.Cleanup(func() {
err := helmx.WaitForUninstallation(context.Background(), t, &botkubeDeploymentUninstalled)
assert.NoError(t, err)
Expand Down Expand Up @@ -347,6 +347,7 @@ func runBotTest(t *testing.T,
})

t.Run("Botkube PluginManagement", func(t *testing.T) {
t.Skip()
t.Run("Echo Executor success", func(t *testing.T) {
command := "echo test"
expectedBody := codeBlock(strings.ToUpper(command))
Expand Down Expand Up @@ -382,8 +383,13 @@ func runBotTest(t *testing.T,
botDriver.PostMessageToBot(t, botDriver.FirstChannel().Identifier(), command)

expectedBody := ".... empty response _*<cricket sounds>*_ :cricket: :cricket: :cricket:"
if botDriver.Type() == commplatform.SlackBot {
switch botDriver.Type() {
case commplatform.SlackBot:
expectedBody = ".... empty response _*&lt;cricket sounds&gt;*_ :cricket: :cricket: :cricket:"
case commplatform.TeamsBot:
// the MS Teams treats the '_*<cricket sounds>*_' as the HTML tag and renders it into '<em><em></em></em>'
// which is later dropped by the markdown converter
expectedBody = ".... empty response :cricket: :cricket: :cricket:"
}

err = waitForLastPlaintextMessageWithHeaderEqual(appCfg, botDriver, command, expectedBody)
Expand Down Expand Up @@ -544,6 +550,7 @@ func runBotTest(t *testing.T,
})

t.Run("Show config", func(t *testing.T) {
t.Skip()
t.Run("With custom cluster name and filter", func(t *testing.T) {
command := fmt.Sprintf("show config --filter=cacheDir --cluster-name %s", appCfg.ClusterName)
expectedFilteredBody := codeBlock(heredoc.Doc(`cacheDir: /tmp`))
Expand Down Expand Up @@ -589,6 +596,7 @@ func runBotTest(t *testing.T,
})

t.Run("Executor", func(t *testing.T) {
t.Skip()
hasValidHeader := func(cmd, msg string) bool {
if botDriver.Type() == commplatform.TeamsBot {
// Teams uses AdaptiveCard and the built-in table format, that's the reason why we can't
Expand Down Expand Up @@ -812,7 +820,7 @@ func runBotTest(t *testing.T,
})
}
})

var firstCMUpdate commplatform.ExpAttachmentInput
limitMessages := func() int {
if botDriver.Type().IsCloud() {
Expand Down Expand Up @@ -1738,15 +1746,15 @@ func trimRightWhitespace(input string) string {

func waitForLastPlaintextMessageEqual(driver commplatform.BotDriver, channelID, expectedMsg string) error {
switch driver.Type() {
case commplatform.TeamsBot:
// in this case of a plain text message, Teams renderer uses Adaptive Cards format
return driver.WaitForLastInteractiveMessagePostedEqual(driver.BotUserID(), channelID, interactive.CoreMessage{
Message: api.Message{
BaseBody: api.Body{
Plaintext: expectedMsg,
},
},
})
//case commplatform.TeamsBot:
// // in this case of a plain text message, Teams renderer uses Adaptive Cards format
// return driver.WaitForLastInteractiveMessagePostedEqual(driver.BotUserID(), channelID, interactive.CoreMessage{
// Message: api.Message{
// BaseBody: api.Body{
// Plaintext: expectedMsg,
// },
// },
// })
default:
return driver.OnChannel().WaitForLastMessageEqual(driver.BotUserID(), channelID, expectedMsg)
}
Expand All @@ -1757,10 +1765,6 @@ func waitForLastPlaintextMessageWithHeaderEqual(cfg Config, driver commplatform.
}

func waitForLastCodeBlockMessageWithHeaderEqual(cfg Config, driver commplatform.BotDriver, cmd, expectedBody string) error {
return waitForLastMessageWithHeaderEqual(cfg, driver, cmd, expectedBody, true)
}

func waitForLastMessageWithHeaderEqual(cfg Config, driver commplatform.BotDriver, cmd, expectedBody string, asCodeBlock bool) error {
cmdHeader := func(command string) string {
return fmt.Sprintf("`%s` on `%s`", command, cfg.ClusterName)
}
Expand All @@ -1772,13 +1776,35 @@ func waitForLastMessageWithHeaderEqual(cfg Config, driver commplatform.BotDriver
Description: cmdHeader(cmd),
Message: api.Message{},
}
if asCodeBlock {
msg.Message.BaseBody.CodeBlock = expectedBody
} else {
msg.Message.BaseBody.Plaintext = expectedBody
}
msg.Message.BaseBody.CodeBlock = expectedBody

return driver.WaitForLastInteractiveMessagePostedEqual(driver.BotUserID(), driver.FirstChannel().ID(), msg)
default:
expectedBody = codeBlock(expectedBody)
expectedMessage := fmt.Sprintf("%s\n%s", cmdHeader(cmd), expectedBody)
return driver.WaitForLastMessageEqual(driver.BotUserID(), driver.FirstChannel().ID(), expectedMessage)
}
}

func waitForLastMessageWithHeaderEqual(cfg Config, driver commplatform.BotDriver, cmd, expectedBody string, asCodeBlock bool) error {
cmdHeader := func(command string) string {
return fmt.Sprintf("`%s` on `%s`", command, cfg.ClusterName)
}

switch driver.Type() {
//case commplatform.TeamsBot:
// // Teams renderer uses Adaptive Cards format to render header in a more readable way
// msg := interactive.CoreMessage{
// Description: cmdHeader(cmd),
// Message: api.Message{},
// }
// if asCodeBlock {
// msg.Message.BaseBody.CodeBlock = expectedBody
// } else {
// msg.Message.BaseBody.Plaintext = expectedBody
// }
//
// return driver.WaitForLastInteractiveMessagePostedEqual(driver.BotUserID(), driver.FirstChannel().ID(), msg)
default:
if asCodeBlock {
expectedBody = codeBlock(expectedBody)
Expand Down
19 changes: 19 additions & 0 deletions test/msteamsx/d_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package msteamsx

import (
"fmt"
md "github.com/JohannesKaufmann/html-to-markdown"
"testing"
)

func Test(t *testing.T) {

in := `<p><code>echo help</code> on <code>test-first-633bc317-3e61-4711-b3f0-9b0c10b1c673</code> .... empty response <em><em></em></em> 🦗 🦗 🦗</p>`

converter := md.NewConverter("", true, nil)
markdown, err := converter.ConvertString(in)
if err != nil {
fmt.Println(err)
}
fmt.Println(markdown)
}

0 comments on commit fa12d1c

Please sign in to comment.