fixup! wip: refactor: API

This commit is contained in:
Rob Watson 2025-05-10 08:15:23 +02:00
parent 7706bb363f
commit aa6f50715d
5 changed files with 54 additions and 39 deletions

View File

@ -9,7 +9,6 @@ import (
"git.netflux.io/rob/octoplex/internal/client" "git.netflux.io/rob/octoplex/internal/client"
"git.netflux.io/rob/octoplex/internal/domain" "git.netflux.io/rob/octoplex/internal/domain"
"git.netflux.io/rob/octoplex/internal/event"
"golang.design/x/clipboard" "golang.design/x/clipboard"
) )
@ -53,7 +52,6 @@ func run() error {
} }
app := client.NewApp( app := client.NewApp(
event.NewBus(logger),
clipboardAvailable, clipboardAvailable,
domain.BuildInfo{ domain.BuildInfo{
GoVersion: buildInfo.GoVersion, GoVersion: buildInfo.GoVersion,
@ -61,6 +59,7 @@ func run() error {
Commit: commit, Commit: commit,
Date: date, Date: date,
}, },
nil,
logger, logger,
) )

View File

@ -43,7 +43,6 @@ type Params struct {
ConfigService *config.Service ConfigService *config.Service
DockerClient container.DockerClient DockerClient container.DockerClient
ChanSize int ChanSize int
Screen *terminal.Screen // Screen may be nil.
ClipboardAvailable bool ClipboardAvailable bool
ConfigFilePath string ConfigFilePath string
BuildInfo domain.BuildInfo BuildInfo domain.BuildInfo
@ -61,7 +60,6 @@ func New(params Params) *App {
eventBus: event.NewBus(params.Logger.With("component", "event_bus")), eventBus: event.NewBus(params.Logger.With("component", "event_bus")),
dispatchC: make(chan event.Command, cmp.Or(params.ChanSize, defaultChanSize)), dispatchC: make(chan event.Command, cmp.Or(params.ChanSize, defaultChanSize)),
dockerClient: params.DockerClient, dockerClient: params.DockerClient,
screen: params.Screen,
clipboardAvailable: params.ClipboardAvailable, clipboardAvailable: params.ClipboardAvailable,
configFilePath: params.ConfigFilePath, configFilePath: params.ConfigFilePath,
buildInfo: params.BuildInfo, buildInfo: params.BuildInfo,

View File

@ -15,6 +15,7 @@ import (
"time" "time"
"git.netflux.io/rob/octoplex/internal/app" "git.netflux.io/rob/octoplex/internal/app"
"git.netflux.io/rob/octoplex/internal/client"
"git.netflux.io/rob/octoplex/internal/config" "git.netflux.io/rob/octoplex/internal/config"
"git.netflux.io/rob/octoplex/internal/container" "git.netflux.io/rob/octoplex/internal/container"
"git.netflux.io/rob/octoplex/internal/domain" "git.netflux.io/rob/octoplex/internal/domain"
@ -37,12 +38,6 @@ func buildAppParams(
return app.Params{ return app.Params{
ConfigService: configService, ConfigService: configService,
DockerClient: dockerClient, DockerClient: dockerClient,
Screen: &terminal.Screen{
Screen: screen,
Width: 180,
Height: 25,
CaptureC: screenCaptureC,
},
ClipboardAvailable: false, ClipboardAvailable: false,
BuildInfo: domain.BuildInfo{Version: "0.0.1", GoVersion: "go1.16.3"}, BuildInfo: domain.BuildInfo{Version: "0.0.1", GoVersion: "go1.16.3"},
Logger: logger, Logger: logger,
@ -50,9 +45,35 @@ func buildAppParams(
} }
func buildClientServer( func buildClientServer(
t *testing.T, configService *config.Service,
dockerClient container.DockerClient,
screen tcell.SimulationScreen,
screenCaptureC chan<- terminal.ScreenCapture,
logger *slog.Logger,
) (*client.App, *app.App) { ) (*client.App, *app.App) {
buildInfo := domain.BuildInfo{Version: "0.0.1", GoVersion: "go1.16.3"}
clientApp := client.NewApp(
false,
buildInfo,
&terminal.Screen{
Screen: screen,
Width: 160,
Height: 25,
CaptureC: screenCaptureC,
},
logger,
)
// TODO: use buildAppParams
srvApp := app.New(app.Params{
ConfigService: configService,
DockerClient: dockerClient,
ClipboardAvailable: false,
BuildInfo: buildInfo,
Logger: logger,
})
return clientApp, srvApp
} }
func setupSimulationScreen(t *testing.T) (tcell.SimulationScreen, chan<- terminal.ScreenCapture, func() []string) { func setupSimulationScreen(t *testing.T) (tcell.SimulationScreen, chan<- terminal.ScreenCapture, func() []string) {

View File

@ -12,6 +12,7 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"sync"
"testing" "testing"
"time" "time"
@ -20,7 +21,6 @@ import (
"git.netflux.io/rob/octoplex/internal/container" "git.netflux.io/rob/octoplex/internal/container"
"git.netflux.io/rob/octoplex/internal/container/mocks" "git.netflux.io/rob/octoplex/internal/container/mocks"
"git.netflux.io/rob/octoplex/internal/domain" "git.netflux.io/rob/octoplex/internal/domain"
"git.netflux.io/rob/octoplex/internal/terminal"
"git.netflux.io/rob/octoplex/internal/testhelpers" "git.netflux.io/rob/octoplex/internal/testhelpers"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client" dockerclient "github.com/docker/docker/client"
@ -126,25 +126,19 @@ func testIntegration(t *testing.T, mediaServerConfig config.MediaServerSource) {
Destinations: []config.Destination{{Name: "Local server 1", URL: destURL1}}, Destinations: []config.Destination{{Name: "Local server 1", URL: destURL1}},
}) })
done := make(chan struct{}) client, server := buildClientServer(configService, dockerClient, screen, screenCaptureC, logger)
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
defer func() { defer wg.Done()
done <- struct{}{} assert.NoError(t, client.Run(ctx))
}() }()
require.Equal(t, context.Canceled, app.New(app.Params{ wg.Add(1)
ConfigService: configService, go func() {
DockerClient: dockerClient, defer wg.Done()
Screen: &terminal.Screen{ assert.NoError(t, server.Run(ctx))
Screen: screen,
Width: 160,
Height: 25,
CaptureC: screenCaptureC,
},
ClipboardAvailable: false,
BuildInfo: domain.BuildInfo{Version: "0.0.1", GoVersion: "go1.16.3"},
Logger: logger,
}).Run(ctx))
}() }()
require.EventuallyWithT( require.EventuallyWithT(
@ -286,13 +280,9 @@ func testIntegration(t *testing.T, mediaServerConfig config.MediaServerSource) {
printScreen(t, getContents, "After stopping the first destination") printScreen(t, getContents, "After stopping the first destination")
// TODO:
// - Source error
// - Additional features (copy URL, etc.)
cancel() cancel()
<-done wg.Wait()
} }
func TestIntegrationCustomHost(t *testing.T) { func TestIntegrationCustomHost(t *testing.T) {

View File

@ -15,27 +15,33 @@ import (
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
) )
// App is the client application.
type App struct { type App struct {
bus *event.Bus bus *event.Bus
clipboardAvailable bool clipboardAvailable bool
buildInfo domain.BuildInfo buildInfo domain.BuildInfo
screen *terminal.Screen
logger *slog.Logger logger *slog.Logger
} }
// NewApp creates a new App instance.
//
// TODO: params
func NewApp( func NewApp(
bus *event.Bus,
clipboardAvailable bool, clipboardAvailable bool,
buildInfo domain.BuildInfo, buildInfo domain.BuildInfo,
screen *terminal.Screen,
logger *slog.Logger, logger *slog.Logger,
) *App { ) *App {
return &App{ return &App{
bus: bus, bus: event.NewBus(logger),
clipboardAvailable: clipboardAvailable, clipboardAvailable: clipboardAvailable,
buildInfo: buildInfo, buildInfo: buildInfo,
logger: logger, logger: logger,
} }
} }
// Run starts the application, and blocks until it is closed.
func (a *App) Run(ctx context.Context) error { func (a *App) Run(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx) g, ctx := errgroup.WithContext(ctx)
@ -77,6 +83,7 @@ func (a *App) Run(ctx context.Context) error {
}, },
ClipboardAvailable: a.clipboardAvailable, ClipboardAvailable: a.clipboardAvailable,
BuildInfo: a.buildInfo, BuildInfo: a.buildInfo,
Screen: a.screen,
Logger: a.logger.With("component", "ui"), Logger: a.logger.With("component", "ui"),
}) })
if err != nil { if err != nil {