Service is now a proper struct with OnStart/OnStop/OnReload lifecycle
functions — not a registry wrapping any. Packages create Service{} with
typed fields, same pattern as Command and Option.
Result drops generics — Value is any. The struct is the container,
Value is the generic. No more Result[T] ceremony.
Service(name, Service{}) to register, Service(name) to get — both
return Result. ServiceFactory returns Result not (any, error).
NewWithFactories/NewRuntime return Result.
232 tests, 77.8% coverage.
Co-Authored-By: Virgil <virgil@lethean.io>
75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package core_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
. "forge.lthn.ai/core/go/pkg/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// --- Service Registration ---
|
|
|
|
func TestService_Register_Good(t *testing.T) {
|
|
c := New()
|
|
r := c.Service("auth", Service{})
|
|
assert.True(t, r.OK)
|
|
}
|
|
|
|
func TestService_Register_Duplicate_Bad(t *testing.T) {
|
|
c := New()
|
|
c.Service("auth", Service{})
|
|
r := c.Service("auth", Service{})
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestService_Register_Empty_Bad(t *testing.T) {
|
|
c := New()
|
|
r := c.Service("", Service{})
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestService_Get_Good(t *testing.T) {
|
|
c := New()
|
|
c.Service("brain", Service{OnStart: func() Result { return Result{OK: true} }})
|
|
r := c.Service("brain")
|
|
assert.True(t, r.OK)
|
|
assert.NotNil(t, r.Value)
|
|
}
|
|
|
|
func TestService_Get_Bad(t *testing.T) {
|
|
c := New()
|
|
r := c.Service("nonexistent")
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestService_Names_Good(t *testing.T) {
|
|
c := New()
|
|
c.Service("a", Service{})
|
|
c.Service("b", Service{})
|
|
names := c.Services()
|
|
assert.Len(t, names, 2)
|
|
assert.Contains(t, names, "a")
|
|
assert.Contains(t, names, "b")
|
|
}
|
|
|
|
// --- Service Lifecycle ---
|
|
|
|
func TestService_Lifecycle_Good(t *testing.T) {
|
|
c := New()
|
|
started := false
|
|
stopped := false
|
|
c.Service("lifecycle", Service{
|
|
OnStart: func() Result { started = true; return Result{OK: true} },
|
|
OnStop: func() Result { stopped = true; return Result{OK: true} },
|
|
})
|
|
|
|
startables := c.Startables()
|
|
assert.Len(t, startables, 1)
|
|
startables[0].OnStart()
|
|
assert.True(t, started)
|
|
|
|
stoppables := c.Stoppables()
|
|
assert.Len(t, stoppables, 1)
|
|
stoppables[0].OnStop()
|
|
assert.True(t, stopped)
|
|
}
|