Compare commits

..

2 Commits

@ -22,10 +22,10 @@ func (s *Store) AddAbsence(ctx context.Context, session time.Time, userId string
))
defer span.End()
if session.Weekday() != time.Monday {
log.Debug().Ctx(ctx).Msg(fmt.Sprintf("%s is not a monday", session))
span.RecordError(fmt.Errorf("%s is not a monday", session))
return fmt.Errorf("not a monday")
if session.Weekday() != time.Wednesday {
log.Debug().Ctx(ctx).Msg(fmt.Sprintf("%s is not a wednesday", session))
span.RecordError(fmt.Errorf("%s is not a wednesday", session))
return fmt.Errorf("not a wednesday")
}
defer s.Audit(ctx, &AuditableEvent{

@ -18,7 +18,7 @@ func TestAddAbsence(t *testing.T) {
store, err := NewStore(db, zerolog.Nop())
require.NoError(t, err)
now := NextWednesday()
now := NextWednesday(nil)
assert.NoError(t, store.AddAbsence(context.TODO(), now, "foobarbaz"))
absentees, err := store.GetAbsentees(context.TODO(), now)
assert.NoError(t, err)
@ -40,7 +40,7 @@ func TestGetSchedule(t *testing.T) {
store, err := NewStore(db, zerolog.Nop())
require.NoError(t, err)
now := NextWednesday()
now := NextWednesday(nil)
_ = store.AddAbsence(context.TODO(), now.Add(7*24*time.Hour), "foobar")

@ -580,8 +580,8 @@ func main() {
return nil
},
"schedule": func(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) error {
// get schedule from now to 4 mondays into the future
sched, err := store.GetSchedule(ctx, themis.NextWednesday(), themis.NextWednesday().Add(4*7*24*time.Hour))
// get schedule from now to 4 wednesdays into the future
sched, err := store.GetSchedule(ctx, themis.NextWednesday(nil), themis.NextWednesday(nil).Add(4*7*24*time.Hour))
if err != nil {
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
@ -643,7 +643,7 @@ func main() {
"absent": func(ctx context.Context, s *discordgo.Session, i *discordgo.InteractionCreate) error {
var rawDate string
if len(i.ApplicationCommandData().Options) == 0 {
rawDate = themis.NextWednesday().Format(time.DateOnly)
rawDate = themis.NextWednesday(nil).Format(time.DateOnly)
} else {
rawDate = i.ApplicationCommandData().Options[0].StringValue()
}
@ -675,17 +675,17 @@ func main() {
return nil
}
if date.Weekday() != time.Monday {
if date.Weekday() != time.Wednesday {
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: "The date you provided is not a Monday.",
Content: "The date you provided is not a Wednesday.",
},
}); err != nil {
return fmt.Errorf("failed to respond to interaction: %w", err)
}
// TODO(wperron) suggest Mondays before and after?
// TODO(wperron) suggest Wednesdays before and after?
return nil
}
@ -739,7 +739,7 @@ func main() {
defer span.End()
log.Info().Msg("sending weekly reminder")
absentees, err := store.GetAbsentees(ctx, themis.NextWednesday())
absentees, err := store.GetAbsentees(ctx, themis.NextWednesday(nil))
if err != nil {
log.Error().Err(err).Msg("failed to get absentees for next session")
return
@ -753,7 +753,7 @@ func main() {
var msg string
var components []discordgo.MessageComponent
if len(absentees) == 0 {
msg = "Everybody can make it next Monday, see you then! 🎉"
msg = "Everybody can make it next Wednesday, see you then! 🎉"
components = []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
@ -770,7 +770,7 @@ func main() {
},
}
} else {
msg = fmt.Sprintf("%s can't make it next Monday. 🙁", themis.FormatStringSlice(absentees))
msg = fmt.Sprintf("%s can't make it next Wednesday. 🙁", themis.FormatStringSlice(absentees))
}
_, err = discord.ChannelMessageSendComplex(channelId, &discordgo.MessageSend{
@ -884,7 +884,7 @@ func registerHandlers(sess *discordgo.Session, handlers map[string]Handler) {
userId := i.Member.User.ID
log.Info().Ctx(ctx).Str("message_component", "schedule-response").Str("userid", userId).Msg("handling message component interaction")
if err := store.AddAbsence(ctx, themis.NextWednesday(), userId); err != nil {
if err := store.AddAbsence(ctx, themis.NextWednesday(nil), userId); err != nil {
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{

@ -26,7 +26,7 @@ func NewNotifier(c chan context.Context) *Notifier {
}
func (n *Notifier) Start(ctx context.Context) {
m := NextWednesday()
m := NextWednesday(nil)
sat := m.AddDate(0, 0, -4)
if sat.Before(time.Now()) {
sat = sat.AddDate(0, 0, 7)
@ -34,7 +34,7 @@ func (n *Notifier) Start(ctx context.Context) {
t, err := time.ParseInLocation(time.DateTime, fmt.Sprintf("%s 17:00:00", sat.Format(time.DateOnly)), loc)
if err != nil {
panic("failed to parse next monday notif time. this is likely a bug.")
panic("failed to parse next wednesday notif time. this is likely a bug.")
}
log.Debug().Time("next", t).Msg("starting notifier instance")

@ -2,16 +2,22 @@ package themis
import "time"
var now func() time.Time
var defaultClock Clock = DefaultClock{}
func init() {
now = time.Now
type Clock interface {
Now() time.Time
}
func NextMonday() time.Time {
return now().AddDate(0, 0, int((8-now().Weekday())%7))
type DefaultClock struct{}
func (DefaultClock) Now() time.Time {
return time.Now()
}
func NextWednesday() time.Time {
return now().AddDate(0, 0, int((10-now().Weekday())%7))
func NextWednesday(clock Clock) time.Time {
if clock == nil {
clock = defaultClock
}
now := clock.Now()
return now.AddDate(0, 0, int((10-now.Weekday())%7))
}

@ -4,42 +4,56 @@ import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestNextMonday(t *testing.T) {
type TestClock struct {
now time.Time
}
func (c TestClock) Now() time.Time {
return c.now
}
func TestNextWednesday(t *testing.T) {
tests := []struct {
name string
seed string
want string
}{
{
name: "on monday",
seed: "2023-11-13T15:04:05Z07:00",
want: "2023-11-13T15:04:05Z07:00",
name: "on wednesday",
seed: "2023-11-15T15:04:05Z",
want: "2023-11-15T15:04:05Z",
},
{
name: "on sunday",
seed: "2023-11-12T15:04:05Z07:00",
want: "2023-11-13T15:04:05Z07:00",
seed: "2023-11-12T15:04:05Z",
want: "2023-11-15T15:04:05Z",
},
{
name: "on tuesday",
seed: "2023-11-14T15:04:05Z07:00",
want: "2023-11-20T15:04:05Z07:00",
name: "on thursday",
seed: "2023-11-16T15:04:05Z",
want: "2023-11-22T15:04:05Z",
},
{
name: "on saturday",
seed: "2023-11-18T15:04:05Z07:00",
want: "2023-11-20T15:04:05Z07:00",
seed: "2023-11-18T15:04:05Z",
want: "2023-11-22T15:04:05Z",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
seedt, _ := time.Parse(time.RFC3339, tt.seed)
now = func() time.Time { return seedt }
wantt, _ := time.Parse(time.RFC3339, tt.want)
if got := NextMonday(); !reflect.DeepEqual(got, wantt) {
t.Errorf("NextMonday() = %v, want %v", got, wantt)
seedt, err := time.Parse(time.RFC3339, tt.seed)
require.NoError(t, err)
wantt, err := time.Parse(time.RFC3339, tt.want)
require.NoError(t, err)
testClock := TestClock{now: seedt}
if got := NextWednesday(testClock); !reflect.DeepEqual(got, wantt) {
t.Errorf("NextWednesday() = %v, want %v", got, wantt)
}
})
}

Loading…
Cancel
Save