| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/wire" |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | "gorm.io/gorm" |
| 16 | ) |
| 17 | |
| 18 | // Checks need a Mac when any Engineering round of the task touched Apple |
| 19 | // files, not only the round whose commit is the candidate: a delegated |
| 20 | // Copy commit, or a later fix round that touched only the README, makes |
| 21 | // a new candidate that still holds the Swift changes (Astra, |
| 22 | // mac-placement-wait). |
| 23 | func TestChecksNeedAMacForEveryAppleRound(t *testing.T) { |
| 24 | src, err := hkTemplatePath() |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | raw, err := os.ReadFile(src) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | path := filepath.Join(t.TempDir(), "macneeds.db") |
| 33 | if err := os.WriteFile(path, raw, 0o600); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | t.Cleanup(func() { _ = d.Close() }) |
| 41 | s := NewStore(d, Authority{Copy: func(key string) string { return key }, PrimaryRunner: func(*gorm.DB, string) (string, error) { return "", nil }}, nil) |
| 42 | const person = "owner@example.com" |
| 43 | now := time.Now().UTC() |
| 44 | sha := func(c string) string { return strings.Repeat(c, 40) } |
| 45 | if err := d.G.Create(&projectRecord{ID: "p1", Repo: "oficina/sample"}).Error; err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | for _, k := range []taskRecord{{ID: "tk1", ProjectID: "p1", Creator: person, Stage: wire.StageInProgress, Revision: 1, CreatedAt: now, UpdatedAt: now}, {ID: "tk2", ProjectID: "p1", Creator: person, Stage: wire.StageInProgress, Revision: 1, CreatedAt: now, UpdatedAt: now}, {ID: "tk3", ProjectID: "p1", Creator: person, Stage: wire.StageInProgress, Revision: 1, CreatedAt: now, UpdatedAt: now}} { |
| 49 | if err := d.G.Create(&k).Error; err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | } |
| 53 | for _, st := range []stepRecord{ |
| 54 | {ID: "e1", TaskID: "tk1", Idx: 1, Specialist: wire.SpecialistEngineering, Person: person, State: wire.StepDone, Round: 1, CandidateSHA: sha("a"), Apple: true}, |
| 55 | {ID: "c1", TaskID: "tk1", Idx: 2, Specialist: wire.SpecialistCopy, Person: person, State: wire.StepDone, Round: 1, CandidateSHA: sha("b")}, |
| 56 | {ID: "e2", TaskID: "tk1", Idx: 3, Specialist: wire.SpecialistEngineering, Person: person, State: wire.StepDone, Round: 2, CandidateSHA: sha("c")}, |
| 57 | {ID: "e3", TaskID: "tk2", Idx: 1, Specialist: wire.SpecialistEngineering, Person: person, State: wire.StepDone, Round: 1, CandidateSHA: sha("d")}, |
| 58 | // Moved to another project: the Swift change was retired with |
| 59 | // the old project's steps, and the new branch touched README only. |
| 60 | {ID: "e4", TaskID: "tk3", Idx: 1, Specialist: wire.SpecialistEngineering, Person: person, State: wire.StepDone, Round: 1, CandidateSHA: sha("e"), Apple: true, Retired: true}, |
| 61 | {ID: "e5", TaskID: "tk3", Idx: 2, Specialist: wire.SpecialistEngineering, Person: person, State: wire.StepDone, Round: 1, CandidateSHA: sha("f")}, |
| 62 | } { |
| 63 | if err := d.G.Create(&st).Error; err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | } |
| 67 | for _, c := range []struct { |
| 68 | task, candidate string |
| 69 | mac bool |
| 70 | }{{"tk1", sha("a"), true}, {"tk1", sha("b"), true}, {"tk1", sha("c"), true}, {"tk2", sha("d"), false}, {"tk3", sha("f"), false}} { |
| 71 | var needs []string |
| 72 | err := s.tx(context.Background(), func(t *txn) error { |
| 73 | k, err := t.loadTask(c.task) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | needs, err = t.checksNeeds(k, stepRecord{TaskID: c.task, Specialist: wire.SpecialistChecks, Person: person, CandidateSHA: c.candidate}) |
| 78 | return err |
| 79 | }) |
| 80 | if err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | if got := strings.Join(needs, ","); (got == "macos,xcode") != c.mac { |
| 84 | t.Errorf("%s at %s: needs %q", c.task, c.candidate[:1], got) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |