311 lines
8.8 KiB
Go
311 lines
8.8 KiB
Go
package hooks
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/d1nch8g/jules/database"
|
|
"github.com/d1nch8g/jules/engine/actions"
|
|
"github.com/d1nch8g/jules/engine/user"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newTestUser() *user.User {
|
|
return &user.User{
|
|
User: &database.User{
|
|
ID: uuid.New(),
|
|
Timezone: "UTC",
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestCollect_NoTimezone(t *testing.T) {
|
|
u := newTestUser()
|
|
u.Timezone = ""
|
|
result := Collect(u, "", "hello")
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestCollect_AlreadyComplete(t *testing.T) {
|
|
u := newTestUser()
|
|
u.Facts = []database.Fact{{Value: complete}}
|
|
result := Collect(u, "", "hello")
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestCollect_HasSystemNotification(t *testing.T) {
|
|
u := newTestUser()
|
|
u.IncomingNotifications = []database.Notification{
|
|
{Content: "SYSTEM: some system notification"},
|
|
}
|
|
result := Collect(u, "", "hello")
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestCollect_HasSystemNotificationLowercase(t *testing.T) {
|
|
u := newTestUser()
|
|
u.IncomingNotifications = []database.Notification{
|
|
{Content: "system: some system notification"},
|
|
}
|
|
result := Collect(u, "", "hello")
|
|
assert.Nil(t, result)
|
|
}
|
|
|
|
func TestCollect_DefaultFirstRun(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "hello")
|
|
require.NotNil(t, result)
|
|
|
|
notif, ok := result.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Contains(t, notif.Content, "STAGE 1 TRIGGER")
|
|
}
|
|
|
|
func TestCollect_Stage1ToStage2(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "SYSTEM: STAGE 1 TRIGGER processed")
|
|
require.NotNil(t, result)
|
|
|
|
notif, ok := result.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Contains(t, notif.Content, "STAGE 2 TRIGGER")
|
|
}
|
|
|
|
func TestCollect_Stage2ToStage3(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "SYSTEM: STAGE 2 TRIGGER done")
|
|
require.NotNil(t, result)
|
|
|
|
notif, ok := result.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Contains(t, notif.Content, "STAGE 3 TRIGGER")
|
|
}
|
|
|
|
func TestCollect_Stage3ToStage4(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "SYSTEM: STAGE 3 TRIGGER now")
|
|
require.NotNil(t, result)
|
|
|
|
notif, ok := result.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Contains(t, notif.Content, "STAGE 4 TRIGGER")
|
|
}
|
|
|
|
func TestCollect_Stage4ToStage5(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "SYSTEM: STAGE 4 TRIGGER completed")
|
|
require.NotNil(t, result)
|
|
|
|
notif, ok := result.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Contains(t, notif.Content, "STAGE 5 TRIGGER")
|
|
}
|
|
|
|
func TestCollect_Stage5ToStage6(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "SYSTEM: STAGE 5 TRIGGER fired")
|
|
require.NotNil(t, result)
|
|
|
|
notif, ok := result.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Contains(t, notif.Content, "STAGE 6 TRIGGER")
|
|
}
|
|
|
|
func TestCollect_Stage6ToStage7(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "SYSTEM: STAGE 6 TRIGGER sent")
|
|
require.NotNil(t, result)
|
|
|
|
notif, ok := result.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Contains(t, notif.Content, "STAGE 7 TRIGGER")
|
|
}
|
|
|
|
func TestCollect_Stage7ToStage8(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "SYSTEM: STAGE 7 TRIGGER done")
|
|
require.NotNil(t, result)
|
|
|
|
notif, ok := result.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Contains(t, notif.Content, "STAGE 8 TRIGGER")
|
|
}
|
|
|
|
func TestCollect_Stage8Completes(t *testing.T) {
|
|
u := newTestUser()
|
|
result := Collect(u, "", "SYSTEM: STAGE 8 TRIGGER final")
|
|
|
|
fact, ok := result.(actions.AddFact)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_fact", fact.Type)
|
|
assert.Equal(t, complete, fact.Value)
|
|
}
|
|
|
|
func TestCollect_TimeFormats(t *testing.T) {
|
|
u := newTestUser()
|
|
u.Timezone = "Europe/Moscow"
|
|
|
|
result := Collect(u, "", "hello")
|
|
notif := result.(actions.AddNotification)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
|
|
stages := []string{
|
|
"SYSTEM: STAGE 1 TRIGGER", "SYSTEM: STAGE 2 TRIGGER", "SYSTEM: STAGE 3 TRIGGER",
|
|
"SYSTEM: STAGE 4 TRIGGER", "SYSTEM: STAGE 5 TRIGGER", "SYSTEM: STAGE 6 TRIGGER",
|
|
"SYSTEM: STAGE 7 TRIGGER",
|
|
}
|
|
for _, stage := range stages {
|
|
result := Collect(u, "", stage)
|
|
notif, ok := result.(actions.AddNotification)
|
|
if ok {
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time,
|
|
"invalid time format for %s", stage)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCollect_ContentNotLost(t *testing.T) {
|
|
u := newTestUser()
|
|
|
|
result := Collect(u, "", "hello")
|
|
notif := result.(actions.AddNotification)
|
|
assert.True(t, strings.Contains(notif.Content, "compliment someone they care about"))
|
|
assert.False(t, strings.Contains(notif.Content, "\n"))
|
|
}
|
|
|
|
func TestCollect_AllFieldsValid(t *testing.T) {
|
|
u := newTestUser()
|
|
|
|
tests := []struct {
|
|
name string
|
|
content string
|
|
check func(t *testing.T, a actions.Action)
|
|
}{
|
|
{
|
|
name: "default first run",
|
|
content: "hello",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
notif, ok := a.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Empty(t, notif.Target)
|
|
assert.NotEmpty(t, notif.Time)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
assert.Contains(t, notif.Content, "STAGE 1 TRIGGER")
|
|
},
|
|
},
|
|
{
|
|
name: "stage 1 to 2",
|
|
content: "SYSTEM: STAGE 1 TRIGGER done",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
notif, ok := a.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Empty(t, notif.Target)
|
|
assert.NotEmpty(t, notif.Time)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
assert.Contains(t, notif.Content, "STAGE 2 TRIGGER")
|
|
},
|
|
},
|
|
{
|
|
name: "stage 2 to 3",
|
|
content: "SYSTEM: STAGE 2 TRIGGER done",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
notif, ok := a.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Empty(t, notif.Target)
|
|
assert.NotEmpty(t, notif.Time)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
assert.Contains(t, notif.Content, "STAGE 3 TRIGGER")
|
|
},
|
|
},
|
|
{
|
|
name: "stage 3 to 4",
|
|
content: "SYSTEM: STAGE 3 TRIGGER done",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
notif, ok := a.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Empty(t, notif.Target)
|
|
assert.NotEmpty(t, notif.Time)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
assert.Contains(t, notif.Content, "STAGE 4 TRIGGER")
|
|
},
|
|
},
|
|
{
|
|
name: "stage 4 to 5",
|
|
content: "SYSTEM: STAGE 4 TRIGGER done",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
notif, ok := a.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Empty(t, notif.Target)
|
|
assert.NotEmpty(t, notif.Time)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
assert.Contains(t, notif.Content, "STAGE 5 TRIGGER")
|
|
},
|
|
},
|
|
{
|
|
name: "stage 5 to 6",
|
|
content: "SYSTEM: STAGE 5 TRIGGER done",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
notif, ok := a.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Empty(t, notif.Target)
|
|
assert.NotEmpty(t, notif.Time)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
assert.Contains(t, notif.Content, "STAGE 6 TRIGGER")
|
|
},
|
|
},
|
|
{
|
|
name: "stage 6 to 7",
|
|
content: "SYSTEM: STAGE 6 TRIGGER done",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
notif, ok := a.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Empty(t, notif.Target)
|
|
assert.NotEmpty(t, notif.Time)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
assert.Contains(t, notif.Content, "STAGE 7 TRIGGER")
|
|
},
|
|
},
|
|
{
|
|
name: "stage 7 to 8",
|
|
content: "SYSTEM: STAGE 7 TRIGGER done",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
notif, ok := a.(actions.AddNotification)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_notification", notif.Type)
|
|
assert.Empty(t, notif.Target)
|
|
assert.NotEmpty(t, notif.Time)
|
|
assert.Regexp(t, `^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$`, notif.Time)
|
|
assert.Contains(t, notif.Content, "STAGE 8 TRIGGER")
|
|
},
|
|
},
|
|
{
|
|
name: "stage 8 completes",
|
|
content: "SYSTEM: STAGE 8 TRIGGER done",
|
|
check: func(t *testing.T, a actions.Action) {
|
|
fact, ok := a.(actions.AddFact)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "add_fact", fact.Type)
|
|
assert.Equal(t, complete, fact.Value)
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := Collect(u, "", tt.content)
|
|
require.NotNil(t, result)
|
|
tt.check(t, result)
|
|
})
|
|
}
|
|
}
|