Phase 7: notify and send via Apprise (Email + WhatsApp)

The morning briefs go out through the house Apprise API on `.35:8210`: one
brief per boy and one combined brief to Jo and Sam (FR-9, FR-14). The service
names an Apprise tag (`harry_email`, `joanna_whatsapp`, ...) and never holds an
address, a phone number or an SMTP password (AGENTS.md §10 rule 3).

- internal/apprise: keyed and stateless client, tag filtering, and error
  redaction so nothing sensitive reaches a log, a row or a screen.
- internal/brief: builds the per-boy and the combined brief, delivers on email
  and WhatsApp, records every attempt in `notifications` (migration 004 adds
  scope, channel, tag, ok and error) and fails loudly.
- Send panel (§5 screen 4): recipients, channels, an exact preview, Send now,
  plus the Send action on a stored draft (FR-10). The dashboard shows a
  Morning briefs banner and the Sends list.
- POST /notify/morning: the bearer-token endpoint the 6:00am n8n schedule
  calls. 207 when a channel or a scan failed; `dry_run=1` checks the wiring
  without messaging the family.
- scripts/morning-brief.sh: scan both children, then send. It is the n8n job;
  deploy/n8n-morning-brief.json is the ready-to-import workflow (06:00 daily).
- FR-12: a failed scan is never "nothing due" -- the brief says so, and the run
  exits non-zero. A failed delivery is recorded with no sent_at.
- The long no-due-date tail is capped in the sent brief so it stays brief
  (§10 rule 6); overdue work is never capped.

Verified live: 8 deliveries recorded (4 email, 4 WhatsApp) with HTTP 200 and
sent_at, and the endpoint answers 401/200/207 as documented. Evidence in
docs/phase-7-notify-notes.md.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
2026-10-15 19:28:01 +11:00
parent da57efad73
commit 09639c5125
31 changed files with 4266 additions and 169 deletions

View File

@@ -167,3 +167,96 @@ func equal(a, b []string) bool {
}
return true
}
// TestRenderCombinedBothBoys proves FR-14: the combined brief for Jo and Sam
// carries both boys, overdue first, and every line names the child.
func TestRenderCombinedBothBoys(t *testing.T) {
loc := sydney(t)
now := time.Date(2026, 10, 15, 9, 0, 0, 0, loc)
harry := Classify([]store.Item{
{Type: "task", Title: "Essay", Subject: "English", DueAt: due(t, loc, "2026-10-10 09:00"), Body: "Status: Overdue"},
{Type: "task", Title: "Worksheet", Subject: "Maths", DueAt: due(t, loc, "2026-10-15 15:00")},
{Type: "message", Title: "Sports day", DueAt: due(t, loc, "2026-10-14 07:00")},
}, now, loc)
finn := Classify([]store.Item{
{Type: "task", Title: "Lab report", Subject: "Science", DueAt: due(t, loc, "2026-10-09 09:00"), Body: "Status: Overdue"},
}, now, loc)
text := RenderCombined([]Combined{
{Child: "Harry", Buckets: harry},
{Child: "Finn", Buckets: finn},
}, loc)
if !strings.HasPrefix(text, "Harry and Finn — 2 overdue · 1 due today · 0 this week · 1 message(s).\n") {
t.Errorf("headline = %q", firstLineOf(text))
}
overdue := strings.Index(text, "OVERDUE")
today := strings.Index(text, "DUE TODAY")
if overdue < 0 || today < 0 || overdue > today {
t.Errorf("overdue must come first:\n%s", text)
}
for _, want := range []string{
"- Harry: English: Essay (due Sat 10 Oct 09:00) [overdue]",
"- Finn: Science: Lab report (due Fri 09 Oct 09:00) [overdue]",
"- Harry: Maths: Worksheet (due Thu 15 Oct 15:00)",
"MESSAGES",
"- Harry: Sports day (Wed 14 Oct 07:00)",
} {
if !strings.Contains(text, want) {
t.Errorf("combined brief is missing %q:\n%s", want, text)
}
}
}
// TestRenderCombinedSingleChild proves one child's brief still renders a
// sensible combined message.
func TestRenderCombinedSingleChild(t *testing.T) {
loc := sydney(t)
text := RenderCombined([]Combined{{Child: "Harry"}}, loc)
if !strings.HasPrefix(text, "Harry — 0 overdue") {
t.Errorf("text = %q", text)
}
if strings.Contains(text, "OVERDUE") {
t.Errorf("an empty brief must not show an OVERDUE heading: %q", text)
}
}
func firstLineOf(s string) string {
if i := strings.IndexByte(s, '\n'); i >= 0 {
return s[:i]
}
return s
}
// TestRenderWithLimitsKeepsTheBriefBrief proves the long tail is summarised, so
// the morning message stays a brief (AGENTS.md §10 rule 6) without hiding work.
func TestRenderWithLimitsKeepsTheBriefBrief(t *testing.T) {
loc := sydney(t)
now := time.Date(2026, 10, 15, 9, 0, 0, 0, loc)
items := make([]store.Item, 0, 8)
for _, title := range []string{"A", "B", "C", "D", "E", "F", "G", "H"} {
items = append(items, store.Item{Type: "task", Title: "Pending " + title, Body: "Status: Pending"})
}
items = append(items, store.Item{
Type: "task", Title: "Essay", Subject: "English",
DueAt: due(t, loc, "2026-10-10 09:00"), Body: "Status: Overdue",
})
b := Classify(items, now, loc)
limited := RenderWithLimits("Harry", b, loc, Limits{NoDue: 3})
if !strings.Contains(limited, "… and 5 more with no due date") {
t.Errorf("the long tail is not summarised:\n%s", limited)
}
if strings.Contains(limited, "Pending D") {
t.Errorf("the cap did not apply:\n%s", limited)
}
// Overdue work is never hidden by a cap.
if !strings.Contains(limited, "English: Essay") {
t.Errorf("overdue work went missing:\n%s", limited)
}
if full := Render("Harry", b, loc); !strings.Contains(full, "Pending H") {
t.Errorf("the full render must keep every item:\n%s", full)
}
}