Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
5b98484
style(utils-test): use capitalization on `got ..., want ...`
bruhtus Aug 27, 2025
2e3ddc0
feat(utils): add test setup temporary file and create status file
bruhtus Aug 27, 2025
3c13cec
test: add case for pause subcommand
bruhtus Aug 27, 2025
3c46648
test: add is_notify correct case for pause subcommand
bruhtus Aug 28, 2025
cacdb83
chore: rename file internal/on-going.go to internal/on_going.go
bruhtus Aug 28, 2025
ae46dc5
refactor: put directory path as TestSetupTempFile() parameter
bruhtus Aug 28, 2025
2df4d63
refactor(utils): separate cleanup file test into another function
bruhtus Aug 30, 2025
8fd64b1
refactor(pause_test): move cleanup function before looping cases
bruhtus Aug 30, 2025
caca966
Revert "refactor(utils): separate cleanup file test into another func…
bruhtus Aug 30, 2025
b8fe469
test: add case for end time in on going function
bruhtus Aug 30, 2025
bf661d0
test: add case for is_notify i on going function
bruhtus Aug 30, 2025
07126c6
chore: remove todo in reset subcommand
bruhtus Aug 30, 2025
5ddb7ff
test: add case for status pause indicator
bruhtus Aug 30, 2025
17c877a
test: add case for status remaining indicator
bruhtus Aug 30, 2025
3a83c40
ci: add test pipeline on pull request
bruhtus Aug 30, 2025
b365163
ci: add synchronize type event in test pipeline
bruhtus Aug 30, 2025
bb7d878
test: fix TestOnGoingEndTime first cases
bruhtus Aug 30, 2025
f2596c3
test: use `Compare()` instead of directly compare time value
bruhtus Aug 30, 2025
698c3eb
chore: add comment about cleanup in TestOnGoingEndTime/not_exist
bruhtus Aug 30, 2025
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
21 changes: 21 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: test
on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout branch
uses: actions/checkout@v4

- name: Setup go
uses: actions/setup-go@v5

- name: Install dependencies
run: go get .

- name: Run test cases
run: go test ./... -v
1 change: 0 additions & 1 deletion internal/on-going.go → internal/on_going.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/bruhtus/simo/utils"
)

// TODO: add test case.
func OnGoing(
statusPath string,
duration time.Duration,
Expand Down
239 changes: 239 additions & 0 deletions internal/on_going_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package scmd_test

import (
"encoding/json"
"errors"
"fmt"
"os"
"testing"
"time"

scmd "github.com/bruhtus/simo/internal"
"github.com/bruhtus/simo/utils"
)

func TestOnGoingEndTime(t *testing.T) {
durationCases := []time.Duration{
time.Duration(0 * time.Second),
time.Duration(1 * time.Second),
time.Duration(1 * time.Minute),
time.Duration(1 * time.Hour),
}

endTimeCases := []struct {
duration time.Duration
output time.Time
}{
{
durationCases[0],
time.
Now().
Add(durationCases[0]).
Round(time.Second),
},
{
durationCases[1],
time.
Now().
Add(durationCases[1]).
Round(time.Second),
},
{
durationCases[2],
time.
Now().
Add(durationCases[2]).
Round(time.Second),
},
{
durationCases[3],
time.
Now().
Add(durationCases[3]).
Round(time.Second),
},
}

t.Run("Status file exist", func(t *testing.T) {
var (
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,
)
}
})

for _, tt := range endTimeCases {
t.Run(
tt.duration.String(),
func(t *testing.T) {
status := utils.Status{
State: utils.StateFocus,
IsNotify: true,
PausePoint: nil,
}

utils.TestSetupStatusFile(t, status, file)

scmd.OnGoing(
file.Name(),
tt.duration,
utils.StateFocus,
true,
true,
)

resultJSON := utils.ReadStatusFile(file.Name())
endTime := resultJSON.EndTime.Round(time.Second)

comparation := endTime.Compare(tt.output)

if comparation != 0 {
t.Errorf(
"Got %v, want %v. Compare result: %d",
endTime,
tt.output,
comparation,
)
}
},
)
}
})

t.Run("Status file not exist", func(t *testing.T) {
dirPath := t.TempDir()

filePath := fmt.Sprintf(
"%s/simo-%d.json",
dirPath,
time.Now().UnixMilli(),
)

for _, tt := range endTimeCases {
t.Run(
tt.duration.String(),
func(t *testing.T) {
_, err := os.Stat(filePath)

if err != nil && !errors.Is(err, os.ErrNotExist) {
t.Fatalf(
"Error read status file: %v",
err,
)
}

scmd.OnGoing(
filePath,
tt.duration,
utils.StateFocus,
true,
true,
)

resultJSON := utils.ReadStatusFile(filePath)
endTime := resultJSON.EndTime.Round(time.Second)

t.Cleanup(func() {
// Reset the end time so that it become expired.
if !errors.Is(err, os.ErrNotExist) {
resultJSON.EndTime = time.Now()
statusJSON, err := json.Marshal(resultJSON)

if err != nil {
t.Fatalf(
"Error json marshal existing status file: %v",
err,
)
}

utils.WriteStatusFile(filePath, statusJSON)
}
})

comparation := endTime.Compare(tt.output)

if comparation != 0 {
t.Errorf(
"Got %v, want %v. Compare result: %d",
endTime,
tt.output,
comparation,
)
}
},
)
}
})
}

func TestOnGoingIsNotify(t *testing.T) {
isNotifyCases := []struct {
inputUseNotify bool
inputNotifyCmdExist bool
output bool
}{
{true, true, true},
{false, true, false},
{true, false, false},
{false, false, false},
}

var (
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,
)
}
})

for _, tt := range isNotifyCases {
t.Run(
fmt.Sprintf(
"isUseNotify %t, isNotifyCmdExist %t",
tt.inputUseNotify, tt.inputNotifyCmdExist,
),
func(t *testing.T) {
status := utils.Status{
State: utils.StateFocus,
IsNotify: true,
PausePoint: nil,
}

utils.TestSetupStatusFile(t, status, file)

scmd.OnGoing(
file.Name(),
time.Duration(0*time.Second),
utils.StateFocus,
tt.inputUseNotify,
tt.inputNotifyCmdExist,
)

resultJSON := utils.ReadStatusFile(file.Name())

if resultJSON.IsNotify != tt.output {
t.Errorf(
"Got %v, want %v",
resultJSON.IsNotify,
tt.output,
)
}
},
)
}
}
1 change: 0 additions & 1 deletion internal/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/bruhtus/simo/utils"
)

// TODO: add test case.
func Pause(statusPath string) {
var (
status = utils.ReadStatusFile(statusPath)
Expand Down
77 changes: 77 additions & 0 deletions internal/pause_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package scmd_test

import (
"testing"
"time"

scmd "github.com/bruhtus/simo/internal"
"github.com/bruhtus/simo/utils"
)

func TestPause(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 {
duration time.Duration
remainingDurationOutput string
isNotifyInput bool
isNotifyOutput bool
}{
{
time.Duration(1 * time.Second), "0m1s",
true, true,
},
{
time.Duration(1 * time.Minute), "1m0s",
false, false,
},
{
time.Duration(1 * time.Hour), "60m0s",
true, true,
},
}

for _, tt := range remainingDurationCases {
t.Run(
tt.duration.String(),
func(t *testing.T) {
status := utils.Status{
State: utils.StateFocus,
IsNotify: tt.isNotifyInput,
PausePoint: nil,
EndTime: time.Now().Add(tt.duration),
}

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

resultJSON := utils.ReadStatusFile(file.Name())
if resultJSON.PausePoint == nil {
t.Errorf(
"Got nil, want %s",
tt.remainingDurationOutput,
)
}

if resultJSON.IsNotify != tt.isNotifyOutput {
t.Errorf(
"Got %t, want %t",
tt.isNotifyInput,
tt.isNotifyOutput,
)
}
},
)
}
}
1 change: 0 additions & 1 deletion internal/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
)

// TODO: add test case.
func Reset(statusPath string) error {
err := os.Remove(statusPath)

Expand Down
Loading
Loading