feat: harden runtime import and frontend verification workflows
Some checks failed
CI / Build & Test (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / Docker Build (push) Has been cancelled
CI / Release (push) Has been cancelled

This commit is contained in:
phamnazage-jpg
2026-06-04 20:02:36 +08:00
parent 7ce72cbc35
commit 77b7f7f660
32 changed files with 2657 additions and 109 deletions

View File

@@ -8,8 +8,8 @@ import (
"strings"
"time"
"log/slog"
"gopkg.in/natefinch/lumberjack.v2"
"log/slog"
)
var logger *slog.Logger
@@ -21,7 +21,7 @@ type Config struct {
Rotation bool // enable file rotation
MaxSize int // MB
MaxBackups int
MaxAge int // days
MaxAge int // days
Compress bool
}
@@ -75,7 +75,7 @@ func InitWithConfig(cfg Config) {
}
var handler slog.Handler
switch cfg.Output {
case "stdout":
handler = slog.NewJSONHandler(os.Stdout, opts)
@@ -100,7 +100,7 @@ func InitWithConfig(cfg Config) {
handler = slog.NewJSONHandler(file, opts)
}
}
logger = slog.New(handler)
slog.SetDefault(logger)
}

View File

@@ -12,7 +12,7 @@ func TestInit(t *testing.T) {
defer func() { logger = oldLogger }()
Init()
if logger == nil {
t.Error("logger should not be nil after Init")
}
@@ -21,7 +21,7 @@ func TestInit(t *testing.T) {
func TestInitWithLevel(t *testing.T) {
// Test different levels
levels := []string{"DEBUG", "INFO", "WARN", "ERROR", "unknown"}
for _, level := range levels {
InitWithLevel(level)
if logger == nil {
@@ -46,7 +46,7 @@ func TestParseLevel(t *testing.T) {
{"unknown", slog.LevelInfo},
{"", slog.LevelInfo},
}
for _, test := range tests {
result := parseLevel(test.input)
if result != test.expected {
@@ -64,19 +64,19 @@ func TestIsSensitive(t *testing.T) {
"access_token",
"PRIVATE_KEY",
}
for _, field := range sensitive {
if !IsSensitive(field) {
t.Errorf("IsSensitive(%q) should be true", field)
}
}
notSensitive := []string{
"name",
"email",
"user_id",
}
for _, field := range notSensitive {
if IsSensitive(field) {
t.Errorf("IsSensitive(%q) should be false", field)
@@ -96,7 +96,7 @@ func TestSanitizeAttrs(t *testing.T) {
{"secret_key", "xyz789", "[REDACTED]"},
{"name", "test", "test"},
}
for _, test := range tests {
attr := slog.String(test.key, test.value)
result := sanitizeAttrs(nil, attr)
@@ -109,7 +109,7 @@ func TestSanitizeAttrs(t *testing.T) {
func TestLoggingMethods(t *testing.T) {
// Just verify methods don't panic
Init()
Info("test info message", "key", "value")
Debug("test debug message", "key", "value")
Warn("test warn message", "key", "value")
@@ -138,7 +138,7 @@ func TestInitWithConfig(t *testing.T) {
cfg.Output = "stdout"
cfg.Level = "DEBUG"
InitWithConfig(cfg)
if logger == nil {
t.Error("logger should not be nil after InitWithConfig")
}
@@ -151,9 +151,9 @@ func TestInitWithConfigFileOutput(t *testing.T) {
cfg.Output = tmpFile
cfg.Rotation = false
InitWithConfig(cfg)
Info("test message for file")
// Verify file was created
if _, err := os.Stat(tmpFile); os.IsNotExist(err) {
t.Errorf("log file %s should exist", tmpFile)
@@ -162,27 +162,27 @@ func TestInitWithConfigFileOutput(t *testing.T) {
func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
if cfg.Level != "INFO" {
t.Errorf("default Level = %s, want INFO", cfg.Level)
}
if cfg.Output != "stdout" {
t.Errorf("default Output = %s, want stdout", cfg.Output)
}
if cfg.MaxSize != 100 {
t.Errorf("default MaxSize = %d, want 100", cfg.MaxSize)
}
if cfg.MaxBackups != 3 {
t.Errorf("default MaxBackups = %d, want 3", cfg.MaxBackups)
}
if cfg.MaxAge != 7 {
t.Errorf("default MaxAge = %d, want 7", cfg.MaxAge)
}
if !cfg.Compress {
t.Error("default Compress should be true")
}