diff --git a/Makefile b/Makefile index 454929b..6af7a0c 100644 --- a/Makefile +++ b/Makefile @@ -48,3 +48,8 @@ uninstall: clean .PHONY: start start: get-deps go run ./cmd/app + +## test: Run unit tests +.PHONY: test +test: + @go test ./... diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 631c0fa..aaaf9bf 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -2,7 +2,6 @@ package utils import ( "errors" - "fmt" "math/rand/v2" "os/exec" "runtime" @@ -35,10 +34,15 @@ func IsInWorkingHours(timeWindow string) bool { return true } - startTimeBefore := MakeRandomTime(parts[0]) - endTimeAfter := MakeRandomTime(parts[1]) + // Get the current date currentTime := time.Now() + // Generate a datetime 3-14 minutes earlier than the specified time + startTimeBefore := addRandomTimeInterval(currentTime, parts[0]) + + // Generate a datetime 3-14 minutes later than the specified time + endTimeAfter := subtractRandomTimeInterval(currentTime, parts[1]) + // Check if the current time is within the dynamic range if currentTime.After(startTimeBefore) && currentTime.Before(endTimeAfter) { return true @@ -46,28 +50,37 @@ func IsInWorkingHours(timeWindow string) bool { return false } -func MakeRandomTime(inputTime string) time.Time { - // Parse input time like 19:00 to date time - parsedTime, err := time.Parse(timeLayout, inputTime) +func addRandomTimeInterval(currentTime time.Time, inputTime string) time.Time { + shiftTime, err := time.Parse(timeLayout, inputTime) if err != nil { - fmt.Println("Error parsing time:", err) - return time.Time{} + panic(err) } - // Generate a random interval between 3 to 14 minutes - randomInterval := time.Duration(rand.N(12)+3) * time.Minute + shiftTime = shiftTime.Add(generateTimeInterval()) + + // Combine the current date with the parsed time + return combineNowAndShiftTime(currentTime, shiftTime) +} - // Randomly decide whether to add or subtract the interval - if rand.N(2) == 0 { - parsedTime = parsedTime.Add(randomInterval) - } else { - parsedTime = parsedTime.Add(-randomInterval) +func subtractRandomTimeInterval(currentTime time.Time, inputTime string) time.Time { + shiftTime, err := time.Parse(timeLayout, inputTime) + if err != nil { + panic(err) } - // Get the current date - now := time.Now() + shiftTime = shiftTime.Add(-generateTimeInterval()) // Combine the current date with the parsed time + return combineNowAndShiftTime(currentTime, shiftTime) +} + +// Generate a random interval between 3 to 14 minutes +func generateTimeInterval() time.Duration { + return time.Duration(rand.N(12)+3) * time.Minute +} + +// Combine the current date with the parsed time (hour-minute) +func combineNowAndShiftTime(now, shiftTime time.Time) time.Time { return time.Date(now.Year(), now.Month(), now.Day(), - parsedTime.Hour(), parsedTime.Minute(), parsedTime.Second(), 0, now.Location()) + shiftTime.Hour(), shiftTime.Minute(), now.Second(), 0, now.Location()) } diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go new file mode 100644 index 0000000..49d0803 --- /dev/null +++ b/internal/utils/utils_test.go @@ -0,0 +1,28 @@ +package utils + +import ( + "testing" + "time" +) + +// Test value is within the range of 3 to 14 minutes +func TestGenerateTimeInterval(t *testing.T) { + for range 100 { + result := generateTimeInterval() + if result < 3*time.Minute || result >= 15*time.Minute { + t.Errorf("generateTimeInterval() = %v, want %v to %v", result, 3*time.Minute, 15*time.Minute) + } + } +} + +// Test checks if the function merges two times correctly +func TestCombineNowAndShiftTime(t *testing.T) { + now := time.Date(2024, 4, 7, 10, 0, 45, 0, time.UTC) + shiftTime := time.Date(2000, 1, 1, 15, 30, 0, 0, time.UTC) + expected := time.Date(2024, 4, 7, 15, 30, 45, 0, time.UTC) + result := combineNowAndShiftTime(now, shiftTime) + + if !result.Equal(expected) { + t.Errorf("combineNowAndShiftTime(%v, %v) = %v, want %v", now, shiftTime, result, expected) + } +}