Files
mesh-host/internal/profile/seat_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
The node-engine held 1,707 top-level unit tests against the budget of 500
a module. Each unit test file with two or more tests now runs them as
subtests of one test named for the file, each named as its function was,
in the same order: every check is kept, and the check counts 383. Files
with a TestMain, a re-run of a test by its name, or the integration tag
are left. Four files of this branch's earlier commits are formatted.

Builds: design 47 H2 §819; H-A-349 DQ-14
2026-11-22 00:30:05 +01:00

125 lines
4.8 KiB
Go

package profile
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
// drm builds what the kernel would expose, so the cases below are the real ones rather than a
// fake's idea of them: a workstation, a server with a card and no monitor, a machine with no
// graphics at all.
func drm(t *testing.T, connectors map[string]string) {
t.Helper()
dir := t.TempDir()
for name, status := range connectors {
if err := os.MkdirAll(filepath.Join(dir, name), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, name, "status"), []byte(status+"\n"), 0o644); err != nil {
t.Fatal(err)
}
}
// The card itself has no status file, and skipping it must not be mistaken for an answer.
if err := os.MkdirAll(filepath.Join(dir, "card0"), 0o755); err != nil {
t.Fatal(err)
}
old := SeatSource
SeatSource = dir
t.Cleanup(func() { SeatSource = old })
}
func testAMachineWithAMonitorHasASeat(t *testing.T) {
drm(t, map[string]string{"card0-DP-1": "connected", "card0-HDMI-A-1": "disconnected"})
got := seat{}.Detect(context.Background())
if !got.Present {
t.Fatalf("a machine with a connected display has no seat: %s", got.Detail)
}
if !strings.Contains(got.Detail, "card0-DP-1") {
t.Errorf("the answer does not say which connector: %q", got.Detail)
}
if strings.Contains(got.Detail, "HDMI") {
t.Errorf("a disconnected connector was counted: %q", got.Detail)
}
}
func testAServerWithAGraphicsCardAndNoMonitorHasNoSeat(t *testing.T) {
// The case this capability exists for. A display server assigned here would install, start,
// and have nowhere to draw — and the mesh would have thought it succeeded.
drm(t, map[string]string{"card0-HDMI-A-1": "disconnected", "card0-DP-1": "disconnected"})
got := seat{}.Detect(context.Background())
if got.Present {
t.Fatal("a machine with nothing plugged in reported a seat")
}
// And it says which of the two "no" answers this is, because a person deciding where a
// desktop goes wants to know whether the hardware is there.
if !strings.Contains(got.Detail, "no display attached") {
t.Errorf("the answer does not distinguish this from having no graphics at all: %q", got.Detail)
}
}
func testAMachineWithNoGraphicsAtAllHasNoSeat(t *testing.T) {
// A container, or a phone, where the DRM subsystem is not there to read. The ordinary answer
// on most nodes and a fault on none of them, so it is a plain "no" rather than an error.
old := SeatSource
SeatSource = filepath.Join(t.TempDir(), "not-here")
t.Cleanup(func() { SeatSource = old })
got := seat{}.Detect(context.Background())
if got.Present {
t.Fatal("a machine with no graphics subsystem reported a seat")
}
if got.Detail == "" || strings.Contains(strings.ToLower(got.Detail), "error") {
t.Errorf("absence was reported as a failure: %q", got.Detail)
}
}
func testAnUnknownConnectorIsNotASeat(t *testing.T) {
// Writeback connectors and some virtual devices report "unknown". Counting those would give
// a seat to machines that have none — and this workstation has one, so it is not theoretical.
drm(t, map[string]string{"card0-Writeback-1": "unknown"})
if (seat{}).Detect(context.Background()).Present {
t.Fatal("a connector reporting \"unknown\" was counted as a display")
}
}
func testTheAnswerSaysHowItKnows(t *testing.T) {
// Every detector says how, so a wrong answer can be found rather than argued about.
drm(t, map[string]string{"card0-DP-1": "connected"})
if !strings.Contains((seat{}).Detect(context.Background()).How, "status") {
t.Error("the seat detector does not say what it looked at")
}
}
func testASeatIsNotAGraphicalSession(t *testing.T) {
// The two are different questions and the whole point of adding one was that they had been
// answered by the same thing. A machine can have a seat and no session — that is every
// workstation before anything is installed on it, and exactly where a display server should
// be assigned.
drm(t, map[string]string{"card0-DP-1": "connected"})
t.Setenv("DISPLAY", "")
t.Setenv("WAYLAND_DISPLAY", "")
if !(seat{}).Detect(context.Background()).Present {
t.Fatal("a machine with a monitor and no session has no seat")
}
if (graphicalSession{}).Detect(context.Background()).Present {
t.Fatal("a machine with no session reported one")
}
}
// TestSeat runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestSeat(t *testing.T) {
t.Run("AMachineWithAMonitorHasASeat", testAMachineWithAMonitorHasASeat)
t.Run("AServerWithAGraphicsCardAndNoMonitorHasNoSeat", testAServerWithAGraphicsCardAndNoMonitorHasNoSeat)
t.Run("AMachineWithNoGraphicsAtAllHasNoSeat", testAMachineWithNoGraphicsAtAllHasNoSeat)
t.Run("AnUnknownConnectorIsNotASeat", testAnUnknownConnectorIsNotASeat)
t.Run("TheAnswerSaysHowItKnows", testTheAnswerSaysHowItKnows)
t.Run("ASeatIsNotAGraphicalSession", testASeatIsNotAGraphicalSession)
}