Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions internal/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ import (
"github.com/bruhtus/simo/utils"
)

func Pause(statusPath string) {
func Pause(
now func() time.Time,
statusPath string,
) {
// Substitute this in test.
// Reference:
// https://stackoverflow.com/a/25791617
if now == nil {
now = time.Now
}

var (
status = utils.ReadStatusFile(statusPath)
isExpired = utils.DetermineIsExpired(status.EndTime)
Expand All @@ -19,21 +29,9 @@ func Pause(statusPath string) {
newPausePoint = fmt.Sprintf("%dm%ds", minutes, seconds)
)

if isExpired {
if status.PausePoint != nil {
status.PausePoint = nil

statusJSON, err := json.Marshal(status)
utils.CheckError(err)

utils.WriteStatusFile(statusPath, statusJSON)
}
return
}

if status.PausePoint != nil {
remainingDuration := utils.ParseDuration(*status.PausePoint)
newEndTime := time.Now().Add(remainingDuration)
newEndTime := now().Add(remainingDuration)

status.PausePoint = nil
status.EndTime = newEndTime
Expand All @@ -45,6 +43,10 @@ func Pause(statusPath string) {
return
}

if isExpired {
return
}

status.PausePoint = &newPausePoint

statusJSON, err := json.Marshal(status)
Expand Down
112 changes: 108 additions & 4 deletions internal/pause_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func TestPause(t *testing.T) {
isNotifyInput bool
isNotifyOutput bool
}{
{
time.Duration(-1 * time.Second), "",
false, false,
},
{
time.Duration(0 * time.Second), "",
false, false,
},
{
time.Duration(1 * time.Second), "0m1s",
true, true,
Expand All @@ -37,7 +45,11 @@ func TestPause(t *testing.T) {
false, false,
},
{
time.Duration(1 * time.Hour), "60m0s",
time.Duration(60 * time.Minute), "60m0s",
true, true,
},
{
time.Duration(90 * time.Minute), "90m0s",
true, true,
},
}
Expand All @@ -54,12 +66,20 @@ func TestPause(t *testing.T) {
}

utils.TestSetupStatusFile(t, status, file)
scmd.Pause(file.Name())
scmd.Pause(nil, file.Name())

resultJSON := utils.ReadStatusFile(file.Name())
if resultJSON.PausePoint == nil {

if resultJSON.PausePoint == nil &&
tt.remainingDurationOutput != "" {
t.Errorf("Got unexpected nil")
}

if resultJSON.PausePoint != nil &&
*resultJSON.PausePoint != tt.remainingDurationOutput {
t.Errorf(
"Got nil, want %s",
"Got %s, want %s",
*resultJSON.PausePoint,
tt.remainingDurationOutput,
)
}
Expand All @@ -75,3 +95,87 @@ func TestPause(t *testing.T) {
)
}
}

func TestResume(t *testing.T) {
dirPath := t.TempDir()
file := utils.TestSetupTempFile(t, dirPath)

t.Cleanup(func() {
err := file.Close()
if err != nil {
t.Fatalf(
"Failed to close file: %v",
err,
)
}
})

remainingDurationCases := []struct {
pausePoint string
endTime time.Duration
expectedEndTime time.Duration
}{
{
"0m1s",
time.Duration(-1 * time.Second),
time.Duration(1 * time.Second),
},
{
"0m10s",
time.Duration(-10 * time.Second),
time.Duration(10 * time.Second),
},
{
"1m0s",
time.Duration(-1 * time.Minute),
time.Duration(1 * time.Minute),
},
{
"60m0s",
time.Duration(-60 * time.Minute),
time.Duration(60 * time.Minute),
},
{
"90m0s",
time.Duration(-90 * time.Minute),
time.Duration(90 * time.Minute),
},
}

for _, tt := range remainingDurationCases {
t.Run(
tt.endTime.String(),
func(t *testing.T) {
endTime := time.Now().Add(tt.endTime)
expectedEndTime := endTime.Add(tt.expectedEndTime)

status := utils.Status{
State: utils.StateFocus,
IsNotify: false,
PausePoint: &tt.pausePoint,
EndTime: endTime,
}

utils.TestSetupStatusFile(t, status, file)

// Substitute the time.Now() result.
now := func() time.Time {
return endTime
}

scmd.Pause(now, file.Name())
resultJSON := utils.ReadStatusFile(file.Name())

comparation := expectedEndTime.Compare(resultJSON.EndTime)

if comparation != 0 {
t.Errorf(
"Got %v, want %v",
endTime,
expectedEndTime,
)
}
},
)
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func main() {
utils.CheckError(err)

case "pause":
scmd.Pause(statusPath)
scmd.Pause(nil, statusPath)

default:
utils.HelpUsage()
Expand Down
Loading