Files
mesh-catalog/modules/mesh-mail-reader/cmd/mesh-mail-reader/signal_test.go
T

102 lines
3.9 KiB
Go

package main
import (
"strings"
"testing"
"time"
)
func d(day int) time.Time { return time.Date(2026, 11, day, 0, 0, 0, 0, time.UTC) }
// 1429/AC1: one mail gives headline "1 mail matters" and one line; the first line names the mail with the
// nearest due date as "<sender> — <summary> — due <date>", no due part without one.
func TestBuildSignalOneMail(t *testing.T) {
w := BuildSignal([]SignalMail{{SenderName: "Alice", Summary: "pay the invoice", DueDate: d(1)}})
if w.Headline != "1 mail matters" {
t.Fatalf("headline = %q", w.Headline)
}
if len(w.Lines) != 1 || w.Lines[0] != "Alice — pay the invoice — due 2026-11-01" {
t.Fatalf("lines = %v", w.Lines)
}
}
// 1429/AC1: a mail with no due date carries no due part.
func TestBuildSignalNoDueDate(t *testing.T) {
w := BuildSignal([]SignalMail{{SenderName: "Alice", Summary: "fyi"}})
if w.Lines[0] != "Alice — fyi" {
t.Fatalf("lines[0] = %q, want no due part", w.Lines[0])
}
}
// 1429/AC1: N mails give "N mails matter"; the first line names the nearest due date, the second the next
// one, ending with the "and N-2 more" tail once there are more than two.
func TestBuildSignalSeveralMails(t *testing.T) {
mails := []SignalMail{
{SenderName: "Carol", Summary: "c", Date: d(1)}, // no due date, oldest of the no-due-date ones
{SenderName: "Bob", Summary: "b", DueDate: d(10)},
{SenderName: "Alice", Summary: "a", DueDate: d(5)}, // nearest due date: first
{SenderName: "Dave", Summary: "d", Date: d(2)},
}
w := BuildSignal(mails)
if w.Headline != "4 mails matter" {
t.Fatalf("headline = %q", w.Headline)
}
if len(w.Lines) != 2 {
t.Fatalf("lines = %v, want 2", w.Lines)
}
if !strings.HasPrefix(w.Lines[0], "Alice —") {
t.Fatalf("lines[0] = %q, want Alice first (nearest due date)", w.Lines[0])
}
if !strings.HasPrefix(w.Lines[1], "Bob —") || !strings.HasSuffix(w.Lines[1], ", and 2 more on the mail page") {
t.Fatalf("lines[1] = %q, want Bob, and the N-2 tail", w.Lines[1])
}
}
// 1429/AC1: with exactly two mails, the second line carries no "more" tail.
func TestBuildSignalTwoMailsNoTail(t *testing.T) {
w := BuildSignal([]SignalMail{
{SenderName: "Alice", Summary: "a", DueDate: d(1)},
{SenderName: "Bob", Summary: "b", DueDate: d(2)},
})
if strings.Contains(w.Lines[1], "more") {
t.Fatalf("lines[1] = %q, want no 'more' tail with only two mails", w.Lines[1])
}
}
// 1429/AC2: a sender without a display name is "a sender without a name", never its address.
func TestBuildSignalSenderWithNoName(t *testing.T) {
w := BuildSignal([]SignalMail{{SenderName: "", Summary: "x"}})
if !strings.HasPrefix(w.Lines[0], "a sender without a name —") {
t.Fatalf("lines[0] = %q, want it to start with 'a sender without a name'", w.Lines[0])
}
}
// 1429/AC3: the safe form keeps the same headline and says only "open the mail page".
func TestSafeSignal(t *testing.T) {
w := SafeSignal(3)
if w.Headline != "3 mails matter" {
t.Fatalf("headline = %q", w.Headline)
}
if len(w.Lines) != 1 || w.Lines[0] != "open the mail page" {
t.Fatalf("lines = %v", w.Lines)
}
one := SafeSignal(1)
if one.Headline != "1 mail matters" {
t.Fatalf("one mail's headline = %q", one.Headline)
}
}
// 1429/AC4: mails carried over from earlier (untold) are counted in N and ordered with the new ones — this
// function takes no notice of which are new and which are carried, only their own due date and date.
func TestBuildSignalCountsCarriedMailsWithTheNewOnes(t *testing.T) {
carried := SignalMail{SenderName: "Old", Summary: "carried", DueDate: d(1)} // told=false from an earlier batch
newMail := SignalMail{SenderName: "New", Summary: "fresh", DueDate: d(2)}
w := BuildSignal([]SignalMail{carried, newMail})
if w.Headline != "2 mails matter" {
t.Fatalf("headline = %q, want the carried mail counted too", w.Headline)
}
if !strings.HasPrefix(w.Lines[0], "Old —") {
t.Fatalf("lines[0] = %q, want the carried mail (nearer due date) first", w.Lines[0])
}
}