2025-10-30 14:18:37 +00:00
|
|
|
package help
|
|
|
|
|
|
|
|
|
|
import (
|
2025-10-30 16:23:00 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-10-30 14:18:37 +00:00
|
|
|
"github.com/Snider/Core/pkg/core"
|
2025-10-30 16:23:00 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/wailsapp/wails/v3/pkg/application"
|
2025-10-30 14:18:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MockDisplay is a mock implementation of the core.Display interface.
|
|
|
|
|
type MockDisplay struct {
|
|
|
|
|
ShowCalled bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockDisplay) Show() error {
|
|
|
|
|
m.ShowCalled = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockDisplay) ShowAt(anchor string) error {
|
|
|
|
|
m.ShowCalled = true
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MockDisplay) Hide() error { return nil }
|
|
|
|
|
func (m *MockDisplay) HideAt(anchor string) error { return nil }
|
|
|
|
|
func (m *MockDisplay) OpenWindow(opts ...core.WindowOption) error { return nil }
|
|
|
|
|
|
|
|
|
|
// MockCore is a mock implementation of the *core.Core type.
|
|
|
|
|
type MockCore struct {
|
|
|
|
|
Core *core.Core
|
|
|
|
|
ActionCalled bool
|
2025-10-30 16:23:00 +00:00
|
|
|
ActionMsg core.Message
|
2025-10-30 14:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-30 16:23:00 +00:00
|
|
|
// ACTION matches the signature required by RegisterAction.
|
|
|
|
|
func (m *MockCore) ACTION(c *core.Core, msg core.Message) error {
|
2025-10-30 14:18:37 +00:00
|
|
|
m.ActionCalled = true
|
2025-10-30 16:23:00 +00:00
|
|
|
m.ActionMsg = msg
|
2025-10-30 14:18:37 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
2025-10-30 16:23:00 +00:00
|
|
|
|
|
|
|
|
func setupService(t *testing.T) (*Service, *MockCore, *MockDisplay) {
|
|
|
|
|
s, err := New()
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
app := application.New(application.Options{})
|
|
|
|
|
c, err := core.New(core.WithWails(app))
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
mockCore := &MockCore{Core: c}
|
|
|
|
|
mockDisplay := &MockDisplay{}
|
|
|
|
|
|
|
|
|
|
s.Runtime = core.NewRuntime(c, Options{})
|
|
|
|
|
s.display = mockDisplay
|
|
|
|
|
// Register our mock handler. When the real s.Core().ACTION is called,
|
|
|
|
|
// our mock handler will be executed.
|
|
|
|
|
c.RegisterAction(mockCore.ACTION)
|
|
|
|
|
|
|
|
|
|
return s, mockCore, mockDisplay
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
|
s, err := New()
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestShow(t *testing.T) {
|
|
|
|
|
s, mockCore, _ := setupService(t)
|
|
|
|
|
|
|
|
|
|
err := s.Show()
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.True(t, mockCore.ActionCalled)
|
|
|
|
|
|
|
|
|
|
msg, ok := mockCore.ActionMsg.(map[string]any)
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
assert.Equal(t, "display.open_window", msg["action"])
|
|
|
|
|
assert.Equal(t, "help", msg["name"])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestShowAt(t *testing.T) {
|
|
|
|
|
s, mockCore, _ := setupService(t)
|
|
|
|
|
|
|
|
|
|
err := s.ShowAt("test-anchor")
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.True(t, mockCore.ActionCalled)
|
|
|
|
|
|
|
|
|
|
msg, ok := mockCore.ActionMsg.(map[string]any)
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
assert.Equal(t, "display.open_window", msg["action"])
|
|
|
|
|
assert.Equal(t, "help", msg["name"])
|
|
|
|
|
|
|
|
|
|
opts, ok := msg["options"].(map[string]any)
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
assert.Equal(t, "/#test-anchor", opts["URL"])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHandleIPCEvents_ServiceStartup(t *testing.T) {
|
|
|
|
|
s, _, _ := setupService(t)
|
|
|
|
|
err := s.HandleIPCEvents(s.Core(), core.ActionServiceStartup{})
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
}
|