package thintest import ( "amadan.net/oficina/equipo/internal/wire" "fmt" "net/url" "strings" "testing" ) func TestCardNamesComposerDevice(t *testing.T) { for _, tc := range []struct { name, text, state string protocol int project, attachment, aside, answer bool }{ {name: "exact", text: "oficina/equipo", answer: true}, {name: "phrase", text: "It should go in equipo please", answer: true}, {name: "negated", text: "Don't use choice03"}, {name: "old-runner", text: "oficina/equipo", protocol: wire.ProtocolPlacement - 1}, {name: "unrelated", text: "Actually never mind. What is the status of the login task?"}, {name: "unknown", text: "no such project"}, {name: "multiple", text: "It should go in equipo or sample please"}, {name: "project", text: "oficina/equipo", project: true}, {name: "attachment", text: "oficina/equipo", attachment: true}, {name: "aside", text: "oficina/equipo", aside: true}, {name: "answered", text: "oficina/equipo", state: wire.CardAnswered}, {name: "closed", text: "oficina/equipo", state: wire.CardResolved}, } { t.Run(tc.name, func(t *testing.T) { protocol := tc.protocol if protocol == 0 { protocol = wire.ProtocolCurrent } r := newToolRigAt(t, protocol) p, err := r.client.AddProject(r.ctx, "oficina/equipo") if err != nil { t.Fatal(err) } if tc.name == "negated" { if err := r.client.do(r.ctx, "PUT", "/api/v1/projects/"+p.ID+"/details", wire.EditProject{Revision: p.Revision, Title: p.Title, Description: p.Description, Names: []string{"choice03"}}, nil); err != nil { t.Fatal(err) } } state := tc.state if state == "" { state = wire.CardOpen } raw := `{"by_name":true,"requested_name":"model suggestion","repos":[],"labels":[]}` if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_cards(id,person,kind,state,turn_id,lane,question,options,placement,revision) VALUES(?,?,?,?,?,?,?,?,?,?)", "c_typed", Owner, wire.CardQuestion, state, r.turn, "", "Which project?", "[]", raw, 1); err != nil { t.Fatal(err) } if tc.aside { if err := r.client.do(r.ctx, "POST", "/api/v1/cards/c_typed/visibility", wire.SetCardVisibility{Revision: 1, Aside: true}, nil); err != nil { t.Fatal(err) } } in := wire.SendMessage{Text: tc.text, IdempotencyKey: "typed"} if tc.project { in.ProjectID = p.ID } if tc.attachment { a, err := r.client.Upload(r.ctx, "note.txt", "text/plain", []byte("hello")) if err != nil { t.Fatal(err) } in.Attachments = []string{a.ID} } var sent, again wire.Sent if err := r.client.do(r.ctx, "POST", "/api/v1/messages", in, &sent); err != nil { t.Fatal("composer refused by open card", err) } if sent.Message.Text != in.Text || sent.Message.ProjectID != in.ProjectID || sent.Turn.ID == "" { t.Fatal("message lost words, project or turn", sent) } if len(sent.Message.Attachments) != len(in.Attachments) { t.Fatal("attachments lost") } if err := r.client.do(r.ctx, "POST", "/api/v1/messages", in, &again); err != nil || again.Message.ID != sent.Message.ID { t.Fatal("retry", err) } var after string if err := r.inst.DB.Writer().QueryRow("SELECT state FROM chat_cards WHERE id='c_typed'").Scan(&after); err != nil { t.Fatal(err) } want := state if tc.answer { want = wire.CardAnswered } if after != want { t.Fatal("card state", after, "want", want) } var names int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_project_names WHERE person=?", Owner).Scan(&names); err != nil || names != 0 { t.Fatal("composer learned unseen suggestion", names, err) } if tc.answer { later, err := r.client.Send(r.ctx, "Next unrelated request", "after-answer") if err != nil || later.Turn.ID == sent.Turn.ID { t.Fatal("plain message joined answer turn", err) } } if err := r.runner.Send(r.ctx, "prior-reply", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Ready"}); err != nil { t.Fatal(err) } _, body := r.expect(wire.FrameMessage) cmd := body.(*wire.MessageCommand) if cmd.TurnID != sent.Turn.ID || !strings.Contains(cmd.Text, in.Text) { t.Fatal("message did not dispatch", cmd) } if tc.answer && !strings.Contains(cmd.Text, p.Repo) { t.Fatal("answer lost project", cmd.Text) } }) } } func TestCardNamesWebControls(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) p, err := r.client.AddProject(r.ctx, "oficina/equipo") if err != nil { t.Fatal(err) } page := r.get(r.inst.Cookie(), "/projects/"+p.ID, 200) if !strings.Contains(page, `name="use_title"`) || !strings.Contains(page, "Use the repository title for this project") { t.Fatal("explicit title control missing") } status, _ := r.post(r.inst.Cookie(), "/projects/"+p.ID+"/details", url.Values{"revision": {fmt.Sprint(p.Revision)}, "title": {p.Title}, "description": {p.Description}, "use_title": {"1"}}) if status != 303 { t.Fatal("adopt title", status) } var trusted bool if err := r.inst.DB.Writer().QueryRow("SELECT title_set FROM chat_projects WHERE id=?", p.ID).Scan(&trusted); err != nil || !trusted { t.Fatal("web title adoption ignored", err) } raw := `{"requested_name":"tito","repos":["amadan.net/oficina/equipo"],"labels":["oficina/equipo"]}` if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_cards(id,person,kind,state,turn_id,lane,question,options,placement,revision) VALUES(?,?,?,?,?,?,?,?,?,?)", "c_remember", Owner, wire.CardQuestion, wire.CardOpen, r.turn, "", "Where should this go?", `["oficina/equipo"]`, raw, 1); err != nil { t.Fatal(err) } page = r.get(r.inst.Cookie(), "/conversation", 200) if !strings.Contains(page, `name="remember" value="1" checked`) || !strings.Contains(page, `name="remember_name" value="tito"`) { t.Fatal("remember control missing or not prefilled") } status, _ = r.post(r.inst.Cookie(), "/conversation/cards/c_remember/answer", url.Values{"revision": {"1"}, "choice": {"oficina/equipo"}, "remember_present": {"1"}, "remember": {"1"}, "remember_name": {"my portal"}}) if status != 303 { t.Fatal("web remember answer", status) } var name string if err := r.inst.DB.Writer().QueryRow("SELECT name FROM chat_project_names WHERE person=?", Owner).Scan(&name); err != nil || name != "my portal" { t.Fatal("edited web name ignored", name, err) } } func TestCardNamesCarriedDevice(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) p, err := r.client.AddProject(r.ctx, "oficina/equipo") if err != nil { t.Fatal(err) } // End the rig's initial turn; the selected request then fails before the model. if err := r.runner.Send(r.ctx, "carried-initial", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Ready"}); err != nil { t.Fatal(err) } var chosen wire.Sent if err := r.client.do(r.ctx, "POST", "/api/v1/messages", wire.SendMessage{Text: "Fix this project", ProjectID: p.ID, IdempotencyKey: "carried-selected"}, &chosen); err != nil { t.Fatal(err) } r.expect(wire.FrameMessage) r.failTurn(r.runner, chosen.Turn.ID) next, err := r.client.Send(r.ctx, "Fix oficina/sample", "carried-next") if err != nil { t.Fatal(err) } _, body := r.expect(wire.FrameMessage) cmd := body.(*wire.MessageCommand) if !strings.Contains(cmd.Text, p.Repo) || strings.Join(cmd.PersonText, "|") != "Fix this project|Fix oficina/sample" { t.Fatal("carried dispatch lost selection or person words", cmd.Text, cmd.PersonText) } r.turn = next.Turn.ID if result := r.call("carried-new-request", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Fix sample", "brief": "Fix the sample project"}); result.IsError { t.Fatal("carried selection incorrectly bound newest request", result) } }