| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "gorm.io/gorm" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func placementRig(t *testing.T) *meteredRig { |
| 15 | r := newMeteredRig(t) |
| 16 | r.s.authority.Me = func(_ *gorm.DB, person string) (wire.Me, error) { |
| 17 | role := "member" |
| 18 | if person == hkOwner { |
| 19 | role = "owner" |
| 20 | } |
| 21 | return wire.Me{Role: role}, nil |
| 22 | } |
| 23 | r.s.authority.Copy = func(key string) string { |
| 24 | switch key { |
| 25 | case "projects.carried_selection": |
| 26 | return "Equipo's note for the preceding request only: the person selected project %s." |
| 27 | case "projects.selected": |
| 28 | return "The person selected project %s for this request." |
| 29 | case "placement.new_named": |
| 30 | return "New project: %s" |
| 31 | case "placement.learning": |
| 32 | return "Task %s moved from %s to %s." |
| 33 | case "placement.moved_note_unstarted": |
| 34 | return "Moved from %s to %s." |
| 35 | case "placement.moved_note": |
| 36 | return "Moved from %s to %s. The old branch %s was left untouched." |
| 37 | } |
| 38 | return key |
| 39 | } |
| 40 | h, _ := json.Marshal(wire.Hello{Repos: []string{"amadan.net/oficina/old", "amadan.net/oficina/new"}, CreateIn: []string{"oficina"}}) |
| 41 | for _, v := range []any{ |
| 42 | &runnerSessionRecord{RunnerID: "RUNNER1", Hello: string(h), Protocol: wire.ProtocolCurrent}, |
| 43 | &projectRecord{ID: "p_old", Repo: "amadan.net/oficina/old", Title: "Old", State: wire.ProjectActive, Revision: 1}, |
| 44 | &projectRecord{ID: "p_new", Repo: "amadan.net/oficina/new", Title: "New", State: wire.ProjectActive, Revision: 1}, |
| 45 | &taskRecord{ID: "tk_move", ProjectID: "p_old", Title: "Keep this work", Brief: "The original brief", Branch: "equipo/original", Creator: hkOwner, Stage: wire.StageNoted, Revision: 1}, |
| 46 | } { |
| 47 | if err := r.s.db.G.Create(v).Error; err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | } |
| 51 | return r |
| 52 | } |
| 53 | |
| 54 | func TestPlacementMoveGuards(t *testing.T) { |
| 55 | for _, tc := range []struct { |
| 56 | name, person, stage, repo string |
| 57 | rev int64 |
| 58 | want error |
| 59 | }{ |
| 60 | {"non-owner", hkMember, wire.StageNoted, "oficina/new", 1, ErrForbidden}, |
| 61 | {"merged", hkOwner, wire.StageDone, "oficina/new", 1, ErrTaskEnded}, |
| 62 | {"merging", hkOwner, wire.StageMerging, "oficina/new", 1, ErrMerging}, |
| 63 | {"stale", hkOwner, wire.StageNoted, "oficina/new", 0, ErrCardConflict}, |
| 64 | {"unreachable", hkOwner, wire.StageNoted, "oficina/secret", 1, ErrNotAllowed}, |
| 65 | } { |
| 66 | t.Run(tc.name, func(t *testing.T) { |
| 67 | r := placementRig(t) |
| 68 | r.s.db.G.Model(&taskRecord{}).Where("id = ?", "tk_move").Update("stage", tc.stage) |
| 69 | err := r.s.MoveTask(context.Background(), tc.person, "tk_move", wire.MoveTask{Repo: tc.repo, Revision: tc.rev}) |
| 70 | if !errors.Is(err, tc.want) { |
| 71 | t.Fatalf("want %v, got %v", tc.want, err) |
| 72 | } |
| 73 | var k taskRecord |
| 74 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 75 | if k.ProjectID != "p_old" || k.Branch != "equipo/original" { |
| 76 | t.Fatalf("refusal changed task: %+v", k) |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestPlacementInstantKeepsBriefAndRecordsLearning(t *testing.T) { |
| 83 | r := placementRig(t) |
| 84 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | var k taskRecord |
| 88 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 89 | if k.ProjectID != "p_new" || k.Brief != "The original brief" || k.Stage != wire.StageNoted || k.Branch == "equipo/original" { |
| 90 | t.Fatalf("move: %+v", k) |
| 91 | } |
| 92 | var notes notebookRecord |
| 93 | if err := r.s.db.G.First(¬es, "person = ?", hkOwner).Error; err != nil || !strings.Contains(notes.Corrections, "oficina/new") { |
| 94 | t.Fatalf("learning: %+v %v", notes, err) |
| 95 | } |
| 96 | if len(r.audits) < 2 { |
| 97 | t.Fatal("missing audit", r.audits) |
| 98 | } |
| 99 | var commands []commandRecord |
| 100 | r.s.db.G.Find(&commands) |
| 101 | for _, c := range commands { |
| 102 | if strings.Contains(c.Body, "equipo/original") { |
| 103 | t.Fatalf("old branch command: %+v", c) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestPlacementRestartWaitsForTermination(t *testing.T) { |
| 109 | r := placementRig(t) |
| 110 | s := stepRecord{ID: "s_old", TaskID: "tk_move", Idx: 1, Person: hkOwner, Specialist: wire.SpecialistEngineering, RunnerID: "RUNNER1", State: wire.StepFailed, Attempt: 1, Pushed: true, CandidateSHA: strings.Repeat("a", 40), Terminated: false} |
| 111 | if err := r.s.db.G.Create(&s).Error; err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | var k taskRecord |
| 118 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 119 | if k.ProjectID != "p_old" || k.MoveProject == "" { |
| 120 | t.Fatalf("moved without proof: %+v", k) |
| 121 | } |
| 122 | if err := r.s.tx(context.Background(), func(tx *txn) error { return tx.finishMove(&k) }); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 126 | if k.ProjectID != "p_old" { |
| 127 | t.Fatal("terminal state replaced proof") |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestPlacementCreateNeedsConsent(t *testing.T) { |
| 132 | r := placementRig(t) |
| 133 | err := r.s.tx(context.Background(), func(tx *txn) error { |
| 134 | got, err := tx.authorizeCreate(turnRecord{ID: "turn", Person: hkOwner}, "amadan.net/oficina/fresh") |
| 135 | if err == nil && got.Code == "create" { |
| 136 | t.Fatal("created without person consent") |
| 137 | } |
| 138 | return err |
| 139 | }) |
| 140 | if err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestPlacementExplicitPermissionIsConservative(t *testing.T) { |
| 146 | for _, text := range []string{"Build a calendar", "Don't create a new project", "Should we create a new project?", "The README says create a new project", "create a new project?"} { |
| 147 | if explicitNewProject(text) { |
| 148 | t.Errorf("not an explicit request: %q", text) |
| 149 | } |
| 150 | } |
| 151 | if !explicitNewProject("Create a new project for the calendar") { |
| 152 | t.Fatal("explicit request rejected") |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestPlacementCardBindsConsentAndAsideDoesNotConsent(t *testing.T) { |
| 157 | r := placementRig(t) |
| 158 | ctx := context.Background() |
| 159 | r.s.authority.Copy = func(key string) string { |
| 160 | return map[string]string{"placement.question": "Where should this go?", "placement.add": "Add to %s", "placement.new_named": "New project: %s"}[key] |
| 161 | } |
| 162 | tr := turnRecord{ID: "tr_place", Person: hkOwner, Kind: wire.TurnPerson, Lane: "", Attempt: 1, State: wire.TurnRunning} |
| 163 | if err := r.s.db.G.Create(&tr).Error; err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 167 | _, err := tx.askPlacement(tr, wire.Placement{Repos: []string{"oficina/old"}, NewRepo: "oficina/fresh"}) |
| 168 | return err |
| 169 | }); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | var c cardRecord |
| 173 | r.s.db.G.First(&c, "turn_id = ?", tr.ID) |
| 174 | if c.Options != `["Add to oficina/old","New project: oficina/fresh"]` { |
| 175 | t.Fatal(c.Options) |
| 176 | } |
| 177 | if err := r.s.SetCardAside(ctx, hkOwner, c.ID, 1, true); err != nil { |
| 178 | t.Fatal(err) |
| 179 | } |
| 180 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 181 | ok, err := tx.createConsent(tr, "amadan.net/oficina/fresh") |
| 182 | if ok { |
| 183 | t.Fatal("aside authorised create") |
| 184 | } |
| 185 | return err |
| 186 | }); err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | if err := r.s.db.G.Create(&messageRecord{ID: "m_answer", Person: hkOwner, TurnID: "tr_answer", Role: wire.RolePerson, Text: "New project: oficina/fresh", IdempotencyKey: "place-answer"}).Error; err != nil { |
| 190 | t.Fatal(err) |
| 191 | } |
| 192 | r.s.db.G.Model(&c).Updates(map[string]any{"state": wire.CardAnswered, "answer_message": "m_answer", "choice": "New project: oficina/fresh"}) |
| 193 | if err := r.s.tx(ctx, func(tx *txn) error { |
| 194 | for _, tc := range []struct { |
| 195 | person, turn, lane, repo string |
| 196 | want bool |
| 197 | }{ |
| 198 | {hkOwner, "tr_answer", "", "amadan.net/oficina/fresh", true}, |
| 199 | {hkOwner, "tr_answer", "", "amadan.net/oficina/other", false}, |
| 200 | {hkMember, "tr_answer", "", "amadan.net/oficina/fresh", false}, |
| 201 | {hkOwner, tr.ID, "", "amadan.net/oficina/fresh", false}, |
| 202 | {hkOwner, "tr_answer", "job:x", "amadan.net/oficina/fresh", false}, |
| 203 | } { |
| 204 | ok, err := tx.createConsent(turnRecord{Person: tc.person, ID: tc.turn, Lane: tc.lane}, tc.repo) |
| 205 | if err != nil { |
| 206 | return err |
| 207 | } |
| 208 | if ok != tc.want { |
| 209 | t.Fatalf("consent %+v = %v", tc, ok) |
| 210 | } |
| 211 | } |
| 212 | return nil |
| 213 | }); err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func TestPlacementReturnNeverReusesBranch(t *testing.T) { |
| 219 | r := placementRig(t) |
| 220 | r.s.db.G.Model(&taskRecord{}).Where("id = ?", "tk_move").Update("title", strings.Repeat("longtitle", 20)) |
| 221 | seen := map[string]bool{"equipo/original": true} |
| 222 | for _, repo := range []string{"oficina/new", "oficina/old", "oficina/new"} { |
| 223 | var k taskRecord |
| 224 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 225 | if err := r.s.MoveTask(context.Background(), hkOwner, k.ID, wire.MoveTask{Repo: repo, Revision: k.Revision}); err != nil { |
| 226 | t.Fatal(err) |
| 227 | } |
| 228 | r.s.db.G.First(&k, "id = ?", k.ID) |
| 229 | if seen[k.Branch] { |
| 230 | t.Fatal("reused branch", k.Branch) |
| 231 | } |
| 232 | seen[k.Branch] = true |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func TestPlacementRosterFailureAndAuditRollback(t *testing.T) { |
| 237 | for _, kind := range []string{"removed", "lookup", "audit"} { |
| 238 | t.Run(kind, func(t *testing.T) { |
| 239 | r := placementRig(t) |
| 240 | switch kind { |
| 241 | case "removed": |
| 242 | r.removed = true |
| 243 | case "lookup": |
| 244 | r.s.authority.Me = func(*gorm.DB, string) (wire.Me, error) { return wire.Me{}, errors.New("roster unreadable") } |
| 245 | case "audit": |
| 246 | r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return errors.New("audit unavailable") } |
| 247 | } |
| 248 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err == nil { |
| 249 | t.Fatal("failed open") |
| 250 | } |
| 251 | var k taskRecord |
| 252 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 253 | if k.Revision != 1 || k.ProjectID != "p_old" || k.MoveProject != "" { |
| 254 | t.Fatal("partial move", k) |
| 255 | } |
| 256 | }) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | func TestPlacementRevocationKeepsPending(t *testing.T) { |
| 261 | r := placementRig(t) |
| 262 | step := stepRecord{ID: "s_wait", TaskID: "tk_move", Idx: 1, Person: hkOwner, Specialist: wire.SpecialistEngineering, RunnerID: "RUNNER1", State: wire.StepRunning, Attempt: 1} |
| 263 | if err := r.s.db.G.Create(&step).Error; err != nil { |
| 264 | t.Fatal(err) |
| 265 | } |
| 266 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err != nil { |
| 267 | t.Fatal(err) |
| 268 | } |
| 269 | r.s.db.G.Model(&step).Updates(map[string]any{"terminated": true, "state": wire.StepCancelled}) |
| 270 | r.s.authority.Me = func(*gorm.DB, string) (wire.Me, error) { return wire.Me{Role: "member"}, nil } |
| 271 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 272 | k, err := tx.loadTask("tk_move") |
| 273 | if err != nil { |
| 274 | return err |
| 275 | } |
| 276 | return tx.finishMove(&k) |
| 277 | }); err != nil { |
| 278 | t.Fatal(err) |
| 279 | } |
| 280 | var k taskRecord |
| 281 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 282 | if k.ProjectID != "p_old" || k.MoveProject == "" { |
| 283 | t.Fatal("revoked owner moved", k) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | func TestPlacementQuestionAndPublicationGuards(t *testing.T) { |
| 288 | t.Run("question", func(t *testing.T) { |
| 289 | r := placementRig(t) |
| 290 | if err := r.s.db.G.Create(&questionRecord{ID: "q_open", TaskID: "tk_move", State: wire.QuestionRunning}).Error; err != nil { |
| 291 | t.Fatal(err) |
| 292 | } |
| 293 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); !errors.Is(err, ErrBusy) { |
| 294 | t.Fatal("move during question", err) |
| 295 | } |
| 296 | }) |
| 297 | t.Run("unknown_count", func(t *testing.T) { |
| 298 | r := placementRig(t) |
| 299 | if err := r.s.db.G.Exec("DROP TABLE chat_questions").Error; err != nil { |
| 300 | t.Fatal(err) |
| 301 | } |
| 302 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err == nil { |
| 303 | t.Fatal("unknown count became zero") |
| 304 | } |
| 305 | }) |
| 306 | t.Run("publication", func(t *testing.T) { |
| 307 | r := placementRig(t) |
| 308 | if err := r.s.db.G.Create(&stepRecord{ID: "s_publish", TaskID: "tk_move", Person: hkOwner, Specialist: wire.SpecialistEngineering, State: wire.StepDone, Terminated: true, Publication: wire.PublicationPending}).Error; err != nil { |
| 309 | t.Fatal(err) |
| 310 | } |
| 311 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err != nil { |
| 312 | t.Fatal(err) |
| 313 | } |
| 314 | var k taskRecord |
| 315 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 316 | if k.ProjectID != "p_old" || !k.CancelRequested { |
| 317 | t.Fatal("pending push not fenced", k) |
| 318 | } |
| 319 | }) |
| 320 | t.Run("old_protocol", func(t *testing.T) { |
| 321 | r := placementRig(t) |
| 322 | r.s.db.G.Model(&runnerSessionRecord{}).Where("runner_id = ?", "RUNNER1").Update("protocol", wire.Protocol24) |
| 323 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err == nil { |
| 324 | t.Fatal("old primary moved task") |
| 325 | } |
| 326 | }) |
| 327 | } |
| 328 | |
| 329 | func TestPlacementMovingOwnerRevocation(t *testing.T) { |
| 330 | r := placementRig(t) |
| 331 | r.s.authority.Me = func(*gorm.DB, string) (wire.Me, error) { return wire.Me{Role: "owner"}, nil } |
| 332 | step := stepRecord{ID: "s_research", TaskID: "tk_move", Person: hkOwner, Specialist: wire.SpecialistResearch, State: wire.StepRunning, RunnerID: "RUNNER1", Attempt: 1} |
| 333 | if err := r.s.db.G.Create(&step).Error; err != nil { |
| 334 | t.Fatal(err) |
| 335 | } |
| 336 | if err := r.s.MoveTask(context.Background(), hkMember, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err != nil { |
| 337 | t.Fatal(err) |
| 338 | } |
| 339 | r.s.db.G.Model(&step).Updates(map[string]any{"state": wire.StepCancelled, "terminated": true}) |
| 340 | r.s.authority.Me = func(_ *gorm.DB, person string) (wire.Me, error) { |
| 341 | role := "owner" |
| 342 | if person == hkMember { |
| 343 | role = "member" |
| 344 | } |
| 345 | return wire.Me{Role: role}, nil |
| 346 | } |
| 347 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 348 | k, err := tx.loadTask("tk_move") |
| 349 | if err != nil { |
| 350 | return err |
| 351 | } |
| 352 | return tx.finishMove(&k) |
| 353 | }); err != nil { |
| 354 | t.Fatal(err) |
| 355 | } |
| 356 | var k taskRecord |
| 357 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 358 | if k.ProjectID != "p_old" || k.MoveProject == "" { |
| 359 | t.Fatal("owner revoked after request still moved", k) |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | func TestPlacementDropsRepositoryBoundRevert(t *testing.T) { |
| 364 | r := placementRig(t) |
| 365 | if err := r.s.db.G.Model(&taskRecord{}).Where("id = ?", "tk_move").Updates(map[string]any{"revert_of": "tk_merged_old", "revert_sha": strings.Repeat("a", 40)}).Error; err != nil { |
| 366 | t.Fatal(err) |
| 367 | } |
| 368 | if err := r.s.MoveTask(context.Background(), hkOwner, "tk_move", wire.MoveTask{Repo: "oficina/new", Revision: 1}); err != nil { |
| 369 | t.Fatal(err) |
| 370 | } |
| 371 | var k taskRecord |
| 372 | r.s.db.G.First(&k, "id = ?", "tk_move") |
| 373 | if k.RevertOf != "" || k.RevertSHA != "" { |
| 374 | t.Fatal("old repository's revert instruction moved", k.RevertOf, k.RevertSHA) |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | func TestPlacementQuotedOrWithdrawnRequestDoesNotConsent(t *testing.T) { |
| 379 | for _, text := range []string{"Create a new project is the label on that button", "Create a new project only if none fits", "Create a new project unless this belongs in Calendar", "Create a new project for this?", "Create a new project. Actually, do not create it."} { |
| 380 | if explicitNewProject(text) { |
| 381 | t.Errorf("not unconditional permission: %q", text) |
| 382 | } |
| 383 | } |
| 384 | r := placementRig(t) |
| 385 | for i, text := range []string{"Create a new project for the calendar", "Actually, keep it in the existing project."} { |
| 386 | if err := r.s.db.G.Create(&messageRecord{ID: fmt.Sprintf("m%d", i), Person: hkOwner, IdempotencyKey: fmt.Sprintf("withdrawn%d", i), TurnID: "t_withdrawn", Role: wire.RolePerson, Text: text}).Error; err != nil { |
| 387 | t.Fatal(err) |
| 388 | } |
| 389 | } |
| 390 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 391 | ok, err := tx.createConsent(turnRecord{ID: "t_withdrawn", Person: hkOwner}, "amadan.net/oficina/calendar") |
| 392 | if ok { |
| 393 | t.Error("earlier message overrode the latest correction") |
| 394 | } |
| 395 | return err |
| 396 | }); err != nil { |
| 397 | t.Fatal(err) |
| 398 | } |
| 399 | } |
| 400 | |