| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | "gorm.io/gorm" |
| 13 | ) |
| 14 | |
| 15 | func TestProjectsReviewMemberToolFrame(t *testing.T) { |
| 16 | for _, name := range []string{wire.ToolAddProject, wire.ToolRecordTask} { |
| 17 | t.Run(name, func(t *testing.T) { |
| 18 | r := placementRig(t) |
| 19 | r.s.authority.CurrentRunner = func(*gorm.DB, RunnerPrincipal) error { return nil } |
| 20 | if err := r.s.db.G.Delete(&projectRecord{}, "id = ?", "p_new").Error; err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | tr := turnRecord{ID: "t_member", Person: hkMember, State: wire.TurnRunning, Attempt: 1} |
| 24 | if err := r.s.db.G.Create(&tr).Error; err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | lease := Lease{Runner: RunnerPrincipal{Person: hkMember, RunnerID: "RUNNER1"}, Protocol: wire.ProtocolCurrent} |
| 28 | args, _ := json.Marshal(map[string]string{"repo": "oficina/new", "title": "Fix the button", "brief": "Fix it"}) |
| 29 | if name == wire.ToolAddProject { |
| 30 | args, _ = json.Marshal(map[string]string{"repo": "oficina/new"}) |
| 31 | } |
| 32 | call := wire.ToolCall{CallID: "member-call", TurnID: tr.ID, Attempt: 1, Name: name, Args: args} |
| 33 | frame, _ := wire.NewFrame("member-frame", wire.FrameToolCall, call) |
| 34 | if err := r.s.Apply(context.Background(), lease, frame, &call); err != nil { |
| 35 | t.Fatalf("ordinary member frame became transport error: %v", err) |
| 36 | } |
| 37 | var cmd commandRecord |
| 38 | if err := r.s.db.G.Last(&cmd, "kind = ?", wire.FrameToolResult).Error; err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | var result wire.ToolResult |
| 42 | if err := json.Unmarshal([]byte(cmd.Body), &result); err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | if result.Code != "owner_required" || !result.IsError { |
| 46 | t.Fatalf("missing owner refusal: %+v", result) |
| 47 | } |
| 48 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 49 | result, err := tx.status(lease) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | var status struct { |
| 54 | CanAdd []string `json:"can_add"` |
| 55 | } |
| 56 | if err := json.Unmarshal([]byte(result.Text), &status); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | if len(status.CanAdd) != 0 { |
| 60 | t.Fatalf("member invited to add: %+v", status) |
| 61 | } |
| 62 | return nil |
| 63 | }); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if err := r.s.Confirm(context.Background(), lease); err != nil { |
| 67 | t.Fatalf("runner stopped: %v", err) |
| 68 | } |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestProjectsReviewUnknownSelectedTask(t *testing.T) { |
| 74 | r := placementRig(t) |
| 75 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 76 | args, _ := json.Marshal(map[string]string{"task": "tk_missing"}) |
| 77 | ok, err := tx.matchesProjectWrite(turnRecord{Person: hkOwner}, &wire.ToolCall{Name: wire.ToolStartTask, Args: args}, "amadan.net/oficina/new") |
| 78 | if err != nil || !ok { |
| 79 | t.Fatalf("unknown task did not reach tool refusal: %v %v", ok, err) |
| 80 | } |
| 81 | return nil |
| 82 | }); err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestProjectsReviewStaleSelection(t *testing.T) { |
| 88 | for _, missing := range []bool{true, false} { |
| 89 | t.Run(map[bool]string{true: "removed", false: "unreachable"}[missing], func(t *testing.T) { |
| 90 | r := placementRig(t) |
| 91 | ctx := context.Background() |
| 92 | sent, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "Fix it", IdempotencyKey: "selected", ProjectID: "p_new"}) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if missing { |
| 97 | err = r.s.db.G.Model(&projectRecord{}).Where("id = ?", "p_new").Update("state", wire.ProjectRemoved).Error |
| 98 | } else { |
| 99 | err = r.s.db.G.Model(&runnerSessionRecord{}).Where("runner_id = ?", "RUNNER1").Update("hello", `{"repos":[]}`).Error |
| 100 | } |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 105 | var m messageRecord |
| 106 | if err := tx.db.First(&m, "id = ?", sent.Message.ID).Error; err != nil { |
| 107 | return err |
| 108 | } |
| 109 | text, err := tx.answerPrefix(m) |
| 110 | if err != nil { |
| 111 | t.Fatalf("stale selection broke dispatch: %v", err) |
| 112 | } |
| 113 | if !strings.Contains(text, "projects.selection_lost") { |
| 114 | t.Fatalf("missing selection notice: %q", text) |
| 115 | } |
| 116 | if err := tx.db.Model(&projectRecord{}).Where("id = ?", "p_new").Update("state", wire.ProjectActive).Error; err != nil { |
| 117 | return err |
| 118 | } |
| 119 | if err := tx.db.Model(&runnerSessionRecord{}).Where("runner_id = ?", "RUNNER1").Update("hello", `{"repos":["amadan.net/oficina/new"]}`).Error; err != nil { |
| 120 | return err |
| 121 | } |
| 122 | repo, err := tx.turnProject(turnRecord{ID: sent.Turn.ID, Person: hkOwner}) |
| 123 | if err != nil || repo != "" { |
| 124 | t.Fatalf("stale tool binding: %q %v", repo, err) |
| 125 | } |
| 126 | _, err = tx.answerPrefix(m) |
| 127 | return err |
| 128 | }); err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | var n int64 |
| 132 | if err := r.s.db.G.Model(&messageRecord{}).Where("role = ? AND text = ?", wire.RoleCoordinator, "projects.selection_lost").Count(&n).Error; err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | if n != 1 { |
| 136 | t.Fatalf("want one visible notice, got %d", n) |
| 137 | } |
| 138 | // The original selection stays part of the idempotency contract. |
| 139 | if _, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "Fix it", IdempotencyKey: "selected"}); !errors.Is(err, ErrConflict) { |
| 140 | t.Fatalf("lost original idempotency: %v", err) |
| 141 | } |
| 142 | }) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestProjectsReviewNamePrecedenceAndIsolation(t *testing.T) { |
| 147 | r := placementRig(t) |
| 148 | ctx := context.Background() |
| 149 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 150 | if err := tx.learnProjectName(hkOwner, "p_new", "Portal"); err != nil { |
| 151 | return err |
| 152 | } |
| 153 | return tx.learnProjectName(hkMember, "p_new", "Portal") |
| 154 | }); err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | if _, err := r.s.EditProject(ctx, hkOwner, "p_old", wire.EditProject{Revision: 1, Title: "Portal", Names: []string{"team"}}); err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | if _, err := r.s.EditProject(ctx, hkOwner, "p_new", wire.EditProject{Revision: 1, Title: "New"}); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 164 | p, err := tx.namedProject(hkOwner, "Portal") |
| 165 | if err != nil || p == nil || p.ID != "p_old" { |
| 166 | t.Fatalf("personal alias beat unique trusted match: %+v %v", p, err) |
| 167 | } |
| 168 | var n int64 |
| 169 | if err := tx.db.Model(&projectNameRecord{}).Where("person <> '' AND name = ?", "portal").Count(&n).Error; err != nil { |
| 170 | return err |
| 171 | } |
| 172 | if n != 2 { |
| 173 | t.Fatalf("owner edit erased personal names: %d", n) |
| 174 | } |
| 175 | if err := tx.learnProjectName(hkOwner, "p_old", "Portal"); err != nil { |
| 176 | return err |
| 177 | } |
| 178 | if err := tx.db.Model(&projectNameRecord{}).Where("person = ? AND project_id = ? AND name = ?", hkMember, "p_new", "portal").Count(&n).Error; err != nil { |
| 179 | return err |
| 180 | } |
| 181 | if n != 1 { |
| 182 | t.Fatal("learning erased another person's name") |
| 183 | } |
| 184 | return nil |
| 185 | }); err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func TestProjectsReviewDetailsProvenance(t *testing.T) { |
| 191 | for _, change := range []string{"names", "title", "description"} { |
| 192 | t.Run(change, func(t *testing.T) { |
| 193 | r := placementRig(t) |
| 194 | ctx := context.Background() |
| 195 | list, err := r.s.Projects(ctx, hkOwner) |
| 196 | if err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | var old wire.Project |
| 200 | for _, p := range list.Items { |
| 201 | if p.ID == "p_old" { |
| 202 | old = p |
| 203 | } |
| 204 | } |
| 205 | in := wire.EditProject{Revision: old.Revision, Title: old.Title, Description: old.Description, Names: []string{"Portal", " "}} |
| 206 | if change == "title" { |
| 207 | in.Title = "Owner title" |
| 208 | } |
| 209 | if change == "description" { |
| 210 | in.Description = "Owner description" |
| 211 | } |
| 212 | got, err := r.s.EditProject(ctx, hkOwner, old.ID, in) |
| 213 | if err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | ts, ds := "repository", "repository" |
| 217 | if change == "title" { |
| 218 | ts = "owner" |
| 219 | } |
| 220 | if change == "description" { |
| 221 | ds = "owner" |
| 222 | } |
| 223 | if got.TitleSource != ts || got.DescriptionSource != ds || len(got.Names) != 1 { |
| 224 | t.Fatalf("unchanged text blessed: %+v", got) |
| 225 | } |
| 226 | }) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | func TestProjectsReviewDiscoveryAndLearning(t *testing.T) { |
| 231 | for _, tc := range []struct { |
| 232 | kind, text, query string |
| 233 | ask bool |
| 234 | }{ |
| 235 | {wire.TurnMoveIn, "Fix it in Woodstar", "Woodstar", false}, |
| 236 | {wire.TurnExplore, "Fix it in Woodstar", "Woodstar", false}, |
| 237 | {wire.TurnPerson, "Add swipe back to the list", "swipe back", false}, |
| 238 | {wire.TurnPerson, "Fix the button", "the", false}, |
| 239 | {wire.TurnPerson, "Fix it in Ab", "Ab", false}, |
| 240 | {wire.TurnPerson, "Fix it in Woodstar", "Woodstar", true}, |
| 241 | {wire.TurnPerson, "Fix it in Woodstar", "woodstar", true}, |
| 242 | {wire.TurnPerson, "Tell me about project Woodstar", "Woodstar", false}, |
| 243 | {wire.TurnPerson, "Add a control for swipe back", "Swipe Back", false}, |
| 244 | } { |
| 245 | t.Run(tc.kind+tc.query, func(t *testing.T) { |
| 246 | r := placementRig(t) |
| 247 | tr := turnRecord{ID: "t_search", Person: hkOwner, Kind: tc.kind} |
| 248 | if err := r.s.db.G.Create(&messageRecord{ID: "m_search", Person: hkOwner, TurnID: tr.ID, Text: tc.text, Origin: originComposer}).Error; err != nil { |
| 249 | t.Fatal(err) |
| 250 | } |
| 251 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 252 | got, err := tx.find(tr, tc.query) |
| 253 | if err != nil { |
| 254 | return err |
| 255 | } |
| 256 | if (got.Code == "asked") != tc.ask { |
| 257 | t.Fatalf("discovery raised placement: %+v", got) |
| 258 | } |
| 259 | var n int64 |
| 260 | if err := tx.db.Model(&cardRecord{}).Where("turn_id = ? AND coalesce(placement, '') <> ''", tr.ID).Count(&n).Error; err != nil { |
| 261 | return err |
| 262 | } |
| 263 | if !tc.ask && n != 0 { |
| 264 | t.Fatalf("discovery card: %d", n) |
| 265 | } |
| 266 | if !tc.ask && containsProjectPhrase(tc.text, tc.query) && tc.kind == wire.TurnPerson && !strings.HasPrefix(tc.text, "Tell me") { |
| 267 | t.Fatal("feature/stopword accepted as name") |
| 268 | } |
| 269 | return nil |
| 270 | }); err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | }) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | func TestProjectsReviewSelectedTurnGuards(t *testing.T) { |
| 278 | for _, sequence := range [][]string{{"", "p_old"}, {"p_old", ""}, {"p_old", "p_new"}} { |
| 279 | t.Run(strings.Join(sequence, "-"), func(t *testing.T) { |
| 280 | r := placementRig(t) |
| 281 | ctx := context.Background() |
| 282 | // With no dispatchable runner the first request remains an eligible batch. |
| 283 | r.s.authority.PrimaryRunner = func(*gorm.DB, string) (string, error) { return "", nil } |
| 284 | // Selection still uses a reachable report; queue explicitly in a transaction. |
| 285 | r.s.authority.PrimaryRunner = func(*gorm.DB, string) (string, error) { return "RUNNER1", nil } |
| 286 | var ids []string |
| 287 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 288 | for i, p := range sequence { |
| 289 | sent, _, err := tx.sendSelectedOrigin("", hkOwner, []string{"a", "b"}[i], "Fix it", nil, originComposer, p, nil) |
| 290 | if err != nil { |
| 291 | return err |
| 292 | } |
| 293 | ids = append(ids, sent.Turn.ID) |
| 294 | if err := tx.db.Model(&turnRecord{}).Where("id = ?", sent.Turn.ID).Updates(map[string]any{"dispatched": false, "state": wire.TurnQueued}).Error; err != nil { |
| 295 | return err |
| 296 | } |
| 297 | } |
| 298 | return nil |
| 299 | }); err != nil { |
| 300 | t.Fatal(err) |
| 301 | } |
| 302 | if ids[0] == ids[1] { |
| 303 | t.Fatal("selected/plain messages shared a turn") |
| 304 | } |
| 305 | }) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | func TestProjectsReviewSelectedStartTaskAndStar(t *testing.T) { |
| 310 | r := placementRig(t) |
| 311 | ctx := context.Background() |
| 312 | sent, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "Fix it", IdempotencyKey: "select", ProjectID: "p_new"}) |
| 313 | if err != nil { |
| 314 | t.Fatal(err) |
| 315 | } |
| 316 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 317 | tr := turnRecord{ID: sent.Turn.ID, Person: hkOwner} |
| 318 | ok, err := tx.placementWrite(tr, &wire.ToolCall{Name: wire.ToolStartTask, Args: json.RawMessage(`{"task":"tk_move"}`)}) |
| 319 | if err != nil { |
| 320 | return err |
| 321 | } |
| 322 | if ok { |
| 323 | t.Fatal("started task outside selected project") |
| 324 | } |
| 325 | star := true |
| 326 | result, err := tx.askNamedProject(tr, "unknown", &star) |
| 327 | if err != nil { |
| 328 | return err |
| 329 | } |
| 330 | if result.Code != "asked" { |
| 331 | t.Fatalf("selection swallowed star request: %+v", result) |
| 332 | } |
| 333 | var c cardRecord |
| 334 | if err := tx.db.First(&c, "turn_id = ?", tr.ID).Error; err != nil { |
| 335 | return err |
| 336 | } |
| 337 | var data placementData |
| 338 | if err := json.Unmarshal([]byte(c.Placement), &data); err != nil { |
| 339 | return err |
| 340 | } |
| 341 | if err := tx.learnPlacement(c, data.Labels[0], false); err != nil { |
| 342 | return err |
| 343 | } |
| 344 | stars, err := tx.stars(hkOwner) |
| 345 | if err != nil { |
| 346 | return err |
| 347 | } |
| 348 | var p projectRecord |
| 349 | if err := tx.db.First(&p, "repo = ?", data.Repos[0]).Error; err != nil { |
| 350 | return err |
| 351 | } |
| 352 | if !stars[p.ID].Starred { |
| 353 | t.Fatal("card answer did not set star") |
| 354 | } |
| 355 | return nil |
| 356 | }); err != nil { |
| 357 | t.Fatal(err) |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | func TestProjectsReviewViewerStarsAndNames(t *testing.T) { |
| 362 | r := placementRig(t) |
| 363 | ctx := context.Background() |
| 364 | if err := r.s.SetStar(ctx, hkOwner, "p_new", true); err != nil { |
| 365 | t.Fatal(err) |
| 366 | } |
| 367 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 368 | if err := tx.learnProjectName(hkOwner, "p_new", "Private title"); err != nil { |
| 369 | return err |
| 370 | } |
| 371 | tx.viewer = hkMember |
| 372 | block, err := tx.projectBlock(hkOwner) |
| 373 | if err != nil { |
| 374 | return err |
| 375 | } |
| 376 | if !strings.Contains(block, "private title") { |
| 377 | t.Fatal("own context omitted personal name") |
| 378 | } |
| 379 | if tx.viewer != hkMember { |
| 380 | t.Fatal("viewer not restored") |
| 381 | } |
| 382 | var p projectRecord |
| 383 | if err := tx.db.First(&p, "id = ?", "p_new").Error; err != nil { |
| 384 | return err |
| 385 | } |
| 386 | return tx.projectEvent(p) |
| 387 | }); err != nil { |
| 388 | t.Fatal(err) |
| 389 | } |
| 390 | for _, person := range []string{hkOwner, hkMember} { |
| 391 | var e eventRecord |
| 392 | if err := r.s.db.G.Where("person = ? AND type = ?", person, wire.EventProject).Last(&e).Error; err != nil { |
| 393 | t.Fatal(err) |
| 394 | } |
| 395 | var ev wire.Event |
| 396 | if err := json.Unmarshal([]byte(e.Body), &ev); err != nil { |
| 397 | t.Fatal(err) |
| 398 | } |
| 399 | var p wire.Project |
| 400 | if err := json.Unmarshal(ev.Data, &p); err != nil { |
| 401 | t.Fatal(err) |
| 402 | } |
| 403 | if p.Starred != (person == hkOwner) { |
| 404 | t.Fatalf("event star for %s: %+v", person, p) |
| 405 | } |
| 406 | |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | func TestProjectsReviewBoundedChoices(t *testing.T) { |
| 411 | r := placementRig(t) |
| 412 | ctx := context.Background() |
| 413 | h := wire.Hello{Repos: []string{}} |
| 414 | for i := 0; i < 15; i++ { |
| 415 | id := fmt.Sprintf("p_choice%02d", i) |
| 416 | repo := fmt.Sprintf("amadan.net/oficina/choice%02d", i) |
| 417 | h.Repos = append(h.Repos, repo) |
| 418 | if err := r.s.db.G.Create(&projectRecord{ID: id, Repo: repo, State: wire.ProjectActive, Revision: 1}).Error; err != nil { |
| 419 | t.Fatal(err) |
| 420 | } |
| 421 | } |
| 422 | longRepo := "amadan.net/" + strings.Repeat("a", 60) + "/" + strings.Repeat("b", 60) |
| 423 | h.Repos = append(h.Repos, longRepo) |
| 424 | if err := r.s.db.G.Create(&projectRecord{ID: "p_long", Repo: longRepo, State: wire.ProjectActive, Revision: 1}).Error; err != nil { |
| 425 | t.Fatal(err) |
| 426 | } |
| 427 | raw, _ := json.Marshal(h) |
| 428 | if err := r.s.db.G.Model(&runnerSessionRecord{}).Where("runner_id = ?", "RUNNER1").Update("hello", string(raw)).Error; err != nil { |
| 429 | t.Fatal(err) |
| 430 | } |
| 431 | if err := r.s.SetStar(ctx, hkOwner, "p_long", true); err != nil { |
| 432 | t.Fatal(err) |
| 433 | } |
| 434 | if err := r.s.SetStar(ctx, hkOwner, "p_choice14", true); err != nil { |
| 435 | t.Fatal(err) |
| 436 | } |
| 437 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 438 | _, err := tx.askNamedProject(turnRecord{ID: "t_choices", Person: hkOwner}, "unknown", nil) |
| 439 | return err |
| 440 | }); err != nil { |
| 441 | t.Fatal(err) |
| 442 | } |
| 443 | var c cardRecord |
| 444 | if err := r.s.db.G.First(&c, "turn_id = ?", "t_choices").Error; err != nil { |
| 445 | t.Fatal(err) |
| 446 | } |
| 447 | var options []string |
| 448 | if err := json.Unmarshal([]byte(c.Options), &options); err != nil { |
| 449 | t.Fatal(err) |
| 450 | } |
| 451 | if len(options) != wire.MaxOptions { |
| 452 | t.Fatalf("unbounded options: %d", len(options)) |
| 453 | } |
| 454 | for _, o := range options { |
| 455 | if len(o) > wire.MaxOption { |
| 456 | t.Fatalf("long option: %s", o) |
| 457 | } |
| 458 | } |
| 459 | if options[0] != "oficina/choice14" { |
| 460 | t.Fatal("star lost priority", options) |
| 461 | } |
| 462 | answered, _, err := r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Revision: c.Revision, Choice: options[len(options)-1]}) |
| 463 | if err != nil { |
| 464 | t.Fatal(err) |
| 465 | } |
| 466 | if answered.Card.State != wire.CardOpen || len(answered.Card.Options) != 0 { |
| 467 | t.Fatalf("no name entry: %+v", answered.Card) |
| 468 | } |
| 469 | var saved cardRecord |
| 470 | if err := r.s.db.G.First(&saved, "id = ?", c.ID).Error; err != nil { |
| 471 | t.Fatal(err) |
| 472 | } |
| 473 | if saved.Options != "[]" || saved.Question != "projects.name_question" { |
| 474 | t.Fatalf("typed card not persisted: %+v", saved) |
| 475 | } |
| 476 | answered, _, err = r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Revision: answered.Card.Revision, Choice: "choice00"}) |
| 477 | if err != nil { |
| 478 | t.Fatal(err) |
| 479 | } |
| 480 | if answered.Message.Text != "choice00" { |
| 481 | t.Fatalf("typed name replaced with server text: %q", answered.Message.Text) |
| 482 | } |
| 483 | if answered.Card.State != wire.CardAnswered { |
| 484 | t.Fatalf("typed project not accepted: %+v", answered) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | func TestProjectsReviewLiteralName(t *testing.T) { |
| 489 | for _, tc := range []struct { |
| 490 | text, name string |
| 491 | want bool |
| 492 | }{ |
| 493 | {"K fix it in Porto", "Porto", true}, |
| 494 | {"Fix it in Wood Star", "Wood", false}, |
| 495 | {"Fix it in Wood Star", "wood star", true}, |
| 496 | {"Fix the control for swipe back", "Swipe Back", false}, |
| 497 | {"Fix it in The", "The", false}, |
| 498 | {"Fix it in And", "And", false}, |
| 499 | {"Fix it in Ab", "Ab", false}, |
| 500 | } { |
| 501 | if got := containsProjectPhrase(tc.text, tc.name); got != tc.want { |
| 502 | t.Errorf("name %q in %q: got %v", tc.name, tc.text, got) |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | func TestProjectsReviewForgetAuthority(t *testing.T) { |
| 508 | r := placementRig(t) |
| 509 | ctx := context.Background() |
| 510 | if err := r.s.tx(ctx, func(tx *txn) error { return tx.learnProjectName(hkOwner, "p_new", "Portal") }); err != nil { |
| 511 | t.Fatal(err) |
| 512 | } |
| 513 | r.s.authority.CurrentPerson = func(context.Context, *gorm.DB) error { return ErrForbidden } |
| 514 | if err := r.s.ForgetProjectName(ctx, hkOwner, "p_new", "Portal"); !errors.Is(err, ErrForbidden) { |
| 515 | t.Fatalf("removed person forgot name: %v", err) |
| 516 | } |
| 517 | var n int64 |
| 518 | if err := r.s.db.G.Model(&projectNameRecord{}).Where("person = ?", hkOwner).Count(&n).Error; err != nil { |
| 519 | t.Fatal(err) |
| 520 | } |
| 521 | if n != 1 { |
| 522 | t.Fatal("name removed without current roster") |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | func TestProjectsReviewForgetEventRevision(t *testing.T) { |
| 527 | r := placementRig(t) |
| 528 | ctx := context.Background() |
| 529 | if err := r.s.tx(ctx, func(tx *txn) error { return tx.learnProjectName(hkOwner, "p_new", "Portal") }); err != nil { |
| 530 | t.Fatal(err) |
| 531 | } |
| 532 | for i := 0; i < 2; i++ { |
| 533 | if err := r.s.ForgetProjectName(ctx, hkOwner, "p_new", "Portal"); err != nil { |
| 534 | t.Fatal(err) |
| 535 | } |
| 536 | var p projectRecord |
| 537 | if err := r.s.db.G.First(&p, "id = ?", "p_new").Error; err != nil { |
| 538 | t.Fatal(err) |
| 539 | } |
| 540 | if p.Revision != 1 { |
| 541 | t.Fatalf("private removal changed shared revision: %d", p.Revision) |
| 542 | } |
| 543 | } |
| 544 | var e eventRecord |
| 545 | if err := r.s.db.G.Where("person = ? AND type = ?", hkOwner, wire.EventProject).Last(&e).Error; err != nil { |
| 546 | t.Fatal(err) |
| 547 | } |
| 548 | var ev wire.Event |
| 549 | if err := json.Unmarshal([]byte(e.Body), &ev); err != nil { |
| 550 | t.Fatal(err) |
| 551 | } |
| 552 | var p wire.Project |
| 553 | if err := json.Unmarshal(ev.Data, &p); err != nil { |
| 554 | t.Fatal(err) |
| 555 | } |
| 556 | if ev.Revision != 1 || p.Revision != 1 || len(p.PersonalNames) != 0 { |
| 557 | t.Fatalf("stale removal event: %+v %+v", ev, p) |
| 558 | } |
| 559 | var count int64 |
| 560 | if err := r.s.db.G.Model(&eventRecord{}).Where("type = ? AND person <> ?", wire.EventProject, hkOwner).Count(&count).Error; err != nil || count != 0 { |
| 561 | t.Fatal("private event reached another viewer", count, err) |
| 562 | } |
| 563 | if _, err := r.s.EditProject(ctx, hkOwner, p.ID, wire.EditProject{Revision: 1, Title: p.Title, Description: p.Description}); err != nil { |
| 564 | t.Fatal("private removal conflicted with owner form", err) |
| 565 | } |
| 566 | |
| 567 | } |
| 568 | |
| 569 | func TestProjectsReviewBlankNamesDoNotUseLimit(t *testing.T) { |
| 570 | r := placementRig(t) |
| 571 | ctx := context.Background() |
| 572 | names := []string{} |
| 573 | for i := 0; i < 12; i++ { |
| 574 | names = append(names, fmt.Sprintf("name%d", i)) |
| 575 | } |
| 576 | names = append(names, " ") |
| 577 | p, err := r.s.EditProject(ctx, hkOwner, "p_old", wire.EditProject{Revision: 1, Title: "Old", Names: names}) |
| 578 | if err != nil || len(p.Names) != 12 { |
| 579 | t.Fatalf("trailing blank consumed name limit: %+v %v", p.Names, err) |
| 580 | } |
| 581 | names[len(names)-1] = "one too many" |
| 582 | if _, err := r.s.EditProject(ctx, hkOwner, "p_old", wire.EditProject{Revision: p.Revision, Title: "Old", Names: names}); !errors.Is(err, ErrInvalid) { |
| 583 | t.Fatalf("thirteen names accepted: %v", err) |
| 584 | } |
| 585 | } |
| 586 | |