diff --git a/constitutional.go b/constitutional.go index c16c18f..4d77fd3 100644 --- a/constitutional.go +++ b/constitutional.go @@ -116,11 +116,13 @@ func EvaluatePolls() { quorum := CalculateQuorum(*poll) notVoted := make([]*OIDCUser, 0) + notVotedString := make([]string, 0) votedCount := 0 // check all voters to see if they have voted if poll.AllowedUsers == nil { logging.Logger.WithFields(logrus.Fields{"method": "EvaluatePolls checkQuorum"}).Error( - "Users allowed to vote is nil for \"" + poll.Title + "\" !! This should not happen!!") + "Users allowed to vote is nil for \"" + poll.Title + "\" !! This should not happen!!", + ) continue } for _, user := range poll.AllowedUsers { @@ -144,12 +146,29 @@ func EvaluatePolls() { slack.MsgOptionText( "Hello, you have not yet voted on \""+poll.Title+"\". We have not yet hit quorum"+ " and we need YOU :index_pointing_at_the_viewer: to complete your responsibility as a "+ - "member of house and vote. \n"+pollLink+"\nThank you!", false)) + "member of house and vote. \n"+pollLink+"\nThank you!", false, + )) + if user.SlackUID != "" { + notVotedString = append(notVotedString, "<@"+user.SlackUID+">") + } else { + notVotedString = append(notVotedString, user.Username) + } if err != nil { logging.Logger.WithFields(logrus.Fields{"method": "EvaluatePolls dm"}).Error(err) continue } } + // print non-voters to #announcements if we can't close the vote + if !poll.OpenedTime.AddDate(0, 0, 2).After(now) { + _, _, err = slackData.Client.PostMessage(slackData.AnnouncementsChannel, + slack.MsgOptionText("It has been past the default time required to vote on \""+poll.Title+ + "\" These people have not completed their responsibility as a Voting Member of House. \n"+ + strings.Join(notVotedString, ", "), false)) + if err != nil { + logging.Logger.WithFields(logrus.Fields{"method": "EvaluatePolls notVoted announce"}).Error(err) + continue + } + } continue } // close poll after 48 hours diff --git a/database/poll.go b/database/poll.go index 77b9f2b..dcf0c39 100644 --- a/database/poll.go +++ b/database/poll.go @@ -5,6 +5,7 @@ import ( "math" "slices" "sort" + "strings" "time" "github.com/sirupsen/logrus" @@ -236,7 +237,13 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st // Eliminate lowest vote getter minVote := math.MaxInt //the smallest number of votes received thus far (to find who is in last) minPerson := make([]string, 0) //the person(s) with the least votes that need removed + eliminable := 0 //the number of remaining candidates that are allowed to be eliminated for person, vote := range tallied { + // No Confidence can never be eliminated, skip it and move on to the next lowest + if isNoConfidence(person) { + continue + } + eliminable++ if vote < minVote { // this should always be true round one, to set a true "who is in last" minVote = vote minPerson = make([]string, 0) @@ -245,6 +252,18 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st minPerson = append(minPerson, person) } } + // If every remaining eliminable option is tied for last, eliminating them + // all would hand the vote to No Confidence. That's only fair if No + // Confidence had more votes than them (in which case they would have all + // been eliminated anyway), otherwise this is an unresolvable tie. + noConfidenceTie := false + if eliminable > 0 && len(minPerson) == eliminable { + for person, vote := range tallied { + if isNoConfidence(person) && vote <= minVote { + noConfidenceTie = true + } + } + } eliminated = append(eliminated, minPerson...) finalResult = append(finalResult, tallied) @@ -270,7 +289,7 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st end = false } } - if end { + if end || noConfidenceTie { break } } @@ -278,6 +297,12 @@ func calculateRankedResult(ctx context.Context, votesRaw []RankedVote) ([]map[st } +// isNoConfidence reports whether an option is the "No Confidence" option, +// which is never allowed to be eliminated from a ranked choice vote +func isNoConfidence(option string) bool { + return strings.EqualFold(strings.TrimSpace(option), "no confidence") +} + func (poll *Poll) GetResult(ctx context.Context) ([]map[string]int, error) { ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() diff --git a/database/poll_test.go b/database/poll_test.go index 9787399..bb6a4df 100644 --- a/database/poll_test.go +++ b/database/poll_test.go @@ -223,3 +223,166 @@ func TestOrderOptions(t *testing.T) { }) } } + +// ballot builds a RankedVote from options listed in order of preference +func ballot(options ...string) RankedVote { + ranks := make(map[string]int, len(options)) + for i, option := range options { + ranks[option] = i + 1 + } + return RankedVote{Options: ranks} +} + +// ballots repeats the same ballot count times +func ballots(count int, options ...string) []RankedVote { + votes := make([]RankedVote, 0, count) + for i := 0; i < count; i++ { + votes = append(votes, ballot(options...)) + } + return votes +} + +func concatVotes(groups ...[]RankedVote) []RankedVote { + votes := make([]RankedVote, 0) + for _, group := range groups { + votes = append(votes, group...) + } + return votes +} + +func TestNoConfidenceNeverEliminated(t *testing.T) { + tests := []struct { + name string + votes []RankedVote + results []map[string]int + }{ + { + name: "Lowest is No Confidence, next lowest eliminated instead", + votes: concatVotes( + ballots(3, "a"), + ballots(2, "b"), + ballots(1, "c", "a"), + ballots(1, "No Confidence", "b"), + ), + results: []map[string]int{ + {"a": 3, "b": 2, "c": 1, "No Confidence": 1}, + {"a": 4, "b": 2, "No Confidence": 1}, + {"a": 4}, + }, + }, + { + name: "No Confidence survives multiple rounds in last place", + votes: concatVotes( + ballots(2, "a"), + ballots(2, "b"), + ballots(1, "c", "a"), + ballots(1, "No Confidence"), + ), + results: []map[string]int{ + {"a": 2, "b": 2, "c": 1, "No Confidence": 1}, + {"a": 3, "b": 2, "No Confidence": 1}, + {"a": 3, "No Confidence": 1}, + {"a": 3}, + }, + }, + { + name: "No Confidence wins after other options are eliminated", + votes: concatVotes( + ballots(2, "No Confidence"), + ballots(2, "a", "No Confidence"), + ballots(1, "b", "No Confidence"), + ), + results: []map[string]int{ + {"No Confidence": 2, "a": 2, "b": 1}, + {"No Confidence": 3, "a": 2}, + {"No Confidence": 3}, + }, + }, + { + name: "Remaining options tied above No Confidence is a tie, not a No Confidence win", + votes: concatVotes( + ballots(2, "a"), + ballots(2, "b"), + ballots(1, "No Confidence"), + ), + results: []map[string]int{ + {"a": 2, "b": 2, "No Confidence": 1}, + }, + }, + { + name: "Remaining options tied below No Confidence are all eliminated", + votes: concatVotes( + ballots(3, "No Confidence"), + ballots(2, "a"), + ballots(2, "b"), + ), + results: []map[string]int{ + {"No Confidence": 3, "a": 2, "b": 2}, + {"No Confidence": 3}, + }, + }, + { + name: "No Confidence tied with only remaining option is a tie", + votes: concatVotes( + ballots(2, "No Confidence"), + ballots(2, "a"), + ), + results: []map[string]int{ + {"No Confidence": 2, "a": 2}, + }, + }, + { + name: "Only No Confidence", + votes: ballots(1, "No Confidence"), + results: []map[string]int{ + {"No Confidence": 1}, + }, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + results, err := calculateRankedResult(context.Background(), test.votes) + assert.NoError(t, err) + assert.Equal(t, test.results, results) + }) + } +} + +func TestNoConfidenceCaseInsensitive(t *testing.T) { + for _, nc := range []string{"No Confidence", "no confidence", "NO CONFIDENCE", "nO cOnFiDeNcE", " No Confidence "} { + t.Run(nc, func(t *testing.T) { + votes := concatVotes( + ballots(3, "a"), + ballots(2, "b"), + ballots(1, "c", "a"), + ballots(1, nc, "b"), + ) + results, err := calculateRankedResult(context.Background(), votes) + assert.NoError(t, err) + assert.Equal(t, []map[string]int{ + {"a": 3, "b": 2, "c": 1, nc: 1}, + {"a": 4, "b": 2, nc: 1}, + {"a": 4}, + }, results) + }) + } +} + +func TestIsNoConfidence(t *testing.T) { + tests := map[string]bool{ + "No Confidence": true, + "no confidence": true, + "NO CONFIDENCE": true, + " no confidence ": true, + "No-Confidence": false, + "NoConfidence": false, + "Confidence": false, + "No Confidence 2": false, + "": false, + } + for input, expected := range tests { + t.Run(input, func(t *testing.T) { + assert.Equal(t, expected, isNoConfidence(input)) + }) + } +} diff --git a/templates/result.tmpl b/templates/result.tmpl index e293bd2..64209d0 100644 --- a/templates/result.tmpl +++ b/templates/result.tmpl @@ -34,7 +34,9 @@ {{/* Displays information about required quorum and number of voters */}}