cli/pkg/runtime/runtime_test.go
Snider 32f1d0ab5d 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>
2025-11-02 22:29:17 +00:00

74 lines
3.3 KiB
Go

package runtime
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/workspace"
"github.com/stretchr/testify/assert"
)
// TestNew ensures that New correctly initializes a Runtime instance.
func TestNew(t *testing.T) {
// Pass nil for the application, as it is not required for this test.
runtime, err := New(nil)
assert.NoError(t, err)
assert.NotNil(t, runtime)
// Assert that key services are initialized
assert.NotNil(t, runtime.Core, "Core service should be initialized")
assert.NotNil(t, runtime.Config, "Config service should be initialized")
assert.NotNil(t, runtime.Display, "Display service should be initialized")
assert.NotNil(t, runtime.Help, "Help service should be initialized")
assert.NotNil(t, runtime.Crypt, "Crypt service should be initialized")
assert.NotNil(t, runtime.I18n, "I18n service should be initialized")
assert.NotNil(t, runtime.Workspace, "Workspace service should be initialized")
// Verify services are properly wired through Core
configFromCore := runtime.Core.Service("config")
assert.NotNil(t, configFromCore, "Config should be registered in Core")
assert.Equal(t, runtime.Config, configFromCore, "Config from Core should match direct reference")
displayFromCore := runtime.Core.Service("display")
assert.NotNil(t, displayFromCore, "Display should be registered in Core")
assert.Equal(t, runtime.Display, displayFromCore, "Display from Core should match direct reference")
helpFromCore := runtime.Core.Service("help")
assert.NotNil(t, helpFromCore, "Help should be registered in Core")
assert.Equal(t, runtime.Help, helpFromCore, "Help from Core should match direct reference")
cryptFromCore := runtime.Core.Service("crypt")
assert.NotNil(t, cryptFromCore, "Crypt should be registered in Core")
assert.Equal(t, runtime.Crypt, cryptFromCore, "Crypt from Core should match direct reference")
i18nFromCore := runtime.Core.Service("i18n")
assert.NotNil(t, i18nFromCore, "I18n should be registered in Core")
assert.Equal(t, runtime.I18n, i18nFromCore, "I18n from Core should match direct reference")
workspaceFromCore := runtime.Core.Service("workspace")
assert.NotNil(t, workspaceFromCore, "Workspace should be registered in Core")
assert.Equal(t, runtime.Workspace, workspaceFromCore, "Workspace from Core should match direct reference")
}
// TestNewServiceInitializationError tests the error path in New.
func TestNewServiceInitializationError(t *testing.T) {
factories := map[string]ServiceFactory{
"config": func() (any, error) { return config.New() },
"display": func() (any, error) { return display.New() },
"help": func() (any, error) { return help.New() },
"crypt": func() (any, error) { return crypt.New() },
"i18n": func() (any, error) { return nil, errors.New("i18n service failed to initialize") }, // This factory will fail
"workspace": func() (any, error) { return workspace.New() },
}
// Pass nil for the application, as it is not required for this test.
runtime, err := NewWithFactories(nil, factories)
assert.Error(t, err)
assert.Nil(t, runtime)
assert.Contains(t, err.Error(), "failed to create service i18n: i18n service failed to initialize")
}