From aa6f50715d2c1a9452d336b3aeef90e34c312b66 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Sat, 10 May 2025 08:15:23 +0200 Subject: [PATCH] fixup! wip: refactor: API --- cmd/client/main.go | 3 +- internal/app/app.go | 2 -- internal/app/integration_helpers_test.go | 39 ++++++++++++++++++------ internal/app/integration_test.go | 38 +++++++++-------------- internal/client/clientapp.go | 11 +++++-- 5 files changed, 54 insertions(+), 39 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index e89b0ec..89b9303 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -9,7 +9,6 @@ import ( "git.netflux.io/rob/octoplex/internal/client" "git.netflux.io/rob/octoplex/internal/domain" - "git.netflux.io/rob/octoplex/internal/event" "golang.design/x/clipboard" ) @@ -53,7 +52,6 @@ func run() error { } app := client.NewApp( - event.NewBus(logger), clipboardAvailable, domain.BuildInfo{ GoVersion: buildInfo.GoVersion, @@ -61,6 +59,7 @@ func run() error { Commit: commit, Date: date, }, + nil, logger, ) diff --git a/internal/app/app.go b/internal/app/app.go index 49589c3..7c0de9c 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -43,7 +43,6 @@ type Params struct { ConfigService *config.Service DockerClient container.DockerClient ChanSize int - Screen *terminal.Screen // Screen may be nil. ClipboardAvailable bool ConfigFilePath string BuildInfo domain.BuildInfo @@ -61,7 +60,6 @@ func New(params Params) *App { eventBus: event.NewBus(params.Logger.With("component", "event_bus")), dispatchC: make(chan event.Command, cmp.Or(params.ChanSize, defaultChanSize)), dockerClient: params.DockerClient, - screen: params.Screen, clipboardAvailable: params.ClipboardAvailable, configFilePath: params.ConfigFilePath, buildInfo: params.BuildInfo, diff --git a/internal/app/integration_helpers_test.go b/internal/app/integration_helpers_test.go index 53f032f..b24af28 100644 --- a/internal/app/integration_helpers_test.go +++ b/internal/app/integration_helpers_test.go @@ -15,6 +15,7 @@ import ( "time" "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/container" "git.netflux.io/rob/octoplex/internal/domain" @@ -35,14 +36,8 @@ func buildAppParams( t.Helper() return app.Params{ - ConfigService: configService, - DockerClient: dockerClient, - Screen: &terminal.Screen{ - Screen: screen, - Width: 180, - Height: 25, - CaptureC: screenCaptureC, - }, + ConfigService: configService, + DockerClient: dockerClient, ClipboardAvailable: false, BuildInfo: domain.BuildInfo{Version: "0.0.1", GoVersion: "go1.16.3"}, Logger: logger, @@ -50,9 +45,35 @@ func buildAppParams( } 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) { + 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) { diff --git a/internal/app/integration_test.go b/internal/app/integration_test.go index fc85993..636071e 100644 --- a/internal/app/integration_test.go +++ b/internal/app/integration_test.go @@ -12,6 +12,7 @@ import ( "fmt" "net" "os" + "sync" "testing" "time" @@ -20,7 +21,6 @@ import ( "git.netflux.io/rob/octoplex/internal/container" "git.netflux.io/rob/octoplex/internal/container/mocks" "git.netflux.io/rob/octoplex/internal/domain" - "git.netflux.io/rob/octoplex/internal/terminal" "git.netflux.io/rob/octoplex/internal/testhelpers" "github.com/docker/docker/api/types/network" 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}}, }) - done := make(chan struct{}) - go func() { - defer func() { - done <- struct{}{} - }() + client, server := buildClientServer(configService, dockerClient, screen, screenCaptureC, logger) + var wg sync.WaitGroup - require.Equal(t, context.Canceled, app.New(app.Params{ - ConfigService: configService, - DockerClient: dockerClient, - Screen: &terminal.Screen{ - 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)) + wg.Add(1) + go func() { + defer wg.Done() + assert.NoError(t, client.Run(ctx)) + }() + + wg.Add(1) + go func() { + defer wg.Done() + assert.NoError(t, server.Run(ctx)) }() require.EventuallyWithT( @@ -286,13 +280,9 @@ func testIntegration(t *testing.T, mediaServerConfig config.MediaServerSource) { printScreen(t, getContents, "After stopping the first destination") - // TODO: - // - Source error - // - Additional features (copy URL, etc.) - cancel() - <-done + wg.Wait() } func TestIntegrationCustomHost(t *testing.T) { diff --git a/internal/client/clientapp.go b/internal/client/clientapp.go index 4a78174..5163cd4 100644 --- a/internal/client/clientapp.go +++ b/internal/client/clientapp.go @@ -15,27 +15,33 @@ import ( "google.golang.org/grpc/credentials/insecure" ) +// App is the client application. type App struct { bus *event.Bus clipboardAvailable bool buildInfo domain.BuildInfo + screen *terminal.Screen logger *slog.Logger } +// NewApp creates a new App instance. +// +// TODO: params func NewApp( - bus *event.Bus, clipboardAvailable bool, buildInfo domain.BuildInfo, + screen *terminal.Screen, logger *slog.Logger, ) *App { return &App{ - bus: bus, + bus: event.NewBus(logger), clipboardAvailable: clipboardAvailable, buildInfo: buildInfo, logger: logger, } } +// Run starts the application, and blocks until it is closed. func (a *App) Run(ctx context.Context) error { g, ctx := errgroup.WithContext(ctx) @@ -77,6 +83,7 @@ func (a *App) Run(ctx context.Context) error { }, ClipboardAvailable: a.clipboardAvailable, BuildInfo: a.buildInfo, + Screen: a.screen, Logger: a.logger.With("component", "ui"), }) if err != nil {