| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo" |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // Stale legacy question cards (Paul's live check, 2026-11-14). A legacy |
| 12 | // hand-off (lane "") mirrored its team question into the Assistant lane |
| 13 | // as a question card (slice 1, kept by ruling P3j). Such a card follows |
| 14 | // its source while the source changes; it is resolved, with the approved |
| 15 | // resolved line, once the hand-off is cancelled or its work has settled |
| 16 | // or stopped, or once its source has been open for a day with no answer |
| 17 | // and none of the hand-off's work is running. Each time the card's |
| 18 | // event in the Assistant lane carries the resolution. |
| 19 | |
| 20 | const legacyQuestion = "Which login page?" |
| 21 | |
| 22 | // legacyMirror is a legacy hand-off whose turn asked the person a |
| 23 | // question, that turn ended, and the question mirrored into the |
| 24 | // Assistant lane as slice 1 did: request, the turn, the team question |
| 25 | // card and its Assistant mirror. With task, the turn records a task |
| 26 | // first (its id is task's). |
| 27 | func (o *teamRig) legacyMirror(key string, task *string) (request, turn, source, mirror string) { |
| 28 | o.t.Helper() |
| 29 | o.ask("Can we fix a bug in Tito? Login is broken") |
| 30 | request, turn = o.legacyHandoff(key, "Login is broken") |
| 31 | o.in(turn, func() { |
| 32 | if task != nil { |
| 33 | *task = o.record(key+"-rec", "Login page") |
| 34 | } |
| 35 | if res := o.call(key+"-ask", wire.ToolAskPerson, map[string]any{"question": legacyQuestion, "options": []string{"Web", "App"}}); res.Code != "asked" { |
| 36 | o.t.Fatalf("ask %+v", res) |
| 37 | } |
| 38 | }) |
| 39 | o.reply(turn) |
| 40 | source = o.teamCard(wire.CardQuestion, turn, "").ID |
| 41 | if source == "" { |
| 42 | o.t.Fatal("no team question") |
| 43 | } |
| 44 | mirror = "c_mirror" + key |
| 45 | o.exec("INSERT INTO chat_cards(id, person, kind, state, turn_id, question, options, revision, created_at, updated_at, lane, source_card, links, result, handoff_id) SELECT ?, person, kind, state, turn_id, question, '[{\"value\":\"Web\",\"label\":\"Web\"},{\"value\":\"App\",\"label\":\"App\"}]', 1, created_at, updated_at, ?, id, '[]', '', ? FROM chat_cards WHERE id = ?", mirror, wire.LaneAssistant, request, source) |
| 46 | if c := o.mirrorCard(mirror); c.State != wire.CardOpen { |
| 47 | o.t.Fatalf("the mirror %+v is not open", c) |
| 48 | } |
| 49 | return request, turn, source, mirror |
| 50 | } |
| 51 | |
| 52 | // mirrorCard is the Assistant snapshot's card id (zero when absent). |
| 53 | func (o *teamRig) mirrorCard(id string) wire.AssistantCard { |
| 54 | o.t.Helper() |
| 55 | for _, c := range o.assistantCards() { |
| 56 | if c.ID == id { |
| 57 | return c |
| 58 | } |
| 59 | } |
| 60 | return wire.AssistantCard{} |
| 61 | } |
| 62 | |
| 63 | // cardState is a card's state as stored. |
| 64 | func (o *teamRig) cardState(id string) string { |
| 65 | o.t.Helper() |
| 66 | var st string |
| 67 | if err := o.inst.DB.Writer().QueryRow("SELECT state FROM chat_cards WHERE id = ?", id).Scan(&st); err != nil { |
| 68 | o.t.Fatal(err) |
| 69 | } |
| 70 | return st |
| 71 | } |
| 72 | |
| 73 | // tick is the instance's housekeeping minute. |
| 74 | func (o *teamRig) tick() { |
| 75 | o.t.Helper() |
| 76 | if err := o.inst.Store.Chat().HousekeepingTick(o.ctx); err != nil { |
| 77 | o.t.Fatal(err) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // resolvedMirror fails unless the mirror is resolved with the approved |
| 82 | // line, gone from the open cards, and its newest event says so. |
| 83 | func (o *teamRig) resolvedMirror(id string) { |
| 84 | o.t.Helper() |
| 85 | line := equipo.RunnerCopy("assistant.outcome.resolved") |
| 86 | o.until("the mirror resolved", func() bool { return o.mirrorCard(id).State == wire.CardResolved }) |
| 87 | if c := o.mirrorCard(id); c.Result != line || c.Options == nil || c.Links == nil { |
| 88 | o.t.Fatalf("the resolved mirror %+v", c) |
| 89 | } |
| 90 | events := o.jobEvents(id) |
| 91 | if len(events) == 0 { |
| 92 | o.t.Fatal("no event for the mirror") |
| 93 | } |
| 94 | if last := events[len(events)-1]; last.State != wire.CardResolved || last.Result != line { |
| 95 | o.t.Fatalf("the mirror's event %+v", last) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // openMirror fails unless the mirror is still open and no event moved it. |
| 100 | func (o *teamRig) openMirror(id string) { |
| 101 | o.t.Helper() |
| 102 | if c := o.mirrorCard(id); c.State != wire.CardOpen { |
| 103 | o.t.Fatalf("the mirror %+v closed", c) |
| 104 | } |
| 105 | for _, e := range o.jobEvents(id) { |
| 106 | if e.State != wire.CardOpen { |
| 107 | o.t.Fatalf("an event closed the mirror: %+v", e) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // TestContractAssistantLegacyQuestionCancelled: the person's request |
| 113 | // cancelled, its mirrored question is resolved. |
| 114 | func TestContractAssistantLegacyQuestionCancelled(t *testing.T) { |
| 115 | o := newTeamRig(t) |
| 116 | request, _, _, mirror := o.legacyMirror("qc", nil) |
| 117 | o.in(o.turn, func() { |
| 118 | if res := o.use("qc-cancel", "team_cancel", map[string]any{"request": request}); res.IsError { |
| 119 | t.Fatalf("team_cancel %+v", res) |
| 120 | } |
| 121 | }) |
| 122 | o.resolvedMirror(mirror) |
| 123 | } |
| 124 | |
| 125 | // TestContractAssistantLegacyQuestionSettled: the hand-off's work |
| 126 | // settled (its task ended), its mirrored question is resolved. |
| 127 | func TestContractAssistantLegacyQuestionSettled(t *testing.T) { |
| 128 | o := newTeamRig(t) |
| 129 | var id string |
| 130 | request, _, _, mirror := o.legacyMirror("qs", &id) |
| 131 | if !o.linked(request)[id] { |
| 132 | t.Fatal("the task is not the hand-off's") |
| 133 | } |
| 134 | o.openMirror(mirror) |
| 135 | o.exec("UPDATE chat_tasks SET stage = ? WHERE id = ?", wire.StageDone, id) |
| 136 | o.tick() |
| 137 | o.resolvedMirror(mirror) |
| 138 | } |
| 139 | |
| 140 | // TestContractAssistantLegacyQuestionStopped: the hand-off's work |
| 141 | // stopped and needs someone (a step failed, its end proven), its |
| 142 | // mirrored question is |
| 143 | // resolved when the hand-off hears of it. |
| 144 | func TestContractAssistantLegacyQuestionStopped(t *testing.T) { |
| 145 | o := newTeamRig(t) |
| 146 | var id string |
| 147 | request, _, _, mirror := o.legacyMirror("qp", &id) |
| 148 | if !o.linked(request)[id] { |
| 149 | t.Fatal("the task is not the hand-off's") |
| 150 | } |
| 151 | o.exec("INSERT INTO chat_steps(id, task_id, specialist, state, terminated, attempt, created_at, updated_at) VALUES ('s_qp', ?, ?, ?, 1, 1, datetime('now'), datetime('now'))", id, wire.SpecialistEngineering, wire.StepFailed) |
| 152 | o.tick() |
| 153 | o.resolvedMirror(mirror) |
| 154 | } |
| 155 | |
| 156 | // TestContractAssistantLegacyQuestionSourceAnswered: the source question |
| 157 | // answered (here by team_reply), the mirror follows it out of the open |
| 158 | // cards, and its event says so. |
| 159 | func TestContractAssistantLegacyQuestionSourceAnswered(t *testing.T) { |
| 160 | o := newTeamRig(t) |
| 161 | request, _, source, mirror := o.legacyMirror("qa", nil) |
| 162 | replied(t, o.teamReply("qa-reply", request, "App"), request) |
| 163 | o.until("the source answered", func() bool { return o.cardState(source) == wire.CardAnswered }) |
| 164 | o.until("the mirror closed", func() bool { return o.mirrorCard(mirror).State != wire.CardOpen }) |
| 165 | if c := o.mirrorCard(mirror); c.State != wire.CardAnswered { |
| 166 | t.Fatalf("the mirror %+v", c) |
| 167 | } |
| 168 | events := o.jobEvents(mirror) |
| 169 | if len(events) == 0 || events[len(events)-1].State != wire.CardAnswered { |
| 170 | t.Fatalf("the mirror's events %+v", events) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // TestContractAssistantLegacyQuestionSourceWithdrawn: a source question |
| 175 | // no longer open but not answered (withdrawn) leaves its mirror |
| 176 | // resolved, even when nothing moved the mirror at the time and the |
| 177 | // hand-off's work still runs. |
| 178 | func TestContractAssistantLegacyQuestionSourceWithdrawn(t *testing.T) { |
| 179 | o := newTeamRig(t) |
| 180 | var id string |
| 181 | _, _, source, mirror := o.legacyMirror("qw", &id) |
| 182 | o.exec("INSERT INTO chat_steps(id, task_id, specialist, state, attempt, created_at, updated_at) VALUES ('s_qw', ?, ?, ?, 1, datetime('now'), datetime('now'))", id, wire.SpecialistEngineering, wire.StepRunning) |
| 183 | o.exec("UPDATE chat_cards SET state = ? WHERE id = ?", wire.CardResolved, source) |
| 184 | o.tick() |
| 185 | o.resolvedMirror(mirror) |
| 186 | } |
| 187 | |
| 188 | // TestContractAssistantLegacyQuestionStale: a source question open for |
| 189 | // more than a day with no answer is resolved when none of the hand-off's |
| 190 | // work runs; before the day is out, or while its work runs (a step under |
| 191 | // way), it stays. |
| 192 | func TestContractAssistantLegacyQuestionStale(t *testing.T) { |
| 193 | o := newTeamRig(t) |
| 194 | var id string |
| 195 | request, _, source, mirror := o.legacyMirror("qx", &id) |
| 196 | if !o.linked(request)[id] { |
| 197 | t.Fatal("the task is not the hand-off's") |
| 198 | } |
| 199 | var asked time.Time |
| 200 | if err := o.inst.DB.Writer().QueryRow("SELECT created_at FROM chat_cards WHERE id = ?", source).Scan(&asked); err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | // Its work does not run (a step done, the task not settled), but the |
| 204 | // question is under a day old: it stays. |
| 205 | o.exec("INSERT INTO chat_steps(id, task_id, specialist, state, attempt, created_at, updated_at) VALUES ('s_qx', ?, ?, ?, 1, datetime('now'), datetime('now'))", id, wire.SpecialistEngineering, wire.StepDone) |
| 206 | o.exec("UPDATE chat_cards SET created_at = ? WHERE id = ?", asked.Add(-23*time.Hour), source) |
| 207 | o.tick() |
| 208 | o.openMirror(mirror) |
| 209 | // A day old, but its work runs again: it stays. |
| 210 | o.exec("UPDATE chat_steps SET state = ? WHERE id = 's_qx'", wire.StepRunning) |
| 211 | o.exec("UPDATE chat_cards SET created_at = ? WHERE id = ?", asked.Add(-25*time.Hour), source) |
| 212 | o.tick() |
| 213 | o.openMirror(mirror) |
| 214 | // The work no longer runs: resolved; the team's own question stays. |
| 215 | o.exec("UPDATE chat_steps SET state = ? WHERE id = 's_qx'", wire.StepDone) |
| 216 | o.tick() |
| 217 | o.resolvedMirror(mirror) |
| 218 | if st := o.cardState(source); st != wire.CardOpen { |
| 219 | t.Fatalf("the team's own question moved to %s", st) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // slice1Mirror makes the mirror as slice 1 wrote it, before mirrors |
| 224 | // stored their hand-off: its handoff_id is empty. |
| 225 | func (o *teamRig) slice1Mirror(mirror string) { |
| 226 | o.t.Helper() |
| 227 | o.exec("UPDATE chat_cards SET handoff_id = '' WHERE id = ?", mirror) |
| 228 | } |
| 229 | |
| 230 | // mirrorHandoff is the mirror's stored handoff_id. |
| 231 | func (o *teamRig) mirrorHandoff(id string) string { |
| 232 | o.t.Helper() |
| 233 | var h string |
| 234 | if err := o.inst.DB.Writer().QueryRow("SELECT coalesce(handoff_id, '') FROM chat_cards WHERE id = ?", id).Scan(&h); err != nil { |
| 235 | o.t.Fatal(err) |
| 236 | } |
| 237 | return h |
| 238 | } |
| 239 | |
| 240 | // TestContractAssistantLegacyQuestionSlice1Stopped: a mirror from before |
| 241 | // mirrors stored their hand-off is found through its source card; its |
| 242 | // hand-off's work stopped, so it is resolved, and linked for next time. |
| 243 | func TestContractAssistantLegacyQuestionSlice1Stopped(t *testing.T) { |
| 244 | o := newTeamRig(t) |
| 245 | var id string |
| 246 | request, _, source, mirror := o.legacyMirror("q1", &id) |
| 247 | o.slice1Mirror(mirror) |
| 248 | o.exec("INSERT INTO chat_steps(id, task_id, specialist, state, terminated, attempt, created_at, updated_at) VALUES ('s_q1', ?, ?, ?, 1, 1, datetime('now'), datetime('now'))", id, wire.SpecialistEngineering, wire.StepFailed) |
| 249 | o.tick() |
| 250 | o.resolvedMirror(mirror) |
| 251 | if got := o.mirrorHandoff(mirror); got != request { |
| 252 | t.Fatalf("handoff_id %q, want %q", got, request) |
| 253 | } |
| 254 | if st := o.cardState(source); st != wire.CardOpen { |
| 255 | t.Fatalf("the team's own question moved to %s", st) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // TestContractAssistantLegacyQuestionSlice1Stale: the same, found and |
| 260 | // resolved because its source has waited over a day with nothing running. |
| 261 | func TestContractAssistantLegacyQuestionSlice1Stale(t *testing.T) { |
| 262 | o := newTeamRig(t) |
| 263 | var id string |
| 264 | request, _, source, mirror := o.legacyMirror("q2", &id) |
| 265 | o.slice1Mirror(mirror) |
| 266 | o.exec("UPDATE chat_cards SET created_at = ? WHERE id = ?", time.Now().Add(-25*time.Hour), source) |
| 267 | o.tick() |
| 268 | o.resolvedMirror(mirror) |
| 269 | if got := o.mirrorHandoff(mirror); got != request { |
| 270 | t.Fatalf("handoff_id %q, want %q", got, request) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // TestContractAssistantLegacyQuestionSlice1NoHandoff: no hand-off follows |
| 275 | // the source (it is cancelled): the mirror is resolved once its source is |
| 276 | // no longer open or is over a day old, and stays while young and open. |
| 277 | func TestContractAssistantLegacyQuestionSlice1NoHandoff(t *testing.T) { |
| 278 | o := newTeamRig(t) |
| 279 | request, _, source, mirror := o.legacyMirror("q3", nil) |
| 280 | o.slice1Mirror(mirror) |
| 281 | o.exec("UPDATE chat_handoffs SET state = 'cancelled' WHERE id = ?", request) |
| 282 | o.tick() |
| 283 | o.openMirror(mirror) |
| 284 | o.exec("UPDATE chat_cards SET created_at = ? WHERE id = ?", time.Now().Add(-25*time.Hour), source) |
| 285 | o.tick() |
| 286 | o.resolvedMirror(mirror) |
| 287 | if got := o.mirrorHandoff(mirror); got != "" { |
| 288 | t.Fatalf("handoff_id %q, want none", got) |
| 289 | } |
| 290 | if st := o.cardState(source); st != wire.CardOpen { |
| 291 | t.Fatalf("the team's own question moved to %s", st) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // TestContractAssistantLegacyQuestionSlice1SourceGone: no hand-off and the |
| 296 | // source no longer open: the mirror is resolved. |
| 297 | func TestContractAssistantLegacyQuestionSlice1SourceGone(t *testing.T) { |
| 298 | o := newTeamRig(t) |
| 299 | request, _, source, mirror := o.legacyMirror("q4", nil) |
| 300 | o.slice1Mirror(mirror) |
| 301 | o.exec("UPDATE chat_handoffs SET state = 'cancelled' WHERE id = ?", request) |
| 302 | o.exec("UPDATE chat_cards SET state = ? WHERE id = ?", wire.CardResolved, source) |
| 303 | o.tick() |
| 304 | o.resolvedMirror(mirror) |
| 305 | } |
| 306 | |