go/tests/runtime_test.go
Snider b2d07e7883 test: 200 tests, 50.2% coverage — Data, I18n, Fs, Log, Embed, Runtime
New tests: Data List/ListNames/Extract, I18n with mock Translator,
Fs full surface (EnsureDir, IsDir, IsFile, Exists, List, Stat, Open,
Create, Append, ReadStream, WriteStream, Delete, DeleteAll, Rename),
Log all levels + Security + Username + Default + LogErr + LogPan,
Embed ScanAssets + GeneratePack + MountEmbed, Runtime ServiceName,
Core LogError/LogWarn/Must helpers.

Fixes: NewCommand inits flagset, New() wires Cli root command + app.

Remaining 0% (excluding CLI/App): compress, getAllFiles (internal),
Reports/appendReport (needs ErrorPanic filePath), SetOutput (trivial).

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-20 10:49:33 +00:00

102 lines
2.4 KiB
Go

package core_test
import (
"context"
"errors"
"testing"
. "forge.lthn.ai/core/go/pkg/core"
"github.com/stretchr/testify/assert"
)
// --- ServiceRuntime ---
type testOpts struct {
URL string
Timeout int
}
type runtimeService struct {
*ServiceRuntime[testOpts]
}
func TestServiceRuntime_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.Opts())
assert.Equal(t, "https://api.lthn.ai", rt.Opts().URL)
assert.Equal(t, 30, rt.Opts().Timeout)
assert.NotNil(t, rt.Config())
}
func TestServiceRuntime_Embedded_Good(t *testing.T) {
c := New()
svc := &runtimeService{
ServiceRuntime: NewServiceRuntime(c, testOpts{URL: "https://lthn.sh"}),
}
assert.Equal(t, "https://lthn.sh", svc.Opts().URL)
}
// --- NewWithFactories ---
func TestNewWithFactories_Good(t *testing.T) {
rt, err := NewWithFactories(nil, map[string]ServiceFactory{
"svc1": func() (any, error) { return &testService{name: "one"}, nil },
"svc2": func() (any, error) { return &testService{name: "two"}, nil },
})
assert.NoError(t, err)
assert.NotNil(t, rt)
assert.NotNil(t, rt.Core)
svc := rt.Core.Service("svc1")
assert.NotNil(t, svc)
ts, ok := svc.(*testService)
assert.True(t, ok)
assert.Equal(t, "one", ts.name)
}
func TestNewWithFactories_Bad(t *testing.T) {
// Nil factory
_, err := NewWithFactories(nil, map[string]ServiceFactory{
"bad": nil,
})
assert.Error(t, err)
// Factory returns error
_, err = NewWithFactories(nil, map[string]ServiceFactory{
"fail": func() (any, error) { return nil, errors.New("factory failed") },
})
assert.Error(t, err)
}
func TestNewRuntime_Good(t *testing.T) {
rt, err := NewRuntime(nil)
assert.NoError(t, err)
assert.NotNil(t, rt)
}
// --- Lifecycle via Runtime ---
func TestRuntime_Lifecycle_Good(t *testing.T) {
svc := &testService{name: "lifecycle"}
rt, err := NewWithFactories(nil, map[string]ServiceFactory{
"test": func() (any, error) { return svc, nil },
})
assert.NoError(t, err)
err = rt.ServiceStartup(context.Background(), nil)
assert.NoError(t, err)
assert.True(t, svc.started)
err = rt.ServiceShutdown(context.Background())
assert.NoError(t, err)
assert.True(t, svc.stopped)
}
func TestRuntime_ServiceName_Good(t *testing.T) {
rt, _ := NewRuntime(nil)
assert.Equal(t, "Core", rt.ServiceName())
}