parsimony/internal/cli/posts_test.go
tgiachi 7e43117c74 feat(cli): parsimony posts ls
- posts ls <list> [--limit N] prints ID, DATE, SUBJECT, RECIPIENTS, SENT,
  PENDING, FAILED and DROPPED, newest first, "No posts yet." when empty,
  and the API's JSON with --json
- cells go through the same control-character escaping as the other
  tables; tests for the table, the limit, JSON, errors and escaping
2026-11-10 12:42:15 +01:00

91 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 }
rcpts := []store.NewOutbound{{From: "b@x.org", Rcpt: "a@x.com"}, {From: "b@x.org", Rcpt: "b@x.com"}, {From: "b@x.org", Rcpt: "c@x.com"}, {From: "b@x.org", Rcpt: "d@x.com"}}
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)
e.s.CompleteOutbox(due[0].ID)
e.s.CompleteOutbox(due[1].ID)
e.s.FailOutbox(due[2].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 4 2 1 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(1) || 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)
}
}