Feature add tdd core tests (#22)

* feat: Add TDD tests for core package

Adds a new `tdd/` directory for TDD-style contract tests.

Implements a comprehensive test suite for the `pkg/core` package, covering:
- `New()`
- `WithService()`
- `WithName()`
- `WithWails()`
- `WithAssets()`
- `WithServiceLock()`
- `RegisterService()`
- `Service()`
- `ServiceFor()`
- `MustServiceFor()`
- `ACTION()`
- `RegisterAction()`
- `RegisterActions()`

To support testing, a public `Assets()` method was added to the `Core` struct.

* feat: Add TDD tests for e, io, runtime, and config packages

Adds comprehensive TDD tests to the `tdd/` directory for the following packages:
- `pkg/e`
- `pkg/io`
- `pkg/runtime`
- `pkg/config`

This significantly improves the test coverage of the project.

To support testing the `runtime` package, the `newWithFactories` function was exported as `NewWithFactories`.

The existing tests for the `config` package were moved from the `internal` package to the `tdd/` directory and adapted to use the public API.

* fix: Update tdd tests for config, core, and runtime

Updates the TDD tests for the `config`, `core`, and `runtime` packages to improve their coverage and correctness.

- In `tdd/config_test.go`, the `TestIsFeatureEnabled` test is updated to use `s.Set` to modify the `features` slice, ensuring that the persistence logic is exercised.
- In `tdd/core_test.go`, the `TestCore_WithAssets_Good` test is updated to use a real embedded filesystem with `//go:embed` to verify the contents of a test file.
- In `tdd/runtime_test.go`, the `TestNew_Good` test is converted to a table-driven test to cover the happy path, error cases, and a case with a non-nil `application.App`.

* fix: Fix build and improve test coverage

This commit fixes a build failure in the `pkg/runtime` tests and significantly improves the test coverage for several packages.

- Fixes a build failure in `pkg/runtime/runtime_test.go` by updating a call to an exported function.
- Moves TDD tests for `config` and `e` packages into their respective package directories to ensure accurate coverage reporting.
- Adds a new test suite for the `pkg/i18n` package, including a test helper to inject a mock i18n bundle.
- Moves and updates tests for the `pkg/crypt` package to use its public API.
- The coverage for `config` and `e` is now 100%.
- The coverage for `crypt` and `i18n` has been significantly improved.

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Snider 2025-11-02 22:29:17 +00:00 committed by GitHub
parent 08532b0a27
commit 32f1d0ab5d
12 changed files with 793 additions and 7 deletions

206
pkg/config/config_test.go Normal file
View file

@ -0,0 +1,206 @@
package config
import (
"os"
"path/filepath"
"testing"
"github.com/Snider/Core/pkg/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const appName = "lethean"
const configFileName = "config.json"
// setupTestEnv creates a temporary home directory for testing and ensures a clean environment.
func setupTestEnv(t *testing.T) (string, func()) {
tempHomeDir, err := os.MkdirTemp("", "test_home_*")
require.NoError(t, err, "Failed to create temp home directory")
oldHome := os.Getenv("HOME")
os.Setenv("HOME", tempHomeDir)
// Unset XDG vars to ensure HOME is used for path resolution, creating a hermetic test.
oldXdgData, hadXdgData := os.LookupEnv("XDG_DATA_HOME")
oldXdgCache, hadXdgCache := os.LookupEnv("XDG_CACHE_HOME")
require.NoError(t, os.Unsetenv("XDG_DATA_HOME"))
require.NoError(t, os.Unsetenv("XDG_CACHE_HOME"))
cleanup := func() {
os.Setenv("HOME", oldHome)
if hadXdgData {
os.Setenv("XDG_DATA_HOME", oldXdgData)
} else {
os.Unsetenv("XDG_DATA_HOME")
}
if hadXdgCache {
os.Setenv("XDG_CACHE_HOME", oldXdgCache)
} else {
os.Unsetenv("XDG_CACHE_HOME")
}
os.RemoveAll(tempHomeDir)
}
return tempHomeDir, cleanup
}
func TestConfigService(t *testing.T) {
t.Run("New service creates default config", func(t *testing.T) {
_, cleanup := setupTestEnv(t)
defer cleanup()
serviceInstance, err := New()
require.NoError(t, err, "New() failed")
// Check that the config file was created
assert.FileExists(t, serviceInstance.ConfigPath, "config.json was not created")
// Check default values
assert.Equal(t, "en", serviceInstance.Language, "Expected default language 'en'")
})
t.Run("New service loads existing config", func(t *testing.T) {
tempHomeDir, cleanup := setupTestEnv(t)
defer cleanup()
// Manually create a config file with non-default values
configDir := filepath.Join(tempHomeDir, appName, "config")
require.NoError(t, os.MkdirAll(configDir, os.ModePerm), "Failed to create test config dir")
configPath := filepath.Join(configDir, configFileName)
customConfig := `{"language": "fr", "features": ["beta-testing"]}`
require.NoError(t, os.WriteFile(configPath, []byte(customConfig), 0644), "Failed to write custom config file")
serviceInstance, err := New()
require.NoError(t, err, "New() failed while loading existing config")
assert.Equal(t, "fr", serviceInstance.Language, "Expected language 'fr'")
assert.True(t, serviceInstance.IsFeatureEnabled("beta-testing"), "Expected 'beta-testing' feature to be enabled")
assert.False(t, serviceInstance.IsFeatureEnabled("alpha-testing"), "Did not expect 'alpha-testing' to be enabled")
})
t.Run("Set and Get", func(t *testing.T) {
_, cleanup := setupTestEnv(t)
defer cleanup()
s, err := New()
require.NoError(t, err, "New() failed")
key := "language"
expectedValue := "de"
require.NoError(t, s.Set(key, expectedValue), "Set() failed")
var actualValue string
require.NoError(t, s.Get(key, &actualValue), "Get() failed")
assert.Equal(t, expectedValue, actualValue, "Get() returned unexpected value")
})
}
func TestIsFeatureEnabled(t *testing.T) {
_, cleanup := setupTestEnv(t)
defer cleanup()
s, err := New()
require.NoError(t, err)
// Test with no features enabled
assert.False(t, s.IsFeatureEnabled("beta-feature"))
// Enable a feature
err = s.Set("features", []string{"beta-feature", "alpha-testing"})
require.NoError(t, err)
// Test for an enabled feature
assert.True(t, s.IsFeatureEnabled("beta-feature"))
// Test for another enabled feature
assert.True(t, s.IsFeatureEnabled("alpha-testing"))
// Test for a disabled feature
assert.False(t, s.IsFeatureEnabled("gamma-feature"))
// Test with an empty string
assert.False(t, s.IsFeatureEnabled(""))
}
func TestSet_Good(t *testing.T) {
_, cleanup := setupTestEnv(t)
defer cleanup()
s, err := New()
require.NoError(t, err, "New() failed")
// Test setting a string value
err = s.Set("language", "de")
assert.NoError(t, err)
var lang string
err = s.Get("language", &lang)
assert.NoError(t, err)
assert.Equal(t, "de", lang)
// Test setting a slice value
err = s.Set("features", []string{"new-feature"})
assert.NoError(t, err)
var features []string
err = s.Get("features", &features)
assert.NoError(t, err)
assert.Equal(t, []string{"new-feature"}, features)
}
func TestSet_Bad(t *testing.T) {
_, cleanup := setupTestEnv(t)
defer cleanup()
s, err := New()
require.NoError(t, err, "New() failed")
// Test setting a value with the wrong type
err = s.Set("language", 123)
assert.Error(t, err)
// Test setting a non-existent key
err = s.Set("nonExistentKey", "value")
assert.Error(t, err)
}
func TestSet_Ugly(t *testing.T) {
_, cleanup := setupTestEnv(t)
defer cleanup()
s, err := New()
require.NoError(t, err, "New() failed")
// This should not panic
assert.NotPanics(t, func() {
err = s.Set("features", nil)
})
assert.NoError(t, err)
// Verify the slice is now nil
var features []string
err = s.Get("features", &features)
assert.NoError(t, err)
assert.Nil(t, features)
// Test with a nil slice
err = s.Set("features", nil)
require.NoError(t, err)
assert.False(t, s.IsFeatureEnabled("beta-feature"))
}
func TestRegister_Good(t *testing.T) {
_, cleanup := setupTestEnv(t)
defer cleanup()
c, err := core.New()
require.NoError(t, err)
svc, err := Register(c)
assert.NoError(t, err)
assert.NotNil(t, svc)
configSvc, ok := svc.(*Service)
assert.True(t, ok)
assert.NotNil(t, configSvc.Runtime)
}

View file

@ -211,3 +211,7 @@ func (c *Core) Display() Display {
} }
func (c *Core) Core() *Core { return c } func (c *Core) Core() *Core { return c }
func (c *Core) Assets() embed.FS {
return c.assets
}

View file

@ -1,4 +1,4 @@
package internal package crypt
import ( import (
"testing" "testing"
@ -7,14 +7,16 @@ import (
) )
func TestHash(t *testing.T) { func TestHash(t *testing.T) {
s := &Service{} s, err := New()
assert.NoError(t, err)
payload := "hello" payload := "hello"
hash := s.Hash(LTHN, payload) hash := s.Hash(LTHN, payload)
assert.NotEmpty(t, hash) assert.NotEmpty(t, hash)
} }
func TestLuhn(t *testing.T) { func TestLuhn(t *testing.T) {
s := &Service{} s, err := New()
assert.NoError(t, err)
assert.True(t, s.Luhn("79927398713")) assert.True(t, s.Luhn("79927398713"))
assert.False(t, s.Luhn("79927398714")) assert.False(t, s.Luhn("79927398714"))
} }

29
pkg/e/e_test.go Normal file
View file

@ -0,0 +1,29 @@
package e
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestE_Good(t *testing.T) {
err := E("test.op", "test message", assert.AnError)
assert.Error(t, err)
assert.Equal(t, "test.op: test message: assert.AnError general error for testing", err.Error())
err = E("test.op", "test message", nil)
assert.Error(t, err)
assert.Equal(t, "test.op: test message", err.Error())
}
func TestE_Unwrap(t *testing.T) {
originalErr := errors.New("original error")
err := E("test.op", "test message", originalErr)
assert.True(t, errors.Is(err, originalErr))
var eErr *Error
assert.True(t, errors.As(err, &eErr))
assert.Equal(t, "test.op", eErr.Op)
}

View file

@ -180,3 +180,8 @@ func (s *Service) Translate(messageID string) string {
// Ensure Service implements the core.I18n interface. // Ensure Service implements the core.I18n interface.
var _ core.I18n = (*Service)(nil) var _ core.I18n = (*Service)(nil)
// SetBundle is a test helper to inject a bundle.
func (s *Service) SetBundle(bundle *i18n.Bundle) {
s.bundle = bundle
}

69
pkg/i18n/i18n_test.go Normal file
View file

@ -0,0 +1,69 @@
package i18n
import (
"encoding/json"
"testing"
"github.com/Snider/Core/pkg/core"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/language"
)
func newTestBundle() *i18n.Bundle {
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
bundle.MustParseMessageFileBytes([]byte(`{
"hello": "Hello"
}`), "en.json")
bundle.MustParseMessageFileBytes([]byte(`{
"hello": "Bonjour"
}`), "fr.json")
return bundle
}
func TestNew(t *testing.T) {
s, err := New()
assert.NoError(t, err)
assert.NotNil(t, s)
}
func TestRegister(t *testing.T) {
c, err := core.New()
require.NoError(t, err)
s, err := Register(c)
assert.NoError(t, err)
assert.NotNil(t, s)
}
func TestSetLanguage(t *testing.T) {
s, err := New()
require.NoError(t, err)
s.SetBundle(newTestBundle())
err = s.SetLanguage("en")
assert.NoError(t, err)
err = s.SetLanguage("fr")
assert.NoError(t, err)
err = s.SetLanguage("invalid")
assert.Error(t, err)
}
func TestTranslate(t *testing.T) {
s, err := New()
require.NoError(t, err)
s.SetBundle(newTestBundle())
err = s.SetLanguage("en")
require.NoError(t, err)
assert.Equal(t, "Hello", s.Translate("hello"))
err = s.SetLanguage("fr")
require.NoError(t, err)
assert.Equal(t, "Bonjour", s.Translate("hello"))
}

View file

@ -30,8 +30,8 @@ type Runtime struct {
// ServiceFactory defines a function that creates a service instance. // ServiceFactory defines a function that creates a service instance.
type ServiceFactory func() (any, error) type ServiceFactory func() (any, error)
// newWithFactories creates a new Runtime instance using the provided service factories. // NewWithFactories creates a new Runtime instance using the provided service factories.
func newWithFactories(app *application.App, factories map[string]ServiceFactory) (*Runtime, error) { func NewWithFactories(app *application.App, factories map[string]ServiceFactory) (*Runtime, error) {
services := make(map[string]any) services := make(map[string]any)
coreOpts := []core.Option{ coreOpts := []core.Option{
core.WithWails(app), core.WithWails(app),
@ -98,7 +98,7 @@ func newWithFactories(app *application.App, factories map[string]ServiceFactory)
// New creates and wires together all application services. // New creates and wires together all application services.
func New(app *application.App) (*Runtime, error) { func New(app *application.App) (*Runtime, error) {
return newWithFactories(app, map[string]ServiceFactory{ return NewWithFactories(app, map[string]ServiceFactory{
"config": func() (any, error) { return config.New() }, "config": func() (any, error) { return config.New() },
"display": func() (any, error) { return display.New() }, "display": func() (any, error) { return display.New() },
"help": func() (any, error) { return help.New() }, "help": func() (any, error) { return help.New() },

View file

@ -66,7 +66,7 @@ func TestNewServiceInitializationError(t *testing.T) {
} }
// Pass nil for the application, as it is not required for this test. // Pass nil for the application, as it is not required for this test.
runtime, err := newWithFactories(nil, factories) runtime, err := NewWithFactories(nil, factories)
assert.Error(t, err) assert.Error(t, err)
assert.Nil(t, runtime) assert.Nil(t, runtime)

196
tdd/core_test.go Normal file
View file

@ -0,0 +1,196 @@
package tdd
import (
"embed"
"io"
"testing"
"github.com/Snider/Core/pkg/core"
"github.com/stretchr/testify/assert"
"github.com/wailsapp/wails/v3/pkg/application"
)
func TestCore_New_Good(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
assert.NotNil(t, c)
}
// Mock service for testing
type MockService struct {
Name string
}
func (m *MockService) GetName() string {
return m.Name
}
func TestCore_WithService_Good(t *testing.T) {
factory := func(c *core.Core) (any, error) {
return &MockService{Name: "test"}, nil
}
c, err := core.New(core.WithService(factory))
assert.NoError(t, err)
svc := c.Service("tdd")
assert.NotNil(t, svc)
mockSvc, ok := svc.(*MockService)
assert.True(t, ok)
assert.Equal(t, "test", mockSvc.GetName())
}
func TestCore_WithService_Bad(t *testing.T) {
factory := func(c *core.Core) (any, error) {
return nil, assert.AnError
}
_, err := core.New(core.WithService(factory))
assert.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
}
func TestCore_WithWails_Good(t *testing.T) {
app := &application.App{}
c, err := core.New(core.WithWails(app))
assert.NoError(t, err)
assert.Equal(t, app, c.App)
}
//go:embed testdata
var testFS embed.FS
func TestCore_WithAssets_Good(t *testing.T) {
c, err := core.New(core.WithAssets(testFS))
assert.NoError(t, err)
assets := c.Assets()
file, err := assets.Open("testdata/test.txt")
assert.NoError(t, err)
defer file.Close()
content, err := io.ReadAll(file)
assert.NoError(t, err)
assert.Equal(t, "hello from testdata\n", string(content))
}
func TestCore_WithServiceLock_Good(t *testing.T) {
c, err := core.New(core.WithServiceLock())
assert.NoError(t, err)
err = c.RegisterService("test", &MockService{})
assert.Error(t, err)
}
func TestCore_RegisterService_Good(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
err = c.RegisterService("test", &MockService{Name: "test"})
assert.NoError(t, err)
svc := c.Service("test")
assert.NotNil(t, svc)
mockSvc, ok := svc.(*MockService)
assert.True(t, ok)
assert.Equal(t, "test", mockSvc.GetName())
}
func TestCore_RegisterService_Bad(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
err = c.RegisterService("test", &MockService{})
assert.NoError(t, err)
err = c.RegisterService("test", &MockService{})
assert.Error(t, err)
err = c.RegisterService("", &MockService{})
assert.Error(t, err)
}
func TestCore_ServiceFor_Good(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
err = c.RegisterService("test", &MockService{Name: "test"})
assert.NoError(t, err)
svc, err := core.ServiceFor[*MockService](c, "test")
assert.NoError(t, err)
assert.Equal(t, "test", svc.GetName())
}
func TestCore_ServiceFor_Bad(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
_, err = core.ServiceFor[*MockService](c, "nonexistent")
assert.Error(t, err)
err = c.RegisterService("test", "not a service")
assert.NoError(t, err)
_, err = core.ServiceFor[*MockService](c, "test")
assert.Error(t, err)
}
func TestCore_MustServiceFor_Good(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
err = c.RegisterService("test", &MockService{Name: "test"})
assert.NoError(t, err)
svc := core.MustServiceFor[*MockService](c, "test")
assert.Equal(t, "test", svc.GetName())
}
func TestCore_MustServiceFor_Ugly(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
assert.Panics(t, func() {
core.MustServiceFor[*MockService](c, "nonexistent")
})
err = c.RegisterService("test", "not a service")
assert.NoError(t, err)
assert.Panics(t, func() {
core.MustServiceFor[*MockService](c, "test")
})
}
type MockAction struct {
handled bool
}
func (a *MockAction) Handle(c *core.Core, msg core.Message) error {
a.handled = true
return nil
}
func TestCore_ACTION_Good(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
action := &MockAction{}
c.RegisterAction(action.Handle)
err = c.ACTION(nil)
assert.NoError(t, err)
assert.True(t, action.handled)
}
func TestCore_RegisterActions_Good(t *testing.T) {
c, err := core.New()
assert.NoError(t, err)
action1 := &MockAction{}
action2 := &MockAction{}
c.RegisterActions(action1.Handle, action2.Handle)
err = c.ACTION(nil)
assert.NoError(t, err)
assert.True(t, action1.handled)
assert.True(t, action2.handled)
}
func TestCore_WithName_Good(t *testing.T) {
factory := func(c *core.Core) (any, error) {
return &MockService{Name: "test"}, nil
}
c, err := core.New(core.WithName("my-service", factory))
assert.NoError(t, err)
svc := c.Service("my-service")
assert.NotNil(t, svc)
mockSvc, ok := svc.(*MockService)
assert.True(t, ok)
assert.Equal(t, "test", mockSvc.GetName())
}
func TestCore_WithName_Bad(t *testing.T) {
factory := func(c *core.Core) (any, error) {
return nil, assert.AnError
}
_, err := core.New(core.WithName("my-service", factory))
assert.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
}

160
tdd/io_test.go Normal file
View file

@ -0,0 +1,160 @@
package tdd
import (
"errors"
"testing"
"github.com/Snider/Core/pkg/io"
"github.com/stretchr/testify/assert"
)
type MockMedium struct {
ReadFileFunc func(path string) (string, error)
WriteFileFunc func(path, content string) error
EnsureDirFunc func(path string) error
IsFileFunc func(path string) bool
}
func (m *MockMedium) Read(path string) (string, error) {
if m.ReadFileFunc != nil {
return m.ReadFileFunc(path)
}
return "", errors.New("not implemented")
}
func (m *MockMedium) Write(path, content string) error {
if m.WriteFileFunc != nil {
return m.WriteFileFunc(path, content)
}
return errors.New("not implemented")
}
func (m *MockMedium) EnsureDir(path string) error {
if m.EnsureDirFunc != nil {
return m.EnsureDirFunc(path)
}
return errors.New("not implemented")
}
func (m *MockMedium) IsFile(path string) bool {
if m.IsFileFunc != nil {
return m.IsFileFunc(path)
}
return false
}
func (m *MockMedium) FileGet(path string) (string, error) {
return m.Read(path)
}
func (m *MockMedium) FileSet(path, content string) error {
return m.Write(path, content)
}
func TestIO_Read_Good(t *testing.T) {
medium := &MockMedium{
ReadFileFunc: func(path string) (string, error) {
assert.Equal(t, "test.txt", path)
return "hello", nil
},
}
content, err := io.Read(medium, "test.txt")
assert.NoError(t, err)
assert.Equal(t, "hello", content)
}
func TestIO_Read_Bad(t *testing.T) {
medium := &MockMedium{
ReadFileFunc: func(path string) (string, error) {
return "", assert.AnError
},
}
_, err := io.Read(medium, "test.txt")
assert.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
}
func TestIO_Write_Good(t *testing.T) {
medium := &MockMedium{
WriteFileFunc: func(path, content string) error {
assert.Equal(t, "test.txt", path)
assert.Equal(t, "hello", content)
return nil
},
}
err := io.Write(medium, "test.txt", "hello")
assert.NoError(t, err)
}
func TestIO_Write_Bad(t *testing.T) {
medium := &MockMedium{
WriteFileFunc: func(path, content string) error {
return assert.AnError
},
}
err := io.Write(medium, "test.txt", "hello")
assert.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
}
func TestIO_EnsureDir_Good(t *testing.T) {
medium := &MockMedium{
EnsureDirFunc: func(path string) error {
assert.Equal(t, "testdir", path)
return nil
},
}
err := io.EnsureDir(medium, "testdir")
assert.NoError(t, err)
}
func TestIO_EnsureDir_Bad(t *testing.T) {
medium := &MockMedium{
EnsureDirFunc: func(path string) error {
return assert.AnError
},
}
err := io.EnsureDir(medium, "testdir")
assert.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
}
func TestIO_IsFile_Good(t *testing.T) {
medium := &MockMedium{
IsFileFunc: func(path string) bool {
assert.Equal(t, "test.txt", path)
return true
},
}
assert.True(t, io.IsFile(medium, "test.txt"))
}
func TestIO_Copy_Good(t *testing.T) {
source := &MockMedium{
ReadFileFunc: func(path string) (string, error) {
assert.Equal(t, "source.txt", path)
return "hello", nil
},
}
dest := &MockMedium{
WriteFileFunc: func(path, content string) error {
assert.Equal(t, "dest.txt", path)
assert.Equal(t, "hello", content)
return nil
},
}
err := io.Copy(source, "source.txt", dest, "dest.txt")
assert.NoError(t, err)
}
func TestIO_Copy_Bad(t *testing.T) {
source := &MockMedium{
ReadFileFunc: func(path string) (string, error) {
return "", assert.AnError
},
}
dest := &MockMedium{}
err := io.Copy(source, "source.txt", dest, "dest.txt")
assert.Error(t, err)
assert.ErrorIs(t, err, assert.AnError)
}

114
tdd/runtime_test.go Normal file
View file

@ -0,0 +1,114 @@
package tdd
import (
"errors"
"testing"
"github.com/Snider/Core/pkg/config"
"github.com/Snider/Core/pkg/crypt"
"github.com/Snider/Core/pkg/display"
"github.com/Snider/Core/pkg/help"
"github.com/Snider/Core/pkg/i18n"
"github.com/Snider/Core/pkg/runtime"
"github.com/Snider/Core/pkg/workspace"
"github.com/stretchr/testify/assert"
"github.com/wailsapp/wails/v3/pkg/application"
)
func TestNew(t *testing.T) {
testCases := []struct {
name string
app *application.App
factories map[string]runtime.ServiceFactory
expectErr bool
expectErrStr string
checkRuntime func(*testing.T, *runtime.Runtime)
}{
{
name: "Good path",
app: nil,
factories: map[string]runtime.ServiceFactory{
"config": func() (any, error) { return &config.Service{}, nil },
"display": func() (any, error) { return &display.Service{}, nil },
"help": func() (any, error) { return &help.Service{}, nil },
"crypt": func() (any, error) { return &crypt.Service{}, nil },
"i18n": func() (any, error) { return &i18n.Service{}, nil },
"workspace": func() (any, error) { return &workspace.Service{}, nil },
},
expectErr: false,
checkRuntime: func(t *testing.T, rt *runtime.Runtime) {
assert.NotNil(t, rt)
assert.NotNil(t, rt.Core)
assert.NotNil(t, rt.Config)
assert.NotNil(t, rt.Display)
assert.NotNil(t, rt.Help)
assert.NotNil(t, rt.Crypt)
assert.NotNil(t, rt.I18n)
assert.NotNil(t, rt.Workspace)
},
},
{
name: "Factory returns an error",
app: nil,
factories: map[string]runtime.ServiceFactory{
"config": func() (any, error) { return &config.Service{}, nil },
"display": func() (any, error) { return &display.Service{}, nil },
"help": func() (any, error) { return &help.Service{}, nil },
"crypt": func() (any, error) { return nil, errors.New("crypt service failed") },
"i18n": func() (any, error) { return &i18n.Service{}, nil },
"workspace": func() (any, error) { return &workspace.Service{}, nil },
},
expectErr: true,
expectErrStr: "failed to create service crypt: crypt service failed",
},
{
name: "Factory returns wrong type",
app: nil,
factories: map[string]runtime.ServiceFactory{
"config": func() (any, error) { return &config.Service{}, nil },
"display": func() (any, error) { return "not a display service", nil },
"help": func() (any, error) { return &help.Service{}, nil },
"crypt": func() (any, error) { return &crypt.Service{}, nil },
"i18n": func() (any, error) { return &i18n.Service{}, nil },
"workspace": func() (any, error) { return &workspace.Service{}, nil },
},
expectErr: true,
expectErrStr: "display service has unexpected type",
},
{
name: "With non-nil app",
app: &application.App{},
factories: map[string]runtime.ServiceFactory{
"config": func() (any, error) { return &config.Service{}, nil },
"display": func() (any, error) { return &display.Service{}, nil },
"help": func() (any, error) { return &help.Service{}, nil },
"crypt": func() (any, error) { return &crypt.Service{}, nil },
"i18n": func() (any, error) { return &i18n.Service{}, nil },
"workspace": func() (any, error) { return &workspace.Service{}, nil },
},
expectErr: false,
checkRuntime: func(t *testing.T, rt *runtime.Runtime) {
assert.NotNil(t, rt)
assert.NotNil(t, rt.Core)
assert.NotNil(t, rt.Core.App)
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
rt, err := runtime.NewWithFactories(tc.app, tc.factories)
if tc.expectErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), tc.expectErrStr)
assert.Nil(t, rt)
} else {
assert.NoError(t, err)
if tc.checkRuntime != nil {
tc.checkRuntime(t, rt)
}
}
})
}
}

1
tdd/testdata/test.txt vendored Normal file
View file

@ -0,0 +1 @@
hello from testdata