377 lines
12 KiB
Go
377 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.piksar.eu/piotr/groundstation/internal/display"
|
|
"git.piksar.eu/piotr/groundstation/internal/osd"
|
|
)
|
|
|
|
type recordingCanvas struct {
|
|
width, height int
|
|
lines []string
|
|
clears, flushes int
|
|
}
|
|
|
|
func (c *recordingCanvas) Size() (int, int) { return c.width, c.height }
|
|
func (c *recordingCanvas) Clear() { c.clears++ }
|
|
func (c *recordingCanvas) Text(_, _ int, message string) {
|
|
c.lines = append(c.lines, message)
|
|
}
|
|
func (c *recordingCanvas) Flush() { c.flushes++ }
|
|
|
|
func TestBuildTargetBuildsIndependentVitureLayers(t *testing.T) {
|
|
layers, err := buildTarget("x11", filepath.Join(t.TempDir(), "osd-state.bin"), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(layers) != 2 {
|
|
t.Fatalf("layers=%d want the fallback canvas and the OSD overlay", len(layers))
|
|
}
|
|
canvas, osdLayer := layers[0], layers[1]
|
|
if canvas.Name != "fallback-canvas" || osdLayer.Name != "osd-overlay" {
|
|
t.Fatalf("layer names %q/%q want fallback-canvas/osd-overlay", canvas.Name, osdLayer.Name)
|
|
}
|
|
|
|
canvasOpener, ok := canvas.Opener.(display.X11Opener)
|
|
if !ok {
|
|
t.Fatalf("canvas opener %T must be the X11 target", canvas.Opener)
|
|
}
|
|
if canvasOpener.Role != display.RoleBelow {
|
|
t.Fatalf("canvas layer role %v must stay below video", canvasOpener.Role)
|
|
}
|
|
osdOpener, ok := osdLayer.Opener.(display.X11Opener)
|
|
if !ok {
|
|
t.Fatalf("OSD opener %T must be the X11 target", osdLayer.Opener)
|
|
}
|
|
if osdOpener.Role != display.RoleAbove {
|
|
t.Fatalf("OSD layer role %v must stay above video", osdOpener.Role)
|
|
}
|
|
// The OSD refreshes on its own slower cadence; the canvas stays purely
|
|
// event-driven.
|
|
if osdOpener.RefreshInterval <= 0 {
|
|
t.Fatal("the OSD overlay must redraw on its own refresh interval")
|
|
}
|
|
if canvasOpener.RefreshInterval != 0 {
|
|
t.Fatal("the fallback canvas must keep its event-driven redraws")
|
|
}
|
|
|
|
// Both layers must share one presence signal so the video lifecycle
|
|
// observed by either session drives both templates.
|
|
if canvasOpener.Video == nil || osdOpener.Video == nil || canvasOpener.Video != osdOpener.Video {
|
|
t.Fatal("layers must share one video presence signal")
|
|
}
|
|
|
|
// The canvas layer keeps the target-specific fallback template with the
|
|
// exact fallback text; the OSD layer shows explicit state without a
|
|
// snapshot. Both layers are fullscreen at startup with no video — the
|
|
// canvas below (RoleBelow) and the OSD above (RoleAbove), per the
|
|
// display package's fullscreen-args contract.
|
|
canvasSurface := &recordingCanvas{width: 800, height: 600}
|
|
canvas.Template.Draw(canvasSurface)
|
|
if len(canvasSurface.lines) != 1 || canvasSurface.lines[0] != "Brak połączenia WFB" {
|
|
t.Fatalf("canvas lines=%q want the exact fallback text", canvasSurface.lines)
|
|
}
|
|
|
|
osdSurface := &recordingCanvas{width: 800, height: 600}
|
|
osdLayer.Template.Draw(osdSurface)
|
|
wantStartup := []string{"NO DATA", "VTX tbl:-- ceil:-- req:-- ack:--", "VIDEO OFF"}
|
|
if len(osdSurface.lines) != len(wantStartup) {
|
|
t.Fatalf("OSD lines=%q want the explicit startup state %q", osdSurface.lines, wantStartup)
|
|
}
|
|
for i := range wantStartup {
|
|
if osdSurface.lines[i] != wantStartup[i] {
|
|
t.Fatalf("line %d=%q want %q", i, osdSurface.lines[i], wantStartup[i])
|
|
}
|
|
}
|
|
|
|
// Video presence hides the fallback on the canvas layer only.
|
|
canvasOpener.Video.SetActive(true)
|
|
canvasSurface.lines = nil
|
|
canvas.Template.Draw(canvasSurface)
|
|
if len(canvasSurface.lines) != 0 {
|
|
t.Fatalf("canvas lines=%q must disappear while video is present", canvasSurface.lines)
|
|
}
|
|
canvasOpener.Video.SetActive(false)
|
|
}
|
|
|
|
func TestBuildTargetOSDLayerRendersPublishedSnapshots(t *testing.T) {
|
|
state := filepath.Join(t.TempDir(), "run", "osd-state.bin")
|
|
publisher := osd.NewFilePublisher(state, nil)
|
|
defer publisher.Close()
|
|
|
|
snapshot := osd.Snapshot{
|
|
Sequence: 11,
|
|
Armed: true,
|
|
OpConfig: osd.OperatorConfig{MaxPower: 95},
|
|
Policy: osd.GSPolicy{Pending: true},
|
|
Ack: osd.VTXAck{Pending: true, Lifecycle: osd.VTXLifecyclePending},
|
|
Link: osd.GSObservedLink{Known: false},
|
|
}
|
|
publisher.Publish(snapshot)
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if _, err := osd.DecodeFile(state); err == nil {
|
|
break
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
|
|
layers, err := buildTarget("x11", state, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
osdLayer := layers[1]
|
|
osdOpener := osdLayer.Opener.(display.X11Opener)
|
|
osdOpener.Video.SetActive(true)
|
|
|
|
surface := &recordingCanvas{width: 800, height: 600}
|
|
osdLayer.Template.Draw(surface)
|
|
|
|
want := []string{"ARM ARMED", "VTX tbl:-- ceil:95 req:-- ack:--", "VIDEO ON"}
|
|
if len(surface.lines) != len(want) {
|
|
t.Fatalf("OSD lines=%q want %q from the supervisor-published snapshot", surface.lines, want)
|
|
}
|
|
for i := range want {
|
|
if surface.lines[i] != want[i] {
|
|
t.Fatalf("line %d=%q want %q", i, surface.lines[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildTargetRejectsUnknownTarget(t *testing.T) {
|
|
if _, err := buildTarget("hdmi-mirror", "", nil); err == nil {
|
|
t.Fatal("unknown target must fail")
|
|
}
|
|
}
|
|
|
|
func TestFormatDiagnosticRendersStructuredFields(t *testing.T) {
|
|
got := formatDiagnostic(display.Diagnostic{
|
|
Target: "x11",
|
|
Layer: "fallback-canvas",
|
|
Event: display.EventOpenFailed,
|
|
Err: errors.New("connect X11: refused"),
|
|
RetryDelay: 500 * time.Millisecond,
|
|
})
|
|
want := `display_diag kind=reconnect role=fallback-canvas target=x11 event=open_failed retry_delay=500ms error="connect X11: refused"`
|
|
if got != want {
|
|
t.Fatalf("formatDiagnostic=%q want %q", got, want)
|
|
}
|
|
}
|
|
|
|
// harnessOpener mirrors the real opener's diagnostic contract: it emits
|
|
// the role's open record at Open and hands back a session that emits the
|
|
// first-redraw record on its first flush and the summary on close — all
|
|
// through the one shared emitter under test.
|
|
type harnessOpener struct {
|
|
diag *display.Diag
|
|
role string
|
|
session *harnessSession
|
|
}
|
|
|
|
func (o harnessOpener) Open(context.Context) (display.Session, error) {
|
|
o.diag.Emit("open", o.role, "window=0x1")
|
|
return o.session, nil
|
|
}
|
|
|
|
type harnessSession struct {
|
|
diag *display.Diag
|
|
role string
|
|
firstRed sync.Once
|
|
}
|
|
|
|
func (s *harnessSession) Canvas() display.Canvas { return s }
|
|
func (s *harnessSession) Size() (int, int) { return 800, 600 }
|
|
func (s *harnessSession) Clear() {}
|
|
func (s *harnessSession) Text(int, int, string) {}
|
|
func (s *harnessSession) Flush() {
|
|
s.firstRed.Do(func() { s.diag.Emit("first_redraw", s.role, "trigger=initial") })
|
|
}
|
|
|
|
func (s *harnessSession) Draw(display.Canvas) {
|
|
s.Flush()
|
|
}
|
|
|
|
func (s *harnessSession) WaitEvent(ctx context.Context) (bool, error) {
|
|
<-ctx.Done()
|
|
return false, ctx.Err()
|
|
}
|
|
|
|
func (s *harnessSession) Close() error {
|
|
s.diag.EmitForced("summary", s.role, "flushes=1")
|
|
return nil
|
|
}
|
|
|
|
// stalledOpener reproduces the device evidence: the open sequence emits
|
|
// its progress markers (open_begin, the stage marker for the next
|
|
// potentially blocking operation) and then never returns, while the other
|
|
// layer completes normally through the same shared emitter.
|
|
type stalledOpener struct {
|
|
diag *display.Diag
|
|
role string
|
|
stall chan struct{}
|
|
stages func(*display.Diag, string)
|
|
}
|
|
|
|
func (o stalledOpener) Open(ctx context.Context) (display.Session, error) {
|
|
o.diag.Emit("open_begin", o.role)
|
|
o.stages(o.diag, o.role)
|
|
<-o.stall
|
|
return nil, fmt.Errorf("stalled open never returned")
|
|
}
|
|
|
|
func TestRunLayersEmitsProgressRecordsBeforeAStalledOpen(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
diag := display.NewDiag(&buf)
|
|
|
|
stall := make(chan struct{})
|
|
stalled := &harnessSession{diag: diag, role: "stalled"}
|
|
live := &harnessSession{diag: diag, role: "canvas"}
|
|
|
|
progress := func(d *display.Diag, role string) {
|
|
d.Emit("open_begin", role)
|
|
d.Emit("open_stage", role, "stage=map_confirm")
|
|
}
|
|
layers := []display.Layer{
|
|
{
|
|
Name: "stalled-osd",
|
|
Opener: stalledOpener{diag: diag, role: "osd", stall: stall, stages: progress},
|
|
Template: stalled,
|
|
},
|
|
{
|
|
Name: "fallback-canvas",
|
|
Opener: harnessOpener{diag: diag, role: "canvas", session: live},
|
|
Template: live,
|
|
},
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
display.RunLayers(ctx, layers)
|
|
close(done)
|
|
}()
|
|
|
|
// The stall evidence must already be visible while the stalled open is
|
|
// in progress, and the collector must stay bounded.
|
|
waitForRecords(t, diag, func(records []string) bool {
|
|
return hasDiag(records, "kind=open_begin", "role=osd") &&
|
|
hasDiag(records, "kind=open_stage", "role=osd", "stage=map_confirm") &&
|
|
hasDiag(records, "kind=open", "role=canvas") &&
|
|
hasDiag(records, "kind=first_redraw", "role=canvas")
|
|
})
|
|
if len(diag.Records()) > 70 {
|
|
t.Fatalf("collector unbounded: %d distinct records", len(diag.Records()))
|
|
}
|
|
|
|
close(stall)
|
|
cancel()
|
|
<-done
|
|
}
|
|
|
|
func TestRunLayersCarriesOneSharedDiagToBothRoles(t *testing.T) {
|
|
// The value-copy regression pin: the same emitter wired into both
|
|
// real target layer structs must survive the RunLayers boundary so
|
|
// both roles' open, first-redraw and summary records reach one
|
|
// writer. Mirrors the real layer flow: the template draws through the
|
|
// session canvas, so one session instance owns the diagnostics.
|
|
var buf bytes.Buffer
|
|
diag := display.NewDiag(&buf)
|
|
|
|
canvas := &harnessSession{diag: diag, role: "canvas"}
|
|
osd := &harnessSession{diag: diag, role: "osd"}
|
|
layers := []display.Layer{
|
|
{
|
|
Name: "fallback-canvas",
|
|
Opener: harnessOpener{diag: diag, role: "canvas", session: canvas},
|
|
Template: canvas,
|
|
},
|
|
{
|
|
Name: "osd-overlay",
|
|
Opener: harnessOpener{diag: diag, role: "osd", session: osd},
|
|
Template: osd,
|
|
},
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
display.RunLayers(ctx, layers)
|
|
close(done)
|
|
}()
|
|
|
|
waitForRecords(t, diag, func(records []string) bool {
|
|
return hasDiag(records, "kind=open", "role=canvas") &&
|
|
hasDiag(records, "kind=open", "role=osd") &&
|
|
hasDiag(records, "kind=first_redraw", "role=canvas") &&
|
|
hasDiag(records, "kind=first_redraw", "role=osd")
|
|
})
|
|
cancel()
|
|
<-done
|
|
|
|
waitForRecords(t, diag, func(records []string) bool {
|
|
return hasDiag(records, "kind=summary", "role=canvas") &&
|
|
hasDiag(records, "kind=summary", "role=osd")
|
|
})
|
|
}
|
|
|
|
// hasDiag reports whether one captured record line carries both tokens.
|
|
func hasDiag(records []string, tokens ...string) bool {
|
|
for _, line := range records {
|
|
match := true
|
|
for _, token := range tokens {
|
|
if !strings.Contains(line, token) {
|
|
match = false
|
|
}
|
|
}
|
|
if match {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func waitForRecords(t *testing.T, diag *display.Diag, satisfied func([]string) bool) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if satisfied(diag.Records()) {
|
|
return
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
t.Fatalf("records never reached the shared writer in the required combination: %v", diag.Records())
|
|
}
|
|
|
|
func TestWireDiagnosticsTagsEveryLayerWithItsName(t *testing.T) {
|
|
layers, err := buildTarget("x11", filepath.Join(t.TempDir(), "osd-state.bin"), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
wireDiagnostics("x11", layers)
|
|
|
|
for _, layer := range layers {
|
|
if layer.Options.Target != "x11" {
|
|
t.Fatalf("layer %q target=%q want x11", layer.Name, layer.Options.Target)
|
|
}
|
|
if layer.Options.Layer != layer.Name {
|
|
t.Fatalf("layer %q diagnostic layer=%q", layer.Name, layer.Options.Layer)
|
|
}
|
|
if layer.Options.Diagnostic == nil {
|
|
t.Fatalf("layer %q must carry the reconnect diagnostic hook", layer.Name)
|
|
}
|
|
}
|
|
|
|
// The shared hook must be safe to call from any layer goroutine.
|
|
layers[0].Options.Diagnostic(display.Diagnostic{Target: "x11", Layer: layers[0].Name})
|
|
}
|