package equipotest import ( "context" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "gorm.io/gorm" ) func TestIdlePollDoesNotWrite(t *testing.T) { f := runnerFixtureNew(t, false) before := scalar(t, f.d, "SELECT revision FROM service") ctx, cancel := context.WithTimeout(connectionContext, 350*time.Millisecond) defer cancel() _, _ = f.x.Poll(ctx, f.principal, f.a.ID) if after := scalar(t, f.d, "SELECT revision FROM service"); after != before { t.Fatalf("idle poll committed %d write transactions", after-before) } } func TestJourneySkipsOldFinishedBeforeLoading(t *testing.T) { f := runnerFixtureNew(t, false) mustSQL(t, f.d, "UPDATE jobs SET state='failed',updated_at=? WHERE id=?", f.clock.Now().AddDate(0, 0, -14).Format(time.RFC3339Nano), f.a.Scope.Job) mustSQL(t, f.d, "UPDATE task_outboxes SET engineering='not-json' WHERE task_id=?", f.a.TaskID) views, err := f.s.Journey(connectionContext, ownerActor) executionMust(t, err) if len(views) != 0 { t.Fatal("loaded old finished task", views) } } func TestWriteNotifiesOnlyTouchedJob(t *testing.T) { f := runnerFixtureNew(t, false) task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "second", "second") executionMust(t, err) task, err = f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f.connectionFixture)) executionMust(t, err) _, err = f.s.StartBug(connectionContext, f.principal, task.ID, task.Revision) executionMust(t, err) n := &taskNotices{} f.s.SetJobNotifier(n) view, err := f.s.TaskView(connectionContext, ownerActor, f.a.TaskID) executionMust(t, err) executionMust(t, f.x.Write(connectionContext, func(tx *gorm.DB) error { return tx.Model(&equipo.Task{}).Where("id=?", f.a.TaskID).Update("description", "changed").Error })) if len(n.topics) != 2 { t.Fatal("write notified unrelated jobs", n.topics) } found := false for _, topic := range n.topics { found = found || topic == view.Topic } if !found { t.Fatal("touched job not notified", n.topics) } } func TestRailCountsUseAggregates(t *testing.T) { f := runnerFixtureNew(t, true) mustSQL(t, f.d, "UPDATE task_outboxes SET engineering='not-json' WHERE task_id=?", f.a.TaskID) counts, err := f.s.SpecialistCounts(connectionContext, ownerActor) executionMust(t, err) if counts["engineering"].Working != 1 || counts["review"].Queued != 1 { t.Fatal("incorrect aggregate counts", counts) } } func TestRailCountsPreserveWeeklyAndUnknownRules(t *testing.T) { t.Run("unknown", func(t *testing.T) { f := bugFixture(t) bugTask(t, f) counts, err := f.s.SpecialistCounts(connectionContext, ownerActor) executionMust(t, err) if counts["engineering"].UsageKnown { t.Fatal("preparation usage reported known") } }) t.Run("previous week", func(t *testing.T) { f := runnerFixtureNew(t, true) mustSQL(t, f.d, "UPDATE steps SET state='succeeded' WHERE capability IN ('reproduce','implement')") mustSQL(t, f.d, "UPDATE attempts SET updated_at=?", f.clock.Now().AddDate(0, 0, -14).Format(time.RFC3339Nano)) counts, err := f.s.SpecialistCounts(connectionContext, ownerActor) executionMust(t, err) if counts["engineering"].Finished != 0 { t.Fatal("previous week's specialist work counted", counts) } }) }