cli/pkg/runtime/runtime.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

126 lines
3.7 KiB
Go

package runtime
import (
"context"
"fmt"
"github.com/Snider/Core/pkg/config"
"github.com/Snider/Core/pkg/core"
"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/workspace"
"github.com/wailsapp/wails/v3/pkg/application"
)
// Runtime is the container that holds all instantiated services.
// Its fields are the concrete types, allowing Wails to bind them directly.
type Runtime struct {
app *application.App
Core *core.Core
Config *config.Service
Display *display.Service
Help *help.Service
Crypt *crypt.Service
I18n *i18n.Service
Workspace *workspace.Service
}
// ServiceFactory defines a function that creates a service instance.
type ServiceFactory func() (any, error)
// NewWithFactories creates a new Runtime instance using the provided service factories.
func NewWithFactories(app *application.App, factories map[string]ServiceFactory) (*Runtime, error) {
services := make(map[string]any)
coreOpts := []core.Option{
core.WithWails(app),
}
for _, name := range []string{"config", "display", "help", "crypt", "i18n", "workspace"} {
factory, ok := factories[name]
if !ok {
return nil, fmt.Errorf("service %s factory not provided", name)
}
svc, err := factory()
if err != nil {
return nil, fmt.Errorf("failed to create service %s: %w", name, err)
}
services[name] = svc
svcCopy := svc
coreOpts = append(coreOpts, core.WithName(name, func(c *core.Core) (any, error) { return svcCopy, nil }))
}
coreInstance, err := core.New(coreOpts...)
if err != nil {
return nil, err
}
// --- Type Assertions ---
configSvc, ok := services["config"].(*config.Service)
if !ok {
return nil, fmt.Errorf("config service has unexpected type")
}
displaySvc, ok := services["display"].(*display.Service)
if !ok {
return nil, fmt.Errorf("display service has unexpected type")
}
helpSvc, ok := services["help"].(*help.Service)
if !ok {
return nil, fmt.Errorf("help service has unexpected type")
}
cryptSvc, ok := services["crypt"].(*crypt.Service)
if !ok {
return nil, fmt.Errorf("crypt service has unexpected type")
}
i18nSvc, ok := services["i18n"].(*i18n.Service)
if !ok {
return nil, fmt.Errorf("i18n service has unexpected type")
}
workspaceSvc, ok := services["workspace"].(*workspace.Service)
if !ok {
return nil, fmt.Errorf("workspace service has unexpected type")
}
rt := &Runtime{
app: app,
Core: coreInstance,
Config: configSvc,
Display: displaySvc,
Help: helpSvc,
Crypt: cryptSvc,
I18n: i18nSvc,
Workspace: workspaceSvc,
}
return rt, nil
}
// New creates and wires together all application services.
func New(app *application.App) (*Runtime, error) {
return NewWithFactories(app, 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 i18n.New() },
"workspace": func() (any, error) { return workspace.New() },
})
}
// ServiceName returns the name of the service. This is used by Wails to identify the service.
func (r *Runtime) ServiceName() string {
return "Core"
}
// ServiceStartup is called by Wails at application startup.
func (r *Runtime) ServiceStartup(ctx context.Context, options application.ServiceOptions) {
r.Core.ServiceStartup(ctx, options)
}
// ServiceShutdown is called by Wails at application shutdown.
func (r *Runtime) ServiceShutdown(ctx context.Context) {
if r.Core != nil {
r.Core.ServiceShutdown(ctx)
}
}