Skip to content

Commit

Permalink
Merge pull request #38 from rneatherway/rneatherway/fix-tests
Browse files Browse the repository at this point in the history
Group adjacent messages by the same author v2
  • Loading branch information
rneatherway committed May 2, 2023
2 parents 476dca0 + f0ccc94 commit f87eab8
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 42 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand Down
30 changes: 7 additions & 23 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/billgraziano/dpapi v0.4.0 h1:t39THI1Ld1hkkLVrhkOX6u5TUxwzRddOffq4jcwh2AE=
github.com/billgraziano/dpapi v0.4.0/go.mod h1:gi1Lin0jvovT53j0EXITkY6UPb3hTfI92POaZgj9JBA=
github.com/cli/go-gh v0.0.3 h1:GcVgUa7q0SeauIRbch3VSUXVij6+c49jtAHv7WuWj5c=
github.com/cli/go-gh v0.0.3/go.mod h1:J1eNgrPJYAUy7TwPKj7GW1ibqI+WCiMndtyzrCyZIiQ=
github.com/cli/go-gh v1.2.1 h1:xFrjejSsgPiwXFP6VYynKWwxLQcNJy3Twbu82ZDlR/o=
github.com/cli/go-gh v1.2.1/go.mod h1:Jxk8X+TCO4Ui/GarwY9tByWm/8zp4jJktzVZNlTW5VM=
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
github.com/cli/shurcooL-graphql v0.0.1 h1:/9J3t9O6p1B8zdBBtQighq5g7DQRItBwuwGh3SocsKM=
github.com/cli/shurcooL-graphql v0.0.1/go.mod h1:U7gCSuMZP/Qy7kbqkk5PrqXEeDgtfG5K+W+u8weorps=
github.com/cli/shurcooL-graphql v0.0.2 h1:rwP5/qQQ2fM0TzkUTwtt6E2LbIYf6R+39cUXTa04NYk=
github.com/cli/shurcooL-graphql v0.0.2/go.mod h1:tlrLmw/n5Q/+4qSvosT+9/W5zc8ZMjnJeYBxSdb4nWA=
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/godbus/dbus/v5 v5.0.6 h1:mkgN1ofwASrYnJ5W6U/BxG15eXXXjirgZc7CLqkcaro=
Expand All @@ -23,6 +18,7 @@ github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
Expand All @@ -32,13 +28,10 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:C
github.com/keybase/go-keychain v0.0.0-20220506172723-c18928ccd7f2 h1:Qh9gCBYNeGqpMv7SMVGgansYlZgnMkuPOQZpoZNxSJs=
github.com/keybase/go-keychain v0.0.0-20220506172723-c18928ccd7f2/go.mod h1:5p1xgRXY2da7ggc/67EZO7WlWAZ8TXftfCU6RtvWZJ0=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
Expand All @@ -47,6 +40,7 @@ github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.10 h1:MLn+5bFRlWMGoSRmJour3CL1w/qL96mvipqpwQW/Sfk=
github.com/mattn/go-sqlite3 v1.14.10/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/termenv v0.12.0 h1:KuQRUE3PgxRFWhq4gHvZtPSLCGDqM5q/cYr1pZ39ytc=
github.com/muesli/termenv v0.12.0/go.mod h1:WCCv32tusQ/EEZ5S8oUIIrC/nIuBcxCVqlN4Xfkv+7A=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -57,9 +51,7 @@ github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6O
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand All @@ -73,7 +65,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
Expand All @@ -84,10 +75,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200828161417-c663848e9a16/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -99,13 +88,11 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
Expand All @@ -118,10 +105,7 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
lukechampine.com/uint128 v1.1.1 h1:pnxCASz787iMf+02ssImqk6OLt+Z5QHMoZyUXR4z6JU=
Expand Down
18 changes: 18 additions & 0 deletions internal/httpclient/httpclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package httpclient

import (
"net/http"
)

// See https://www.thegreatcodeadventure.com/mocking-http-requests-in-golang/
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

var (
Client HTTPClient
)

func init() {
Client = &http.Client{}
}
56 changes: 40 additions & 16 deletions internal/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func interpolateUsers(client UserProvider, s string) (string, error) {
func parseUnixTimestamp(s string) (*time.Time, error) {
tsParts := strings.Split(s, ".")
if len(tsParts) != 2 {
return nil, fmt.Errorf("timestamp '%s' in not in <seconds>.<milliseconds> format", s)
return nil, fmt.Errorf("timestamp '%s' is not in <seconds>.<milliseconds> format", s)
}

seconds, err := strconv.ParseInt(tsParts[0], 10, 64)
Expand Down Expand Up @@ -102,24 +102,44 @@ func FromMessages(client *slackclient.SlackClient, history *slackclient.HistoryR
return msgTimes[messages[i].Ts].Before(msgTimes[messages[j].Ts])
})

for _, message := range messages {
var username string
var err error

if message.User != "" {
username, err = client.UsernameForID(message.User)
} else if message.BotID != "" {
username = fmt.Sprintf("bot %s", message.BotID)
} else {
username = "ghost"
}
lastSpeakerID := ""

for i, message := range messages {
username, err := client.UsernameForMessage(message)
if err != nil {
return "", err
}

fmt.Fprintf(b, "> **%s** at %s\n>\n",
username,
msgTimes[message.Ts].Format("2006-01-02 15:04"))
speakerID := message.User
if speakerID == "" {
speakerID = message.BotID
}

messageTime := msgTimes[message.Ts]
messageTimeDiffInMinutes := 0

// How far apart in minutes can two messages be, by the same author, before we repeat the header line?
messageTimeMinuteCutoff := 60

if i > 0 {
prevMessage := messages[i-1]
prevMessageTime := msgTimes[prevMessage.Ts]
messageTimeDiffInMinutes = int(messageTime.Sub(prevMessageTime).Minutes())
}

if lastSpeakerID != "" && speakerID != lastSpeakerID || messageTimeDiffInMinutes > messageTimeMinuteCutoff {
fmt.Fprintf(b, "\n")
}

includeSpeakerHeader := lastSpeakerID == "" || speakerID != lastSpeakerID ||
messageTimeDiffInMinutes > messageTimeMinuteCutoff

if includeSpeakerHeader {
fmt.Fprintf(b, "> **%s** at %s\n",
username,
messageTime.In(client.GetLocation()).Format("2006-01-02 15:04 MST"))
}
fmt.Fprintf(b, ">\n")

if message.Text != "" {
err = convert(client, b, message.Text)
Expand All @@ -136,7 +156,11 @@ func FromMessages(client *slackclient.SlackClient, history *slackclient.HistoryR
}
}

b.WriteString("\n")
if !includeSpeakerHeader {
b.WriteString("\n")
}

lastSpeakerID = speakerID
}

return b.String(), nil
Expand Down
124 changes: 124 additions & 0 deletions internal/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,133 @@ package markdown

import (
"fmt"
"strings"
"testing"

"github.com/rneatherway/gh-slack/internal/httpclient"
"github.com/rneatherway/gh-slack/internal/mocks"
"github.com/rneatherway/gh-slack/internal/slackclient"
)

func TestFromMessagesCombinesAdjacentMessagesFromSameUser(t *testing.T) {
httpclient.Client = &mocks.MockClient{}
mocks.MockSuccessfulAuthResponse()
client, err := slackclient.Null("test")
if err != nil {
t.Fatal(err)
}
messages := []slackclient.Message{
{Text: "hello", User: "82317", BotID: "", Ts: "123.456"},
{Text: "second message", User: "82317", BotID: "", Ts: "124.567"},
}
history := &slackclient.HistoryResponse{Ok: true, HasMore: false, Messages: messages}
mocks.MockSuccessfulUsersResponse([]slackclient.User{{ID: "82317", Name: "cheshire137"}})
actual, err := FromMessages(client, history)
if err != nil {
t.Fatal(err)
}
expected := `> **cheshire137** at 1970-01-01 00:02 UTC
>
> hello
>
> second message`
if expected != strings.TrimSpace(actual) {
t.Fatal("expected:\n\n", expected, "\n\ngot:\n\n", actual)
}
}

func TestFromMessagesCombinesAdjacentMessagesFromSameBot(t *testing.T) {
httpclient.Client = &mocks.MockClient{}
mocks.MockSuccessfulAuthResponse()
client, err := slackclient.Null("test")
if err != nil {
t.Fatal(err)
}
messages := []slackclient.Message{
{Text: "hello", User: "", BotID: "bot123", Ts: "123.456"},
{Text: "second message", User: "", BotID: "bot123", Ts: "124.567"},
}
history := &slackclient.HistoryResponse{Ok: true, HasMore: false, Messages: messages}
actual, err := FromMessages(client, history)
if err != nil {
t.Fatal(err)
}
expected := `> **bot bot123** at 1970-01-01 00:02 UTC
>
> hello
>
> second message`
if expected != strings.TrimSpace(actual) {
t.Fatal("expected:\n\n", expected, "\n\ngot:\n\n", actual)
}
}

func TestFromMessagesSeparatesMessagesFromSameUserWhenFarApartInTime(t *testing.T) {
httpclient.Client = &mocks.MockClient{}
mocks.MockSuccessfulAuthResponse()
client, err := slackclient.Null("test")
if err != nil {
t.Fatal(err)
}
messages := []slackclient.Message{
{Text: "hello", User: "82317", BotID: "", Ts: "1679058753.0"},
{Text: "second message", User: "82317", BotID: "", Ts: "1679064168.0"},
}
history := &slackclient.HistoryResponse{Ok: true, HasMore: false, Messages: messages}
mocks.MockSuccessfulUsersResponse([]slackclient.User{{ID: "82317", Name: "cheshire137"}})
actual, err := FromMessages(client, history)
if err != nil {
t.Fatal(err)
}
expected := `> **cheshire137** at 2023-03-17 13:12 UTC
>
> hello
> **cheshire137** at 2023-03-17 14:42 UTC
>
> second message`
if expected != strings.TrimSpace(actual) {
t.Fatal("expected:\n\n", expected, "\n\ngot:\n\n", actual)
}
}

func TestFromMessagesSeparatesMessagesFromSameUserWhenNotAdjacent(t *testing.T) {
httpclient.Client = &mocks.MockClient{}
mocks.MockSuccessfulAuthResponse()
client, err := slackclient.Null("test")
if err != nil {
t.Fatal(err)
}
messages := []slackclient.Message{
{Text: "first!", User: "82317", BotID: "", Ts: "123.456"},
{Text: "Message the Second", User: "1234", BotID: "", Ts: "124.567"},
{Text: "third message", User: "82317", BotID: "", Ts: "125.678"},
}
history := &slackclient.HistoryResponse{Ok: true, HasMore: false, Messages: messages}
mocks.MockSuccessfulUsersResponse([]slackclient.User{
{ID: "82317", Name: "cheshire137"},
{ID: "1234", Name: "octokatherine"},
})
actual, err := FromMessages(client, history)
if err != nil {
t.Fatal(err)
}
expected := `> **cheshire137** at 1970-01-01 00:02 UTC
>
> first!
> **octokatherine** at 1970-01-01 00:02 UTC
>
> Message the Second
> **cheshire137** at 1970-01-01 00:02 UTC
>
> third message`
if expected != strings.TrimSpace(actual) {
t.Fatal("expected:\n\n", expected, "\n\ngot:\n\n", actual)
}
}

func TestUserRE(t *testing.T) {
results := userRE.FindAllStringIndex("<@UP7UAV3NH> <@UPA5ANVNJ> hello", -1)
if len(results) != 2 {
Expand Down
Loading

0 comments on commit f87eab8

Please sign in to comment.