Signed the message

By signed the message I just mean I added a box to the output
to know who sent it
This commit is contained in:
2024-05-25 12:31:18 +01:00
parent a7d044321b
commit f3020dd209
2 changed files with 217 additions and 1 deletions
+58 -1
View File
@@ -8,7 +8,10 @@ import (
"io"
"log"
"net/http"
"strings"
"thermalFax/models"
"time"
"unicode/utf8"
)
type PrintServerResponse struct {
@@ -83,7 +86,8 @@ func SendFax(w http.ResponseWriter, r *http.Request) {
return
}
_, err = sendRequestToPrinter(formData.Message)
msg := signMessage(formData.Message, session.User, time.Now().Format("2006-01-02 15:04:05"))
_, err = sendRequestToPrinter(msg)
if err != nil {
log.Println("| Printer failed to process fax:", err)
w.Write([]byte("{\"state\":\"failed\"}"))
@@ -93,3 +97,56 @@ func SendFax(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/json")
w.Write([]byte("{\"state\":\"success\"}"))
}
// NOTE: this isn't multi-Unicode-codepoint aware, like specifying skintone or
// gender of an emoji: https://unicode.org/emoji/charts/full-emoji-modifiers.html
// This thing was straight up copied from SO
func substr(input string, start int, length int) string {
asRunes := []rune(input)
if start >= len(asRunes) {
return ""
}
if start+length > len(asRunes) {
length = len(asRunes) - start
}
return string(asRunes[start : start+length])
}
// truncateString given a string and a lim truncates that string if its longer than the lim
// with "..."
func truncateString(s string, lim int) string {
runeCount := utf8.RuneCountInString(s)
if runeCount > lim {
s = substr(s, 0, lim-3) + "..."
} else {
s = s + strings.Repeat(" ", lim-runeCount)
}
return s
}
// signMessage adds a small box to the message so the reader knows who sent it and the time
func signMessage(msg string, user string, time string) string {
// defWidht := 48
nameWidth := 39
userLine := "| User: "
userLine = userLine + truncateString(user, nameWidth) + "|"
// defWidht := 48
timeWidth := 39
timeLine := "| Time: "
timeLine = timeLine + truncateString(time, timeWidth) + "|"
sign := fmt.Sprintf(`
+----------------------------------------------+
%s
%s
+----------------------------------------------+`,
userLine, timeLine)
truncation := "+- END ---------------------------------- END -+"
return sign + "\n" + msg + "\n\n" + truncation
}
+159
View File
@@ -0,0 +1,159 @@
package controllers
import (
"testing"
"unicode/utf8"
)
func TestSignMessage_NormalUserName(t *testing.T) {
// Arrange
user := "Alice"
msg := "Hello, world!"
tm := "2024-05-31 15:02:23"
expected := `
+----------------------------------------------+
| User: Alice |
| Time: 2024-05-31 15:02:23 |
+----------------------------------------------+`
truncation := "\n\n+- END ---------------------------------- END -+"
expectedOutput := expected + "\n" + msg + truncation
// Act
result := signMessage(msg, user, tm)
// Assert
if result != expectedOutput {
t.Errorf("Expected:\n%v\nGot:\n%v", expectedOutput, result)
}
}
func TestSignMessage_VeryLongUserName(t *testing.T) {
// Arrange
user := "Alice Cuja Mãe Deu Um Nome Demasiado Longo"
msg := "Hello, world!"
tm := "2024-05-31 15:02:23"
expected := `
+----------------------------------------------+
| User: Alice Cuja Mãe Deu Um Nome Demasiado...|
| Time: 2024-05-31 15:02:23 |
+----------------------------------------------+`
truncation := "\n\n+- END ---------------------------------- END -+"
expectedOutput := expected + "\n" + msg + truncation
// Act
result := signMessage(msg, user, tm)
// Assert
if result != expectedOutput {
t.Errorf("Expected:\n%v\nGot:\n%v", expectedOutput, result)
}
}
func TestSignMessage_UserNameMatchesAvailableSpace(t *testing.T) {
// Arrange
user := "Alice Cuja Mãe Deu Um Nome Demasiado Longo"
msg := "Hello, world!"
tm := "2024-05-31 15:02:23"
// Alice Cuja Mãe Deu Um Nome Demasiado...
// umnomeextrasuperduperlongoesperoquei...
expected := `
+----------------------------------------------+
| User: Alice Cuja Mãe Deu Um Nome Demasiado...|
| Time: 2024-05-31 15:02:23 |
+----------------------------------------------+`
truncation := "\n\n+- END ---------------------------------- END -+"
expectedOutput := expected + "\n" + msg + truncation
// Act
result := signMessage(msg, user, tm)
// Assert
if result != expectedOutput {
t.Errorf("Expected:\n%v\nGot:\n%v", expectedOutput, result)
}
}
func TestTruncateString_StringLargerThenLim(t *testing.T) {
// Arrange
user := "umnomeextrasuperduperlongoesperoqueistochegue"
// Alice Cuja Mãe Deu Um Nome Demasiado...
// umnomeextrasuperduperlongoesperoquei...
expected := "umnomeextrasuperduperlongoesperoquei..."
// Act
result := truncateString(user, 39)
// Assert
if result != expected {
t.Errorf("Expected:\n%v\nGot:\n%v", expected, result)
}
}
func TestTruncateString_StringSmallerThenLim(t *testing.T) {
// Arrange
user := "umnome"
// Alice Cuja Mãe Deu Um Nome Demasiado...
// umnomeextrasuperduperlongoesperoquei...
expected := "umnome "
// Act
result := truncateString(user, 39)
// Assert
if result != expected {
t.Errorf("Expected:\n%v\nGot:\n%v", expected, result)
}
}
func TestTruncateString_StringEqualsThenLim(t *testing.T) {
// Arrange
user := "umnomeextrasuperduperlongoesperoqueisto"
// Alice Cuja Mãe Deu Um Nome Demasiado...
// umnomeextrasuperduperlongoesperoquei...
expected := "umnomeextrasuperduperlongoesperoqueisto"
// Act
result := truncateString(user, 39)
// Assert
if result != expected {
t.Errorf("Expected:\n%v\nGot:\n[%v]", expected, result)
}
}
// This is the right method to use to get the number of characters
// in a string
func TestRuneCount(t *testing.T) {
// Arrange
str := "Mãe"
expected := 3
// Act
result := utf8.RuneCountInString(str)
// Assert
if result != expected {
t.Errorf("Expected:\n%v\nGot:\n%v", expected, result)
}
}
func TestSubstr_Correct(t *testing.T) {
// Arrange
str := "Mãe"
expected := "M"
// Act
result := substr(str, 0, 1)
// Assert
if result != expected {
t.Errorf("Expected:\n%v\nGot:\n%v", expected, result)
}
}