| new file mode 100644 |
| index 00000000..0d1f759f |
| --- /dev/null |
| +++ b/internal/thintest/assistant_legacy_question_contract_test.go |
| @@ -0,0 +1,221 @@ |
| +package thintest |
| + |
| +import ( |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/equipo" |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// Stale legacy question cards (Paul's live check, 2026-11-14). A legacy |
| +// hand-off (lane "") mirrored its team question into the Assistant lane |
| +// as a question card (slice 1, kept by ruling P3j). Such a card follows |
| +// its source while the source changes; it is resolved, with the approved |
| +// resolved line, once the hand-off is cancelled or its work has settled |
| +// or stopped, or once its source has been open for a day with no answer |
| +// and none of the hand-off's work is running. Each time the card's |
| +// event in the Assistant lane carries the resolution. |
| + |
| +const legacyQuestion = "Which login page?" |
| + |
| +// legacyMirror is a legacy hand-off whose turn asked the person a |
| +// question, that turn ended, and the question mirrored into the |
| +// Assistant lane as slice 1 did: request, the turn, the team question |
| +// card and its Assistant mirror. With task, the turn records a task |
| +// first (its id is task's). |
| +func (o *teamRig) legacyMirror(key string, task *string) (request, turn, source, mirror string) { |
| + o.t.Helper() |
| + o.ask("Can we fix a bug in Tito? Login is broken") |
| + request, turn = o.legacyHandoff(key, "Login is broken") |
| + o.in(turn, func() { |
| + if task != nil { |
| + *task = o.record(key+"-rec", "Login page") |
| + } |
| + if res := o.call(key+"-ask", wire.ToolAskPerson, map[string]any{"question": legacyQuestion, "options": []string{"Web", "App"}}); res.Code != "asked" { |
| + o.t.Fatalf("ask %+v", res) |
| + } |
| + }) |
| + o.reply(turn) |
| + source = o.teamCard(wire.CardQuestion, turn, "").ID |
| + if source == "" { |
| + o.t.Fatal("no team question") |
| + } |
| + mirror = "c_mirror" + key |
| + 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) |
| + if c := o.mirrorCard(mirror); c.State != wire.CardOpen { |
| + o.t.Fatalf("the mirror %+v is not open", c) |
| + } |
| + return request, turn, source, mirror |
| +} |
| + |
| +// mirrorCard is the Assistant snapshot's card id (zero when absent). |
| +func (o *teamRig) mirrorCard(id string) wire.AssistantCard { |
| + o.t.Helper() |
| + for _, c := range o.assistantCards() { |
| + if c.ID == id { |
| + return c |
| + } |
| + } |
| + return wire.AssistantCard{} |
| +} |
| + |
| +// cardState is a card's state as stored. |
| +func (o *teamRig) cardState(id string) string { |
| + o.t.Helper() |
| + var st string |
| + if err := o.inst.DB.Writer().QueryRow("SELECT state FROM chat_cards WHERE id = ?", id).Scan(&st); err != nil { |
| + o.t.Fatal(err) |
| + } |
| + return st |
| +} |
| + |
| +// tick is the instance's housekeeping minute. |
| +func (o *teamRig) tick() { |
| + o.t.Helper() |
| + if err := o.inst.Store.Chat().HousekeepingTick(o.ctx); err != nil { |
| + o.t.Fatal(err) |
| + } |
| +} |
| + |
| +// resolvedMirror fails unless the mirror is resolved with the approved |
| +// line, gone from the open cards, and its newest event says so. |
| +func (o *teamRig) resolvedMirror(id string) { |
| + o.t.Helper() |
| + line := equipo.RunnerCopy("assistant.outcome.resolved") |
| + o.until("the mirror resolved", func() bool { return o.mirrorCard(id).State == wire.CardResolved }) |
| + if c := o.mirrorCard(id); c.Result != line || c.Options == nil || c.Links == nil { |
| + o.t.Fatalf("the resolved mirror %+v", c) |
| + } |
| + events := o.jobEvents(id) |
| + if len(events) == 0 { |
| + o.t.Fatal("no event for the mirror") |
| + } |
| + if last := events[len(events)-1]; last.State != wire.CardResolved || last.Result != line { |
| + o.t.Fatalf("the mirror's event %+v", last) |
| + } |
| +} |
| + |
| +// openMirror fails unless the mirror is still open and no event moved it. |
| +func (o *teamRig) openMirror(id string) { |
| + o.t.Helper() |
| + if c := o.mirrorCard(id); c.State != wire.CardOpen { |
| + o.t.Fatalf("the mirror %+v closed", c) |
| + } |
| + for _, e := range o.jobEvents(id) { |
| + if e.State != wire.CardOpen { |
| + o.t.Fatalf("an event closed the mirror: %+v", e) |
| + } |
| + } |
| +} |
| + |
| +// TestContractAssistantLegacyQuestionCancelled: the person's request |
| +// cancelled, its mirrored question is resolved. |
| +func TestContractAssistantLegacyQuestionCancelled(t *testing.T) { |
| + o := newTeamRig(t) |
| + request, _, _, mirror := o.legacyMirror("qc", nil) |
| + o.in(o.turn, func() { |
| + if res := o.use("qc-cancel", "team_cancel", map[string]any{"request": request}); res.IsError { |
| + t.Fatalf("team_cancel %+v", res) |
| + } |
| + }) |
| + o.resolvedMirror(mirror) |
| +} |
| + |
| +// TestContractAssistantLegacyQuestionSettled: the hand-off's work |
| +// settled (its task ended), its mirrored question is resolved. |
| +func TestContractAssistantLegacyQuestionSettled(t *testing.T) { |
| + o := newTeamRig(t) |
| + var id string |
| + request, _, _, mirror := o.legacyMirror("qs", &id) |
| + if !o.linked(request)[id] { |
| + t.Fatal("the task is not the hand-off's") |
| + } |
| + o.openMirror(mirror) |
| + o.exec("UPDATE chat_tasks SET stage = ? WHERE id = ?", wire.StageDone, id) |
| + o.tick() |
| + o.resolvedMirror(mirror) |
| +} |
| + |
| +// TestContractAssistantLegacyQuestionStopped: the hand-off's work |
| +// stopped and needs someone (a step failed, its end proven), its |
| +// mirrored question is |
| +// resolved when the hand-off hears of it. |
| +func TestContractAssistantLegacyQuestionStopped(t *testing.T) { |
| + o := newTeamRig(t) |
| + var id string |
| + request, _, _, mirror := o.legacyMirror("qp", &id) |
| + if !o.linked(request)[id] { |
| + t.Fatal("the task is not the hand-off's") |
| + } |
| + 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) |
| + o.tick() |
| + o.resolvedMirror(mirror) |
| +} |
| + |
| +// TestContractAssistantLegacyQuestionSourceAnswered: the source question |
| +// answered (here by team_reply), the mirror follows it out of the open |
| +// cards, and its event says so. |
| +func TestContractAssistantLegacyQuestionSourceAnswered(t *testing.T) { |
| + o := newTeamRig(t) |
| + request, _, source, mirror := o.legacyMirror("qa", nil) |
| + replied(t, o.teamReply("qa-reply", request, "App"), request) |
| + o.until("the source answered", func() bool { return o.cardState(source) == wire.CardAnswered }) |
| + o.until("the mirror closed", func() bool { return o.mirrorCard(mirror).State != wire.CardOpen }) |
| + if c := o.mirrorCard(mirror); c.State != wire.CardAnswered { |
| + t.Fatalf("the mirror %+v", c) |
| + } |
| + events := o.jobEvents(mirror) |
| + if len(events) == 0 || events[len(events)-1].State != wire.CardAnswered { |
| + t.Fatalf("the mirror's events %+v", events) |
| + } |
| +} |
| + |
| +// TestContractAssistantLegacyQuestionSourceWithdrawn: a source question |
| +// no longer open but not answered (withdrawn) leaves its mirror |
| +// resolved, even when nothing moved the mirror at the time and the |
| +// hand-off's work still runs. |
| +func TestContractAssistantLegacyQuestionSourceWithdrawn(t *testing.T) { |
| + o := newTeamRig(t) |
| + var id string |
| + _, _, source, mirror := o.legacyMirror("qw", &id) |
| + 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) |
| + o.exec("UPDATE chat_cards SET state = ? WHERE id = ?", wire.CardResolved, source) |
| + o.tick() |
| + o.resolvedMirror(mirror) |
| +} |
| + |
| +// TestContractAssistantLegacyQuestionStale: a source question open for |
| +// more than a day with no answer is resolved when none of the hand-off's |
| +// work runs; before the day is out, or while its work runs (a step under |
| +// way), it stays. |
| +func TestContractAssistantLegacyQuestionStale(t *testing.T) { |
| + o := newTeamRig(t) |
| + var id string |
| + request, _, source, mirror := o.legacyMirror("qx", &id) |
| + if !o.linked(request)[id] { |
| + t.Fatal("the task is not the hand-off's") |
| + } |
| + var asked time.Time |
| + if err := o.inst.DB.Writer().QueryRow("SELECT created_at FROM chat_cards WHERE id = ?", source).Scan(&asked); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Its work does not run (a step done, the task not settled), but the |
| + // question is under a day old: it stays. |
| + 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) |
| + o.exec("UPDATE chat_cards SET created_at = ? WHERE id = ?", asked.Add(-23*time.Hour), source) |
| + o.tick() |
| + o.openMirror(mirror) |
| + // A day old, but its work runs again: it stays. |
| + o.exec("UPDATE chat_steps SET state = ? WHERE id = 's_qx'", wire.StepRunning) |
| + o.exec("UPDATE chat_cards SET created_at = ? WHERE id = ?", asked.Add(-25*time.Hour), source) |
| + o.tick() |
| + o.openMirror(mirror) |
| + // The work no longer runs: resolved; the team's own question stays. |
| + o.exec("UPDATE chat_steps SET state = ? WHERE id = 's_qx'", wire.StepDone) |
| + o.tick() |
| + o.resolvedMirror(mirror) |
| + if st := o.cardState(source); st != wire.CardOpen { |
| + t.Fatalf("the team's own question moved to %s", st) |
| + } |
| +} |