Phase 6b/7b: per-child decoded periods, separate brief blocks, idempotent calendar (AUD-119)
- Parse each child's own "Today" day widget for the timetable (FR-16); drop the school-wide week calendar's lesson grid and clean the leaked routine rows. - Add the editable codes map (FR-17): subjects, rooms, teachers and period labels, seeded, an unknown code falls back to the raw code; `school codes` CLI. - Brief: Finn's block first, then Harry's, four sections always present (FR-9); periods decoded into plain English; the undated tail is one counted line (FR-18). - Calendar: write periods + work due + work overdue + major events, per child, on every pass. Identity is the stable per-item key, so a re-run never re-inserts (FR-8). morning-brief.sh now runs scan -> calendar push -> notify. - Day-widget labels are 12-hour: 1-6 read as PM, fixing afternoon periods. Proven live on .13: image 0.6.2-phase6b7b; both children's briefs decoded and separate; 35 events in calendar.school; runs 2 and 3 of the scan added nothing. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -15,12 +15,14 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/apprise"
|
||||
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/config"
|
||||
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/dashboard"
|
||||
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/decode"
|
||||
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/store"
|
||||
)
|
||||
|
||||
@@ -119,7 +121,11 @@ func (s *Service) ChildrenText(ctx context.Context, child store.Child) (string,
|
||||
return "", false, fmt.Errorf("items for %s: %w", child.Name, err)
|
||||
}
|
||||
buckets := dashboard.Classify(items, s.Now(), s.Loc)
|
||||
text := dashboard.RenderWithLimits(child.Name, buckets, s.Loc, dashboard.MorningLimits)
|
||||
dec, err := s.decoder(ctx)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
text := dashboard.RenderBrief(child.Name, buckets, dec, s.Loc)
|
||||
|
||||
sc, err := s.Store.ScanSummaryForChild(ctx, child.ID)
|
||||
if err != nil {
|
||||
@@ -132,18 +138,32 @@ func (s *Service) ChildrenText(ctx context.Context, child store.Child) (string,
|
||||
return notice + "\n" + text, false, nil
|
||||
}
|
||||
|
||||
// decoder loads the editable codes map and returns the lesson-title decoder the
|
||||
// brief uses (FR-17). An unknown code renders raw, never blank.
|
||||
func (s *Service) decoder(ctx context.Context) (func(string) string, error) {
|
||||
codes, err := s.Store.Codes(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode map: %w", err)
|
||||
}
|
||||
return func(title string) string { return decode.Period(title, codes) }, nil
|
||||
}
|
||||
|
||||
// CombinedText returns the single brief for Jo and Sam — both boys in one
|
||||
// message — and the children whose scan did not complete.
|
||||
func (s *Service) CombinedText(ctx context.Context, children []store.Child) (string, []string, error) {
|
||||
parts := make([]dashboard.Combined, 0, len(children))
|
||||
parts := make([]dashboard.CombinedBrief, 0, len(children))
|
||||
var failed []string
|
||||
var notices []string
|
||||
dec, err := s.decoder(ctx)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
for _, c := range children {
|
||||
items, err := s.Store.ItemsForChild(ctx, c.ID)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("items for %s: %w", c.Name, err)
|
||||
}
|
||||
parts = append(parts, dashboard.Combined{
|
||||
parts = append(parts, dashboard.CombinedBrief{
|
||||
Child: c.Name,
|
||||
Buckets: dashboard.Classify(items, s.Now(), s.Loc),
|
||||
})
|
||||
@@ -156,7 +176,7 @@ func (s *Service) CombinedText(ctx context.Context, children []store.Child) (str
|
||||
failed = append(failed, c.Name)
|
||||
}
|
||||
}
|
||||
text := dashboard.RenderCombinedWithLimits(parts, s.Loc, dashboard.MorningLimits)
|
||||
text := dashboard.RenderCombinedBrief(parts, dec, s.Loc)
|
||||
if len(notices) > 0 {
|
||||
text = strings.Join(notices, "\n") + "\n\n" + text
|
||||
}
|
||||
@@ -328,6 +348,8 @@ func (s *Service) plan(ctx context.Context) ([]planned, error) {
|
||||
if len(children) == 0 {
|
||||
return nil, fmt.Errorf("no children on file: nothing to brief")
|
||||
}
|
||||
// Finn's block first, then Harry's: the required order (FR-9).
|
||||
children = OrderChildren(children)
|
||||
|
||||
var out []planned
|
||||
for _, c := range children {
|
||||
@@ -379,6 +401,15 @@ func (s *Service) plan(ctx context.Context) ([]planned, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// OrderChildren returns the children in brief order: Finn's block first, then
|
||||
// Harry's (FR-9). The order is by name, which puts Finn before Harry; it is
|
||||
// shared by the send path and the dry-run build so both agree.
|
||||
func OrderChildren(children []store.Child) []store.Child {
|
||||
out := append([]store.Child(nil), children...)
|
||||
sort.SliceStable(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out
|
||||
}
|
||||
|
||||
func combinedNames(children []store.Child) string {
|
||||
names := make([]string, 0, len(children))
|
||||
for _, c := range children {
|
||||
|
||||
@@ -148,16 +148,20 @@ func TestMorningSendsPerBoyAndCombined(t *testing.T) {
|
||||
t.Fatalf("morning: %v", err)
|
||||
}
|
||||
if len(sends) != 3 {
|
||||
t.Fatalf("sends = %d, want Harry, Finn and combined", len(sends))
|
||||
t.Fatalf("sends = %d, want Finn, Harry and combined", len(sends))
|
||||
}
|
||||
for _, s := range sends {
|
||||
if !s.OK() {
|
||||
t.Errorf("send %s not OK: %+v", s.Scope, s)
|
||||
}
|
||||
}
|
||||
// Finn's block comes first, then Harry's (FR-9).
|
||||
if sends[0].Scope != "Finn" || sends[1].Scope != "Harry" {
|
||||
t.Fatalf("send order = %s, %s; want Finn then Harry", sends[0].Scope, sends[1].Scope)
|
||||
}
|
||||
|
||||
// Harry: 1 recipient x 2 channels. Combined: 2 recipients x 2 channels.
|
||||
if got := len(sends[0].Deliveries); got != 2 {
|
||||
if got := len(sends[1].Deliveries); got != 2 {
|
||||
t.Errorf("Harry deliveries = %d, want 2", got)
|
||||
}
|
||||
combined := sends[2]
|
||||
@@ -193,10 +197,16 @@ func TestMorningSendsPerBoyAndCombined(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if !strings.Contains(combined.Text, "OVERDUE") ||
|
||||
!strings.Contains(combined.Text, "Harry: English: Essay") ||
|
||||
!strings.Contains(combined.Text, "Finn: Science: Lab report") {
|
||||
!strings.Contains(combined.Text, "=== FINN ===") ||
|
||||
!strings.Contains(combined.Text, "=== HARRY ===") ||
|
||||
!strings.Contains(combined.Text, "Science: Lab report") ||
|
||||
!strings.Contains(combined.Text, "English: Essay") {
|
||||
t.Errorf("combined text = %q", combined.Text)
|
||||
}
|
||||
// Finn's block is first and the two children are never interleaved.
|
||||
if i, j := strings.Index(combined.Text, "=== FINN ==="), strings.Index(combined.Text, "=== HARRY ==="); i < 0 || j < 0 || i > j {
|
||||
t.Errorf("combined blocks are not Finn then Harry: %q", combined.Text)
|
||||
}
|
||||
|
||||
rows, err := st.RecentNotifications(context.Background(), 20)
|
||||
if err != nil {
|
||||
@@ -278,19 +288,19 @@ func TestFailedScanIsNeverNothingDue(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("morning: %v", err)
|
||||
}
|
||||
if len(sends[0].ScanFailed) != 1 || sends[0].ScanFailed[0] != "Harry" {
|
||||
t.Errorf("Harry scan failure not reported: %+v", sends[0].ScanFailed)
|
||||
if len(sends[1].ScanFailed) != 1 || sends[1].ScanFailed[0] != "Harry" {
|
||||
t.Errorf("Harry scan failure not reported: %+v", sends[1].ScanFailed)
|
||||
}
|
||||
if !strings.Contains(sends[0].Text, "SCAN FAILED") {
|
||||
t.Errorf("brief does not say the scan failed: %q", sends[0].Text)
|
||||
if !strings.Contains(sends[1].Text, "SCAN FAILED") {
|
||||
t.Errorf("brief does not say the scan failed: %q", sends[1].Text)
|
||||
}
|
||||
if !strings.Contains(sends[0].Text, "UNKNOWN") {
|
||||
t.Errorf("brief does not say due work is unknown: %q", sends[0].Text)
|
||||
if !strings.Contains(sends[1].Text, "UNKNOWN") {
|
||||
t.Errorf("brief does not say due work is unknown: %q", sends[1].Text)
|
||||
}
|
||||
if !strings.Contains(sends[2].Text, "SCAN FAILED") {
|
||||
t.Errorf("combined brief does not carry the failure: %q", sends[2].Text)
|
||||
}
|
||||
if sends[0].OK() {
|
||||
if sends[1].OK() {
|
||||
t.Error("a scan failure must make the run not OK")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user