From c600a3d105ecdc7221180d46de1b18d646aa1d1d Mon Sep 17 00:00:00 2001 From: ikopke23 Date: Mon, 23 Nov 2026 20:09:11 -0500 Subject: [PATCH 1/2] proper custom options ui --- api.go | 45 +++++++++++-- api_test.go | 103 +++++++++++++++++++++++++++++ templates/create.tmpl | 149 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 278 insertions(+), 19 deletions(-) create mode 100644 api_test.go diff --git a/api.go b/api.go index 8fe6c7c..44c3ae2 100644 --- a/api.go +++ b/api.go @@ -3,6 +3,7 @@ package main import ( "context" "encoding/json" + "errors" "fmt" "math" "net/http" @@ -21,6 +22,8 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +const MAX_CUSTOM_OPTIONS = 50 + // Gets the number of people eligible to vote in a poll func GetVoterCount(poll database.Poll) int { return len(poll.AllowedUsers) @@ -181,6 +184,10 @@ func CreatePoll(c *gin.Context) { if c.PostForm("rankedChoice") == "true" { poll.VoteType = database.POLL_TYPE_RANKED } + if poll.VoteType == database.POLL_TYPE_RANKED && c.PostForm("options") != "custom" { + c.JSON(http.StatusBadRequest, gin.H{"error": "ranked choice polls must use custom options"}) + return + } switch c.PostForm("options") { case "pass-fail-conditional": @@ -188,12 +195,10 @@ func CreatePoll(c *gin.Context) { case "fail-conditional": poll.Options = []string{"Fail", "Conditional", "Abstain"} case "custom": - poll.Options = []string{} - for opt := range strings.SplitSeq(c.PostForm("customOptions"), ",") { - poll.Options = append(poll.Options, strings.TrimSpace(opt)) - if !slices.Contains(poll.Options, "Abstain") && (poll.VoteType == database.POLL_TYPE_SIMPLE) { - poll.Options = append(poll.Options, "Abstain") - } + poll.Options, err = parseCustomOptions(c.PostFormArray("customOptions"), poll.VoteType == database.POLL_TYPE_RANKED) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return } case "pass-fail": default: @@ -613,6 +618,34 @@ func containsPoll(polls []*database.Poll, poll *database.Poll) bool { return false } +// Trims and validates custom poll options, appending Abstain for simple polls +func parseCustomOptions(raw []string, ranked bool) ([]string, error) { + options := []string{} + seen := make(map[string]bool) + for _, opt := range raw { + opt = strings.TrimSpace(opt) + if opt == "" { + continue + } + key := strings.ToLower(opt) + if seen[key] { + return nil, fmt.Errorf("duplicate option: %s", opt) + } + seen[key] = true + options = append(options, opt) + } + if len(options) == 0 { + return nil, errors.New("at least one option is required") + } + if len(options) > MAX_CUSTOM_OPTIONS { + return nil, fmt.Errorf("a poll may have at most %d options", MAX_CUSTOM_OPTIONS) + } + if !ranked && !seen["abstain"] { + options = append(options, "Abstain") + } + return options, nil +} + func hasOption(poll *database.Poll, option string) bool { for _, opt := range poll.Options { if opt == option { diff --git a/api_test.go b/api_test.go new file mode 100644 index 0000000..7770088 --- /dev/null +++ b/api_test.go @@ -0,0 +1,103 @@ +package main + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func makeOptions(n int) []string { + options := make([]string, n) + for i := range options { + options[i] = fmt.Sprintf("Option %d", i+1) + } + return options +} + +func TestParseCustomOptions(t *testing.T) { + tests := []struct { + name string + raw []string + ranked bool + want []string + wantErr bool + }{ + { + name: "trims and keeps order", + raw: []string{" A ", "B"}, + want: []string{"A", "B", "Abstain"}, + }, + { + name: "commas kept", + raw: []string{"Smith, John", "Doe, Jane"}, + want: []string{"Smith, John", "Doe, Jane", "Abstain"}, + }, + { + name: "blanks dropped", + raw: []string{"A", "", " ", "B"}, + want: []string{"A", "B", "Abstain"}, + }, + { + name: "user abstain kept without a second one", + raw: []string{"Yes", "abstain"}, + want: []string{"Yes", "abstain"}, + }, + { + name: "ranked gets no abstain", + raw: []string{"A", "B", "C"}, + ranked: true, + want: []string{"A", "B", "C"}, + }, + { + name: "single option", + raw: []string{"Only"}, + ranked: true, + want: []string{"Only"}, + }, + { + name: "no options", + raw: []string{}, + wantErr: true, + }, + { + name: "only blank options", + raw: []string{"", " "}, + ranked: true, + wantErr: true, + }, + { + name: "case insensitive duplicate", + raw: []string{"Alice", " alice"}, + wantErr: true, + }, + { + name: "50 options allowed", + raw: makeOptions(50), + ranked: true, + want: makeOptions(50), + }, + { + name: "51 options rejected", + raw: makeOptions(51), + ranked: true, + wantErr: true, + }, + { + name: "limit applies before abstain", + raw: makeOptions(50), + want: append(makeOptions(50), "Abstain"), + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := parseCustomOptions(test.raw, test.ranked) + if test.wantErr { + assert.Error(t, err) + return + } + assert.NoError(t, err) + assert.Equal(t, test.want, got) + }) + } +} diff --git a/templates/create.tmpl b/templates/create.tmpl index 84e823e..e38fcc7 100644 --- a/templates/create.tmpl +++ b/templates/create.tmpl @@ -2,7 +2,7 @@

Create Poll

-
+
-
-
- - +
+ +
+
+ + +
+
+
+ + +
+
+
+ + +
Maximum of 50 options
Additional Settings
@@ -60,7 +82,9 @@ class="form-check-input" type="checkbox" name="rankedChoice" + id="rankedChoice" value="true" + onchange="onRankedChange()" >
@@ -133,13 +157,112 @@ }