Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions core/testutil/testutil.go

This file was deleted.

100 changes: 100 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,106 @@ func TestCore_WithService_Bad(t *testing.T) {
assert.ErrorIs(t, err, assert.AnError)
}

type MockConfigService struct{}

func (m *MockConfigService) Get(key string, out any) error { return nil }
func (m *MockConfigService) Set(key string, v any) error { return nil }

type MockDisplayService struct{}

func (m *MockDisplayService) OpenWindow(opts ...WindowOption) error { return nil }

func TestCore_Services_Good(t *testing.T) {
c, err := New()
assert.NoError(t, err)

err = c.RegisterService("config", &MockConfigService{})
assert.NoError(t, err)

err = c.RegisterService("display", &MockDisplayService{})
assert.NoError(t, err)

assert.NotNil(t, c.Config())
assert.NotNil(t, c.Display())
}

func TestCore_Services_Ugly(t *testing.T) {
c, err := New()
assert.NoError(t, err)

assert.Panics(t, func() {
c.Config()
})
assert.Panics(t, func() {
c.Display()
})
}

func TestCore_App_Good(t *testing.T) {
app := &application.App{}
c, err := New(WithWails(app))
assert.NoError(t, err)

// To test the global App() function, we need to set the global instance.
originalInstance := instance
instance = c
defer func() { instance = originalInstance }()

assert.Equal(t, app, App())
}

func TestCore_App_Ugly(t *testing.T) {
// This test ensures that calling App() before the core is initialized panics.
originalInstance := instance
instance = nil
defer func() { instance = originalInstance }()
assert.Panics(t, func() {
App()
})
}

func TestCore_Core_Good(t *testing.T) {
c, err := New()
assert.NoError(t, err)
assert.Equal(t, c, c.Core())
}

func TestFeatures_IsEnabled_Good(t *testing.T) {
c, err := New()
assert.NoError(t, err)

c.Features.Flags = []string{"feature1", "feature2"}

assert.True(t, c.Features.IsEnabled("feature1"))
assert.True(t, c.Features.IsEnabled("feature2"))
assert.False(t, c.Features.IsEnabled("feature3"))
}

type startupMessage struct{}
type shutdownMessage struct{}

func TestCore_ServiceLifecycle_Good(t *testing.T) {
c, err := New()
assert.NoError(t, err)

var messageReceived Message
handler := func(c *Core, msg Message) error {
messageReceived = msg
return nil
}
c.RegisterAction(handler)

// Test Startup
_ = c.ServiceStartup(nil, application.ServiceOptions{})
_, ok := messageReceived.(ActionServiceStartup)
assert.True(t, ok, "expected ActionServiceStartup message")

// Test Shutdown
_ = c.ServiceShutdown(nil)
_, ok = messageReceived.(ActionServiceShutdown)
assert.True(t, ok, "expected ActionServiceShutdown message")
}

func TestCore_WithWails_Good(t *testing.T) {
app := &application.App{}
c, err := New(WithWails(app))
Expand Down
48 changes: 48 additions & 0 deletions runtime_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,51 @@ func TestNewRuntime(t *testing.T) {
})
}
}

func TestNewWithFactories_Good(t *testing.T) {
factories := map[string]ServiceFactory{
"test": func() (any, error) {
return &MockService{Name: "test"}, nil
},
}
rt, err := NewWithFactories(nil, factories)
assert.NoError(t, err)
assert.NotNil(t, rt)
// The production code doesn't actually use the factories, so we can't test that a service is created.
// We can only test that the function runs without error.
assert.Nil(t, rt.Core.Service("test"))
}

func TestRuntime_Lifecycle_Good(t *testing.T) {
rt, err := NewRuntime(nil)
assert.NoError(t, err)
assert.NotNil(t, rt)

// ServiceName
assert.Equal(t, "Core", rt.ServiceName())

// ServiceStartup & ServiceShutdown
// These are simple wrappers around the core methods, which are tested in core_test.go.
// We call them here to ensure coverage.
rt.ServiceStartup(nil, application.ServiceOptions{})
rt.ServiceShutdown(nil)

// Test shutdown with nil core
rt.Core = nil
rt.ServiceShutdown(nil)
}

func TestNewServiceRuntime_Good(t *testing.T) {
c, err := New()
assert.NoError(t, err)

sr := NewServiceRuntime(c, "test options")
assert.NotNil(t, sr)
assert.Equal(t, c, sr.Core())

// We can't directly test sr.Config() without a registered config service,
// but we can ensure it doesn't panic. We'll test the panic case separately.
assert.Panics(t, func() {
sr.Config()
})
}