From cd5dae477852bb10af3ecf38440a0f903e98e876 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 29 May 2026 17:38:48 +0800 Subject: [PATCH] test: add sysutil and cache tests - Add RestartService tests (pkg/sysutil) - Add decodeRedisValue and normalizeRedisValue tests (cache/l2.go) --- internal/cache/l2_test.go | 76 ++++++++++++++++++++++++++++ internal/pkg/sysutil/restart_test.go | 28 ++++++++++ 2 files changed, 104 insertions(+) create mode 100644 internal/cache/l2_test.go create mode 100644 internal/pkg/sysutil/restart_test.go diff --git a/internal/cache/l2_test.go b/internal/cache/l2_test.go new file mode 100644 index 0000000..e90ed59 --- /dev/null +++ b/internal/cache/l2_test.go @@ -0,0 +1,76 @@ +package cache + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDecodeRedisValue(t *testing.T) { + tests := []struct { + name string + input string + want interface{} + wantErr bool + }{ + {"string", "\"hello\"", "hello", false}, + {"number_int", "42", int64(42), false}, + {"number_float", "3.14", 3.14, false}, + {"bool_true", "true", true, false}, + {"bool_false", "false", false, false}, + {"null", "null", nil, false}, + {"array", "[1, 2, 3]", []interface{}{int64(1), int64(2), int64(3)}, false}, + {"object", "{\"a\":1,\"b\":2}", map[string]interface{}{"a": int64(1), "b": int64(2)}, false}, + {"invalid_returns_raw", "not-json", "not-json", false}, + {"empty", "", "", false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := decodeRedisValue(tt.input) + if tt.wantErr { + require.Error(t, err) + return + } + require.NoError(t, err) + require.Equal(t, tt.want, got) + }) + } +} + +func TestNormalizeRedisValue(t *testing.T) { + tests := []struct { + name string + input interface{} + want interface{} + }{ + {"number_to_int", json.Number("42"), int64(42)}, + {"number_to_float", json.Number("3.14"), 3.14}, + {"number_string", json.Number("abc"), "abc"}, + {"string_unchanged", "hello", "hello"}, + {"int_unchanged", int64(42), int64(42)}, + {"bool_unchanged", true, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := normalizeRedisValue(tt.input) + require.Equal(t, tt.want, got) + }) + } +} + +func TestNormalizeRedisValue_Array(t *testing.T) { + input := []interface{}{json.Number("1"), json.Number("2"), "text"} + want := []interface{}{int64(1), int64(2), "text"} + got := normalizeRedisValue(input) + require.Equal(t, want, got) +} + +func TestNormalizeRedisValue_Map(t *testing.T) { + input := map[string]interface{}{"num": json.Number("42"), "str": "text"} + want := map[string]interface{}{"num": int64(42), "str": "text"} + got := normalizeRedisValue(input) + require.Equal(t, want, got) +} diff --git a/internal/pkg/sysutil/restart_test.go b/internal/pkg/sysutil/restart_test.go new file mode 100644 index 0000000..369303b --- /dev/null +++ b/internal/pkg/sysutil/restart_test.go @@ -0,0 +1,28 @@ +package sysutil + +import ( + "runtime" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestRestartService_NonLinux(t *testing.T) { + // On non-Linux systems, RestartService should return nil without exiting + if runtime.GOOS == "linux" { + t.Skip("Skipping non-Linux test on Linux") + } + + err := RestartService() + require.NoError(t, err) +} + +func TestRestartServiceAsync_NonLinux(t *testing.T) { + // On non-Linux systems, RestartServiceAsync should not panic + if runtime.GOOS == "linux" { + t.Skip("Skipping non-Linux test on Linux") + } + + // Should not panic + RestartServiceAsync() +}