| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestCardNamesConfirmed(t *testing.T) { |
| 13 | for _, tc := range []struct { |
| 14 | name, answer string |
| 15 | omit bool |
| 16 | want string |
| 17 | }{ |
| 18 | {"tito", "", true, ""}, {"model suggestion", "my portal", false, "my portal"}, |
| 19 | {"tito", "", false, ""}, {"tito", "the", false, ""}, {"tito", "xy", false, ""}, |
| 20 | {"tito", "swipe back", false, ""}, |
| 21 | } { |
| 22 | t.Run(tc.name+"/"+tc.answer, func(t *testing.T) { |
| 23 | r := placementRig(t) |
| 24 | ctx := context.Background() |
| 25 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 26 | _, err := tx.askNamedProject(turnRecord{ID: "t_confirm", Person: hkOwner}, tc.name, nil) |
| 27 | return err |
| 28 | }); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | var c cardRecord |
| 32 | if err := r.s.db.G.First(&c, "turn_id = ?", "t_confirm").Error; err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | var data placementData |
| 36 | json.Unmarshal([]byte(c.Placement), &data) |
| 37 | // Decode wire JSON to exercise absent versus explicit empty without requiring new fields at compile time. |
| 38 | raw, _ := json.Marshal(map[string]any{"revision": c.Revision, "choice": data.Labels[0], "remember_name": tc.answer}) |
| 39 | if tc.omit { |
| 40 | raw, _ = json.Marshal(map[string]any{"revision": c.Revision, "choice": data.Labels[0]}) |
| 41 | } |
| 42 | var in wire.AnswerCard |
| 43 | json.Unmarshal(raw, &in) |
| 44 | if _, _, err := r.s.Answer(ctx, hkOwner, c.ID, in); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | var names []projectNameRecord |
| 48 | r.s.db.G.Where("person = ?", hkOwner).Find(&names) |
| 49 | if tc.want == "" { |
| 50 | if len(names) != 0 { |
| 51 | t.Fatalf("filtered or skipped name learned: %+v", names) |
| 52 | } |
| 53 | } else if len(names) != 1 || names[0].Name != tc.want { |
| 54 | t.Fatalf("confirmed name not learned: %+v want %q", names, tc.want) |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestCardNamesExplicitTitle(t *testing.T) { |
| 61 | r := placementRig(t) |
| 62 | ctx := context.Background() |
| 63 | var p wire.Project |
| 64 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 65 | var row projectRecord |
| 66 | tx.db.First(&row, "id = ?", "p_new") |
| 67 | var err error |
| 68 | p, err = tx.projectView(row) |
| 69 | return err |
| 70 | }); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | raw, _ := json.Marshal(map[string]any{"revision": p.Revision, "title": p.Title, "description": p.Description, "use_title": true}) |
| 74 | var in wire.EditProject |
| 75 | json.Unmarshal(raw, &in) |
| 76 | out, err := r.s.EditProject(ctx, hkOwner, p.ID, in) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | if out.TitleSource != "owner" { |
| 81 | t.Fatal("explicit title adoption ignored", out.TitleSource) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestCardNamesCarriedSelection(t *testing.T) { |
| 86 | r := placementRig(t) |
| 87 | ctx := context.Background() |
| 88 | m := messageRecord{ID: "m_carried_selected", Person: hkOwner, Lane: "", TurnID: "t_failed_selected", Origin: originComposer, Role: wire.RolePerson, Text: "Fix this", ProjectID: "p_new"} |
| 89 | if err := r.s.db.G.Create(&m).Error; err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | if err := r.s.db.G.Create(&carriedRecord{TurnID: "t_next", MessageID: m.ID, Person: hkOwner}).Error; err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 96 | prompt, err := tx.withUnanswered([]messageRecord{m}, "Try again") |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | if !strings.Contains(prompt, "amadan.net/oficina/new") || !strings.Contains(prompt, m.Text) { |
| 101 | t.Fatal("carried prompt lost selection", prompt) |
| 102 | } |
| 103 | if !strings.Contains(prompt, fmt.Sprintf(noteUnansweredMessage, m.Text)+"\n") || !strings.Contains(prompt, "\nEquipo's note for the preceding request only: the person selected project amadan.net/oficina/new.\n") { |
| 104 | t.Fatal("system note quoted as person words", prompt) |
| 105 | } |
| 106 | repo, err := tx.turnProject(turnRecord{ID: "t_next", Person: hkOwner}) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | if repo != "" { |
| 111 | t.Fatal("carried selection bound newest request", repo) |
| 112 | } |
| 113 | return nil |
| 114 | }); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestCardNamesOtherRetainsOptOut(t *testing.T) { |
| 120 | r := placementRig(t) |
| 121 | ctx := context.Background() |
| 122 | data := placementData{RequestedName: "tito", OtherLabel: "Other", Labels: []string{"Other"}} |
| 123 | raw, _ := json.Marshal(data) |
| 124 | c := cardRecord{ID: "c_optout", Person: hkOwner, Kind: wire.CardQuestion, State: wire.CardOpen, Options: `["Other"]`, Placement: string(raw), Revision: 1} |
| 125 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | skip := "" |
| 129 | out, _, err := r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Revision: 1, Choice: "Other", RememberName: &skip}) |
| 130 | if err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | if _, _, err = r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Revision: out.Card.Revision, Choice: "oficina/new"}); err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | var count int64 |
| 137 | r.s.db.G.Model(&projectNameRecord{}).Count(&count) |
| 138 | if count != 0 { |
| 139 | t.Fatal("opt-out forgotten by text card", count) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestCardNamesComposerScope(t *testing.T) { |
| 144 | for _, tc := range []struct{ person, lane string }{{hkMember, ""}, {hkOwner, "job:other"}} { |
| 145 | t.Run(tc.person+tc.lane, func(t *testing.T) { |
| 146 | r := placementRig(t) |
| 147 | ctx := context.Background() |
| 148 | c := cardRecord{ID: "c_elsewhere", Person: tc.person, Lane: tc.lane, Kind: wire.CardQuestion, State: wire.CardOpen, Revision: 1, Options: "[]", Placement: `{"by_name":true}`} |
| 149 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | if _, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "oficina/new", IdempotencyKey: "scope"}); err != nil { |
| 153 | t.Fatal("another conversation captured message", err) |
| 154 | } |
| 155 | var after cardRecord |
| 156 | if err := r.s.db.G.First(&after, "id = ?", c.ID).Error; err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | if after.State != wire.CardOpen { |
| 160 | t.Fatal("another conversation answered card") |
| 161 | } |
| 162 | }) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestCardNamesCardSuggestionAndNoEarlyLearning(t *testing.T) { |
| 167 | r := placementRig(t) |
| 168 | ctx := context.Background() |
| 169 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 170 | if _, err := tx.askNamedProject(turnRecord{ID: "t_suggestion", Person: hkOwner}, "tito", nil); err != nil { |
| 171 | return err |
| 172 | } |
| 173 | var c cardRecord |
| 174 | if err := tx.db.First(&c, "turn_id = ?", "t_suggestion").Error; err != nil { |
| 175 | return err |
| 176 | } |
| 177 | v, err := tx.cardView(c) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | if v.RememberName != "tito" { |
| 182 | t.Fatal("suggestion missing from card", v.RememberName) |
| 183 | } |
| 184 | var n int64 |
| 185 | if err := tx.db.Model(&projectNameRecord{}).Count(&n).Error; err != nil { |
| 186 | return err |
| 187 | } |
| 188 | if n != 0 { |
| 189 | t.Fatal("model suggestion learned before person's answer") |
| 190 | } |
| 191 | return nil |
| 192 | }); err != nil { |
| 193 | t.Fatal(err) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func TestCardNamesAnswerOwnTurn(t *testing.T) { |
| 198 | r := placementRig(t) |
| 199 | ctx := context.Background() |
| 200 | first, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "First", IdempotencyKey: "first"}) |
| 201 | if err != nil { |
| 202 | t.Fatal(err) |
| 203 | } |
| 204 | queued, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "Queued", IdempotencyKey: "queued"}) |
| 205 | if err != nil { |
| 206 | t.Fatal(err) |
| 207 | } |
| 208 | c := cardRecord{ID: "c_ownturn", Person: hkOwner, TurnID: first.Turn.ID, Kind: wire.CardQuestion, State: wire.CardOpen, Revision: 1, Options: "[]", Placement: `{"by_name":true}`} |
| 209 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | answer, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "oficina/new", IdempotencyKey: "answer"}) |
| 213 | if err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | if answer.Turn.ID == queued.Turn.ID { |
| 217 | t.Fatal("typed answer joined ordinary queued turn") |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func TestCardNamesOnlyTypedCard(t *testing.T) { |
| 222 | r := placementRig(t) |
| 223 | ctx := context.Background() |
| 224 | c := cardRecord{ID: "c_options", Person: hkOwner, Kind: wire.CardQuestion, State: wire.CardOpen, Revision: 1, Options: `["oficina/new"]`, Placement: `{"repos":["amadan.net/oficina/new"],"labels":["oficina/new"]}`} |
| 225 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 226 | t.Fatal(err) |
| 227 | } |
| 228 | if _, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "oficina/new", IdempotencyKey: "ordinary"}); err != nil { |
| 229 | t.Fatal(err) |
| 230 | } |
| 231 | var after cardRecord |
| 232 | r.s.db.G.First(&after, "id = ?", c.ID) |
| 233 | if after.State != wire.CardOpen { |
| 234 | t.Fatal("option card captured composer") |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | func TestCardNamesPlacementIdentity(t *testing.T) { |
| 239 | for _, suggestion := range []string{"", "portal"} { |
| 240 | for _, choice := range []string{"old", "new"} { |
| 241 | t.Run("suggestion="+suggestion+"/"+choice, func(t *testing.T) { |
| 242 | r := placementRig(t) |
| 243 | // The new-project option precedes two existing repositories. |
| 244 | data := placementData{RequestedName: suggestion, Placement: wire.Placement{Repos: []string{"amadan.net/oficina/old", "amadan.net/oficina/new"}, NewRepo: "amadan.net/oficina/fresh", NewFirst: true}, Labels: []string{"New project: oficina/fresh", "oficina/old", "oficina/new"}, NewLabel: "New project: oficina/fresh"} |
| 245 | raw, _ := json.Marshal(data) |
| 246 | options, _ := json.Marshal(data.Labels) |
| 247 | c := cardRecord{ID: "c_identity", Person: hkOwner, Kind: wire.CardQuestion, State: wire.CardOpen, Revision: 1, Options: string(options), Placement: string(raw)} |
| 248 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | name := "portal" |
| 252 | if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Revision: 1, Choice: "oficina/" + choice, RememberName: &name}); err != nil { |
| 253 | t.Fatal(err) |
| 254 | } |
| 255 | var names []projectNameRecord |
| 256 | r.s.db.G.Where("person = ?", hkOwner).Find(&names) |
| 257 | if suggestion == "" { |
| 258 | if len(names) != 0 { |
| 259 | t.Fatal("ordinary card accepted name", names) |
| 260 | } |
| 261 | } else if len(names) != 1 || names[0].ProjectID != "p_"+choice { |
| 262 | t.Fatal("name learned for the wrong repository", names, "want", choice) |
| 263 | } |
| 264 | }) |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | func TestCardNamesTypedExplicit(t *testing.T) { |
| 270 | r := placementRig(t) |
| 271 | c := cardRecord{ID: "c_typed_explicit", Person: hkOwner, Kind: wire.CardQuestion, State: wire.CardOpen, Revision: 1, Options: "[]", Placement: `{"by_name":true,"requested_name":"portal","star":true}`} |
| 272 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 273 | t.Fatal(err) |
| 274 | } |
| 275 | // A typed answer may use an owner-set title rather than the option label. |
| 276 | if err := r.s.db.G.Model(&projectRecord{}).Where("id = ?", "p_new").Updates(map[string]any{"title": "Invoices", "title_set": true}).Error; err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | name := "billing" |
| 280 | if _, _, err := r.s.Answer(context.Background(), hkOwner, c.ID, wire.AnswerCard{Revision: 1, Choice: "Invoices", RememberName: &name}); err != nil { |
| 281 | t.Fatal(err) |
| 282 | } |
| 283 | var learned projectNameRecord |
| 284 | if err := r.s.db.G.First(&learned, "person = ? AND name = ?", hkOwner, name).Error; err != nil || learned.ProjectID != "p_new" { |
| 285 | t.Fatal("typed name not learned for resolved identity", learned, err) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | func TestCardNamesRetryBeforeCard(t *testing.T) { |
| 290 | r := placementRig(t) |
| 291 | ctx := context.Background() |
| 292 | in := wire.SendMessage{Text: "oficina/new", IdempotencyKey: "before-card"} |
| 293 | first, _, err := r.s.Send(ctx, hkOwner, in) |
| 294 | if err != nil { |
| 295 | t.Fatal(err) |
| 296 | } |
| 297 | c := cardRecord{ID: "c_later", Person: hkOwner, Kind: wire.CardQuestion, State: wire.CardOpen, Revision: 1, Options: "[]", Placement: `{"by_name":true}`} |
| 298 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | again, created, err := r.s.Send(ctx, hkOwner, in) |
| 302 | if err != nil || created || again.Message.ID != first.Message.ID { |
| 303 | t.Fatal("retry changed outcome", created, err) |
| 304 | } |
| 305 | if err := r.s.db.G.First(&c, "id = ?", c.ID).Error; err != nil { |
| 306 | t.Fatal(err) |
| 307 | } |
| 308 | if c.State != wire.CardOpen { |
| 309 | t.Fatal("retry answered later card") |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func TestCardNamesComposerUnavailableOrAmbiguous(t *testing.T) { |
| 314 | for _, ambiguous := range []bool{false, true} { |
| 315 | t.Run(fmt.Sprint(ambiguous), func(t *testing.T) { |
| 316 | r := placementRig(t) |
| 317 | if ambiguous { |
| 318 | if err := r.s.db.G.Create(&projectRecord{ID: "p_duplicate", Repo: "amadan.net/elsewhere/new", State: wire.ProjectActive, Revision: 1}).Error; err != nil { |
| 319 | t.Fatal(err) |
| 320 | } |
| 321 | } else { |
| 322 | if err := r.s.db.G.Model(&runnerSessionRecord{}).Where("runner_id = ?", "RUNNER1").Update("hello", `{"repos":["amadan.net/oficina/old"]}`).Error; err != nil { |
| 323 | t.Fatal(err) |
| 324 | } |
| 325 | } |
| 326 | c := cardRecord{ID: "c_available", Person: hkOwner, Kind: wire.CardQuestion, State: wire.CardOpen, Revision: 1, Options: "[]", Placement: `{"by_name":true}`} |
| 327 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 328 | t.Fatal(err) |
| 329 | } |
| 330 | if _, _, err := r.s.Send(context.Background(), hkOwner, wire.SendMessage{Text: "new", IdempotencyKey: "unresolved"}); err != nil { |
| 331 | t.Fatal("unavailable or ambiguous name refused composer", err) |
| 332 | } |
| 333 | if err := r.s.db.G.First(&c, "id = ?", c.ID).Error; err != nil { |
| 334 | t.Fatal(err) |
| 335 | } |
| 336 | if c.State != wire.CardOpen { |
| 337 | t.Fatal("unavailable or ambiguous name answered card") |
| 338 | } |
| 339 | }) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | func TestCardNamesComposerStar(t *testing.T) { |
| 344 | r := placementRig(t) |
| 345 | c := cardRecord{ID: "c_star", Person: hkOwner, Kind: wire.CardQuestion, State: wire.CardOpen, Revision: 1, Options: "[]", Placement: `{"by_name":true,"requested_name":"portal","star":true}`} |
| 346 | if err := r.s.db.G.Create(&c).Error; err != nil { |
| 347 | t.Fatal(err) |
| 348 | } |
| 349 | if _, _, err := r.s.Send(context.Background(), hkOwner, wire.SendMessage{Text: "It should go in new please", IdempotencyKey: "star-answer"}); err != nil { |
| 350 | t.Fatal(err) |
| 351 | } |
| 352 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 353 | stars, err := tx.stars(hkOwner) |
| 354 | if err != nil { |
| 355 | return err |
| 356 | } |
| 357 | if !stars["p_new"].Starred { |
| 358 | t.Fatal("typed phrase lost star target") |
| 359 | } |
| 360 | return nil |
| 361 | }); err != nil { |
| 362 | t.Fatal(err) |
| 363 | } |
| 364 | } |
| 365 | |