feat(browser): add browser core.Service with Platform interface and IPC

Implements pkg/browser with three-layer pattern: IPC Bus -> Service -> Platform.
Stateless service — delegates OpenURL and OpenFile to platform adapter.
No queries or actions, tasks only.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Snider 2026-03-13 14:35:01 +00:00
parent 3954725d45
commit 1e0b89b94e
5 changed files with 174 additions and 0 deletions

14
pkg/browser/messages.go Normal file
View file

@ -0,0 +1,14 @@
// pkg/browser/messages.go
package browser
// --- Tasks (all side-effects, no queries or actions) ---
// TaskOpenURL opens a URL in the default system browser. Result: nil
type TaskOpenURL struct {
URL string `json:"url"`
}
// TaskOpenFile opens a file with the system default application. Result: nil
type TaskOpenFile struct {
Path string `json:"path"`
}

11
pkg/browser/platform.go Normal file
View file

@ -0,0 +1,11 @@
// pkg/browser/platform.go
package browser
// Platform abstracts the system browser/file-opener backend.
type Platform interface {
// OpenURL opens the given URL in the default system browser.
OpenURL(url string) error
// OpenFile opens the given file path with the system default application.
OpenFile(path string) error
}

15
pkg/browser/register.go Normal file
View file

@ -0,0 +1,15 @@
// pkg/browser/register.go
package browser
import "forge.lthn.ai/core/go/pkg/core"
// Register creates a factory closure that captures the Platform adapter.
// The returned function has the signature WithService requires: func(*Core) (any, error).
func Register(p Platform) func(*core.Core) (any, error) {
return func(c *core.Core) (any, error) {
return &Service{
ServiceRuntime: core.NewServiceRuntime[Options](c, Options{}),
platform: p,
}, nil
}
}

42
pkg/browser/service.go Normal file
View file

@ -0,0 +1,42 @@
// pkg/browser/service.go
package browser
import (
"context"
"forge.lthn.ai/core/go/pkg/core"
)
// Options holds configuration for the browser service.
type Options struct{}
// Service is a core.Service that delegates browser/file-open operations
// to the platform. It is stateless — no queries, no actions.
type Service struct {
*core.ServiceRuntime[Options]
platform Platform
}
// OnStartup registers IPC handlers.
func (s *Service) OnStartup(ctx context.Context) error {
s.Core().RegisterTask(s.handleTask)
return nil
}
// HandleIPCEvents is auto-discovered and registered by core.WithService.
func (s *Service) HandleIPCEvents(c *core.Core, msg core.Message) error {
return nil
}
// --- Task Handlers ---
func (s *Service) handleTask(c *core.Core, t core.Task) (any, bool, error) {
switch t := t.(type) {
case TaskOpenURL:
return nil, true, s.platform.OpenURL(t.URL)
case TaskOpenFile:
return nil, true, s.platform.OpenFile(t.Path)
default:
return nil, false, nil
}
}

View file

@ -0,0 +1,92 @@
// pkg/browser/service_test.go
package browser
import (
"context"
"errors"
"testing"
"forge.lthn.ai/core/go/pkg/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, err := core.New(
core.WithService(Register(mp)),
core.WithServiceLock(),
)
require.NoError(t, err)
require.NoError(t, c.ServiceStartup(context.Background(), nil))
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)
_, handled, err := c.PERFORM(TaskOpenURL{URL: "https://example.com"})
require.NoError(t, err)
assert.True(t, handled)
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)
_, handled, err := c.PERFORM(TaskOpenURL{URL: "https://example.com"})
assert.True(t, handled)
assert.Error(t, err)
}
func TestTaskOpenFile_Good(t *testing.T) {
mp := &mockPlatform{}
_, c := newTestBrowserService(t, mp)
_, handled, err := c.PERFORM(TaskOpenFile{Path: "/tmp/readme.txt"})
require.NoError(t, err)
assert.True(t, handled)
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)
_, handled, err := c.PERFORM(TaskOpenFile{Path: "/nonexistent"})
assert.True(t, handled)
assert.Error(t, err)
}
func TestTaskOpenURL_Bad_NoService(t *testing.T) {
c, _ := core.New(core.WithServiceLock())
_, handled, _ := c.PERFORM(TaskOpenURL{URL: "https://example.com"})
assert.False(t, handled)
}