Skip to content

Commit

Permalink
refactor: clean up code and add tests for alarm logic
Browse files Browse the repository at this point in the history
  • Loading branch information
revelaction committed Aug 27, 2024
1 parent 0304417 commit 1fe3fb3
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 12 deletions.
1 change: 0 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package config


import (
"testing"
"time"
Expand Down
2 changes: 1 addition & 1 deletion ical/alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ical

import (
"github.com/revelaction/ical-git/config"
"log/slog"

Check failure on line 5 in ical/alarm.go

View workflow job for this annotation

GitHub Actions / Build binaries (linux, amd64)

"log/slog" imported and not used

Check failure on line 5 in ical/alarm.go

View workflow job for this annotation

GitHub Actions / Build binaries (linux, arm, 6)

"log/slog" imported and not used

Check failure on line 5 in ical/alarm.go

View workflow job for this annotation

GitHub Actions / Build binaries (linux, arm64)

"log/slog" imported and not used

Check failure on line 5 in ical/alarm.go

View workflow job for this annotation

GitHub Actions / Build binaries (darwin, amd64)

"log/slog" imported and not used
"time"
)

Expand Down Expand Up @@ -57,4 +58,3 @@ func (s *Alarms) isInTickPeriod(t time.Time) bool {

return true
}

104 changes: 94 additions & 10 deletions ical/alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,36 @@ import (
"github.com/revelaction/ical-git/config"
)

func TestGetAlarms(t *testing.T) {
// Define a TOML configuration with one alarm 15 hours ago
func TestGetInTick(t *testing.T) {
// start 1 sept 00:00
// tick 24 hours
// -> tick period [00:00, 23:59]
// event 1 sept 20:00
// alarm 15 hours before
// => alarm 1 sept 5:00, IN tick period
tomlConfig := []byte(`
timezone = "UTC"
tick = "1h"
tick = "24h"
alarms = [
{type = "desktop", when = "PT15H"},
{type = "desktop", when = "-PT15H"},
]
notifiers = ["desktop"]
`)

// Load the configuration
conf, err := config.Load(tomlConfig)
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}

// Set start time to 1 September 2024
// Set tick start time to 1 September 2024
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)

// Create Alarms instance
alarms := NewAlarms(conf, start)

// Set event time to 1 September 2024, 12:00
eventTime := time.Date(2024, time.September, 1, 12, 0, 0, 0, time.UTC)
// Set event time to 1 September 2024, 20:00
eventTime := time.Date(2024, time.September, 1, 20, 0, 0, 0, time.UTC)

// Get alarms
result := alarms.Get(eventTime)
Expand All @@ -43,8 +46,89 @@ func TestGetAlarms(t *testing.T) {
t.Errorf("Expected 1 alarm, got %d", len(result))
}

expectedAlarmTime := eventTime.Add(-15 * time.Hour)
// 1 September 2024, 5:00
expectedAlarmTime := time.Date(2024, time.September, 1, 5, 0, 0, 0, time.UTC)
if result[0].Time != expectedAlarmTime {
t.Errorf("Expected alarm time %v, got %v", expectedAlarmTime, result[0].Time)
}
}

func TestGetAfterTick(t *testing.T) {
// start 1 sept 00:00
// tick 4 hours
// -> tick period [00:00, 03:59]
// event 1 sept 20:00
// alarm 15 hours before
// => alarm 1 sept 5:00, after the current tick period
tomlConfig := []byte(`
timezone = "UTC"
tick = "4h"
alarms = [
{type = "desktop", when = "-PT15H"},
]
notifiers = ["desktop"]
`)

conf, err := config.Load(tomlConfig)
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}

// Set tick start time to 1 September 2024
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)

alarms := NewAlarms(conf, start)

// Set event time to 1 September 2024, 20:00
eventTime := time.Date(2024, time.September, 1, 20, 0, 0, 0, time.UTC)

// Get alarms
result := alarms.Get(eventTime)

// Check if the alarm is in the result
if len(result) != 0 {
t.Errorf("Expected 1 alarm, got %d", len(result))
}
}

func TestGetBeforeTick(t *testing.T) {
// start 1 sept 00:00
// tick 12 hours
// -> tick period [00:00, 11:59:59]
// event 1 sept 20:00
// alarm 21 hours before
// => alarm 31 august 23:00, before the current tick period
tomlConfig := []byte(`
timezone = "UTC"
tick = "4h"
alarms = [
{type = "desktop", when = "-PT21H"},
]
notifiers = ["desktop"]
`)

conf, err := config.Load(tomlConfig)
if err != nil {
t.Fatalf("Failed to load config: %v", err)
}

// Set tick start time to 1 September 2024
start := time.Date(2024, time.September, 1, 0, 0, 0, 0, time.UTC)

alarms := NewAlarms(conf, start)

// Set event time to 1 September 2024, 20:00
eventTime := time.Date(2024, time.September, 1, 20, 0, 0, 0, time.UTC)

// Get alarms
result := alarms.Get(eventTime)

// Check if the alarm is in the result
if len(result) != 0 {
t.Errorf("Expected 1 alarm, got %d", len(result))
}
}

0 comments on commit 1fe3fb3

Please sign in to comment.