- WithService now calls factory, discovers service name from package path via
reflect/runtime (last path segment, _test suffix stripped, lowercased), and
calls RegisterService — which handles Startable/Stoppable/HandleIPCEvents
- If factory returns nil Value (self-registered), WithService returns OK without
a second registration
- Add contract_test.go with _Good/_Bad tests covering all three code paths
- Fix core.go Cli() accessor: use ServiceFor[*Cli](c, "cli") (was cli.New())
- Fix pre-existing })) → }}) syntax errors in command_test, service_test, lock_test
- Fix pre-existing Options{...} → NewOptions(...) in core_test, data_test,
drive_test, i18n_test (Options is a struct, not a slice)
Co-Authored-By: Virgil <virgil@lethean.io>
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package core_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
. "dappco.re/go/core"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// --- Service Registration ---
|
|
|
|
func TestService_Register_Good(t *testing.T) {
|
|
c := New().Value.(*Core)
|
|
r := c.Service("auth", Service{})
|
|
assert.True(t, r.OK)
|
|
}
|
|
|
|
func TestService_Register_Duplicate_Bad(t *testing.T) {
|
|
c := New().Value.(*Core)
|
|
c.Service("auth", Service{})
|
|
r := c.Service("auth", Service{})
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestService_Register_Empty_Bad(t *testing.T) {
|
|
c := New().Value.(*Core)
|
|
r := c.Service("", Service{})
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestService_Get_Good(t *testing.T) {
|
|
c := New().Value.(*Core)
|
|
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().Value.(*Core)
|
|
r := c.Service("nonexistent")
|
|
assert.False(t, r.OK)
|
|
}
|
|
|
|
func TestService_Names_Good(t *testing.T) {
|
|
c := New().Value.(*Core)
|
|
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().Value.(*Core)
|
|
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} },
|
|
})
|
|
|
|
sr := c.Startables()
|
|
assert.True(t, sr.OK)
|
|
startables := sr.Value.([]*Service)
|
|
assert.Len(t, startables, 1)
|
|
startables[0].OnStart()
|
|
assert.True(t, started)
|
|
|
|
tr := c.Stoppables()
|
|
assert.True(t, tr.OK)
|
|
stoppables := tr.Value.([]*Service)
|
|
assert.Len(t, stoppables, 1)
|
|
stoppables[0].OnStop()
|
|
assert.True(t, stopped)
|
|
}
|