go/runtime_test.go
Snider 2dff772a40 feat: implement RFC plans 1-5 — Registry[T], Action/Task, Process, primitives
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>
2026-03-25 15:18:25 +00:00

121 lines
3 KiB
Go

package core_test
import (
"context"
"testing"
. "dappco.re/go/core"
"github.com/stretchr/testify/assert"
)
// --- ServiceRuntime ---
type testOpts struct {
URL string
Timeout int
}
func TestRuntime_ServiceRuntime_Good(t *testing.T) {
c := New()
opts := testOpts{URL: "https://api.lthn.ai", Timeout: 30}
rt := NewServiceRuntime(c, opts)
assert.Equal(t, c, rt.Core())
assert.Equal(t, opts, rt.Options())
assert.Equal(t, "https://api.lthn.ai", rt.Options().URL)
assert.NotNil(t, rt.Config())
}
// --- NewWithFactories ---
func TestRuntime_NewWithFactories_Good(t *testing.T) {
r := NewWithFactories(nil, map[string]ServiceFactory{
"svc1": func() Result { return Result{Value: Service{}, OK: true} },
"svc2": func() Result { return Result{Value: Service{}, OK: true} },
})
assert.True(t, r.OK)
rt := r.Value.(*Runtime)
assert.NotNil(t, rt.Core)
}
func TestRuntime_NewWithFactories_NilFactory_Good(t *testing.T) {
r := NewWithFactories(nil, map[string]ServiceFactory{
"bad": nil,
})
assert.True(t, r.OK) // nil factories skipped
}
func TestRuntime_NewRuntime_Good(t *testing.T) {
r := NewRuntime(nil)
assert.True(t, r.OK)
}
func TestRuntime_ServiceName_Good(t *testing.T) {
r := NewRuntime(nil)
rt := r.Value.(*Runtime)
assert.Equal(t, "Core", rt.ServiceName())
}
// --- Lifecycle via Runtime ---
func TestRuntime_Lifecycle_Good(t *testing.T) {
started := false
r := NewWithFactories(nil, map[string]ServiceFactory{
"test": func() Result {
return Result{Value: Service{
OnStart: func() Result { started = true; return Result{OK: true} },
}, OK: true}
},
})
assert.True(t, r.OK)
rt := r.Value.(*Runtime)
result := rt.ServiceStartup(context.Background(), nil)
assert.True(t, result.OK)
assert.True(t, started)
}
func TestRuntime_ServiceShutdown_Good(t *testing.T) {
stopped := false
r := NewWithFactories(nil, map[string]ServiceFactory{
"test": func() Result {
return Result{Value: Service{
OnStart: func() Result { return Result{OK: true} },
OnStop: func() Result { stopped = true; return Result{OK: true} },
}, OK: true}
},
})
assert.True(t, r.OK)
rt := r.Value.(*Runtime)
rt.ServiceStartup(context.Background(), nil)
result := rt.ServiceShutdown(context.Background())
assert.True(t, result.OK)
assert.True(t, stopped)
}
func TestRuntime_ServiceShutdown_NilCore_Good(t *testing.T) {
rt := &Runtime{}
result := rt.ServiceShutdown(context.Background())
assert.True(t, result.OK)
}
func TestCore_ServiceShutdown_Good(t *testing.T) {
stopped := false
c := New()
c.Service("test", Service{
OnStart: func() Result { return Result{OK: true} },
OnStop: func() Result { stopped = true; return Result{OK: true} },
})
c.ServiceStartup(context.Background(), nil)
result := c.ServiceShutdown(context.Background())
assert.True(t, result.OK)
assert.True(t, stopped)
}
func TestCore_Context_Good(t *testing.T) {
c := New()
c.ServiceStartup(context.Background(), nil)
assert.NotNil(t, c.Context())
c.ServiceShutdown(context.Background())
}