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>
318 lines
7.8 KiB
Go
318 lines
7.8 KiB
Go
package core_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
. "dappco.re/go/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// --- New ---
|
|
|
|
func TestCore_New_Good(t *testing.T) {
|
|
c := New()
|
|
assert.NotNil(t, c)
|
|
}
|
|
|
|
func TestCore_New_WithOptions_Good(t *testing.T) {
|
|
c := New(WithOptions(NewOptions(Option{Key: "name", Value: "myapp"})))
|
|
assert.NotNil(t, c)
|
|
assert.Equal(t, "myapp", c.App().Name)
|
|
}
|
|
|
|
func TestCore_New_WithOptions_Bad(t *testing.T) {
|
|
// Empty options — should still create a valid Core
|
|
c := New(WithOptions(NewOptions()))
|
|
assert.NotNil(t, c)
|
|
}
|
|
|
|
func TestCore_New_WithService_Good(t *testing.T) {
|
|
started := false
|
|
c := New(
|
|
WithOptions(NewOptions(Option{Key: "name", Value: "myapp"})),
|
|
WithService(func(c *Core) Result {
|
|
c.Service("test", Service{
|
|
OnStart: func() Result { started = true; return Result{OK: true} },
|
|
})
|
|
return Result{OK: true}
|
|
}),
|
|
)
|
|
|
|
svc := c.Service("test")
|
|
assert.True(t, svc.OK)
|
|
|
|
c.ServiceStartup(context.Background(), nil)
|
|
assert.True(t, started)
|
|
}
|
|
|
|
func TestCore_New_WithServiceLock_Good(t *testing.T) {
|
|
c := New(
|
|
WithService(func(c *Core) Result {
|
|
c.Service("allowed", Service{})
|
|
return Result{OK: true}
|
|
}),
|
|
WithServiceLock(),
|
|
)
|
|
|
|
// Registration after lock should fail
|
|
reg := c.Service("blocked", Service{})
|
|
assert.False(t, reg.OK)
|
|
}
|
|
|
|
func TestCore_New_WithService_Bad_FailingOption(t *testing.T) {
|
|
secondCalled := false
|
|
_ = New(
|
|
WithService(func(c *Core) Result {
|
|
return Result{Value: E("test", "intentional failure", nil), OK: false}
|
|
}),
|
|
WithService(func(c *Core) Result {
|
|
secondCalled = true
|
|
return Result{OK: true}
|
|
}),
|
|
)
|
|
assert.False(t, secondCalled, "second option should not run after first fails")
|
|
}
|
|
|
|
// --- Accessors ---
|
|
|
|
func TestCore_Accessors_Good(t *testing.T) {
|
|
c := New()
|
|
assert.NotNil(t, c.App())
|
|
assert.NotNil(t, c.Data())
|
|
assert.NotNil(t, c.Drive())
|
|
assert.NotNil(t, c.Fs())
|
|
assert.NotNil(t, c.Config())
|
|
assert.NotNil(t, c.Error())
|
|
assert.NotNil(t, c.Log())
|
|
assert.NotNil(t, c.Cli())
|
|
assert.NotNil(t, c.IPC())
|
|
assert.NotNil(t, c.I18n())
|
|
assert.Equal(t, c, c.Core())
|
|
}
|
|
|
|
func TestOptions_Accessor_Good(t *testing.T) {
|
|
c := New(WithOptions(NewOptions(
|
|
Option{Key: "name", Value: "testapp"},
|
|
Option{Key: "port", Value: 8080},
|
|
Option{Key: "debug", Value: true},
|
|
)))
|
|
opts := c.Options()
|
|
assert.NotNil(t, opts)
|
|
assert.Equal(t, "testapp", opts.String("name"))
|
|
assert.Equal(t, 8080, opts.Int("port"))
|
|
assert.True(t, opts.Bool("debug"))
|
|
}
|
|
|
|
func TestOptions_Accessor_Nil(t *testing.T) {
|
|
c := New()
|
|
// No options passed — Options() returns nil
|
|
assert.Nil(t, c.Options())
|
|
}
|
|
|
|
// --- Core Error/Log Helpers ---
|
|
|
|
func TestCore_LogError_Good(t *testing.T) {
|
|
c := New()
|
|
cause := assert.AnError
|
|
r := c.LogError(cause, "test.Operation", "something broke")
|
|
|
|
err, ok := r.Value.(error)
|
|
assert.True(t, ok)
|
|
assert.ErrorIs(t, err, cause)
|
|
}
|
|
|
|
func TestCore_LogWarn_Good(t *testing.T) {
|
|
c := New()
|
|
r := c.LogWarn(assert.AnError, "test.Operation", "heads up")
|
|
|
|
_, ok := r.Value.(error)
|
|
assert.True(t, ok)
|
|
}
|
|
|
|
func TestCore_Must_Ugly(t *testing.T) {
|
|
c := New()
|
|
assert.Panics(t, func() {
|
|
c.Must(assert.AnError, "test.Operation", "fatal")
|
|
})
|
|
}
|
|
|
|
func TestCore_Must_Nil_Good(t *testing.T) {
|
|
c := New()
|
|
assert.NotPanics(t, func() {
|
|
c.Must(nil, "test.Operation", "no error")
|
|
})
|
|
}
|
|
|
|
// --- RegistryOf ---
|
|
|
|
func TestCore_RegistryOf_Good_Services(t *testing.T) {
|
|
c := New(
|
|
WithService(func(c *Core) Result {
|
|
return c.Service("alpha", Service{})
|
|
}),
|
|
WithService(func(c *Core) Result {
|
|
return c.Service("bravo", Service{})
|
|
}),
|
|
)
|
|
reg := c.RegistryOf("services")
|
|
// cli is auto-registered + our 2
|
|
assert.True(t, reg.Has("alpha"))
|
|
assert.True(t, reg.Has("bravo"))
|
|
assert.True(t, reg.Has("cli"))
|
|
}
|
|
|
|
func TestCore_RegistryOf_Good_Commands(t *testing.T) {
|
|
c := New()
|
|
c.Command("deploy", Command{Action: func(_ Options) Result { return Result{OK: true} }})
|
|
c.Command("test", Command{Action: func(_ Options) Result { return Result{OK: true} }})
|
|
|
|
reg := c.RegistryOf("commands")
|
|
assert.True(t, reg.Has("deploy"))
|
|
assert.True(t, reg.Has("test"))
|
|
}
|
|
|
|
func TestCore_RegistryOf_Good_Actions(t *testing.T) {
|
|
c := New()
|
|
c.Action("process.run", func(_ context.Context, _ Options) Result { return Result{OK: true} })
|
|
c.Action("brain.recall", func(_ context.Context, _ Options) Result { return Result{OK: true} })
|
|
|
|
reg := c.RegistryOf("actions")
|
|
assert.True(t, reg.Has("process.run"))
|
|
assert.True(t, reg.Has("brain.recall"))
|
|
assert.Equal(t, 2, reg.Len())
|
|
}
|
|
|
|
func TestCore_RegistryOf_Bad_Unknown(t *testing.T) {
|
|
c := New()
|
|
reg := c.RegistryOf("nonexistent")
|
|
assert.Equal(t, 0, reg.Len(), "unknown registry returns empty")
|
|
}
|
|
|
|
// --- RunE ---
|
|
|
|
func TestCore_RunE_Good(t *testing.T) {
|
|
c := New(
|
|
WithService(func(c *Core) Result {
|
|
return c.Service("healthy", Service{
|
|
OnStart: func() Result { return Result{OK: true} },
|
|
OnStop: func() Result { return Result{OK: true} },
|
|
})
|
|
}),
|
|
)
|
|
err := c.RunE()
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestCore_RunE_Bad_StartupFailure(t *testing.T) {
|
|
c := New(
|
|
WithService(func(c *Core) Result {
|
|
return c.Service("broken", Service{
|
|
OnStart: func() Result {
|
|
return Result{Value: NewError("startup failed"), OK: false}
|
|
},
|
|
})
|
|
}),
|
|
)
|
|
err := c.RunE()
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "startup failed")
|
|
}
|
|
|
|
func TestCore_RunE_Ugly_StartupFailureCallsShutdown(t *testing.T) {
|
|
shutdownCalled := false
|
|
c := New(
|
|
WithService(func(c *Core) Result {
|
|
return c.Service("cleanup", Service{
|
|
OnStart: func() Result { return Result{OK: true} },
|
|
OnStop: func() Result { shutdownCalled = true; return Result{OK: true} },
|
|
})
|
|
}),
|
|
WithService(func(c *Core) Result {
|
|
return c.Service("broken", Service{
|
|
OnStart: func() Result {
|
|
return Result{Value: NewError("boom"), OK: false}
|
|
},
|
|
})
|
|
}),
|
|
)
|
|
err := c.RunE()
|
|
assert.Error(t, err)
|
|
assert.True(t, shutdownCalled, "ServiceShutdown must be called even when startup fails — cleanup service must get OnStop")
|
|
}
|
|
|
|
func TestCore_Run_HelperProcess(t *testing.T) {
|
|
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
|
|
return
|
|
}
|
|
|
|
switch os.Getenv("CORE_RUN_MODE") {
|
|
case "startup-fail":
|
|
c := New(
|
|
WithService(func(c *Core) Result {
|
|
return c.Service("broken", Service{
|
|
OnStart: func() Result {
|
|
return Result{Value: NewError("startup failed"), OK: false}
|
|
},
|
|
})
|
|
}),
|
|
)
|
|
c.Run()
|
|
case "cli-fail":
|
|
shutdownFile := os.Getenv("CORE_RUN_SHUTDOWN_FILE")
|
|
c := New(
|
|
WithService(func(c *Core) Result {
|
|
return c.Service("cleanup", Service{
|
|
OnStop: func() Result {
|
|
if err := os.WriteFile(shutdownFile, []byte("stopped"), 0o600); err != nil {
|
|
return Result{Value: err, OK: false}
|
|
}
|
|
return Result{OK: true}
|
|
},
|
|
})
|
|
}),
|
|
)
|
|
c.Command("explode", Command{
|
|
Action: func(_ Options) Result {
|
|
return Result{Value: NewError("cli failed"), OK: false}
|
|
},
|
|
})
|
|
os.Args = []string{"core-test", "explode"}
|
|
c.Run()
|
|
default:
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
|
|
func TestCore_Run_Bad(t *testing.T) {
|
|
err := runCoreRunHelper(t, "startup-fail")
|
|
var exitErr *exec.ExitError
|
|
if assert.ErrorAs(t, err, &exitErr) {
|
|
assert.Equal(t, 1, exitErr.ExitCode())
|
|
}
|
|
}
|
|
|
|
func TestCore_Run_Ugly(t *testing.T) {
|
|
shutdownFile := filepath.Join(t.TempDir(), "shutdown.txt")
|
|
err := runCoreRunHelper(t, "cli-fail", "CORE_RUN_SHUTDOWN_FILE="+shutdownFile)
|
|
var exitErr *exec.ExitError
|
|
if assert.ErrorAs(t, err, &exitErr) {
|
|
assert.Equal(t, 1, exitErr.ExitCode())
|
|
}
|
|
|
|
data, readErr := os.ReadFile(shutdownFile)
|
|
assert.NoError(t, readErr)
|
|
assert.Equal(t, "stopped", string(data))
|
|
}
|
|
|
|
func runCoreRunHelper(t *testing.T, mode string, extraEnv ...string) error {
|
|
t.Helper()
|
|
|
|
cmd := exec.Command(os.Args[0], "-test.run=^TestCore_Run_HelperProcess$")
|
|
cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=1", "CORE_RUN_MODE="+mode)
|
|
cmd.Env = append(cmd.Env, extraEnv...)
|
|
return cmd.Run()
|
|
}
|