Add format tests (#181)

Co-authored-by: James Mills <1290234+prologic@users.noreply.github.com>
Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/181
This commit is contained in:
James Mills 2023-01-27 23:13:51 +00:00
parent 09a513e2ec
commit 36f9b25ec9
4 changed files with 47 additions and 45 deletions

View File

@ -13,13 +13,11 @@ import (
"os"
"path"
"strings"
"time"
"git.mills.io/prologic/msgbus"
msgbus_client "git.mills.io/prologic/msgbus/client"
"github.com/keys-pub/keys"
log "github.com/sirupsen/logrus"
"go.yarn.social/lextwt"
"go.mills.io/salty"
"go.mills.io/saltyim/internal/exec"
@ -64,32 +62,6 @@ func parseExtraEnvs(extraenvs string) map[string]string {
return env
}
// PackMessage formats an outgoing message in the Message Format
// <timestamp>\t(<sender>) <message>
func PackMessage(me Addr, msg string) []byte {
//log.Debug("pack: ", me.Formatted(), msg)
return []byte(
fmt.Sprint(
time.Now().UTC().Format(time.RFC3339), "\t",
me.Formatted(), "\t",
strings.TrimSpace(msg), "\n",
),
)
}
// PackMessageTime formats an incoming message in the Message Format using the existing timestamp
// <timestamp>\t(<sender>) <message>
func PackMessageTime(me Addr, msg string, t *lextwt.DateTime) []byte {
//log.Debug("pack: ", me.Formatted(), msg)
return []byte(
fmt.Sprint(
t.Literal(), "\t",
me.Formatted(), "\t",
strings.TrimSpace(msg), "\n",
),
)
}
// Client is a Salty IM client that handles talking to a Salty IM Broker
// and Sedngina and Receiving messages to/from Salty IM Users.
type Client struct {

View File

@ -3,12 +3,14 @@ package saltyim
import (
"bytes"
"fmt"
"hash/fnv"
"os"
"strings"
"time"
"github.com/logrusorgru/aurora"
"github.com/posener/formatter"
log "github.com/sirupsen/logrus"
colorhash "github.com/taigrr/go-colorhash"
"go.yarn.social/lextwt"
)
@ -17,18 +19,6 @@ const (
DateTimeFormat = "2006-01-02 15:04:05"
)
func boundedInt(value, low, high uint8) uint8 {
diff := high - low
return (((value - low) % diff) + low)
}
// GetUserColor is used by the TUI (terminal ui) client to compute an appropriate ANSI color for a given user
func GetUserColor(user string, lower, upper uint8) uint8 {
h := fnv.New32()
h.Sum([]byte(user))
return boundedInt(uint8(h.Sum32()), lower, upper)
}
// FormatMessage formats the msg for display on the terminal
func FormatMessage(msg string) string {
s, err := lextwt.ParseSalty(msg)
@ -43,7 +33,7 @@ func FormatMessage(msg string) string {
return ""
}
userColor := GetUserColor(st.User.String(), 16, 231)
userColor := colorhash.HashString(st.User.String())
buf := &bytes.Buffer{}
@ -61,7 +51,32 @@ func FormatMessage(msg string) string {
return fmt.Sprintf(
"%s\t%s\n%s\n",
aurora.Sprintf(aurora.Blue(st.Timestamp.DateTime().Local().Format(DateTimeFormat))),
aurora.Sprintf(aurora.Index(userColor, st.User.String())),
aurora.Sprintf(aurora.Index(uint8(userColor), st.User.String())),
buf.String(),
)
}
// PackMessage formats an outgoing message in the Message Format
// <timestamp>\t(<sender>) <message>
func PackMessage(me Addr, msg string) []byte {
//log.Debug("pack: ", me.Formatted(), msg)
return []byte(
fmt.Sprint(
time.Now().UTC().Format(time.RFC3339), "\t",
me.Formatted(), "\t",
strings.TrimSpace(msg), "\n",
),
)
}
// PackMessageTime formats an incoming message in the Message Format using the existing timestamp
// <timestamp>\t(<sender>) <message>
func PackMessageTime(me Addr, msg string, t *lextwt.DateTime) []byte {
return []byte(
fmt.Sprint(
t.Literal(), "\t",
me.Formatted(), "\t",
strings.TrimSpace(msg), "\n",
),
)
}

View File

@ -9,6 +9,21 @@ import (
"go.yarn.social/lextwt"
)
func TestFormatMessage(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
me, err := saltyim.ParseAddr(userAlice)
require.NoError(err)
message := "Hello World!"
packed := saltyim.PackMessage(me, message)
unpacked := saltyim.FormatMessage(string(packed))
assert.Contains(unpacked, userAlice)
assert.Contains(unpacked, message)
}
func TestPackMessage(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6364f5bc3c2477c8c331b88b8d1d3748dded41539bd7a9752a94e23305cd4b6c
size 29761326
oid sha256:918a7f3370b037e18874307e00ccb8b1f7a458fe7be68913a09ca9d922ee89fd
size 29760905