package thintest import ( "encoding/json" "slices" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // TestResearchCoordinatorTextIsNotThePersons (Astra security finding 1): // only the person's own message rows (and notes they posted on the task // page themselves) are read without asking. A URL the coordinator wrote, // in an add_report entry or a fold's copy of a title and brief, is never // the person's: research with no proposals then has nothing to read and // nothing is dispatched, and with a proposal only the proposal is a // source, waiting for the card. func TestResearchCoordinatorTextIsNotThePersons(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) r.researchOn() r.inventoryOn() id := r.shopTask("pv") 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 { t.Fatalf("report %+v", res) } // A fold's copy of another task's title and brief, written under the // creator's name. 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 { t.Fatal(err) } if res := r.research("pv-r", id); !res.IsError || res.Code != "no_sources" { t.Fatalf("coordinator text read as the person's %+v", res) } r.begin("pv-start", id) if r.researchDispatches() != 0 || r.rows("SELECT count(*) FROM chat_research_sources") != 0 { t.Fatal("a source from coordinator text") } // With a proposal, the proposal alone is the source, on a card. if res := r.research("pv-r2", id, "https://maps.example.org/place/shop"); res.IsError { t.Fatalf("request %+v", res) } card := r.sourcesCard(wire.CardOpen) if !slices.Equal(card.Research.URLs, []string{"https://maps.example.org/place/shop"}) { t.Fatalf("card %+v", card.Research) } if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE origin = ?", wire.ResearchOriginPerson); n != 0 { t.Fatalf("%d sources taken as the person's", n) } if r.researchDispatches() != 0 { t.Fatal("dispatched before the card") } } // TestResearchPersonNoteCounts: a note the creator posted on the task's // page themselves is their own words, read without asking. func TestResearchPersonNoteCounts(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) r.researchOn() id := r.shopTask("pn") if _, err := r.client.PostThread(r.ctx, id, "The shop's page is https://shop.example.com/about", "pn-note"); err != nil { t.Fatal(err) } if res := r.research("pn-r", id); res.IsError { t.Fatalf("request %+v", res) } 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 { t.Fatalf("%d sources from the person's note", n) } if r.sourcesCards() != 0 { t.Fatal("a card for the person's own note") } } func TestResearchGeneratedChoiceRequiresApproval(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) r.researchOn() id := r.shopTask("generated") choice := "Read https://generated.example.com/page" if res := r.call("generated-ask", wire.ToolAskPerson, map[string]any{"question": "Which page?", "options": []string{choice, "Skip"}}); res.Code != "asked" { t.Fatalf("ask %+v", res) } r.next("Please continue.") var card wire.Card r.conv.Locked(func(c *Conversation) { for _, k := range c.Cards { if k.Kind == wire.CardQuestion { card = k } } }) answer, err := r.client.Answer(r.ctx, card.ID, choice, card.Revision) if err != nil { t.Fatal(err) } if _, err = r.inst.DB.Writer().Exec("UPDATE chat_tasks SET turn_id = ? WHERE id = ?", answer.Turn.ID, id); err != nil { t.Fatal(err) } if res := r.research("generated-research", id, "https://generated.example.com/page"); res.IsError { t.Fatalf("research %+v", res) } if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE origin = ? AND approved = 1", wire.ResearchOriginPerson); n != 0 { t.Fatalf("generated choice preapproved %d sources", n) } if r.sourcesCards() != 1 { t.Fatal("generated choice bypassed Research approval card") } } func TestEngineeringDispatchSeparatesComposerWords(t *testing.T) { words := "Do not publish anything. Fix the local mockup." r := newToolRigSaying(t, wire.ProtocolCurrent, words) id := r.shopTask("person-dispatch") r.begin("person-dispatch-start", id) cmd := r.dispatched(wire.SpecialistEngineering, "", "", 0) raw, err := json.Marshal(cmd) if err != nil { t.Fatal(err) } var fields struct { PersonWords []string `json:"person_words"` } if err = json.Unmarshal(raw, &fields); err != nil { t.Fatal(err) } if !slices.Equal(fields.PersonWords, []string{words}) { t.Fatalf("person words missing or changed: %q", fields.PersonWords) } } func TestAssistantTypedAnswerKeepsComposerProvenance(t *testing.T) { o := newTeamRig(t) o.ask("Help me with the preview.") if res := o.call("typed-question", wire.ToolAskPerson, map[string]any{"question": "What should happen next?", "options": []string{}}); res.Code != "asked" { t.Fatalf("ask %+v", res) } snapshot, err := o.a.Snapshot(o.ctx) if err != nil || len(snapshot.Cards) != 1 { t.Fatalf("question snapshot %+v %v", snapshot, err) } card := snapshot.Cards[0] o.reply(o.turn) words := "Do not publish." answer, err := o.a.Answer(o.ctx, card.ID, words, card.Revision) if err != nil { t.Fatal(err) } cmd := o.message(wire.LaneAssistant) if cmd.TurnID != answer.Turn.ID || !slices.Equal(cmd.PersonText, []string{words}) { t.Fatalf("typed answer lost its words: %+v", cmd) } }