| 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 TestChangeFilesStayOutOfCoordinatorListsSnapshotAndEvents(t *testing.T) { |
| 13 | r := newMeteredRig(t) |
| 14 | ctx := context.Background() |
| 15 | must := func(err error) { |
| 16 | t.Helper() |
| 17 | if err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | } |
| 21 | must(r.s.db.G.Exec("CREATE TABLE runners (id text, person text, role text, label text, revoked_at datetime, removing_at datetime, tainted_at datetime)").Error) |
| 22 | k := taskRecord{ID: "tk_files_projection", ProjectID: "p_files", Creator: hkOwner, Stage: wire.StageReadyToMerge, Revision: 1} |
| 23 | must(r.s.db.G.Create(&projectRecord{ID: k.ProjectID, Title: "Files", TitleSet: true, Repo: "oficina/sample"}).Error) |
| 24 | must(r.s.db.G.Create(&k).Error) |
| 25 | for round := 0; round < 3; round++ { |
| 26 | sha := strings.Repeat(fmt.Sprint(round+1), 40) |
| 27 | facts := wire.ChangeFiles{Base: strings.Repeat("a", 40), SHA: sha, Files: 45, Additions: 45, Entries: []wire.ChangedFile{}} |
| 28 | for i := 0; i < 45; i++ { |
| 29 | facts.Entries = append(facts.Entries, wire.ChangedFile{Path: fmt.Sprintf("Sources/round%d/file%02d.swift", round, i), Status: wire.ChangeFileAdded, Additions: 1}) |
| 30 | } |
| 31 | raw, err := json.Marshal(facts) |
| 32 | must(err) |
| 33 | must(r.s.db.G.Create(&stepRecord{ID: fmt.Sprintf("eng%d", round), TaskID: k.ID, Idx: round * 2, Retired: round == 0, Specialist: wire.SpecialistEngineering, State: wire.StepDone, BaseSHA: facts.Base, CandidateSHA: sha, Pushed: true, ChangeFiles: string(raw)}).Error) |
| 34 | must(r.s.db.G.Create(&stepRecord{ID: fmt.Sprintf("review%d", round), TaskID: k.ID, Idx: round*2 + 1, Retired: round == 0, Specialist: wire.SpecialistReview, State: wire.StepDone, CandidateSHA: sha, Verdict: wire.VerdictApprove, Checks: wire.ChecksPassed}).Error) |
| 35 | } |
| 36 | noFacts := func(name string, v any) { |
| 37 | t.Helper() |
| 38 | raw, err := json.Marshal(v) |
| 39 | must(err) |
| 40 | if strings.Contains(string(raw), `"change_files"`) { |
| 41 | t.Errorf("%s leaked change_files", name) |
| 42 | } |
| 43 | } |
| 44 | task, err := r.s.Task(ctx, hkOwner, k.ID) |
| 45 | must(err) |
| 46 | noFacts("ordinary task", task) |
| 47 | detail, err := r.s.TaskDetail(ctx, hkOwner, k.ID) |
| 48 | must(err) |
| 49 | if len(detail.EarlierSteps) != 2 { |
| 50 | t.Fatal("fixture must include a retired round") |
| 51 | } |
| 52 | for _, step := range append(detail.Steps, detail.EarlierSteps...) { |
| 53 | if step.Specialist == wire.SpecialistEngineering && (step.ChangeFiles == nil || len(step.ChangeFiles.Entries) != 45) { |
| 54 | t.Fatal("detail lost facts") |
| 55 | } |
| 56 | } |
| 57 | list, err := r.s.Tasks(ctx, hkOwner) |
| 58 | must(err) |
| 59 | noFacts("list", list) |
| 60 | snapshot, err := r.s.Snapshot(ctx, hkOwner) |
| 61 | must(err) |
| 62 | noFacts("snapshot", snapshot) |
| 63 | must(r.s.tx(ctx, func(tx *txn) error { |
| 64 | result, err := tx.readTask(hkOwner, k.ID) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | var decoded wire.Task |
| 69 | if err := json.Unmarshal([]byte(result.Text), &decoded); err != nil { |
| 70 | t.Errorf("Coordinator task was clipped: %v", err) |
| 71 | } else if decoded.Verdict != wire.VerdictApprove || decoded.NeedsYou != wire.NeedsMerge { |
| 72 | t.Errorf("Coordinator lost current status: verdict=%q needs_you=%q", decoded.Verdict, decoded.NeedsYou) |
| 73 | } |
| 74 | if strings.Contains(result.Text, `"change_files"`) { |
| 75 | t.Error("Coordinator leaked change_files") |
| 76 | } |
| 77 | return tx.taskEvent(k) |
| 78 | })) |
| 79 | events, err := r.s.Events(ctx, hkOwner, 0, 100) |
| 80 | must(err) |
| 81 | noFacts("events", events) |
| 82 | if len(events) == 0 { |
| 83 | t.Fatal("fixture emitted no task event") |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestChangeFilesRejectInvalidBeforeStorage(t *testing.T) { |
| 88 | base, sha := strings.Repeat("a", 40), strings.Repeat("b", 40) |
| 89 | step := stepRecord{Specialist: wire.SpecialistEngineering, State: wire.StepDone, BaseSHA: base, CandidateSHA: sha} |
| 90 | for _, path := range []string{"/absolute", "a\\b", "a\nb", "a\u202eb", string([]byte{255})} { |
| 91 | facts := &wire.ChangeFiles{Base: base, SHA: sha, Files: 1, Entries: []wire.ChangedFile{{Path: path, Status: wire.ChangeFileAdded}}} |
| 92 | if stored := acceptChangeFiles(Lease{Protocol: 31}, wire.RolePrimary, step, facts); stored != "" { |
| 93 | t.Errorf("invalid path stored: %q", path) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |