From 4ad8e8f6a70abe5e66a245a942bef6e121ccee26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffry=20W=C3=BCrmli?= Date: Thu, 12 Nov 2026 13:28:52 +0100 Subject: [PATCH 1/2] test: cover a single owner-not-visible notice in the detail block --- internal/render/detail_test.go | 15 +++++++++++++++ .../render/testdata/text_owner_not_visible.golden | 4 ++++ 2 files changed, 19 insertions(+) create mode 100644 internal/render/testdata/text_owner_not_visible.golden diff --git a/internal/render/detail_test.go b/internal/render/detail_test.go index d9d449a..e25d8a4 100644 --- a/internal/render/detail_test.go +++ b/internal/render/detail_test.go @@ -230,3 +230,18 @@ func TestText_DetailLongHeadlineAndProcessNameFitTheWidth(t *testing.T) { } } } + +func TestText_DetailOwnerNotVisibleAppearsOnce(t *testing.T) { + r := report.PortReport{ + Port: 8080, + Findings: []report.Finding{{ + Socket: socket.Socket{Port: 8080, Protocol: socket.TCP, Addr: netip.MustParseAddr("::"), State: socket.Listen}, + Explanation: detect.Explanation{Source: detect.SourceProcess, Title: "owner not visible"}, + }}, + } + out := string(renderText(t, Colors{}, r)) + if n := strings.Count(out, "owner not visible"); n != 1 { + t.Errorf("want the notice exactly once, got %d:\n%s", n, out) + } + golden(t, "text_owner_not_visible.golden", []byte(out)) +} diff --git a/internal/render/testdata/text_owner_not_visible.golden b/internal/render/testdata/text_owner_not_visible.golden new file mode 100644 index 0000000..1e6d8ad --- /dev/null +++ b/internal/render/testdata/text_owner_not_visible.golden @@ -0,0 +1,4 @@ +───────────────────────── :8080 · owner not visible ────────────────────────── + + ℹ tcp :: + From 2b40c8550dffd4bd5359485f5a9103420d0ae937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffry=20W=C3=BCrmli?= Date: Thu, 12 Nov 2026 13:31:05 +0100 Subject: [PATCH 2/2] test: build the hidden-owner fixture from the real detector and advice --- internal/render/detail_test.go | 66 ++++++++++++++++--- .../testdata/text_owner_not_visible.golden | 3 + 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/internal/render/detail_test.go b/internal/render/detail_test.go index e25d8a4..149d1f4 100644 --- a/internal/render/detail_test.go +++ b/internal/render/detail_test.go @@ -1,6 +1,7 @@ package render import ( + "context" "net/netip" "strconv" "strings" @@ -231,17 +232,66 @@ func TestText_DetailLongHeadlineAndProcessNameFitTheWidth(t *testing.T) { } } -func TestText_DetailOwnerNotVisibleAppearsOnce(t *testing.T) { - r := report.PortReport{ +// hiddenOwnerReport builds a PID 0 report the way the real pipeline does: the +// title comes from the generic detector and the advice from the advise package. +func hiddenOwnerReport(t *testing.T) report.PortReport { + t.Helper() + sk := socket.Socket{Port: 8080, Protocol: socket.TCP, Addr: netip.MustParseAddr("::"), State: socket.Listen} + e, ok := detect.Generic().Detect(context.Background(), sk, nil) + if !ok || e == nil { + t.Fatal("generic detector must explain a socket without a visible owner") + } + return report.PortReport{ Port: 8080, Findings: []report.Finding{{ - Socket: socket.Socket{Port: 8080, Protocol: socket.TCP, Addr: netip.MustParseAddr("::"), State: socket.Listen}, - Explanation: detect.Explanation{Source: detect.SourceProcess, Title: "owner not visible"}, + Socket: sk, + Explanation: *e, + Advice: advise.Advise(advise.Input{GOOS: "linux", Socket: sk, Source: e.Source}), }}, } - out := string(renderText(t, Colors{}, r)) - if n := strings.Count(out, "owner not visible"); n != 1 { - t.Errorf("want the notice exactly once, got %d:\n%s", n, out) +} + +// The hint must appear once in the headline or rule line of every text mode. +// The advice note under it carries the different sudo hint and stays. +func TestText_HiddenOwnerHintOnceInHeadlineAdviceNoteRemains(t *testing.T) { + r := hiddenOwnerReport(t) + modes := []struct { + name string + opts Options + ruleWant string + }{ + {"detail", Options{}, ":8080 · owner not visible"}, + {"overview", Options{Overview: true}, "Owner not visible"}, + {"expanded", Options{Overview: true, Expanded: true}, "Owner not visible · 1 port"}, + } + for _, m := range modes { + t.Run(m.name, func(t *testing.T) { + var buf strings.Builder + if err := Text(&buf, []report.PortReport{r}, m.opts); err != nil { + t.Fatal(err) + } + out := buf.String() + var heads []string + for _, line := range strings.Split(out, "\n") { + lower := strings.ToLower(line) + if strings.Contains(lower, "owner not visible") && !strings.Contains(lower, "for details") { + heads = append(heads, line) + } + } + if len(heads) != 1 || strings.Count(strings.ToLower(heads[0]), "owner not visible") != 1 { + t.Fatalf("want exactly one headline with exactly one hint, got %q in:\n%s", heads, out) + } + if !strings.Contains(heads[0], m.ruleWant) { + t.Errorf("headline %q lacks %q", heads[0], m.ruleWant) + } + if !strings.Contains(out, "sudo") { + t.Errorf("advice note about sudo is missing:\n%s", out) + } + }) + } + var buf strings.Builder + if err := Text(&buf, []report.PortReport{r}, Options{}); err != nil { + t.Fatal(err) } - golden(t, "text_owner_not_visible.golden", []byte(out)) + golden(t, "text_owner_not_visible.golden", []byte(buf.String())) } diff --git a/internal/render/testdata/text_owner_not_visible.golden b/internal/render/testdata/text_owner_not_visible.golden index 1e6d8ad..b40bdfa 100644 --- a/internal/render/testdata/text_owner_not_visible.golden +++ b/internal/render/testdata/text_owner_not_visible.golden @@ -2,3 +2,6 @@ ℹ tcp :: + ⚠ Reachable from the network (bound to all interfaces). + ℹ Owner not visible. Run with sudo for details. +