gui/pkg/browser/service_test.go
Claude 18a455b460
Some checks failed
Security Scan / security (push) Failing after 25s
refactor: migrate entire gui to Core v0.8.0 API
- Import paths: forge.lthn.ai/core/go → dappco.re/go/core
- Import paths: forge.lthn.ai/core/go-log → dappco.re/go/core/log
- Import paths: forge.lthn.ai/core/go-io → dappco.re/go/core/io
- RegisterTask → c.Action("name", handler) across all 15 services
- QueryHandler signature: (any, bool, error) → core.Result
- PERFORM(task) → Action.Run(ctx, opts)
- QUERY returns single core.Result (not 3 values)
- All 17 packages build and test clean on v0.8.0-alpha.1

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-31 16:14:19 +01:00

97 lines
2.4 KiB
Go

// pkg/browser/service_test.go
package browser
import (
"context"
"errors"
"testing"
core "dappco.re/go/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type mockPlatform struct {
lastURL string
lastPath string
urlErr error
fileErr error
}
func (m *mockPlatform) OpenURL(url string) error {
m.lastURL = url
return m.urlErr
}
func (m *mockPlatform) OpenFile(path string) error {
m.lastPath = path
return m.fileErr
}
func newTestBrowserService(t *testing.T, mp *mockPlatform) (*Service, *core.Core) {
t.Helper()
c := core.New(
core.WithService(Register(mp)),
core.WithServiceLock(),
)
require.True(t, c.ServiceStartup(context.Background(), nil).OK)
svc := core.MustServiceFor[*Service](c, "browser")
return svc, c
}
func TestRegister_Good(t *testing.T) {
mp := &mockPlatform{}
svc, _ := newTestBrowserService(t, mp)
assert.NotNil(t, svc)
assert.NotNil(t, svc.platform)
}
func TestTaskOpenURL_Good(t *testing.T) {
mp := &mockPlatform{}
_, c := newTestBrowserService(t, mp)
r := c.Action("browser.openURL").Run(context.Background(), core.NewOptions(
core.Option{Key: "url", Value: "https://example.com"},
))
require.True(t, r.OK)
assert.Equal(t, "https://example.com", mp.lastURL)
}
func TestTaskOpenURL_Bad_PlatformError(t *testing.T) {
mp := &mockPlatform{urlErr: errors.New("browser not found")}
_, c := newTestBrowserService(t, mp)
r := c.Action("browser.openURL").Run(context.Background(), core.NewOptions(
core.Option{Key: "url", Value: "https://example.com"},
))
assert.False(t, r.OK)
}
func TestTaskOpenFile_Good(t *testing.T) {
mp := &mockPlatform{}
_, c := newTestBrowserService(t, mp)
r := c.Action("browser.openFile").Run(context.Background(), core.NewOptions(
core.Option{Key: "path", Value: "/tmp/readme.txt"},
))
require.True(t, r.OK)
assert.Equal(t, "/tmp/readme.txt", mp.lastPath)
}
func TestTaskOpenFile_Bad_PlatformError(t *testing.T) {
mp := &mockPlatform{fileErr: errors.New("file not found")}
_, c := newTestBrowserService(t, mp)
r := c.Action("browser.openFile").Run(context.Background(), core.NewOptions(
core.Option{Key: "path", Value: "/nonexistent"},
))
assert.False(t, r.OK)
}
func TestTaskOpenURL_Bad_NoService(t *testing.T) {
c := core.New(core.WithServiceLock())
r := c.Action("browser.openURL").Run(context.Background(), core.NewOptions(
core.Option{Key: "url", Value: "https://example.com"},
))
assert.False(t, r.OK)
}