78 lines
3.5 KiB
Go
78 lines
3.5 KiB
Go
package agents
|
|
|
|
import (
|
|
"errors"
|
|
"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
|
|
want error
|
|
}{
|
|
{"option", question, api.InboxResponseCreate{Type: api.Option, Options: opts("blue")}, api.Answered, nil},
|
|
{"option with text", question, api.InboxResponseCreate{Type: api.Option, Options: opts("red"), Text: str("but darker")}, "", ErrInvalid},
|
|
{"unknown option", question, api.InboxResponseCreate{Type: api.Option, Options: opts("green")}, "", ErrInvalid},
|
|
{"missing option", question, api.InboxResponseCreate{Type: api.Option}, "", ErrInvalid},
|
|
{"no options", question, api.InboxResponseCreate{Type: api.Option, Options: opts()}, "", ErrInvalid},
|
|
{"two picks on pick-one", question, api.InboxResponseCreate{Type: api.Option, Options: opts("red", "blue")}, "", ErrInvalid},
|
|
{"checked", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("tests", "lint")}, api.Answered, nil},
|
|
{"checked one", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("docs")}, api.Answered, nil},
|
|
{"checked with text", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("tests", "lint"), Text: str("also run go vet")}, api.Answered, nil},
|
|
{"checked twice", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("docs", "docs")}, "", ErrInvalid},
|
|
{"checked unknown", checklist, api.InboxResponseCreate{Type: api.Option, Options: opts("docs", "deploy")}, "", ErrInvalid},
|
|
{"other", question, api.InboxResponseCreate{Type: api.Other, Text: str("green")}, api.Answered, nil},
|
|
{"empty other", question, api.InboxResponseCreate{Type: api.Other, Text: str(" ")}, "", ErrInvalid},
|
|
{"other on error", failure, api.InboxResponseCreate{Type: api.Other, Text: str("retry")}, api.Answered, nil},
|
|
{"other on report", report, api.InboxResponseCreate{Type: api.Other, Text: str("stop")}, "", ErrInvalid},
|
|
{"snooze", question, api.InboxResponseCreate{Type: "snooze"}, "", ErrInvalid},
|
|
{"dismiss report", report, api.InboxResponseCreate{Type: api.Dismiss}, api.Dismissed, nil},
|
|
{"dismiss question", question, api.InboxResponseCreate{Type: api.Dismiss}, "", ErrInvalid},
|
|
{"option on report", report, api.InboxResponseCreate{Type: api.Option, Options: opts("red")}, "", ErrInvalid},
|
|
{"already answered", closed, api.InboxResponseCreate{Type: api.Option, Options: opts("red")}, "", ErrConflict},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
state, err := CheckResponse(c.item, c.create)
|
|
if c.want != nil {
|
|
if !errors.Is(err, c.want) {
|
|
t.Fatalf("want %v, got %v", c.want, err)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("unexpected error %v", err)
|
|
}
|
|
if state != c.state {
|
|
t.Fatalf("want state %s, got %s", c.state, state)
|
|
}
|
|
})
|
|
}
|
|
}
|