File size: 2,868 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package schedulemanager

import (
	"encoding/json"
	"testing"
	"time"

	mapset "github.com/deckarep/golang-set/v2"
	"github.com/google/uuid"
	"github.com/stretchr/testify/require"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/schedule"
	"github.com/target/goalert/util/timeutil"
)

func TestUpdateInfo_calcUpdates_NotifyOnChange(t *testing.T) {
	channelID1 := uuid.MustParse("123e4567-e89b-12d3-a456-426614174000")
	channelID2 := uuid.MustParse("123e4567-e89b-12d3-a456-426614174001")
	var sData schedule.Data
	sData.V1.OnCallNotificationRules = []schedule.OnCallNotificationRule{
		{ChannelID: channelID1}, {ChannelID: channelID2},
	}
	data, err := json.Marshal(sData)
	require.NoError(t, err)
	info := updateInfo{
		ScheduleID:      uuid.New(),
		TimeZone:        time.UTC,
		RawScheduleData: data,
		ScheduleData:    sData,
		CurrentOnCall:   mapset.NewThreadUnsafeSet(uuid.New()), // no rules, so no longer on-call
		Rules:           []gadb.SchedMgrRulesRow{},
		ActiveOverrides: []gadb.SchedMgrOverridesRow{},
	}

	result, err := info.calcUpdates(time.Now())
	require.NoError(t, err)
	require.EqualValues(t, []uuid.UUID{channelID1, channelID2}, result.NotificationChannels.ToSlice())
}

func TestUpdateInfo_calcUpdates_NotifyAtTime(t *testing.T) {
	channelID := uuid.MustParse("123e4567-e89b-12d3-a456-426614174000")
	var sData schedule.Data
	everyDay := timeutil.EveryDay()
	at9am := timeutil.NewClock(9, 0)
	nextRun := time.Date(2023, 10, 1, 9, 0, 0, 0, time.UTC)
	sData.V1.OnCallNotificationRules = []schedule.OnCallNotificationRule{
		{
			ChannelID:        channelID,
			NextNotification: &nextRun,
			WeekdayFilter:    &everyDay,
			Time:             &at9am,
		},
	}
	data, err := json.Marshal(sData)
	require.NoError(t, err)
	info := updateInfo{
		ScheduleID:      uuid.New(),
		TimeZone:        time.UTC,
		RawScheduleData: data,
		ScheduleData:    sData,
		CurrentOnCall:   mapset.NewThreadUnsafeSet[uuid.UUID](),
		Rules:           []gadb.SchedMgrRulesRow{},
		ActiveOverrides: []gadb.SchedMgrOverridesRow{},
	}

	result, err := info.calcUpdates(nextRun.Add(-1 * time.Hour))
	require.NoError(t, err)
	require.Empty(t, result.NotificationChannels.ToSlice()) // 8 am, no notification yet

	require.Empty(t, result.NewRawScheduleData) // no update necessary

	result, err = info.calcUpdates(nextRun)
	require.NoError(t, err)
	require.Equal(t, []uuid.UUID{channelID}, result.NotificationChannels.ToSlice())

	var expected schedule.Data
	expectedNext := nextRun.AddDate(0, 0, 1)
	expected.V1.OnCallNotificationRules = []schedule.OnCallNotificationRule{
		{
			ChannelID:        channelID,
			NextNotification: &expectedNext,
			WeekdayFilter:    &everyDay,
			Time:             &at9am,
		},
	}
	expectedData, err := json.Marshal(expected)
	require.NoError(t, err)
	require.JSONEq(t, string(expectedData), string(result.NewRawScheduleData))
}