cleanup: remove dead code and add regression tests

This commit is contained in:
nayihz 2026-02-19 13:19:31 +08:00
parent 80c8b57533
commit 16f73a3b15
5 changed files with 70 additions and 86 deletions

View file

@ -288,25 +288,6 @@ func (cb *ContextBuilder) AddAssistantMessage(
return messages
}
func (cb *ContextBuilder) loadSkills() string {
allSkills := cb.skillsLoader.ListSkills()
if len(allSkills) == 0 {
return ""
}
var skillNames []string
for _, s := range allSkills {
skillNames = append(skillNames, s.Name)
}
content := cb.skillsLoader.LoadSkillsForContext(skillNames)
if content == "" {
return ""
}
return "# Skill Definitions\n\n" + content
}
// GetSkillsInfo returns information about loaded skills.
func (cb *ContextBuilder) GetSkillsInfo() map[string]any {
allSkills := cb.skillsLoader.ListSkills()

View file

@ -61,41 +61,6 @@ func SetLevel(level LogLevel) {
currentLevel = level
}
func GetLevel() LogLevel {
mu.RLock()
defer mu.RUnlock()
return currentLevel
}
func EnableFileLogging(filePath string) error {
mu.Lock()
defer mu.Unlock()
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
return fmt.Errorf("failed to open log file: %w", err)
}
if logger.file != nil {
logger.file.Close()
}
logger.file = file
log.Println("File logging enabled:", filePath)
return nil
}
func DisableFileLogging() {
mu.Lock()
defer mu.Unlock()
if logger.file != nil {
logger.file.Close()
logger.file = nil
log.Println("File logging disabled")
}
}
func logMessage(level LogLevel, component string, message string, fields map[string]any) {
if level < currentLevel {
return

View file

@ -4,10 +4,18 @@ import (
"testing"
)
func TestLogLevelFiltering(t *testing.T) {
initialLevel := GetLevel()
defer SetLevel(initialLevel)
func preserveLogLevel(t *testing.T) {
t.Helper()
mu.RLock()
prev := currentLevel
mu.RUnlock()
t.Cleanup(func() {
SetLevel(prev)
})
}
func TestLogLevelFiltering(t *testing.T) {
preserveLogLevel(t)
SetLevel(WARN)
tests := []struct {
@ -41,13 +49,10 @@ func TestLogLevelFiltering(t *testing.T) {
})
}
SetLevel(INFO)
}
func TestLoggerWithComponent(t *testing.T) {
initialLevel := GetLevel()
defer SetLevel(initialLevel)
preserveLogLevel(t)
SetLevel(DEBUG)
tests := []struct {
@ -77,7 +82,6 @@ func TestLoggerWithComponent(t *testing.T) {
})
}
SetLevel(INFO)
}
func TestLogLevels(t *testing.T) {
@ -102,24 +106,8 @@ func TestLogLevels(t *testing.T) {
}
}
func TestSetGetLevel(t *testing.T) {
initialLevel := GetLevel()
defer SetLevel(initialLevel)
tests := []LogLevel{DEBUG, INFO, WARN, ERROR, FATAL}
for _, level := range tests {
SetLevel(level)
if GetLevel() != level {
t.Errorf("SetLevel(%v) -> GetLevel() = %v, want %v", level, GetLevel(), level)
}
}
}
func TestLoggerHelperFunctions(t *testing.T) {
initialLevel := GetLevel()
defer SetLevel(initialLevel)
preserveLogLevel(t)
SetLevel(INFO)
Debug("This should not log")
@ -137,3 +125,23 @@ func TestLoggerHelperFunctions(t *testing.T) {
DebugC("test", "Debug with component")
WarnF("Warning with fields", map[string]any{"key": "value"})
}
func TestSetLevelUpdatesGlobalState(t *testing.T) {
preserveLogLevel(t)
SetLevel(WARN)
mu.RLock()
gotWarn := currentLevel
mu.RUnlock()
if gotWarn != WARN {
t.Fatalf("currentLevel = %v, want %v", gotWarn, WARN)
}
SetLevel(DEBUG)
mu.RLock()
gotDebug := currentLevel
mu.RUnlock()
if gotDebug != DEBUG {
t.Fatalf("currentLevel = %v, want %v", gotDebug, DEBUG)
}
}

View file

@ -134,10 +134,3 @@ func DownloadFile(url, filename string, opts DownloadOptions) string {
return localPath
}
// DownloadFileSimple is a simplified version of DownloadFile without options
func DownloadFileSimple(url, filename string) string {
return DownloadFile(url, filename, DownloadOptions{
LoggerPrefix: "media",
})
}

37
pkg/utils/media_test.go Normal file
View file

@ -0,0 +1,37 @@
package utils
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestDownloadFile_WithDefaultOptions(t *testing.T) {
t.Parallel()
const body = "hello from media download"
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(body))
}))
defer srv.Close()
path := DownloadFile(srv.URL, "sample.txt", DownloadOptions{})
if path == "" {
t.Fatal("DownloadFile() returned empty path")
}
t.Cleanup(func() { _ = os.Remove(path) })
if got := filepath.Base(path); got == "sample.txt" {
t.Fatalf("expected uuid-prefixed filename, got %q", got)
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("os.ReadFile() error: %v", err)
}
if string(data) != body {
t.Fatalf("downloaded content = %q, want %q", string(data), body)
}
}