fix: add open progress staging and forced key evidence to display_diag (#3)

This commit is contained in:
2026-11-28 14:43:32 +01:00
parent 903c5c69b6
commit acc721e7e7
5 changed files with 496 additions and 49 deletions
+133 -13
View File
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"strconv"
"strings"
"sync/atomic"
@@ -527,6 +528,104 @@ func (q *scriptedQueries) StackPosition(xproto.Window) (string, string, error) {
return q.pos, q.of, nil
}
func TestOpenStageMarkersAndCompletionRecords(t *testing.T) {
var buf bytes.Buffer
session := &x11Session{role: RoleAbove, diag: NewDiag(&buf)}
// One pre-operation marker per stage; a successful stage emits no
// completion record — success is implied by the next marker or the
// final record.
session.stage("font_list")
session.stageDone("font_list", nil)
session.stageDone("cursor", errors.New("bad\nthing"))
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
if len(lines) != 2 {
t.Fatalf("lines=%v want the font_list marker and the sanitized cursor failure only", lines)
}
if !strings.Contains(lines[0], "display_diag kind=open_stage role=osd stage=font_list") {
t.Fatalf("marker=%q", lines[0])
}
if !strings.Contains(lines[1], "kind=open_stage_done") ||
!strings.Contains(lines[1], "stage=cursor") || !strings.Contains(lines[1], "err=bad thing") {
t.Fatalf("failure=%q want the sanitized error completion", lines[1])
}
}
func TestOpenBeginIsEmittedOncePerRole(t *testing.T) {
var buf bytes.Buffer
session := &x11Session{role: RoleAbove, diag: NewDiag(&buf)}
session.openBegin()
session.openBegin()
session.openBegin()
if strings.Count(buf.String(), "\n") != 1 {
t.Fatalf("buf=%q want one bounded open_begin record per role", buf.String())
}
if !strings.Contains(buf.String(), "display_diag kind=open_begin role=osd") {
t.Fatalf("buf=%q want the open_begin record", buf.String())
}
}
func TestEmitQueryBundleStageMarksEachQueryOnce(t *testing.T) {
// Each bundle query can block on a reply: its stage marker must be
// emitted before the query, with failures recorded, and the whole
// bundle is once-only per session and deduplicated on repeat calls.
var buf bytes.Buffer
session := &x11Session{
role: RoleAbove,
diag: NewDiag(&buf),
window: 0x600001,
queries: &scriptedQueries{shape: "0x0+0+0", geom: "1920x1080+0+26", parent: "0x1a00001", pos: "1", of: "3"},
}
session.emitQueryBundle("osd")
for _, stage := range []string{"query_shape", "query_geom", "query_parent", "query_stack"} {
if !strings.Contains(buf.String(), "stage="+stage) {
t.Fatalf("buf=%q missing stage marker %q", buf.String(), stage)
}
}
session.emitQueryBundle("osd")
// Bounded: one bundle emits four stage markers and one query record,
// and the once-only guard adds nothing on a second call.
if got := strings.Count(buf.String(), "kind=query"); got != 1 {
t.Fatalf("query records=%d want exactly one", got)
}
if got := strings.Count(buf.String(), "kind=open_stage"); got != 4 {
t.Fatalf("stage markers=%d want exactly four", got)
}
}
func TestForcedEvidenceSurvivesACapFilledByReconnectStorms(t *testing.T) {
// Reconnect storms with varying error strings must never consume the
// evidence budget: first_redraw and the query bundle are forced and
// reach the writer even when every ordinary record slot is spent.
var buf bytes.Buffer
session := &x11Session{
role: RoleAbove,
diag: NewDiag(&buf),
video: NewVideoPresence(),
lastTrigger: "initial",
queries: &scriptedQueries{shape: "0x0+0+0", geom: "1920x1080+0+26", parent: "0x1a00001", pos: "1", of: "3"},
}
session.diag.cap = 4
for i := 0; i < 10; i++ {
session.diag.Emit("reconnect", "osd", fmt.Sprintf("event=open_failed"), fmt.Sprintf("error=variant %d", i))
}
session.recordFlush(nil)
session.emitQueryBundle("osd")
lines := buf.String()
if !strings.Contains(lines, "kind=first_redraw") || !strings.Contains(lines, "kind=query") {
t.Fatalf("buf=%q must carry the forced first_redraw and query records", lines)
}
}
func TestRecordFlushEmitsFirstRedrawAndQueryBundleOnce(t *testing.T) {
var buf bytes.Buffer
presence := NewVideoPresence()
@@ -552,19 +651,38 @@ func TestRecordFlushEmitsFirstRedrawAndQueryBundleOnce(t *testing.T) {
session.recordFlush(nil) // the bundle and the first-redraw record are once-only
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
if len(lines) != 2 {
t.Fatalf("lines=%v want exactly the first_redraw and query records", lines)
}
// Once-only per session: one first_redraw record, four query stage
// markers, one query record — successful stages emit no completion
// record (success is implied by the next marker/final record).
wantFirst := "display_diag kind=first_redraw role=osd trigger=initial ops=0 regions=3 bounds=196x82+30+19 snap=none video=off stack=top ok"
if lines[0] != wantFirst {
t.Fatalf("first_redraw=%q want %q", lines[0], wantFirst)
if len(lines) != 6 || lines[0] != wantFirst {
t.Fatalf("lines=%v (n=%d) want the first flush evidence with %q first", lines, len(lines), wantFirst)
}
if strings.Count(buf.String(), "kind=query") != 1 {
t.Fatalf("buf=%q want exactly one query record", buf.String())
}
wantQuery := "display_diag kind=query role=osd shape=196x106+28+15 geom=1920x1080+0+26 parent=0x1a00001 stack=1/3"
if lines[1] != wantQuery {
t.Fatalf("query=%q want %q", lines[1], wantQuery)
if !slices.Contains(lines, wantQuery) {
t.Fatalf("query=%q want %q among %v", lines[9], wantQuery, lines)
}
}
// hasDiag reports whether one captured record line carries all tokens.
func hasDiag(lines []string, tokens ...string) bool {
for _, line := range lines {
match := true
for _, token := range tokens {
if !strings.Contains(line, token) {
match = false
}
}
if match {
return true
}
}
return false
}
func TestRecordFlushReportsShapeErrorsWithoutBlockingTheLayer(t *testing.T) {
var buf bytes.Buffer
session := &x11Session{
@@ -577,19 +695,21 @@ func TestRecordFlushReportsShapeErrorsWithoutBlockingTheLayer(t *testing.T) {
// The shape error arrives with the first flush: it must be carried by
// the first_redraw record and the query bundle reports its own query
// failure alongside.
// failure alongside, with stage markers around every bundle query.
session.recordFlush(errors.New("bounding shape: BadWindow"))
session.recordFlush(nil)
lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
if len(lines) != 2 {
t.Fatalf("lines=%v want the first_redraw error record and the query error record", lines)
}
if !strings.Contains(lines[0], "ok=no") || !strings.Contains(lines[0], "err=bounding shape: BadWindow") {
t.Fatalf("first_redraw=%q must carry the shape error", lines[0])
}
if !strings.Contains(lines[1], "err=") {
t.Fatalf("query=%q must carry the query error", lines[1])
if !hasDiag(lines, "kind=query", "err=BadWindow") {
t.Fatalf("lines=%v: the query record must carry the query error", lines)
}
for _, stage := range []string{"query_shape", "query_geom", "query_parent", "query_stack"} {
if !strings.Contains(buf.String(), "stage="+stage) {
t.Fatalf("buf=%q missing stage marker %q", buf.String(), stage)
}
}
}