| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // backdate moves everything the conversation has recorded so far back |
| 15 | // by d, as though it had happened then: messages, turns, cards and tasks. |
| 16 | func (r *toolRig) backdate(d time.Duration) { |
| 17 | r.t.Helper() |
| 18 | db := r.inst.DB.Writer() |
| 19 | for _, tc := range []struct{ table, col string }{ |
| 20 | {"chat_messages", "created_at"}, {"chat_turns", "created_at"}, {"chat_turns", "updated_at"}, |
| 21 | {"chat_cards", "created_at"}, {"chat_cards", "updated_at"}, {"chat_cards", "visible_at"}, |
| 22 | {"chat_tasks", "created_at"}, {"chat_tasks", "updated_at"}, {"chat_tasks", "done_at"}, {"chat_tasks", "merged_at"}, |
| 23 | } { |
| 24 | rows, err := db.Query("SELECT rowid, " + tc.col + " FROM " + tc.table + " WHERE " + tc.col + " IS NOT NULL") |
| 25 | if err != nil { |
| 26 | r.t.Fatal(err) |
| 27 | } |
| 28 | at := map[int64]time.Time{} |
| 29 | for rows.Next() { |
| 30 | var id int64 |
| 31 | var t time.Time |
| 32 | if err := rows.Scan(&id, &t); err != nil { |
| 33 | r.t.Fatal(err) |
| 34 | } |
| 35 | at[id] = t |
| 36 | } |
| 37 | rows.Close() |
| 38 | for id, t := range at { |
| 39 | r.setAt(tc.table, tc.col, "rowid = ?", id, t.Add(-d)) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func (r *toolRig) setAt(table, col, where string, arg any, at time.Time) { |
| 45 | r.t.Helper() |
| 46 | if _, err := r.inst.DB.Writer().Exec("UPDATE "+table+" SET "+col+" = ? WHERE "+where, at.UTC(), arg); err != nil { |
| 47 | r.t.Fatal(err) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // messageID is the id of the person's message with text. |
| 52 | func (r *toolRig) messageID(text string) string { |
| 53 | r.t.Helper() |
| 54 | var id string |
| 55 | if err := r.inst.DB.Writer().QueryRow("SELECT id FROM chat_messages WHERE text = ? AND role = ?", text, wire.RolePerson).Scan(&id); err != nil { |
| 56 | r.t.Fatal(text, err) |
| 57 | } |
| 58 | return id |
| 59 | } |
| 60 | |
| 61 | // cutOut is the part of page from the first from to the next to. |
| 62 | func cutOut(t *testing.T, page, from, to string) string { |
| 63 | t.Helper() |
| 64 | _, rest, ok := strings.Cut(page, from) |
| 65 | if !ok { |
| 66 | t.Fatalf("no %s in page", from) |
| 67 | } |
| 68 | part, _, _ := strings.Cut(rest, to) |
| 69 | return part |
| 70 | } |
| 71 | |
| 72 | // rollupRig is a conversation with yesterday behind it: two tasks |
| 73 | // recorded and started; one merged without checks on the person's word |
| 74 | // (a decision); one waiting at Ready to merge with its merge card still |
| 75 | // asking (open). Yesterday's last message was written a moment before |
| 76 | // midnight, today's first at midnight exactly. |
| 77 | type rollupRig struct { |
| 78 | *toolRig |
| 79 | today, yesterday time.Time |
| 80 | loc *time.Location |
| 81 | asked, merged string |
| 82 | askCard wire.Card |
| 83 | lastWord, firstDay string |
| 84 | } |
| 85 | |
| 86 | func newRollupRig(t *testing.T) *rollupRig { |
| 87 | r := &rollupRig{toolRig: newToolRigAt(t, wire.ProtocolInjectionDefences)} |
| 88 | ctx := r.ctx |
| 89 | // In Madrid, so the day's edges are not UTC's on any date (review S5). |
| 90 | r.loc = zoneClock(t, r.client, ctx, "Europe/Madrid").loc |
| 91 | base := strings.Repeat("1", 40) |
| 92 | r.asked = r.judgedTask("w", "Swipe back on iOS", base, strings.Repeat("2", 40), wire.ChecksCannotRun, "needs_macos") |
| 93 | r.askCard = r.mergeCardsWait(r.asked) |
| 94 | r.merged = r.judgedTask("x", "Edge swipe on iPad", base, strings.Repeat("4", 40), wire.ChecksCannotRun, "needs_macos") |
| 95 | card := r.mergeCardsWait(r.merged) |
| 96 | if _, err := r.client.Answer(ctx, card.ID, wire.ChoiceMerge, card.Revision); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | m := r.dispatched(wire.SpecialistMerge, base, strings.Repeat("4", 40), 0) |
| 100 | r.mergedStep("x-m", m, strings.Repeat("f", 40)) |
| 101 | r.wait("done", func(c *Conversation) bool { return c.Tasks[r.merged].Stage == wire.StageDone }) |
| 102 | r.expect(wire.FrameForgetTask) |
| 103 | r.next("Leave the iOS one until a Mac is free.") |
| 104 | // All of that was yesterday, around midday. |
| 105 | r.today = chat.DayStart(r.inst.Store.Chat().Now(), r.inst.Store.Chat().Zone(Owner)) |
| 106 | r.yesterday = chat.DayStart(r.today.Add(-time.Hour), r.loc) |
| 107 | r.backdate(r.inst.Store.Chat().Now().Sub(r.yesterday.Add(12 * time.Hour))) |
| 108 | r.lastWord = r.messageID("Leave the iOS one until a Mac is free.") |
| 109 | r.setAt("chat_messages", "created_at", "id = ?", r.lastWord, r.today.Add(-time.Nanosecond)) |
| 110 | r.next("Good morning. What is left?") |
| 111 | r.firstDay = r.messageID("Good morning. What is left?") |
| 112 | r.setAt("chat_messages", "created_at", "id = ?", r.firstDay, r.today) |
| 113 | return r |
| 114 | } |
| 115 | |
| 116 | func (r *rollupRig) key(d time.Time) string { return d.In(r.loc).Format("2006-01-02") } |
| 117 | |
| 118 | // TestRollupDayBox: once a day is over in the person's zone it is one |
| 119 | // day box at its place in the scrollback: its date, what was started, |
| 120 | // merged and decided, and what is still open, each task a link; none of |
| 121 | // its messages. The day ends at midnight: a message a moment before is |
| 122 | // yesterday's, one at midnight today's. Today is never rolled up. What |
| 123 | // still asks the person (the open merge card, the task underway) stands |
| 124 | // at the top of today with its buttons, never inside the box. |
| 125 | func TestRollupDayBox(t *testing.T) { |
| 126 | r := newRollupRig(t) |
| 127 | page := r.get(r.inst.Cookie(), "/conversation", 200) |
| 128 | y, today := r.key(r.yesterday), r.key(r.today) |
| 129 | if !strings.Contains(page, `<li equipo-rollup id="day-`+y+`" data-day="`+y+`">`) || !strings.Contains(page, `<time datetime="`+y+`">`+r.yesterday.In(r.loc).Format("Monday 2 January")+`</time>`) { |
| 130 | t.Fatalf("no box for yesterday: %s", page) |
| 131 | } |
| 132 | if strings.Contains(page, `id="day-`+today+`"`) || strings.Count(page, "<li equipo-rollup ") != 1 { |
| 133 | t.Fatalf("today rolled up, or a box too many: %s", page) |
| 134 | } |
| 135 | box := cutOut(t, page, `id="day-`+y+`"`, "</li>") |
| 136 | row := func(key string) string { return cutOut(t, box, `data-row="`+key+`"`, "</div>") } |
| 137 | for _, want := range []struct{ row, text string }{ |
| 138 | {"started", `<a href="/tasks/` + r.asked + `">Swipe back on iOS</a>`}, |
| 139 | {"started", `<a href="/tasks/` + r.merged + `">Edge swipe on iPad</a>`}, |
| 140 | {"merged", `<a href="/tasks/` + r.merged + `">Edge swipe on iPad</a>`}, |
| 141 | {"decided", `<a href="/tasks/` + r.merged + `">Edge swipe on iPad</a>: <span equipo-day-decision>Merging without checks.</span>`}, |
| 142 | {"open", `<a href="/tasks/` + r.asked + `">Swipe back on iOS</a>`}, |
| 143 | } { |
| 144 | if !strings.Contains(row(want.row), want.text) { |
| 145 | t.Fatalf("%s row lacks %q: %s", want.row, want.text, box) |
| 146 | } |
| 147 | } |
| 148 | if strings.Contains(row("merged"), r.asked) || strings.Contains(row("open"), r.merged) { |
| 149 | t.Fatalf("a task in the wrong row: %s", box) |
| 150 | } |
| 151 | if !strings.Contains(box, `href="/conversation?day=`+y+`#day-`+y+`"`) || !strings.Contains(box, "Show the conversation") || strings.Contains(box, "data-message") { |
| 152 | t.Fatalf("box not collapsed with its way in: %s", box) |
| 153 | } |
| 154 | // The day boundary: a moment before midnight is rolled up, midnight is |
| 155 | // today's. |
| 156 | if strings.Contains(page, `data-message="`+r.lastWord+`"`) || strings.Contains(page, "Leave the iOS one until a Mac is free.") { |
| 157 | t.Fatalf("yesterday's last message outside its box: %s", page) |
| 158 | } |
| 159 | if !strings.Contains(page, `data-message="`+r.firstDay+`"`) { |
| 160 | t.Fatalf("today's first message rolled up: %s", page) |
| 161 | } |
| 162 | // What still needs the person stands at the top of today, once, with |
| 163 | // its buttons. |
| 164 | todayPart := page[strings.Index(page, "data-today"):] |
| 165 | if !strings.Contains(todayPart, "data-still-open") || strings.Count(page, `data-card="`+r.askCard.ID+`"`) != 1 || !strings.Contains(cutOut(t, todayPart, `data-card="`+r.askCard.ID+`"`, "</li>"), `data-merge-choice="merge"`) { |
| 166 | t.Fatalf("the open merge card is not at the top of today: %s", page) |
| 167 | } |
| 168 | if !strings.Contains(todayPart, `<a href="/tasks/`+r.asked+`">Swipe back on iOS</a>`) { |
| 169 | t.Fatalf("the task underway is not at the top of today: %s", todayPart) |
| 170 | } |
| 171 | if strings.Index(todayPart, `data-card="`+r.askCard.ID+`"`) > strings.Index(todayPart, `data-message="`+r.firstDay+`"`) { |
| 172 | t.Fatal("open items after today's messages") |
| 173 | } |
| 174 | // The transcript itself is unchanged: the API's snapshot still has |
| 175 | // yesterday's messages. |
| 176 | snap, err := r.client.Snapshot(r.ctx) |
| 177 | if err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | found := false |
| 181 | for _, m := range snap.Messages { |
| 182 | found = found || m.ID == r.lastWord |
| 183 | } |
| 184 | if !found { |
| 185 | t.Fatal("yesterday's messages left the transcript") |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // TestRollupExpand: Show the conversation (?day=) opens the day's |
| 190 | // transcript in place, under its box: its messages and its receipts, |
| 191 | // with Hide the conversation. A card still asking is shown once, at the |
| 192 | // top of today. A task's "From your conversation" link to a message on |
| 193 | // an earlier day opens that day at the message; Ask about this still |
| 194 | // fills the composer; days that do not exist or are today open nothing. |
| 195 | func TestRollupExpand(t *testing.T) { |
| 196 | r := newRollupRig(t) |
| 197 | y := r.key(r.yesterday) |
| 198 | page := r.get(r.inst.Cookie(), "/conversation?day="+y, 200) |
| 199 | if !strings.Contains(page, `<li equipo-rollup id="day-`+y+`" data-day="`+y+`" data-expanded>`) { |
| 200 | t.Fatalf("day not open: %s", page) |
| 201 | } |
| 202 | feed := cutOut(t, page, `id="day-feed-`+y+`"`, "data-today") |
| 203 | if !strings.Contains(feed, `data-message="`+r.lastWord+`"`) || !strings.Contains(feed, "Leave the iOS one until a Mac is free.") || !strings.Contains(feed, `<a href="/tasks/`+r.merged+`">Edge swipe on iPad</a>`) { |
| 204 | t.Fatalf("the day's transcript is not under its box: %s", feed) |
| 205 | } |
| 206 | if strings.Contains(feed, `data-message="`+r.firstDay+`"`) { |
| 207 | t.Fatal("today's message in yesterday's transcript") |
| 208 | } |
| 209 | if !strings.Contains(page, `href="/conversation#day-`+y+`" data-day-toggle="`+y+`" aria-expanded="true"`) || !strings.Contains(page, "Hide the conversation") { |
| 210 | t.Fatalf("no way to close the day: %s", page) |
| 211 | } |
| 212 | if n := strings.Count(page, `data-card="`+r.askCard.ID+`"`); n != 1 { |
| 213 | t.Fatalf("the open card shows %d times", n) |
| 214 | } |
| 215 | // Nonsense and today open nothing. |
| 216 | for _, q := range []string{"day=" + r.key(r.today), "day=yesterday", "day=" + r.key(r.today.AddDate(0, 0, 3))} { |
| 217 | if p := r.get(r.inst.Cookie(), "/conversation?"+q, 200); strings.Contains(p, "data-expanded") || strings.Count(p, "<li equipo-rollup ") != 1 { |
| 218 | t.Fatalf("?%s opened something: %s", q, p) |
| 219 | } |
| 220 | } |
| 221 | // The task page's link to the message it was noted from. |
| 222 | task := r.get(r.inst.Cookie(), "/tasks/"+r.asked, 200) |
| 223 | m := regexp.MustCompile(`href="(/conversation\?message=[^"#]+)#m-([^"]+)"`).FindStringSubmatch(task) |
| 224 | if m == nil { |
| 225 | t.Fatalf("no link to the conversation on the task page: %s", task) |
| 226 | } |
| 227 | linked := r.get(r.inst.Cookie(), html.UnescapeString(m[1]), 200) |
| 228 | if !strings.Contains(linked, `data-day="`+y+`" data-expanded`) || !strings.Contains(cutOut(t, linked, `id="day-feed-`+y+`"`, "data-today"), `data-message="`+m[2]+`"`) { |
| 229 | t.Fatalf("the link did not open its day at the message %s: %s", m[2], linked) |
| 230 | } |
| 231 | ask := r.get(r.inst.Cookie(), "/conversation?about="+r.merged, 200) |
| 232 | if !strings.Contains(ask, "Edge swipe on iPad") || !strings.Contains(ask, "autofocus>") { |
| 233 | t.Fatalf("Ask about this lost its draft: %s", ask) |
| 234 | } |
| 235 | // A teammate's day is their own: no box, no task of the owner's. |
| 236 | mate := r.memberCookie() |
| 237 | if p := r.get(mate, "/conversation?day="+y, 200); strings.Contains(p, "<li equipo-rollup ") || strings.Contains(p, "Leave the iOS one") { |
| 238 | t.Fatalf("a teammate sees the owner's day: %s", p) |
| 239 | } |
| 240 | } |
| 241 | |