parsimony/internal/cli/posts_test.go
tgiachi 47dc118b98 test: tighten the posts tests found weak by mutation
- ListPosts: a limit of 201 is capped to 200
- posts ls: distinct pending, failed and dropped counts, so a swapped
  column is caught
2026-11-10 12:47:17 +01:00

95 lines
3.4 KiB
Go

package cli
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"codeberg.org/squidd/parsimony/internal/store"
)
func TestPostsLs(t *testing.T) {
e := newEnv(t)
e.ok(t, "list", "add", "dev@example.org")
l, _ := e.s.FindList("dev@example.org")
if out := e.ok(t, "posts", "ls", "dev@example.org"); !strings.Contains(out, "No posts yet.") {
t.Fatalf("empty: %q", out)
}
now := time.Date(2026, 3, 1, 9, 30, 0, 0, time.UTC)
e.s.Now = func() time.Time { return now }
var rcpts []store.NewOutbound
for i := 0; i < 7; i++ {
rcpts = append(rcpts, store.NewOutbound{From: "b@x.org", Rcpt: fmt.Sprintf("u%d@x.com", i)})
}
e.s.EnqueuePost(l.ID, "mid:<1@x>", []byte("Message-ID: <1@x>\r\nSubject: March issue\r\n\r\nb"), rcpts)
due, _ := e.s.DueOutbox(10)
for _, d := range due[:4] {
e.s.CompleteOutbox(d.ID)
}
e.s.FailOutbox(due[4].ID, "no")
now = now.Add(time.Hour)
e.s.EnqueuePost(l.ID, "mid:<2@x>", []byte("Message-ID: <2@x>\r\n\r\nb"), nil)
out := e.ok(t, "posts", "ls", fmt.Sprint(l.ID))
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) != 3 {
t.Fatalf("posts ls:\n%s", out)
}
if f := strings.Fields(lines[0]); strings.Join(f, " ") != "ID DATE SUBJECT RECIPIENTS SENT PENDING FAILED DROPPED" {
t.Fatalf("header: %q", lines[0])
}
if f := strings.Fields(lines[1]); strings.Join(f, " ") != "2 2026-03-01 10:30 (no subject) 0 0 0 0 0" {
t.Fatalf("newest first: %q", lines[1])
}
if f := strings.Fields(lines[2]); strings.Join(f, " ") != "1 2026-03-01 09:30 March issue 7 4 2 1 0" {
t.Fatalf("row: %q", lines[2])
}
if out := e.ok(t, "posts", "ls", "dev@example.org", "--limit", "1"); strings.Contains(out, "March issue") || !strings.Contains(out, "(no subject)") {
t.Fatalf("--limit 1:\n%s", out)
}
var js []map[string]any
if err := json.Unmarshal([]byte(e.ok(t, "--json", "posts", "ls", "dev@example.org")), &js); err != nil || len(js) != 2 ||
js[1]["subject"] != "March issue" || js[1]["pending"] != float64(2) || js[1]["message_id"] != "<1@x>" {
t.Fatalf("json: %v %v", js, err)
}
}
func TestPostsLsErrors(t *testing.T) {
e := newEnv(t)
e.fails(t, 2, "usage: parsimony posts ls", "posts", "ls")
e.fails(t, 2, "usage: parsimony posts ls", "posts", "ls", "a@x", "b@x")
e.fails(t, 2, "flag provided but not defined", "posts", "ls", "a@x", "--nope")
e.fails(t, 1, "no such list", "posts", "ls", "nope@example.org")
e.ok(t, "list", "add", "dev@example.org")
e.fails(t, 1, "limit must be", "posts", "ls", "dev@example.org", "--limit", "-1")
if out := e.ok(t, "help"); !strings.Contains(out, "posts ls <list> [--limit N]") {
t.Fatalf("help lacks posts ls:\n%s", out)
}
}
func TestPostsLsEscapesControlCharacters(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/v1/lists/1":
fmt.Fprint(w, `{"id":1,"address":"dev@example.org"}`)
case "/api/v1/lists/1/posts":
b, _ := json.Marshal([]map[string]any{{"id": 1, "subject": "s " + esc, "created_at": time.Unix(0, 0), "recipients": 1}})
w.Write(b)
default:
http.NotFound(w, r)
}
}))
defer srv.Close()
e := &env{srv: srv}
out := e.ok(t, "posts", "ls", "1")
noControl(t, "posts ls", out)
if !strings.Contains(out, `\x1b[31mRED`) {
t.Fatalf("the escape sequence should stay visible as text:\n%s", out)
}
}