| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "slices" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // The Assistant hears of the cards waiting for the person in Equipo |
| 16 | // (Paul, 2026-11-27): an Assistant person turn's frame carries, after the |
| 17 | // person's words, their own open cards that wait on them in the |
| 18 | // Coordinator's conversation and job threads, newest first, at most |
| 19 | // maxWaiting, each with its kind, question and link, as reference. |
| 20 | |
| 21 | const waitingOrigin = "https://equipo.example" |
| 22 | |
| 23 | // waitingRig is a files rig whose instance has an origin. |
| 24 | func waitingRig(t *testing.T) *hkRig { |
| 25 | t.Helper() |
| 26 | r := filesRig(t, wire.ProtocolAssistantFiles) |
| 27 | r.s.authority.Origin = waitingOrigin + "/" |
| 28 | return r |
| 29 | } |
| 30 | |
| 31 | // card records a card for person in lane, a second after the last. |
| 32 | func (r *hkRig) waitingCard(c cardRecord) cardRecord { |
| 33 | r.t.Helper() |
| 34 | if c.ID == "" { |
| 35 | c.ID = newID("c_") |
| 36 | } |
| 37 | if c.Person == "" { |
| 38 | c.Person = hkOwner |
| 39 | } |
| 40 | if c.State == "" { |
| 41 | c.State = wire.CardOpen |
| 42 | } |
| 43 | c.Revision, c.CreatedAt, c.UpdatedAt = 1, r.now, r.now |
| 44 | r.must(r.s.db.G.Create(&c).Error) |
| 45 | r.now = r.now.Add(time.Second) |
| 46 | return c |
| 47 | } |
| 48 | |
| 49 | // assistantSend sends text in the Assistant lane and returns its frame. |
| 50 | func (r *hkRig) assistantSend(text string) wire.MessageCommand { |
| 51 | r.t.Helper() |
| 52 | sent, _, err := r.s.Lane(wire.LaneAssistant).Send(r.ctx, hkOwner, wire.SendMessage{Text: text, IdempotencyKey: newID("k_")}) |
| 53 | r.must(err) |
| 54 | m := r.lastFrame() |
| 55 | if m.TurnID != sent.Turn.ID || !slices.Equal(m.PersonText, []string{text}) || sent.Message.Text != text { |
| 56 | r.t.Fatalf("frame %+v for %+v", m, sent) |
| 57 | } |
| 58 | // Replied, so the lane's next turn goes at once. |
| 59 | r.must(r.s.db.G.Model(&turnRecord{}).Where("id = ?", sent.Turn.ID).Update("state", wire.TurnReplied).Error) |
| 60 | return m |
| 61 | } |
| 62 | |
| 63 | func cardLink(id string) string { |
| 64 | return waitingOrigin + "/conversation?card=" + id + "#card-" + id |
| 65 | } |
| 66 | |
| 67 | // waitingLine is c's line in the note: what it is about, its kind, what |
| 68 | // it asks and its link. |
| 69 | func waitingLine(c cardRecord, asks, link string) string { |
| 70 | return fmt.Sprintf(noteWaitingCard, waitingLabels[c.Kind], c.Kind, asks+" "+link) |
| 71 | } |
| 72 | |
| 73 | // accessJSON is an access card's request as requestAccess and |
| 74 | // accessReport write it. |
| 75 | func accessJSON(target, state string) string { |
| 76 | raw, _ := json.Marshal(wire.AccessRequest{Target: target, Role: "agent", Reason: "To read the site's code.", State: state}) |
| 77 | return string(raw) |
| 78 | } |
| 79 | |
| 80 | // TestWaitingCardsNote: the person's own open cards that wait on them, |
| 81 | // in the Coordinator's conversation and job threads, ride after their |
| 82 | // words, newest first with what each is about, its kind, what it asks |
| 83 | // and its link. A home or access card says what it asks from what it |
| 84 | // holds, as the code makes them. Never a card that does not wait on this |
| 85 | // person (answered, a report, an access request filed or waiting on the |
| 86 | // agent's operator, final, or the explanatory card), one held for quiet |
| 87 | // hours, one Base already shows (mirrored) or whose question the |
| 88 | // Assistant answers itself (an open hand-off's), one in the Assistant's |
| 89 | // own lane, or another person's. |
| 90 | func TestWaitingCardsNote(t *testing.T) { |
| 91 | r := waitingRig(t) |
| 92 | if plain := r.assistantSend("Hello."); plain.Text != "Hello." { |
| 93 | t.Fatalf("with nothing waiting %q", plain.Text) |
| 94 | } |
| 95 | |
| 96 | place := r.waitingCard(cardRecord{Kind: wire.CardPlace, TaskID: "tkzmnycab3ml", Question: "Publish Nearys on Carloku in account paulca?"}) |
| 97 | job := r.waitingCard(cardRecord{Kind: wire.CardQuestion, Lane: wire.JobLane("h_closed1"), Question: "Which [page] first?"}) |
| 98 | // As setProjectHome makes them: Question the current home, Choice |
| 99 | // the one it would use ("" to clear). |
| 100 | home := r.waitingCard(cardRecord{Kind: wire.CardHome, State: wire.CardHomeAsk, Question: "tito", Choice: "oficina", Options: "[]"}) |
| 101 | clear := r.waitingCard(cardRecord{Kind: wire.CardHome, State: wire.CardHomeAsk, Question: "oficina", Options: "[]"}) |
| 102 | // As requestAccess makes it, waiting for the person to confirm. |
| 103 | access := r.waitingCard(cardRecord{Kind: wire.CardAccess, Access: accessJSON("tito/website", accessConfirmation), Name: "tito/website", Question: "Ask Amadan for access to tito/website?", Options: "[]"}) |
| 104 | |
| 105 | // None of these is listed. |
| 106 | r.waitingCard(cardRecord{Kind: wire.CardQuestion, State: wire.CardAnswered, Question: "Answered already?"}) |
| 107 | r.waitingCard(cardRecord{Kind: wire.CardProject, State: wire.CardAdded, Question: "A project card"}) |
| 108 | r.waitingCard(cardRecord{Kind: wire.CardTask, TaskID: "tk1", Question: "A task report"}) |
| 109 | r.waitingCard(cardRecord{Kind: wire.CardAccess, Access: accessJSON("final/one", "approved"), Name: "final/one", Question: "Ask Amadan for access to final/one?"}) |
| 110 | r.waitingCard(cardRecord{Kind: wire.CardAccess, Access: accessJSON("filed/one", "filing"), Name: "filed/one", Question: "Ask Amadan for access to filed/one?"}) |
| 111 | r.waitingCard(cardRecord{Kind: wire.CardAccess, Access: accessJSON("operator/one", "pending"), Name: "operator/one", Question: "Ask Amadan for access to operator/one?"}) |
| 112 | r.waitingCard(cardRecord{Kind: wire.CardAccess, Name: "explained/one", Question: "explained/one", Options: `["oficina"]`, Choice: "agent"}) |
| 113 | r.waitingCard(cardRecord{Kind: wire.CardHome, State: wire.CardHomeAsk, Options: "[]"}) |
| 114 | r.waitingCard(cardRecord{Kind: wire.CardQuestion, Held: true, Question: "Held for quiet hours?"}) |
| 115 | r.waitingCard(cardRecord{Kind: wire.CardQuestion, Lane: wire.LaneAssistant, Question: "In Base already?"}) |
| 116 | r.waitingCard(cardRecord{Person: hkMember, Kind: wire.CardQuestion, Question: "Someone else's?"}) |
| 117 | merge := r.waitingCard(cardRecord{Kind: wire.CardMerge, TaskID: "tk2", Question: "Mirrored merge?"}) |
| 118 | r.waitingCard(cardRecord{Kind: wire.CardOutcome, Lane: wire.LaneAssistant, SourceCard: merge.ID, Question: "The outcome card"}) |
| 119 | r.must(r.s.db.G.Create(&handoffRecord{ID: "h_open1", Person: hkOwner, LedgerKey: "lk1", State: handoffOpen, Lane: wire.JobLane("h_open1"), Title: "Login", CreatedAt: r.now, UpdatedAt: r.now}).Error) |
| 120 | r.waitingCard(cardRecord{Kind: wire.CardQuestion, Lane: wire.JobLane("h_open1"), Question: "An open job's question?"}) |
| 121 | |
| 122 | m := r.assistantSend("Can you deploy apps yet?") |
| 123 | words, note, ok := strings.Cut(m.Text, "\n\n") |
| 124 | if !ok || words != "Can you deploy apps yet?" { |
| 125 | t.Fatalf("frame text %q", m.Text) |
| 126 | } |
| 127 | // The open job's question rides in its own block, before this one. |
| 128 | questions, note, ok := strings.Cut(note, "\n\n") |
| 129 | if !ok || !strings.HasPrefix(questions, "[Open questions") || !strings.Contains(questions, "An open job's question?") { |
| 130 | t.Fatalf("open questions %q", questions) |
| 131 | } |
| 132 | want := fmt.Sprintf(noteWaiting, strings.Join([]string{ |
| 133 | waitingLine(access, fmt.Sprintf(noteWaitingAccess, "agent", "tito/website"), cardLink(access.ID)), |
| 134 | waitingLine(clear, fmt.Sprintf(noteWaitingHomeClear, "oficina"), cardLink(clear.ID)), |
| 135 | waitingLine(home, fmt.Sprintf(noteWaitingHomeSet, "oficina"), cardLink(home.ID)), |
| 136 | waitingLine(job, `"Which (page) first?"`, waitingOrigin+"/jobs/h_closed1?card="+job.ID+"#card-"+job.ID), |
| 137 | waitingLine(place, `"Publish Nearys on Carloku in account paulca?"`, cardLink(place.ID)), |
| 138 | }, "\n")) |
| 139 | if note != want { |
| 140 | t.Fatalf("note\n%s\nwant\n%s", note, want) |
| 141 | } |
| 142 | for _, not := range []string{"Answered already", "A project card", "A task report", "final/one", "filed/one", "operator/one", "explained/one", "Held for", "In Base already", "Someone else", "Mirrored merge", "The outcome card"} { |
| 143 | if strings.Contains(m.Text, not) { |
| 144 | t.Fatalf("%q listed: %s", not, m.Text) |
| 145 | } |
| 146 | } |
| 147 | if strings.Count(m.Text, "[") != strings.Count(m.Text, "]") { |
| 148 | t.Fatalf("unbalanced markers %s", m.Text) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // TestWaitingCardsNoteCap: at most maxWaiting, newest first, then where |
| 153 | // the rest are. A card with no question says what it is about. |
| 154 | func TestWaitingCardsNoteCap(t *testing.T) { |
| 155 | r := waitingRig(t) |
| 156 | bare := r.waitingCard(cardRecord{Kind: wire.CardMerge, TaskID: "tk1"}) |
| 157 | var cards []cardRecord |
| 158 | for i := range maxWaiting { |
| 159 | cards = append(cards, r.waitingCard(cardRecord{Kind: wire.CardQuestion, Question: fmt.Sprintf("Question %d?", i)})) |
| 160 | } |
| 161 | m := r.assistantSend("Anything for me?") |
| 162 | var lines []string |
| 163 | for i := len(cards) - 1; i >= 0; i-- { |
| 164 | lines = append(lines, waitingLine(cards[i], strconv.Quote(cards[i].Question), cardLink(cards[i].ID))) |
| 165 | } |
| 166 | lines = append(lines, fmt.Sprintf(noteWaitingMore, waitingOrigin+"/tasks?needs=you")) |
| 167 | if want := "Anything for me?\n\n" + fmt.Sprintf(noteWaiting, strings.Join(lines, "\n")); m.Text != want { |
| 168 | t.Fatalf("frame text\n%s\nwant\n%s", m.Text, want) |
| 169 | } |
| 170 | // With room for it, the card with no question. |
| 171 | r.must(r.s.db.G.Model(&cardRecord{}).Where("id = ?", cards[0].ID).Update("state", wire.CardAnswered).Error) |
| 172 | m = r.assistantSend("And now?") |
| 173 | if want := fmt.Sprintf(noteWaitingCard, waitingLabels[wire.CardMerge], wire.CardMerge, cardLink(bare.ID)); !strings.Contains(m.Text, "\n"+want+"\n") || strings.Contains(m.Text, noteWaitingMore[:20]) { |
| 174 | t.Fatalf("frame text %s\nwant the line %s", m.Text, want) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // TestWaitingCardsPastManyOthers: the cards that do not count never take |
| 179 | // the room of one that does, however many and however new they are. |
| 180 | func TestWaitingCardsPastManyOthers(t *testing.T) { |
| 181 | r := waitingRig(t) |
| 182 | old := r.waitingCard(cardRecord{Kind: wire.CardPlace, TaskID: "tk1", Question: "Publish Nearys?"}) |
| 183 | for i := range 60 { |
| 184 | target := fmt.Sprintf("tito/site%d", i) |
| 185 | switch i % 4 { |
| 186 | case 0: |
| 187 | r.waitingCard(cardRecord{Kind: wire.CardAccess, Access: accessJSON(target, "approved"), Name: target}) |
| 188 | case 1: |
| 189 | r.waitingCard(cardRecord{Kind: wire.CardAccess, Access: accessJSON(target, "pending"), Name: target}) |
| 190 | case 2: |
| 191 | r.waitingCard(cardRecord{Kind: wire.CardAccess, Name: target, Question: target}) |
| 192 | default: |
| 193 | r.waitingCard(cardRecord{Kind: wire.CardQuestion, Held: true, Question: target}) |
| 194 | } |
| 195 | } |
| 196 | m := r.assistantSend("Can you publish it?") |
| 197 | want := "Can you publish it?\n\n" + fmt.Sprintf(noteWaiting, waitingLine(old, `"Publish Nearys?"`, cardLink(old.ID))) |
| 198 | if m.Text != want { |
| 199 | t.Fatalf("frame text\n%s\nwant\n%s", m.Text, want) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // TestWaitingCardsNoOrigin: with no origin there is no note, never a |
| 204 | // link relative to Base. |
| 205 | func TestWaitingCardsNoOrigin(t *testing.T) { |
| 206 | r := waitingRig(t) |
| 207 | r.s.authority.Origin = "" |
| 208 | r.waitingCard(cardRecord{Kind: wire.CardPlace, TaskID: "tk1", Question: "Publish it?"}) |
| 209 | if m := r.assistantSend("Hello."); m.Text != "Hello." { |
| 210 | t.Fatalf("frame text %q", m.Text) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // TestWaitingCardsOnlyAssistantPersonTurns: a Coordinator turn and a |
| 215 | // team_update turn never carry the note. |
| 216 | func TestWaitingCardsOnlyAssistantPersonTurns(t *testing.T) { |
| 217 | r := waitingRig(t) |
| 218 | r.waitingCard(cardRecord{Kind: wire.CardPlace, TaskID: "tk1", Question: "Publish it?"}) |
| 219 | update := turnRecord{ID: newID("t_"), Person: hkOwner, State: wire.TurnQueued, Attempt: 1, Kind: wire.TurnTeamUpdate, Note: "The team finished.", Revision: 1, CreatedAt: r.now, UpdatedAt: r.now, Lane: wire.LaneAssistant} |
| 220 | r.must(r.s.db.G.Create(&update).Error) |
| 221 | r.must(r.tx(func(t *txn) error { return t.dispatch(&update) })) |
| 222 | if m := r.lastFrame(); m.Kind != wire.TurnTeamUpdate || m.Text != "The team finished." { |
| 223 | t.Fatalf("team_update frame %+v", m) |
| 224 | } |
| 225 | if _, _, err := r.s.Send(r.ctx, hkOwner, wire.SendMessage{Text: "Coordinator, hello", IdempotencyKey: "kc"}); err != nil { |
| 226 | t.Fatal(err) |
| 227 | } |
| 228 | if m := r.lastFrame(); m.Lane != wire.LaneCoordinator || strings.Contains(m.Text, "Publish it?") { |
| 229 | t.Fatalf("Coordinator frame %+v", m) |
| 230 | } |
| 231 | } |
| 232 | |