| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // A row's description ends on a whole word: its first sentence when that |
| 13 | // fits, else cut at a space with an ellipsis; a summary the runner's |
| 14 | // catalog clipped mid-word ("proo") drops the broken word. |
| 15 | func TestShortDescriptionWholeWords(t *testing.T) { |
| 16 | if got := shortDescription("An iOS app for reading. It has panes and a timeline."); got != "An iOS app for reading." { |
| 17 | t.Fatalf("first sentence: %q", got) |
| 18 | } |
| 19 | long := strings.Repeat("timeline ", 30) |
| 20 | if got := shortDescription(long); !strings.HasSuffix(got, "timeline…") || len(got) > descLimit+len("…") { |
| 21 | t.Fatalf("long sentence: %q", got) |
| 22 | } |
| 23 | clipped := strings.Repeat("word ", 59) + "proo" // 299 bytes |
| 24 | clipped += "f" // 300: the catalog's limit |
| 25 | if len(clipped) != wire.MaxRepoString { |
| 26 | t.Fatal(len(clipped)) |
| 27 | } |
| 28 | whole := wholeWords(clipped) |
| 29 | if strings.Contains(whole, "proo") || !strings.HasSuffix(whole, "word…") { |
| 30 | t.Fatalf("clipped summary kept the broken word: %q", whole) |
| 31 | } |
| 32 | if got := shortDescription(clipped); strings.Contains(got, "proo") || !strings.HasSuffix(got, "…") { |
| 33 | t.Fatalf("row description: %q", got) |
| 34 | } |
| 35 | if got := wholeWords("Short and whole."); got != "Short and whole." { |
| 36 | t.Fatalf("whole text changed: %q", got) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func spread(keys ...string) []spreadPart { |
| 41 | var out []spreadPart |
| 42 | for _, k := range keys { |
| 43 | out = append(out, spreadPart{Key: k, N: 1}) |
| 44 | } |
| 45 | return out |
| 46 | } |
| 47 | |
| 48 | // The order: starred first, then active by what waits on the viewer and |
| 49 | // then the latest activity, and the quiet (nothing open, nothing new for |
| 50 | // 14 days) last; the key names only the states present. |
| 51 | func TestOrderProjects(t *testing.T) { |
| 52 | now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC) |
| 53 | line := func(id string, open, needs int, ago time.Duration, star bool, keys ...string) projectLine { |
| 54 | p := projectLine{Project: wire.Project{ID: id, Title: id}, Open: open, NeedsYou: needs, Spread: spread(keys...)} |
| 55 | if ago >= 0 { |
| 56 | p.LastAt = now.Add(-ago) |
| 57 | } |
| 58 | p.Star.On = star |
| 59 | return p |
| 60 | } |
| 61 | g := orderProjects([]projectLine{ |
| 62 | line("quietOld", 0, 0, 20*24*time.Hour, false, "done"), |
| 63 | line("busy", 2, 0, time.Hour, false, "progress"), |
| 64 | line("needsMe", 1, 1, 48*time.Hour, false, "needs"), |
| 65 | line("recentDone", 0, 0, 3*24*time.Hour, false, "done"), |
| 66 | line("never", 0, 0, -1, false), |
| 67 | line("pinnedQuiet", 0, 0, 30*24*time.Hour, true), |
| 68 | line("oldOpen", 1, 0, 40*24*time.Hour, false, "progress"), |
| 69 | }, now) |
| 70 | ids := func(ps []projectLine) string { |
| 71 | var out []string |
| 72 | for _, p := range ps { |
| 73 | out = append(out, p.ID) |
| 74 | } |
| 75 | return strings.Join(out, ",") |
| 76 | } |
| 77 | if got := ids(g.Starred); got != "pinnedQuiet" { |
| 78 | t.Fatalf("starred: %s", got) |
| 79 | } |
| 80 | // Open work is active however old; nothing open but recent stays too. |
| 81 | if got := ids(g.Active); got != "needsMe,busy,recentDone,oldOpen" { |
| 82 | t.Fatalf("active: %s", got) |
| 83 | } |
| 84 | if got := ids(g.Quiet); got != "quietOld,never" { |
| 85 | t.Fatalf("quiet: %s", got) |
| 86 | } |
| 87 | if got := strings.Join(g.Key, ","); got != "needs,progress,done" { |
| 88 | t.Fatalf("key: %s", got) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestFilterProjects(t *testing.T) { |
| 93 | ps := []projectLine{{Project: wire.Project{ID: "a", Title: "Woodstar", Description: "An iOS app"}}, {Project: wire.Project{ID: "b", Title: "Suite", Name: "oficina/suite"}}} |
| 94 | if got := filterProjects(ps, " ios "); len(got) != 1 || got[0].ID != "a" { |
| 95 | t.Fatalf("by description: %v", got) |
| 96 | } |
| 97 | if got := filterProjects(ps, "OFICINA/"); len(got) != 1 || got[0].ID != "b" { |
| 98 | t.Fatalf("by name: %v", got) |
| 99 | } |
| 100 | if got := filterProjects(ps, ""); len(got) != 2 { |
| 101 | t.Fatal("empty filter") |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // The auto-star rules, by count alone. |
| 106 | func TestStarSignals(t *testing.T) { |
| 107 | now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC) |
| 108 | task := func(project, group, creator, source string, age time.Duration) taskLine { |
| 109 | return taskLine{Task: wire.Task{ProjectID: project, Creator: creator, SourceMessageID: source, CreatedAt: now.Add(-age)}, Group: group} |
| 110 | } |
| 111 | me := "paul@example.com" |
| 112 | // One waiting on me is not enough. |
| 113 | if s := starSignals([]taskLine{task("w", "needs", me, "", time.Hour)}, me, now); len(s) != 0 { |
| 114 | t.Fatalf("one waiting starred: %v", s) |
| 115 | } |
| 116 | // The most waiting, at least two; ties all starred; fewer not. |
| 117 | tasks := []taskLine{ |
| 118 | task("w", "needs", me, "", time.Hour), task("w", "needs", me, "", time.Hour), |
| 119 | task("s", "needs", me, "", time.Hour), task("s", "needs", me, "", time.Hour), |
| 120 | task("e", "needs", me, "", time.Hour), task("e", "progress", me, "", time.Hour), |
| 121 | } |
| 122 | s := starSignals(tasks, me, now) |
| 123 | if len(s) != 2 || s["w"] != (chat.StarSignal{Reason: starWaiting, N: 2}) || s["s"].Reason != starWaiting { |
| 124 | t.Fatalf("waiting: %v", s) |
| 125 | } |
| 126 | // Busy: three noted from my conversation this week; older, others' |
| 127 | // or not from the conversation do not count. |
| 128 | busy := []taskLine{ |
| 129 | task("b", "progress", me, "m1", time.Hour), task("b", "progress", me, "m2", 2*24*time.Hour), task("b", "done", me, "m3", 6*24*time.Hour), |
| 130 | task("c", "progress", me, "m4", time.Hour), task("c", "progress", me, "m5", time.Hour), task("c", "progress", me, "m6", 8*24*time.Hour), |
| 131 | task("d", "progress", "nora@example.com", "m7", time.Hour), task("d", "progress", "nora@example.com", "m8", time.Hour), task("d", "progress", me, "", time.Hour), |
| 132 | } |
| 133 | s = starSignals(busy, me, now) |
| 134 | if len(s) != 1 || s["b"] != (chat.StarSignal{Reason: starBusy, N: 3}) { |
| 135 | t.Fatalf("busy: %v", s) |
| 136 | } |
| 137 | // A project both rules pick is starred for what waits on me. |
| 138 | both := append(busy, task("b", "needs", me, "", time.Hour), task("b", "needs", me, "", time.Hour)) |
| 139 | if s = starSignals(both, me, now); s["b"].Reason != starWaiting || s["b"].N != 2 { |
| 140 | t.Fatalf("both: %v", s) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // The star's hover says who set it; a receipt shows for a day after the |
| 145 | // person is first shown it. |
| 146 | func TestStarLine(t *testing.T) { |
| 147 | now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC) |
| 148 | p := projectLine{Project: wire.Project{ID: "p_w", Title: "Woodstar"}} |
| 149 | if l := starLineOf(p, chat.Star{}, false, now); l.On || l.Label != "Star Woodstar" { |
| 150 | t.Fatalf("unstarred: %+v", l) |
| 151 | } |
| 152 | told := now.Add(-time.Hour) |
| 153 | auto := chat.Star{Starred: true, By: chat.StarByAuto, Reason: starWaiting, N: 2, ToldAt: &told} |
| 154 | l := starLineOf(p, auto, true, now) |
| 155 | if !l.On || !l.Auto || !l.Fresh || l.Who != "Starred by Equipo: 2 tasks are waiting on you" || l.Label != "Unstar Woodstar" { |
| 156 | t.Fatalf("auto: %+v", l) |
| 157 | } |
| 158 | // Each reason in words, one in the singular (Equipo stars at two |
| 159 | // waiting or three started, but the words never say "1 tasks"). |
| 160 | for _, c := range []struct { |
| 161 | s chat.Star |
| 162 | want string |
| 163 | }{ |
| 164 | {chat.Star{Reason: starWaiting, N: 1}, "1 task is waiting on you"}, |
| 165 | {chat.Star{Reason: starBusy, N: 3}, "You started 3 tasks here this week"}, |
| 166 | {chat.Star{Reason: starBusy, N: 1}, "You started 1 task here this week"}, |
| 167 | } { |
| 168 | if got := starWhy(c.s); got != c.want { |
| 169 | t.Fatalf("starWhy %+v: %q, want %q", c.s, got, c.want) |
| 170 | } |
| 171 | } |
| 172 | old := now.Add(-25 * time.Hour) |
| 173 | auto.ToldAt = &old |
| 174 | if l := starLineOf(p, auto, true, now); l.Fresh { |
| 175 | t.Fatal("receipt after a day") |
| 176 | } |
| 177 | if l := starLineOf(p, chat.Star{Starred: true, By: chat.StarByYou}, true, now); l.Who != "Starred by you" || l.Fresh { |
| 178 | t.Fatalf("yours: %+v", l) |
| 179 | } |
| 180 | if l := starLineOf(p, chat.Star{Starred: true, By: chat.StarByCoordinator}, true, now); l.Who != "Starred by your coordinator at your request" { |
| 181 | t.Fatalf("coordinator: %+v", l) |
| 182 | } |
| 183 | if l := starLineOf(p, chat.Star{Starred: false, By: chat.StarByYou}, true, now); l.On { |
| 184 | t.Fatal("an unstarred choice shows starred") |
| 185 | } |
| 186 | p.StarWhy, p.Star = starWhy(auto), starLineOf(p, chat.Star{Starred: true, By: chat.StarByAuto, Reason: starWaiting, N: 2, ToldAt: &told}, true, now) |
| 187 | g := orderProjects([]projectLine{p}, now) |
| 188 | if len(g.Receipts) != 1 || g.Receipts[0].Text != "Starred Woodstar: 2 tasks are waiting on you" || g.Receipts[0].ProjectID != "p_w" { |
| 189 | t.Fatalf("receipt: %+v", g.Receipts) |
| 190 | } |
| 191 | } |
| 192 | |