gte/internal/task/task_test.go

346 lines
5.6 KiB
Go
Raw Normal View History

2021-01-29 17:22:07 +01:00
package task_test
import (
"fmt"
"testing"
"git.sr.ht/~ewintr/go-kit/test"
"git.sr.ht/~ewintr/gte/internal/task"
"git.sr.ht/~ewintr/gte/pkg/mstore"
)
func TestNewFromMessage(t *testing.T) {
id := "an id"
action := "some action"
2021-01-29 19:40:46 +01:00
project := "project"
2021-01-29 17:22:07 +01:00
folder := task.FOLDER_NEW
2021-01-29 19:40:46 +01:00
2021-01-29 17:22:07 +01:00
for _, tc := range []struct {
name string
message *mstore.Message
hasId bool
exp *task.Task
}{
{
name: "empty",
message: &mstore.Message{},
exp: &task.Task{
Dirty: true,
},
},
{
2021-01-29 19:40:46 +01:00
name: "id, action, project and folder",
2021-01-29 17:22:07 +01:00
message: &mstore.Message{
Folder: folder,
Body: fmt.Sprintf(`
id: %s
action: %s
2021-01-29 19:40:46 +01:00
project: %s
`, id, action, project),
2021-01-29 17:22:07 +01:00
},
hasId: true,
exp: &task.Task{
2021-01-29 19:40:46 +01:00
Id: id,
Folder: folder,
Action: action,
Project: project,
2021-01-29 17:22:07 +01:00
},
},
{
name: "folder inbox get updated to new",
message: &mstore.Message{
Folder: task.FOLDER_INBOX,
Body: fmt.Sprintf(`
id: %s
action: %s
`, id, action),
},
hasId: true,
exp: &task.Task{
Id: id,
Folder: task.FOLDER_NEW,
Action: action,
Dirty: true,
},
},
{
name: "action in subject takes precedence",
message: &mstore.Message{
Folder: folder,
Subject: "some other action",
Body: fmt.Sprintf(`
id: %s
action: %s
`, id, action),
},
2021-01-29 17:48:22 +01:00
hasId: true,
2021-01-29 17:22:07 +01:00
exp: &task.Task{
Id: id,
Folder: folder,
Action: action,
},
},
{
name: "action from subject if not present in body",
message: &mstore.Message{
Folder: folder,
Subject: action,
Body: fmt.Sprintf(`id: %s`, id),
},
2021-01-29 17:48:22 +01:00
hasId: true,
exp: &task.Task{
Id: id,
Folder: folder,
Action: action,
Dirty: true,
},
},
{
name: "quoted fields",
message: &mstore.Message{
Folder: folder,
Body: fmt.Sprintf(`
action: %s
Forwarded message:
> id: %s
> action: old action
`, action, id),
},
hasId: true,
2021-01-29 17:22:07 +01:00
exp: &task.Task{
Id: id,
Folder: folder,
Action: action,
Dirty: true,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
act := task.New(tc.message)
if !tc.hasId {
test.Equals(t, false, "" == act.Id)
tc.exp.Id = act.Id
}
tc.exp.Message = tc.message
tc.exp.Current = true
test.Equals(t, tc.exp, act)
})
}
}
func TestFormatSubject(t *testing.T) {
action := "an action"
2021-01-29 19:40:46 +01:00
project := " a project"
2021-01-29 17:22:07 +01:00
for _, tc := range []struct {
name string
task *task.Task
exp string
}{
{
name: "empty",
task: &task.Task{},
},
{
2021-01-29 19:40:46 +01:00
name: "action",
2021-01-29 17:22:07 +01:00
task: &task.Task{Action: action},
exp: action,
},
2021-01-29 19:40:46 +01:00
{
name: "project",
task: &task.Task{Project: project},
exp: project,
},
{
name: "action and project",
task: &task.Task{Action: action, Project: project},
exp: fmt.Sprintf("%s - %s", project, action),
},
2021-01-29 17:22:07 +01:00
} {
t.Run(tc.name, func(t *testing.T) {
test.Equals(t, tc.exp, tc.task.FormatSubject())
})
}
}
func TestFormatBody(t *testing.T) {
id := "an id"
action := "an action"
2021-01-29 19:40:46 +01:00
project := "project"
2021-01-29 17:22:07 +01:00
for _, tc := range []struct {
name string
task *task.Task
exp string
}{
{
name: "empty",
task: &task.Task{},
exp: `
id:
2021-01-29 19:40:46 +01:00
project:
2021-01-29 17:22:07 +01:00
action:
`,
},
{
name: "filled",
task: &task.Task{
2021-01-29 19:40:46 +01:00
Id: id,
Action: action,
Project: project,
2021-01-29 18:10:06 +01:00
Message: &mstore.Message{
Body: "previous body",
},
2021-01-29 17:22:07 +01:00
},
exp: `
2021-01-29 19:40:46 +01:00
id: an id
project: project
action: an action
2021-01-29 18:10:06 +01:00
Previous version:
previous body
2021-01-29 17:22:07 +01:00
`,
},
} {
t.Run(tc.name, func(t *testing.T) {
test.Equals(t, tc.exp, tc.task.FormatBody())
})
}
}
func TestFieldFromBody(t *testing.T) {
for _, tc := range []struct {
2021-01-29 17:48:22 +01:00
name string
field string
body string
expValue string
expDirty bool
2021-01-29 17:22:07 +01:00
}{
{
name: "empty field",
body: `field: value`,
},
{
name: "empty body",
field: "field",
},
{
name: "not present",
field: "field",
body: "another: value",
},
{
name: "present",
field: "fieldb",
body: `
not a field at all
fielda: valuea
fieldb: valueb
fieldc: valuec
`,
2021-01-29 17:48:22 +01:00
expValue: "valueb",
2021-01-29 17:22:07 +01:00
},
{
name: "present twice",
field: "field",
body: `
field: valuea
field: valueb
`,
2021-01-29 17:48:22 +01:00
expValue: "valuea",
expDirty: true,
2021-01-29 17:22:07 +01:00
},
{
2021-01-29 19:40:46 +01:00
name: "colons",
2021-01-29 17:48:22 +01:00
field: "field",
body: "field:: val:ue",
expValue: ": val:ue",
2021-01-29 17:22:07 +01:00
},
{
name: "trim field",
field: "field",
body: " field : value",
2021-01-29 17:48:22 +01:00
expValue: "value",
2021-01-29 17:22:07 +01:00
},
{
name: "trim value",
field: "field",
body: "field: value ",
2021-01-29 17:48:22 +01:00
expValue: "value",
},
{
name: "quoted",
field: "field",
body: "> field: value",
expValue: "value",
2021-01-29 17:22:07 +01:00
},
2021-01-29 18:10:06 +01:00
{
name: "previous body",
field: "field",
body: `
field: valuea
Previous version:
field: valueb
`,
expValue: "valuea",
},
{
name: "quoted previous body",
field: "field",
body: `
field: valuea
> Previous version:
>
> field: valueb
`,
expValue: "valuea",
},
2021-01-29 17:22:07 +01:00
} {
t.Run(tc.name, func(t *testing.T) {
2021-01-29 17:48:22 +01:00
actValue, actDirty := task.FieldFromBody(tc.field, tc.body)
test.Equals(t, tc.expValue, actValue)
test.Equals(t, tc.expDirty, actDirty)
2021-01-29 17:22:07 +01:00
})
}
}
func TestFieldFromSubject(t *testing.T) {
for _, tc := range []struct {
name string
field string
subject string
exp string
}{
{
name: "empty field",
subject: "subject",
},
{
name: "empty subject",
field: task.FIELD_ACTION,
},
{
name: "unknown field",
field: "unknown",
subject: "subject",
},
{
name: "known field",
field: task.FIELD_ACTION,
subject: "subject",
exp: "subject",
},
} {
t.Run(tc.name, func(t *testing.T) {
test.Equals(t, tc.exp, task.FieldFromSubject(tc.field, tc.subject))
})
}
}