cli/runtime_pkg.go
Snider 35400ee927 fix(runtime): Correct NewWithFactories service creation loop (#30)
Previously, the `NewWithFactories` function in `runtime_pkg.go` iterated over an empty slice, which prevented any services from being created from the provided factories. This commit fixes the loop to correctly iterate over the keys of the `factories` map, ensuring that services are properly instantiated and registered.

feat(testing): Add Good, Bad, and Ugly tests for NewWithFactories

To improve test quality and ensure the reliability of the `NewWithFactories` function, this commit replaces the existing basic test with a comprehensive test suite following the Good, Bad, Ugly methodology:

- `TestNewWithFactories_Good`: Verifies the happy path, ensuring a service is created and registered successfully.
- `TestNewWithFactories_Bad`: Checks that the function correctly returns an error when a service factory fails.
- `TestNewWithFactories_Ugly`: Confirms that the function panics when a `nil` factory is provided, as this represents an unrecoverable programmer error.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2025-11-13 21:23:56 +00:00

80 lines
2 KiB
Go

package core
import (
"context"
"fmt"
"sort"
"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
}
// 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 := []Option{
WithWails(app),
}
names := make([]string, 0, len(factories))
for name := range factories {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
factory := factories[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, WithName(name, func(c *Core) (any, error) { return svcCopy, nil }))
}
coreInstance, err := New(coreOpts...)
if err != nil {
return nil, err
}
// --- Type Assertions ---
rt := &Runtime{
app: app,
Core: coreInstance,
}
return rt, nil
}
// NewRuntime creates and wires together all application services.
func NewRuntime(app *application.App) (*Runtime, error) {
return NewWithFactories(app, map[string]ServiceFactory{})
}
// 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)
}
}