package server import ( "net/http" "testing" "g.fc.run/misc/byre/internal/api" ) func TestCheckResponse(t *testing.T) { question := api.InboxItem{ Id: "q1", Kind: api.Question, State: api.Open, Payload: map[string]any{"options": []any{"red", "blue"}}, } checklist := api.InboxItem{ Id: "q2", Kind: api.Question, State: api.Open, Payload: map[string]any{"options": []any{"tests", "docs", "lint"}, "multiple": true}, } report := api.InboxItem{Id: "r1", Kind: api.Report, State: api.Open, Payload: map[string]any{}} failure := api.InboxItem{Id: "e1", Kind: api.SessionError, State: api.Open, Payload: map[string]any{}} closed := question closed.State = api.Answered str := func(s string) *string { return &s } opts := func(s ...string) *[]string { return &s } cases := []struct { name string item api.InboxItem create api.InboxResponseCreate state api.InboxState status int }{ {"option", question, api.InboxResponseCreate{Type: api.Option, Options: opts("blue")}, api.Answered, 0}, {"option with text", question, api.InboxResponseCreate{Type: api.Option, Options: opts("red"), Text: str("but darker")}, "", http.StatusBadRequest}, {"unknown option", question, api.InboxResponseCreate{Type: api.Option, Options: opts("green")}, "", http.StatusBadRequest}, {"missing option", question, api.InboxResponseCreate{Type: api.Option}, "", http.StatusBadRequest}, {"no options", question, api.InboxResponseCreate{Type: api.Option, Options: opts()}, "", http.StatusBadRequest}, {"two picks on pick-one", question, api.InboxResponseCreate{Type: api.Option, Options: opts("red", "blue")}, "", http.StatusBadRequest}, {"checked", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("tests", "lint")}, api.Answered, 0}, {"checked one", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("docs")}, api.Answered, 0}, {"checked with text", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("tests", "lint"), Text: str("also run go vet")}, api.Answered, 0}, {"checked twice", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("docs", "docs")}, "", http.StatusBadRequest}, {"checked unknown", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("docs", "deploy")}, "", http.StatusBadRequest}, {"other", question, api.InboxResponseCreate{Type: api.Other, Text: str("green")}, api.Answered, 0}, {"empty other", question, api.InboxResponseCreate{Type: api.Other, Text: str(" ")}, "", http.StatusBadRequest}, {"other on error", failure, api.InboxResponseCreate{Type: api.Other, Text: str("retry")}, api.Answered, 0}, {"other on report", report, api.InboxResponseCreate{Type: api.Other, Text: str("stop")}, "", http.StatusBadRequest}, {"snooze", question, api.InboxResponseCreate{Type: "snooze"}, "", http.StatusBadRequest}, {"dismiss report", report, api.InboxResponseCreate{Type: api.Dismiss}, api.Dismissed, 0}, {"dismiss question", question, api.InboxResponseCreate{Type: api.Dismiss}, "", http.StatusBadRequest}, {"option on report", report, api.InboxResponseCreate{Type: api.Option, Options: opts("red")}, "", http.StatusBadRequest}, {"already answered", closed, api.InboxResponseCreate{Type: api.Option, Options: opts("red")}, "", http.StatusConflict}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { state, herr := checkResponse(c.item, c.create) if c.status != 0 { if herr == nil || herr.status != c.status { t.Fatalf("want status %d, got %+v", c.status, herr) } return } if herr != nil { t.Fatalf("unexpected error %v", herr.err) } if state != c.state { t.Fatalf("want state %s, got %s", c.state, state) } }) } }