| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "slices" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // TestResearchCoordinatorTextIsNotThePersons (Astra security finding 1): |
| 13 | // only the person's own message rows (and notes they posted on the task |
| 14 | // page themselves) are read without asking. A URL the coordinator wrote, |
| 15 | // in an add_report entry or a fold's copy of a title and brief, is never |
| 16 | // the person's: research with no proposals then has nothing to read and |
| 17 | // nothing is dispatched, and with a proposal only the proposal is a |
| 18 | // source, waiting for the card. |
| 19 | func TestResearchCoordinatorTextIsNotThePersons(t *testing.T) { |
| 20 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 21 | r.researchOn() |
| 22 | r.inventoryOn() |
| 23 | id := r.shopTask("pv") |
| 24 | if res := r.call("pv-rep", wire.ToolAddReport, map[string]string{"task": id, "text": "Also see https://leak.example.org/ghp-in-path/abc on the shop."}); res.IsError { |
| 25 | t.Fatalf("report %+v", res) |
| 26 | } |
| 27 | // A fold's copy of another task's title and brief, written under the |
| 28 | // creator's name. |
| 29 | if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_task_entries(id, task_id, idempotency_key, kind, author, text, created_at, folded_from) VALUES (?,?,?,?,?,?,?,?)", "e_foldcopy", id, "fold:tk_other", "report", Owner, "Old task\n\nBrief naming https://brief.example.org/x", time.Now().UTC().Format(time.RFC3339Nano), "tk_other"); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if res := r.research("pv-r", id); !res.IsError || res.Code != "no_sources" { |
| 33 | t.Fatalf("coordinator text read as the person's %+v", res) |
| 34 | } |
| 35 | r.begin("pv-start", id) |
| 36 | if r.researchDispatches() != 0 || r.rows("SELECT count(*) FROM chat_research_sources") != 0 { |
| 37 | t.Fatal("a source from coordinator text") |
| 38 | } |
| 39 | // With a proposal, the proposal alone is the source, on a card. |
| 40 | if res := r.research("pv-r2", id, "https://maps.example.org/place/shop"); res.IsError { |
| 41 | t.Fatalf("request %+v", res) |
| 42 | } |
| 43 | card := r.sourcesCard(wire.CardOpen) |
| 44 | if !slices.Equal(card.Research.URLs, []string{"https://maps.example.org/place/shop"}) { |
| 45 | t.Fatalf("card %+v", card.Research) |
| 46 | } |
| 47 | if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE origin = ?", wire.ResearchOriginPerson); n != 0 { |
| 48 | t.Fatalf("%d sources taken as the person's", n) |
| 49 | } |
| 50 | if r.researchDispatches() != 0 { |
| 51 | t.Fatal("dispatched before the card") |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // TestResearchPersonNoteCounts: a note the creator posted on the task's |
| 56 | // page themselves is their own words, read without asking. |
| 57 | func TestResearchPersonNoteCounts(t *testing.T) { |
| 58 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 59 | r.researchOn() |
| 60 | id := r.shopTask("pn") |
| 61 | if _, err := r.client.PostThread(r.ctx, id, "The shop's page is https://shop.example.com/about", "pn-note"); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | if res := r.research("pn-r", id); res.IsError { |
| 65 | t.Fatalf("request %+v", res) |
| 66 | } |
| 67 | if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE origin = ? AND approved = 1 AND url = ?", wire.ResearchOriginPerson, "https://shop.example.com/about"); n != 1 { |
| 68 | t.Fatalf("%d sources from the person's note", n) |
| 69 | } |
| 70 | if r.sourcesCards() != 0 { |
| 71 | t.Fatal("a card for the person's own note") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestResearchGeneratedChoiceRequiresApproval(t *testing.T) { |
| 76 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 77 | r.researchOn() |
| 78 | id := r.shopTask("generated") |
| 79 | choice := "Read https://generated.example.com/page" |
| 80 | if res := r.call("generated-ask", wire.ToolAskPerson, map[string]any{"question": "Which page?", "options": []string{choice, "Skip"}}); res.Code != "asked" { |
| 81 | t.Fatalf("ask %+v", res) |
| 82 | } |
| 83 | r.next("Please continue.") |
| 84 | var card wire.Card |
| 85 | r.conv.Locked(func(c *Conversation) { |
| 86 | for _, k := range c.Cards { |
| 87 | if k.Kind == wire.CardQuestion { |
| 88 | card = k |
| 89 | } |
| 90 | } |
| 91 | }) |
| 92 | answer, err := r.client.Answer(r.ctx, card.ID, choice, card.Revision) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if _, err = r.inst.DB.Writer().Exec("UPDATE chat_tasks SET turn_id = ? WHERE id = ?", answer.Turn.ID, id); err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if res := r.research("generated-research", id, "https://generated.example.com/page"); res.IsError { |
| 100 | t.Fatalf("research %+v", res) |
| 101 | } |
| 102 | if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE origin = ? AND approved = 1", wire.ResearchOriginPerson); n != 0 { |
| 103 | t.Fatalf("generated choice preapproved %d sources", n) |
| 104 | } |
| 105 | if r.sourcesCards() != 1 { |
| 106 | t.Fatal("generated choice bypassed Research approval card") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestEngineeringDispatchSeparatesComposerWords(t *testing.T) { |
| 111 | words := "Do not publish anything. Fix the local mockup." |
| 112 | r := newToolRigSaying(t, wire.ProtocolCurrent, words) |
| 113 | id := r.shopTask("person-dispatch") |
| 114 | r.begin("person-dispatch-start", id) |
| 115 | cmd := r.dispatched(wire.SpecialistEngineering, "", "", 0) |
| 116 | raw, err := json.Marshal(cmd) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | var fields struct { |
| 121 | PersonWords []string `json:"person_words"` |
| 122 | } |
| 123 | if err = json.Unmarshal(raw, &fields); err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | if !slices.Equal(fields.PersonWords, []string{words}) { |
| 127 | t.Fatalf("person words missing or changed: %q", fields.PersonWords) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestAssistantTypedAnswerKeepsComposerProvenance(t *testing.T) { |
| 132 | o := newTeamRig(t) |
| 133 | o.ask("Help me with the preview.") |
| 134 | if res := o.call("typed-question", wire.ToolAskPerson, map[string]any{"question": "What should happen next?", "options": []string{}}); res.Code != "asked" { |
| 135 | t.Fatalf("ask %+v", res) |
| 136 | } |
| 137 | snapshot, err := o.a.Snapshot(o.ctx) |
| 138 | if err != nil || len(snapshot.Cards) != 1 { |
| 139 | t.Fatalf("question snapshot %+v %v", snapshot, err) |
| 140 | } |
| 141 | card := snapshot.Cards[0] |
| 142 | o.reply(o.turn) |
| 143 | words := "Do not publish." |
| 144 | answer, err := o.a.Answer(o.ctx, card.ID, words, card.Revision) |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | cmd := o.message(wire.LaneAssistant) |
| 149 | if cmd.TurnID != answer.Turn.ID || !slices.Equal(cmd.PersonText, []string{words}) { |
| 150 | t.Fatalf("typed answer lost its words: %+v", cmd) |
| 151 | } |
| 152 | } |
| 153 | |