57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package cli
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"encoding/json"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
kmsvc "forgejo.riotpiao.homelab.com/homelab/kmsvc-sdk"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestRenderMessagesTable(t *testing.T) {
|
||
|
|
var buf bytes.Buffer
|
||
|
|
msgs := []kmsvc.Message{{MessageID: "m1", ReceiptHandle: "rh1", Body: []byte("hi"), ReceiveCount: 2}}
|
||
|
|
|
||
|
|
if err := renderMessages(&buf, "table", msgs); err != nil {
|
||
|
|
t.Fatalf("renderMessages: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
out := buf.String()
|
||
|
|
if !strings.Contains(out, "MESSAGE_ID") || !strings.Contains(out, "m1") || !strings.Contains(out, "hi") {
|
||
|
|
t.Errorf("table output missing expected content: %q", out)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRenderMessagesJSON(t *testing.T) {
|
||
|
|
var buf bytes.Buffer
|
||
|
|
msgs := []kmsvc.Message{{MessageID: "m1", ReceiptHandle: "rh1", Body: []byte("hi")}}
|
||
|
|
|
||
|
|
if err := renderMessages(&buf, "json", msgs); err != nil {
|
||
|
|
t.Fatalf("renderMessages: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
var got []kmsvc.Message
|
||
|
|
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
|
||
|
|
t.Fatalf("unmarshal output: %v", err)
|
||
|
|
}
|
||
|
|
if len(got) != 1 || got[0].MessageID != "m1" {
|
||
|
|
t.Errorf("unexpected decoded messages: %+v", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRenderBatchResultTable(t *testing.T) {
|
||
|
|
var buf bytes.Buffer
|
||
|
|
successful := []kmsvc.BatchResultEntry{{ID: "1", MessageID: "m1"}}
|
||
|
|
failed := []kmsvc.BatchResultEntry{{ID: "2", Error: "boom"}}
|
||
|
|
|
||
|
|
if err := renderBatchResult(&buf, "table", successful, failed); err != nil {
|
||
|
|
t.Fatalf("renderBatchResult: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
out := buf.String()
|
||
|
|
if !strings.Contains(out, "ok") || !strings.Contains(out, "boom") {
|
||
|
|
t.Errorf("table output missing expected content: %q", out)
|
||
|
|
}
|
||
|
|
}
|