Plans 1-5 complete for core/go scope. 456 tests, 84.4% coverage, 100% AX-7 naming.
Critical bugs (Plan 1):
- P4-3+P7-3: ACTION broadcast calls all handlers with panic recovery
- P7-2+P7-4: RunE() with defer ServiceShutdown, Run() delegates
- P3-1: Startable/Stoppable return Result (breaking, clean)
- P9-1: Zero os/exec — App.Find() rewritten with os.Stat+PATH
- I3: Embed() removed, I15: New() comment fixed
- I9: CommandLifecycle removed → Command.Managed field
Registry[T] (Plan 2):
- Universal thread-safe named collection with 3 lock modes
- All 5 registries migrated: services, commands, drive, data, lock
- Insertion order preserved (fixes P4-1)
- c.RegistryOf("name") cross-cutting accessor
Action/Task system (Plan 3):
- Action type with Run()/Exists(), ActionHandler signature
- c.Action("name") dual-purpose accessor (register/invoke)
- TaskDef with Steps — sequential chain, async dispatch, previous-input piping
- Panic recovery on all Action execution
- broadcast() internal, ACTION() sugar
Process primitive (Plan 4):
- c.Process() returns Action sugar — Run/RunIn/RunWithEnv/Start/Kill/Exists
- No deps added — delegates to c.Action("process.*")
- Permission-by-registration: no handler = no capability
Missing primitives (Plan 5):
- core.ID() — atomic counter + crypto/rand suffix
- ValidateName() / SanitisePath() — reusable validation
- Fs.WriteAtomic() — write-to-temp-then-rename
- Fs.NewUnrestricted() / Fs.Root() — legitimate sandbox bypass
- AX-7: 456/456 tests renamed to TestFile_Function_{Good,Bad,Ugly}
Co-Authored-By: Virgil <virgil@lethean.io>
165 lines
3.6 KiB
Go
165 lines
3.6 KiB
Go
package core_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
. "dappco.re/go/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// --- Log ---
|
|
|
|
func TestLog_New_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelInfo})
|
|
assert.NotNil(t, l)
|
|
}
|
|
|
|
func TestLog_AllLevels_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{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(LogOptions{Level: LevelError})
|
|
l.Debug("suppressed")
|
|
l.Info("suppressed")
|
|
l.Warn("suppressed")
|
|
l.Error("visible")
|
|
}
|
|
|
|
func TestLog_SetLevel_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelInfo})
|
|
l.SetLevel(LevelDebug)
|
|
assert.Equal(t, LevelDebug, l.Level())
|
|
}
|
|
|
|
func TestLog_SetRedactKeys_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{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(LogOptions{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(LogOptions{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 TestLog_LogErr_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelInfo})
|
|
le := NewLogErr(l)
|
|
assert.NotNil(t, le)
|
|
|
|
err := E("test.Operation", "something broke", nil)
|
|
le.Log(err)
|
|
}
|
|
|
|
func TestLog_LogErr_Nil_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelInfo})
|
|
le := NewLogErr(l)
|
|
le.Log(nil) // should not panic
|
|
}
|
|
|
|
// --- LogPanic ---
|
|
|
|
func TestLog_LogPanic_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelInfo})
|
|
lp := NewLogPanic(l)
|
|
assert.NotNil(t, lp)
|
|
}
|
|
|
|
func TestLog_LogPanic_Recover_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelInfo})
|
|
lp := NewLogPanic(l)
|
|
assert.NotPanics(t, func() {
|
|
defer lp.Recover()
|
|
panic("caught")
|
|
})
|
|
}
|
|
|
|
// --- SetOutput ---
|
|
|
|
func TestLog_SetOutput_Good(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelInfo})
|
|
l.SetOutput(os.Stderr)
|
|
l.Info("redirected")
|
|
}
|
|
|
|
// --- Log suppression by level ---
|
|
|
|
func TestLog_Quiet_Suppresses_Ugly(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelQuiet})
|
|
// These should not panic even though nothing is logged
|
|
l.Debug("suppressed")
|
|
l.Info("suppressed")
|
|
l.Warn("suppressed")
|
|
l.Error("suppressed")
|
|
}
|
|
|
|
func TestLog_ErrorLevel_Suppresses_Ugly(t *testing.T) {
|
|
l := NewLog(LogOptions{Level: LevelError})
|
|
l.Debug("suppressed") // below threshold
|
|
l.Info("suppressed") // below threshold
|
|
l.Warn("suppressed") // below threshold
|
|
l.Error("visible") // at threshold
|
|
}
|