go/tests/log_test.go
Snider b2d07e7883 test: 200 tests, 50.2% coverage — Data, I18n, Fs, Log, Embed, Runtime
New tests: Data List/ListNames/Extract, I18n with mock Translator,
Fs full surface (EnsureDir, IsDir, IsFile, Exists, List, Stat, Open,
Create, Append, ReadStream, WriteStream, Delete, DeleteAll, Rename),
Log all levels + Security + Username + Default + LogErr + LogPan,
Embed ScanAssets + GeneratePack + MountEmbed, Runtime ServiceName,
Core LogError/LogWarn/Must helpers.

Fixes: NewCommand inits flagset, New() wires Cli root command + app.

Remaining 0% (excluding CLI/App): compress, getAllFiles (internal),
Reports/appendReport (needs ErrorPanic filePath), SetOutput (trivial).

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-20 10:49:33 +00:00

137 lines
2.9 KiB
Go

package core_test
import (
"testing"
. "forge.lthn.ai/core/go/pkg/core"
"github.com/stretchr/testify/assert"
)
// --- Log ---
func TestLog_New_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelInfo})
assert.NotNil(t, l)
}
func TestLog_AllLevels_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelDebug})
l.Debug("debug")
l.Info("info")
l.Warn("warn")
l.Error("error")
l.Security("security event")
}
func TestLog_LevelFiltering_Good(t *testing.T) {
// At Error level, Debug/Info/Warn should be suppressed (no panic)
l := NewLog(LogOpts{Level: LevelError})
l.Debug("suppressed")
l.Info("suppressed")
l.Warn("suppressed")
l.Error("visible")
}
func TestLog_SetLevel_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelInfo})
l.SetLevel(LevelDebug)
assert.Equal(t, LevelDebug, l.Level())
}
func TestLog_SetRedactKeys_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelInfo})
l.SetRedactKeys("password", "token")
// Redacted keys should mask values in output
l.Info("login", "password", "secret123", "user", "admin")
}
func TestLog_LevelString_Good(t *testing.T) {
assert.Equal(t, "debug", LevelDebug.String())
assert.Equal(t, "info", LevelInfo.String())
assert.Equal(t, "warn", LevelWarn.String())
assert.Equal(t, "error", LevelError.String())
}
func TestLog_CoreLog_Good(t *testing.T) {
c := New()
assert.NotNil(t, c.Log())
}
func TestLog_ErrorSink_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelInfo})
var sink ErrorSink = l
sink.Error("test")
sink.Warn("test")
}
// --- Default Logger ---
func TestLog_Default_Good(t *testing.T) {
d := Default()
assert.NotNil(t, d)
}
func TestLog_SetDefault_Good(t *testing.T) {
original := Default()
defer SetDefault(original)
custom := NewLog(LogOpts{Level: LevelDebug})
SetDefault(custom)
assert.Equal(t, custom, Default())
}
func TestLog_PackageLevelFunctions_Good(t *testing.T) {
// Package-level log functions use the default logger
Debug("debug msg")
Info("info msg")
Warn("warn msg")
Error("error msg")
Security("security msg")
}
func TestLog_PackageSetLevel_Good(t *testing.T) {
original := Default()
defer SetDefault(original)
SetLevel(LevelDebug)
SetRedactKeys("secret")
}
func TestLog_Username_Good(t *testing.T) {
u := Username()
assert.NotEmpty(t, u)
}
// --- LogErr ---
func TestLogErr_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelInfo})
le := NewLogErr(l)
assert.NotNil(t, le)
err := E("test.Op", "something broke", nil)
le.Log(err)
}
func TestLogErr_Nil_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelInfo})
le := NewLogErr(l)
le.Log(nil) // should not panic
}
// --- LogPan ---
func TestLogPan_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelInfo})
lp := NewLogPan(l)
assert.NotNil(t, lp)
}
func TestLogPan_Recover_Good(t *testing.T) {
l := NewLog(LogOpts{Level: LevelInfo})
lp := NewLogPan(l)
assert.NotPanics(t, func() {
defer lp.Recover()
panic("caught")
})
}