From 5b98484e118544211f697e7f20f451051b32b005 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Wed, 27 Aug 2025 15:02:06 +0700 Subject: [PATCH 01/19] style(utils-test): use capitalization on `got ..., want ...` The format of `t.Errorf()` is something like `file.go:69: message`, so if we use lower case for the message, we might miss the error message. Let's try using capitalization at the beginning of the message and see how it goes. --- utils/time_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/time_test.go b/utils/time_test.go index 28745bc..bac762a 100644 --- a/utils/time_test.go +++ b/utils/time_test.go @@ -31,7 +31,7 @@ func TestIsExpired(t *testing.T) { output := utils.DetermineIsExpired(tt.endTime) if output != tt.output { t.Errorf( - "got %t, want %t", + "Got %t, want %t", output, tt.output, ) } @@ -67,7 +67,7 @@ func TestGetDurationMinutesAndSeconds(t *testing.T) { if output != tt.output { t.Errorf( - "got %s, want %s", + "Got %s, want %s", output, tt.output, ) } @@ -100,7 +100,7 @@ func TestRemainingDuration(t *testing.T) { if output != tt.output { t.Errorf( - "got %s, want %s", + "Got %s, want %s", output, tt.output, ) } From 2e3ddc0541d8b2ec21b8bd8b4b386edecb1113e4 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Wed, 27 Aug 2025 15:06:45 +0700 Subject: [PATCH 02/19] feat(utils): add test setup temporary file and create status file --- utils/test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 utils/test.go diff --git a/utils/test.go b/utils/test.go new file mode 100644 index 0000000..c00b27e --- /dev/null +++ b/utils/test.go @@ -0,0 +1,67 @@ +package utils + +import ( + "encoding/json" + "os" + "testing" +) + +func TestSetupTempFile( + t *testing.T, +) *os.File { + t.Helper() + + dir := t.TempDir() + file, err := os.CreateTemp(dir, "simo-*.json") + + if err != nil { + t.Fatalf( + "Failed to create temporary file: %v", + err, + ) + } + + return file +} + +func TestSetupStatusFile( + t *testing.T, + status Status, + file *os.File, +) { + t.Helper() + + statusJSON, err := json.Marshal(status) + if err != nil { + t.Fatalf( + "Failed to marshal status json: %v", + err, + ) + } + + _, err = file.Write(statusJSON) + if err != nil { + t.Fatalf( + "Failed to write data into temporary file: %v", + err, + ) + } + + t.Cleanup(func() { + err := file.Truncate(0) + if err != nil { + t.Fatalf( + "Failed to truncate file: %v", + err, + ) + } + + _, err = file.Seek(0, 0) // Seek the beginning of the file. + if err != nil { + t.Fatalf( + "Failed to seek the begining of file: %v", + err, + ) + } + }) +} From 3c13cecfa83a60520eff7edff6e6f8a9743817f8 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Wed, 27 Aug 2025 15:18:14 +0700 Subject: [PATCH 03/19] test: add case for pause subcommand --- internal/pause.go | 1 - internal/pause_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 internal/pause_test.go diff --git a/internal/pause.go b/internal/pause.go index e119604..63b0c18 100644 --- a/internal/pause.go +++ b/internal/pause.go @@ -8,7 +8,6 @@ import ( "github.com/bruhtus/simo/utils" ) -// TODO: add test case. func Pause(statusPath string) { var ( status = utils.ReadStatusFile(statusPath) diff --git a/internal/pause_test.go b/internal/pause_test.go new file mode 100644 index 0000000..7803aeb --- /dev/null +++ b/internal/pause_test.go @@ -0,0 +1,57 @@ +package scmd_test + +import ( + "testing" + "time" + + scmd "github.com/bruhtus/simo/internal" + "github.com/bruhtus/simo/utils" +) + +func TestPause(t *testing.T) { + file := utils.TestSetupTempFile(t) + + remainingDurationCases := []struct { + duration time.Duration + output string + }{ + {time.Duration(1 * time.Second), "0m1s"}, + {time.Duration(1 * time.Minute), "1m0s"}, + {time.Duration(1 * time.Hour), "60m0s"}, + } + + for _, tt := range remainingDurationCases { + t.Run( + tt.duration.String(), + func(t *testing.T) { + status := utils.Status{ + State: utils.StateFocus, + IsNotify: true, + 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.output, + ) + } + }, + ) + } + + t.Cleanup(func() { + err := file.Close() + if err != nil { + t.Fatalf( + "Failed to close file: %v", + err, + ) + } + }) +} From 3c466483f6e38f42c24f6267e6caa8bfafbaa4fe Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Thu, 28 Aug 2025 13:53:03 +0700 Subject: [PATCH 04/19] test: add is_notify correct case for pause subcommand --- internal/pause_test.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/pause_test.go b/internal/pause_test.go index 7803aeb..3efd7bb 100644 --- a/internal/pause_test.go +++ b/internal/pause_test.go @@ -12,12 +12,23 @@ func TestPause(t *testing.T) { file := utils.TestSetupTempFile(t) remainingDurationCases := []struct { - duration time.Duration - output string + duration time.Duration + remainingDurationOutput string + isNotifyInput bool + isNotifyOutput bool }{ - {time.Duration(1 * time.Second), "0m1s"}, - {time.Duration(1 * time.Minute), "1m0s"}, - {time.Duration(1 * time.Hour), "60m0s"}, + { + 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 { @@ -26,7 +37,7 @@ func TestPause(t *testing.T) { func(t *testing.T) { status := utils.Status{ State: utils.StateFocus, - IsNotify: true, + IsNotify: tt.isNotifyInput, PausePoint: nil, EndTime: time.Now().Add(tt.duration), } @@ -38,7 +49,15 @@ func TestPause(t *testing.T) { if resultJSON.PausePoint == nil { t.Errorf( "Got nil, want %s", - tt.output, + tt.remainingDurationOutput, + ) + } + + if resultJSON.IsNotify != tt.isNotifyOutput { + t.Errorf( + "Got %t, want %t", + tt.isNotifyInput, + tt.isNotifyOutput, ) } }, From cacdb8339e6aa633e07908bb15bf6f80776e345d Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Thu, 28 Aug 2025 13:55:29 +0700 Subject: [PATCH 05/19] chore: rename file internal/on-going.go to internal/on_going.go Make the filename scheme consistent using underscore instead of dash. --- internal/{on-going.go => on_going.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename internal/{on-going.go => on_going.go} (100%) diff --git a/internal/on-going.go b/internal/on_going.go similarity index 100% rename from internal/on-going.go rename to internal/on_going.go From ae46dc514c4332da2aafbae2bfafefdd3fec9681 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Thu, 28 Aug 2025 14:25:43 +0700 Subject: [PATCH 06/19] refactor: put directory path as TestSetupTempFile() parameter This is so that we can debug the generated json file in test by replacing the temporary directory with `/tmp`. --- internal/pause_test.go | 3 ++- utils/test.go | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/pause_test.go b/internal/pause_test.go index 3efd7bb..d240d5e 100644 --- a/internal/pause_test.go +++ b/internal/pause_test.go @@ -9,7 +9,8 @@ import ( ) func TestPause(t *testing.T) { - file := utils.TestSetupTempFile(t) + dirPath := t.TempDir() + file := utils.TestSetupTempFile(t, dirPath) remainingDurationCases := []struct { duration time.Duration diff --git a/utils/test.go b/utils/test.go index c00b27e..034fed8 100644 --- a/utils/test.go +++ b/utils/test.go @@ -8,12 +8,11 @@ import ( func TestSetupTempFile( t *testing.T, + dirPath string, ) *os.File { t.Helper() - dir := t.TempDir() - file, err := os.CreateTemp(dir, "simo-*.json") - + file, err := os.CreateTemp(dirPath, "simo-*.json") if err != nil { t.Fatalf( "Failed to create temporary file: %v", From 2df4d632eb1cb937a4d2826bd7d3c30a7465a164 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 08:32:24 +0700 Subject: [PATCH 07/19] refactor(utils): separate cleanup file test into another function This is so that i can use it separately from TestSetupStatusFile(). Sometimes i need to test the scenario where the status file is not exist yet and need to reset the file state so that we can reuse the file. --- utils/test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/utils/test.go b/utils/test.go index 034fed8..2d04f3a 100644 --- a/utils/test.go +++ b/utils/test.go @@ -46,6 +46,12 @@ func TestSetupStatusFile( ) } + TestResetFile(t, file) +} + +func TestResetFile(t *testing.T, file *os.File) { + t.Helper() + t.Cleanup(func() { err := file.Truncate(0) if err != nil { From 8fd64b152a5d42504db74f40cd335461b8d1db7f Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 08:35:01 +0700 Subject: [PATCH 08/19] refactor(pause_test): move cleanup function before looping cases The cleanup function is called in "last added, first called" order, so let's put it before looping cases to make sure that it's called last. --- internal/pause_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/pause_test.go b/internal/pause_test.go index d240d5e..ae70aea 100644 --- a/internal/pause_test.go +++ b/internal/pause_test.go @@ -12,6 +12,16 @@ 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 @@ -64,14 +74,4 @@ func TestPause(t *testing.T) { }, ) } - - t.Cleanup(func() { - err := file.Close() - if err != nil { - t.Fatalf( - "Failed to close file: %v", - err, - ) - } - }) } From caca96665993968c89b566d5b3ec1f31331d6264 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 08:54:25 +0700 Subject: [PATCH 09/19] Revert "refactor(utils): separate cleanup file test into another function" This reverts commit 2df4d632eb1cb937a4d2826bd7d3c30a7465a164. --- utils/test.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/utils/test.go b/utils/test.go index 2d04f3a..034fed8 100644 --- a/utils/test.go +++ b/utils/test.go @@ -46,12 +46,6 @@ func TestSetupStatusFile( ) } - TestResetFile(t, file) -} - -func TestResetFile(t *testing.T, file *os.File) { - t.Helper() - t.Cleanup(func() { err := file.Truncate(0) if err != nil { From b8fe4694a68ac4962915497f978c99434c9a75a6 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 09:17:55 +0700 Subject: [PATCH 10/19] test: add case for end time in on going function Test whether the end time result correct or not. --- internal/on_going.go | 1 - internal/on_going_test.go | 157 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 internal/on_going_test.go diff --git a/internal/on_going.go b/internal/on_going.go index d10f7ff..2780db6 100644 --- a/internal/on_going.go +++ b/internal/on_going.go @@ -8,7 +8,6 @@ import ( "github.com/bruhtus/simo/utils" ) -// TODO: add test case. func OnGoing( statusPath string, duration time.Duration, diff --git a/internal/on_going_test.go b/internal/on_going_test.go new file mode 100644 index 0000000..f686086 --- /dev/null +++ b/internal/on_going_test.go @@ -0,0 +1,157 @@ +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) + + if endTime != tt.output { + t.Errorf( + "Got %v, want %v", + endTime, + tt.output, + ) + } + }, + ) + } + }) + + 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() { + 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) + } + }) + + if endTime != tt.output { + t.Errorf( + "Got %v, want %v", + endTime, + tt.output, + ) + } + }, + ) + } + }) +} From bf661d0e40a5813e7cf72d9e033dc4cc9440538a Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 09:39:28 +0700 Subject: [PATCH 11/19] test: add case for is_notify i on going function Test whether `is_notify` is true or false depending on input use notify and notify command exist. --- internal/on_going_test.go | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/internal/on_going_test.go b/internal/on_going_test.go index f686086..affdfea 100644 --- a/internal/on_going_test.go +++ b/internal/on_going_test.go @@ -155,3 +155,67 @@ func TestOnGoingEndTime(t *testing.T) { } }) } + +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, + ) + } + }, + ) + } +} From 07126c6ff0e52aca6d622d87f4b0273e7773d3b4 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 09:41:23 +0700 Subject: [PATCH 12/19] chore: remove todo in reset subcommand Feels like we don't need to create test for reset functionality as it's basically only deleting the status file. --- internal/reset.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/reset.go b/internal/reset.go index 886436c..3d40963 100644 --- a/internal/reset.go +++ b/internal/reset.go @@ -5,7 +5,6 @@ import ( "os" ) -// TODO: add test case. func Reset(statusPath string) error { err := os.Remove(statusPath) From 5ddb7ffbb972c9f2638266ec99ac6785f5cc4cd9 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 10:25:52 +0700 Subject: [PATCH 13/19] test: add case for status pause indicator Test the status indicator when we have pause point in status file. --- internal/status_test.go | 68 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 internal/status_test.go diff --git a/internal/status_test.go b/internal/status_test.go new file mode 100644 index 0000000..b2c8b54 --- /dev/null +++ b/internal/status_test.go @@ -0,0 +1,68 @@ +package scmd_test + +import ( + "testing" + "time" + + scmd "github.com/bruhtus/simo/internal" + "github.com/bruhtus/simo/utils" +) + +func TestStatusPause(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, + ) + } + }) + + pausePointCases := []struct { + input string + output string + }{ + {"0m1s", "PF00:01"}, + {"1m0s", "PF01:00"}, + {"60m0s", "PF60:00"}, + } + + for _, tt := range pausePointCases { + t.Run( + tt.input, + func(t *testing.T) { + remainingDuration, err := time.ParseDuration( + tt.input, + ) + + if err != nil { + t.Fatalf( + "Error parsing duration: %v", + err, + ) + } + + status := utils.Status{ + State: utils.StateFocus, + IsNotify: false, + PausePoint: &tt.input, + EndTime: time.Now().Add(remainingDuration), + } + + utils.TestSetupStatusFile(t, status, file) + currentStatus := scmd.Status(file.Name(), "notify-send") + + if currentStatus != tt.output { + t.Errorf( + "Got %s, want %s", + currentStatus, tt.output, + ) + } + }, + ) + } +} From 17c877ab6fbb9952b17300db153672256d5636fc Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 10:33:59 +0700 Subject: [PATCH 14/19] test: add case for status remaining indicator --- internal/status_test.go | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/internal/status_test.go b/internal/status_test.go index b2c8b54..4544c23 100644 --- a/internal/status_test.go +++ b/internal/status_test.go @@ -66,3 +66,53 @@ func TestStatusPause(t *testing.T) { ) } } + +func TestStatusRemainingDuration(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 + output string + }{ + {time.Duration(-1 * time.Second), "--:--"}, + {time.Duration(0 * time.Second), "--:--"}, + {time.Duration(1 * time.Second), "F00:01"}, + {time.Duration(1 * time.Minute), "F01:00"}, + {time.Duration(1 * time.Hour), "F60:00"}, + } + + for _, tt := range remainingDurationCases { + t.Run( + tt.duration.String(), + func(t *testing.T) { + status := utils.Status{ + State: utils.StateFocus, + IsNotify: false, + PausePoint: nil, + EndTime: time.Now().Add(tt.duration), + } + + utils.TestSetupStatusFile(t, status, file) + currentStatus := scmd.Status(file.Name(), "notify-send") + + if currentStatus != tt.output { + t.Errorf( + "Got %s, want %s", + currentStatus, tt.output, + ) + } + }, + ) + } +} From 3a83c40a46791f15b2445e9c3f76a9504f593b48 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 10:45:48 +0700 Subject: [PATCH 15/19] ci: add test pipeline on pull request --- .github/workflows/test.yaml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..a2c4daf --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,21 @@ +name: test +on: + pull_request: + types: [opened, 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 From b365163d9d7c477ba1c2db223b74c8a70f019921 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 10:48:22 +0700 Subject: [PATCH 16/19] ci: add synchronize type event in test pipeline To make sure that the pipeline get triggered in new commit (?). Let's see how it goes. --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index a2c4daf..35b9f6b 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,7 +1,7 @@ name: test on: pull_request: - types: [opened, reopened] + types: [opened, synchronize, reopened] jobs: build: From bb7d8786fe783446b32c0a6e7bd42cdaae423573 Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 10:55:54 +0700 Subject: [PATCH 17/19] test: fix TestOnGoingEndTime first cases Let's see if this changes can fix the failed test. --- internal/on_going_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/on_going_test.go b/internal/on_going_test.go index affdfea..2a3ab5c 100644 --- a/internal/on_going_test.go +++ b/internal/on_going_test.go @@ -26,7 +26,11 @@ func TestOnGoingEndTime(t *testing.T) { }{ { durationCases[0], - time.Now().Add(durationCases[0]).Round(time.Second), + time. + Now(). + Local(). + Add(durationCases[0]). + Round(time.Second), }, { durationCases[1], From f2596c36262cc8709849af026b6ccd65324590ba Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 11:05:44 +0700 Subject: [PATCH 18/19] test: use `Compare()` instead of directly compare time value Let's see if using `Compare()` actually solve the failing test on github CI. --- internal/on_going_test.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/internal/on_going_test.go b/internal/on_going_test.go index 2a3ab5c..95dbb13 100644 --- a/internal/on_going_test.go +++ b/internal/on_going_test.go @@ -28,21 +28,29 @@ func TestOnGoingEndTime(t *testing.T) { durationCases[0], time. Now(). - Local(). Add(durationCases[0]). Round(time.Second), }, { durationCases[1], - time.Now().Add(durationCases[1]).Round(time.Second), + time. + Now(). + Add(durationCases[1]). + Round(time.Second), }, { durationCases[2], - time.Now().Add(durationCases[2]).Round(time.Second), + time. + Now(). + Add(durationCases[2]). + Round(time.Second), }, { durationCases[3], - time.Now().Add(durationCases[3]).Round(time.Second), + time. + Now(). + Add(durationCases[3]). + Round(time.Second), }, } @@ -85,11 +93,14 @@ func TestOnGoingEndTime(t *testing.T) { resultJSON := utils.ReadStatusFile(file.Name()) endTime := resultJSON.EndTime.Round(time.Second) - if endTime != tt.output { + comparation := endTime.Compare(tt.output) + + if comparation != 0 { t.Errorf( - "Got %v, want %v", + "Got %v, want %v. Compare result: %d", endTime, tt.output, + comparation, ) } }, @@ -147,11 +158,14 @@ func TestOnGoingEndTime(t *testing.T) { } }) - if endTime != tt.output { + comparation := endTime.Compare(tt.output) + + if comparation != 0 { t.Errorf( - "Got %v, want %v", + "Got %v, want %v. Compare result: %d", endTime, tt.output, + comparation, ) } }, From 698c3ebee73a276bd6944987d293d3e96377960d Mon Sep 17 00:00:00 2001 From: Robertus Chris Date: Sat, 30 Aug 2025 11:13:13 +0700 Subject: [PATCH 19/19] chore: add comment about cleanup in TestOnGoingEndTime/not_exist --- internal/on_going_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/on_going_test.go b/internal/on_going_test.go index 95dbb13..78afec9 100644 --- a/internal/on_going_test.go +++ b/internal/on_going_test.go @@ -142,9 +142,9 @@ func TestOnGoingEndTime(t *testing.T) { 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 {