| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "fmt" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestCardNamesComposerDevice(t *testing.T) { |
| 12 | for _, tc := range []struct { |
| 13 | name, text, state string |
| 14 | protocol int |
| 15 | project, attachment, aside, answer bool |
| 16 | }{ |
| 17 | {name: "exact", text: "oficina/equipo", answer: true}, |
| 18 | {name: "phrase", text: "It should go in equipo please", answer: true}, |
| 19 | {name: "negated", text: "Don't use choice03"}, |
| 20 | {name: "old-runner", text: "oficina/equipo", protocol: wire.ProtocolPlacement - 1}, |
| 21 | {name: "unrelated", text: "Actually never mind. What is the status of the login task?"}, |
| 22 | {name: "unknown", text: "no such project"}, |
| 23 | {name: "multiple", text: "It should go in equipo or sample please"}, |
| 24 | {name: "project", text: "oficina/equipo", project: true}, |
| 25 | {name: "attachment", text: "oficina/equipo", attachment: true}, |
| 26 | {name: "aside", text: "oficina/equipo", aside: true}, |
| 27 | {name: "answered", text: "oficina/equipo", state: wire.CardAnswered}, |
| 28 | {name: "closed", text: "oficina/equipo", state: wire.CardResolved}, |
| 29 | } { |
| 30 | t.Run(tc.name, func(t *testing.T) { |
| 31 | protocol := tc.protocol |
| 32 | if protocol == 0 { |
| 33 | protocol = wire.ProtocolCurrent |
| 34 | } |
| 35 | r := newToolRigAt(t, protocol) |
| 36 | p, err := r.client.AddProject(r.ctx, "oficina/equipo") |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if tc.name == "negated" { |
| 41 | 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 { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | } |
| 45 | state := tc.state |
| 46 | if state == "" { |
| 47 | state = wire.CardOpen |
| 48 | } |
| 49 | raw := `{"by_name":true,"requested_name":"model suggestion","repos":[],"labels":[]}` |
| 50 | 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 { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if tc.aside { |
| 54 | if err := r.client.do(r.ctx, "POST", "/api/v1/cards/c_typed/visibility", wire.SetCardVisibility{Revision: 1, Aside: true}, nil); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | } |
| 58 | in := wire.SendMessage{Text: tc.text, IdempotencyKey: "typed"} |
| 59 | if tc.project { |
| 60 | in.ProjectID = p.ID |
| 61 | } |
| 62 | if tc.attachment { |
| 63 | a, err := r.client.Upload(r.ctx, "note.txt", "text/plain", []byte("hello")) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | in.Attachments = []string{a.ID} |
| 68 | } |
| 69 | var sent, again wire.Sent |
| 70 | if err := r.client.do(r.ctx, "POST", "/api/v1/messages", in, &sent); err != nil { |
| 71 | t.Fatal("composer refused by open card", err) |
| 72 | } |
| 73 | if sent.Message.Text != in.Text || sent.Message.ProjectID != in.ProjectID || sent.Turn.ID == "" { |
| 74 | t.Fatal("message lost words, project or turn", sent) |
| 75 | } |
| 76 | if len(sent.Message.Attachments) != len(in.Attachments) { |
| 77 | t.Fatal("attachments lost") |
| 78 | } |
| 79 | if err := r.client.do(r.ctx, "POST", "/api/v1/messages", in, &again); err != nil || again.Message.ID != sent.Message.ID { |
| 80 | t.Fatal("retry", err) |
| 81 | } |
| 82 | var after string |
| 83 | if err := r.inst.DB.Writer().QueryRow("SELECT state FROM chat_cards WHERE id='c_typed'").Scan(&after); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | want := state |
| 87 | if tc.answer { |
| 88 | want = wire.CardAnswered |
| 89 | } |
| 90 | if after != want { |
| 91 | t.Fatal("card state", after, "want", want) |
| 92 | } |
| 93 | var names int |
| 94 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_project_names WHERE person=?", Owner).Scan(&names); err != nil || names != 0 { |
| 95 | t.Fatal("composer learned unseen suggestion", names, err) |
| 96 | } |
| 97 | if tc.answer { |
| 98 | later, err := r.client.Send(r.ctx, "Next unrelated request", "after-answer") |
| 99 | if err != nil || later.Turn.ID == sent.Turn.ID { |
| 100 | t.Fatal("plain message joined answer turn", err) |
| 101 | } |
| 102 | } |
| 103 | if err := r.runner.Send(r.ctx, "prior-reply", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Ready"}); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | _, body := r.expect(wire.FrameMessage) |
| 107 | cmd := body.(*wire.MessageCommand) |
| 108 | if cmd.TurnID != sent.Turn.ID || !strings.Contains(cmd.Text, in.Text) { |
| 109 | t.Fatal("message did not dispatch", cmd) |
| 110 | } |
| 111 | if tc.answer && !strings.Contains(cmd.Text, p.Repo) { |
| 112 | t.Fatal("answer lost project", cmd.Text) |
| 113 | } |
| 114 | }) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestCardNamesWebControls(t *testing.T) { |
| 119 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 120 | p, err := r.client.AddProject(r.ctx, "oficina/equipo") |
| 121 | if err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | page := r.get(r.inst.Cookie(), "/projects/"+p.ID, 200) |
| 125 | if !strings.Contains(page, `name="use_title"`) || !strings.Contains(page, "Use the repository title for this project") { |
| 126 | t.Fatal("explicit title control missing") |
| 127 | } |
| 128 | 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"}}) |
| 129 | if status != 303 { |
| 130 | t.Fatal("adopt title", status) |
| 131 | } |
| 132 | var trusted bool |
| 133 | if err := r.inst.DB.Writer().QueryRow("SELECT title_set FROM chat_projects WHERE id=?", p.ID).Scan(&trusted); err != nil || !trusted { |
| 134 | t.Fatal("web title adoption ignored", err) |
| 135 | } |
| 136 | raw := `{"requested_name":"tito","repos":["amadan.net/oficina/equipo"],"labels":["oficina/equipo"]}` |
| 137 | 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 { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | page = r.get(r.inst.Cookie(), "/conversation", 200) |
| 141 | if !strings.Contains(page, `name="remember" value="1" checked`) || !strings.Contains(page, `name="remember_name" value="tito"`) { |
| 142 | t.Fatal("remember control missing or not prefilled") |
| 143 | } |
| 144 | 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"}}) |
| 145 | if status != 303 { |
| 146 | t.Fatal("web remember answer", status) |
| 147 | } |
| 148 | var name string |
| 149 | if err := r.inst.DB.Writer().QueryRow("SELECT name FROM chat_project_names WHERE person=?", Owner).Scan(&name); err != nil || name != "my portal" { |
| 150 | t.Fatal("edited web name ignored", name, err) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func TestCardNamesCarriedDevice(t *testing.T) { |
| 155 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 156 | p, err := r.client.AddProject(r.ctx, "oficina/equipo") |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | // End the rig's initial turn; the selected request then fails before the model. |
| 161 | if err := r.runner.Send(r.ctx, "carried-initial", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Ready"}); err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | var chosen wire.Sent |
| 165 | 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 { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | r.expect(wire.FrameMessage) |
| 169 | r.failTurn(r.runner, chosen.Turn.ID) |
| 170 | next, err := r.client.Send(r.ctx, "Fix oficina/sample", "carried-next") |
| 171 | if err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | _, body := r.expect(wire.FrameMessage) |
| 175 | cmd := body.(*wire.MessageCommand) |
| 176 | if !strings.Contains(cmd.Text, p.Repo) || strings.Join(cmd.PersonText, "|") != "Fix this project|Fix oficina/sample" { |
| 177 | t.Fatal("carried dispatch lost selection or person words", cmd.Text, cmd.PersonText) |
| 178 | } |
| 179 | r.turn = next.Turn.ID |
| 180 | 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 { |
| 181 | t.Fatal("carried selection incorrectly bound newest request", result) |
| 182 | } |
| 183 | } |
| 184 | |