i3: open's own non-killing opener run, the generated answer type, a hand-act on every call

Code review round 1 (C-5, C-6, note (b)):
- open's opener now runs through its own openerRun, which stops waiting at its bound
  without sending any signal, instead of internal/desktop's Exec, which kills the
  command's process group at its deadline; a browser started under the account's
  service manager is never torn down by open's own 10 s bound (DMC-8).
- open imports mesh-contracts' generated nodedisplaysession package at the zero
  version and answers with its OpenAnswer type and Started constants; main.go names
  the tool from its Seat and VerbOpen constants.
- Every call to open, refused or not, writes one act to the log of acts done by hand
  (DQ-7 says "each call"), not only one that reaches the opener.

Builds: design 08 sign-in-page §3, §29
This commit is contained in:
jochen
2026-11-24 21:53:45 +01:00
parent c12d0027e8
commit a94f9f14aa
5 changed files with 175 additions and 80 deletions
+78 -52
View File
@@ -8,6 +8,8 @@ import (
"testing"
"time"
nodedisplaysession "git.novox.be/novox/mesh-contracts/go/seats/nodedisplaysession"
"i3/internal/desktop"
)
@@ -18,25 +20,44 @@ type runCall struct {
args []string
}
// stubOpener builds an i3 whose session is found (unless find answers an error) and whose
// commands are answered from answers, keyed by the command's name ("xdg-mime", "systemd-run");
// every call is recorded in calls.
func stubOpener(t *testing.T, find func() (*desktop.Session, error), answers map[string]desktop.Result) (*i3, *[]runCall) {
// stubOpener builds an i3 whose session is found (unless find answers an error), whose xdg-mime
// query answers xdgMime, and whose opener answers the systemd-run call with opener (nil runs it is
// never reached). Every call, to either, is recorded in calls.
func stubOpener(t *testing.T, find func() (*desktop.Session, error), xdgMime desktop.Result, opener desktop.Runner) (*i3, *[]runCall) {
t.Helper()
calls := &[]runCall{}
run := func(ctx context.Context, env []string, stdin []byte, name string, args ...string) desktop.Result {
*calls = append(*calls, runCall{name, args})
res, ok := answers[name]
if !ok {
t.Fatalf("unexpected command %s %v", name, args)
if name != "xdg-mime" {
t.Fatalf("unexpected command on the session's own run: %s %v", name, args)
}
return xdgMime
}
var wrapped desktop.Runner
if opener != nil {
wrapped = func(ctx context.Context, env []string, stdin []byte, name string, args ...string) desktop.Result {
*calls = append(*calls, runCall{name, args})
return opener(ctx, env, stdin, name, args...)
}
} else {
wrapped = func(ctx context.Context, env []string, stdin []byte, name string, args ...string) desktop.Result {
t.Fatalf("the opener never runs: %s %v", name, args)
return desktop.Result{}
}
}
return &i3{d: desktop.Desk{Find: find, Run: run}, opener: wrapped, openBound: 50 * time.Millisecond}, calls
}
// resultOpener is an opener stub that answers res, blocking on ctx when res.TimedOut (as a real
// still-running opener would appear to this call).
func resultOpener(res desktop.Result) desktop.Runner {
return func(ctx context.Context, env []string, stdin []byte, name string, args ...string) desktop.Result {
if res.TimedOut {
<-ctx.Done()
return desktop.Result{TimedOut: true}
}
return res
}
return &i3{d: desktop.Desk{Find: find, Run: run}, openBound: 50 * time.Millisecond}, calls
}
func foundSession() (*desktop.Session, error) {
@@ -59,7 +80,7 @@ func writeDesktopEntry(t *testing.T, entry, execLine string) {
// 935/I-1: every scheme but http and https, and a file path, is refused.
func TestOpenRefusesEveryOtherScheme(t *testing.T) {
x, calls := stubOpener(t, foundSession, map[string]desktop.Result{})
x, calls := stubOpener(t, foundSession, desktop.Result{}, nil)
for _, address := range []string{
"/etc/passwd", // no scheme: a file path
"file:///etc/passwd", // a scheme other than http or https
@@ -73,13 +94,13 @@ func TestOpenRefusesEveryOtherScheme(t *testing.T) {
}
}
if len(*calls) != 0 {
t.Fatalf("a refused address never reaches the opener: %v", *calls)
t.Fatalf("a refused address never reaches xdg-mime or the opener: %v", *calls)
}
}
// 935/I-1: an address over 2048 bytes once encoded is refused.
func TestOpenRefusesAnAddressTooLongOnceEncoded(t *testing.T) {
x, _ := stubOpener(t, foundSession, map[string]desktop.Result{})
x, _ := stubOpener(t, foundSession, desktop.Result{}, nil)
address := "https://example.net/" + strings.Repeat("a", 2048)
if _, err := run(t, x, "node-display-session.open", map[string]any{"address": address}); err == nil {
t.Fatal("not refused")
@@ -90,7 +111,7 @@ func TestOpenRefusesAnAddressTooLongOnceEncoded(t *testing.T) {
func TestOpenRefusesWithNoGraphicalSession(t *testing.T) {
x, _ := stubOpener(t, func() (*desktop.Session, error) {
return nil, &desktop.NoSession{Reason: "none"}
}, map[string]desktop.Result{})
}, desktop.Result{}, nil)
_, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"})
if !desktop.IsNoSession(err) {
t.Fatalf("%v", err)
@@ -99,36 +120,26 @@ func TestOpenRefusesWithNoGraphicalSession(t *testing.T) {
// 935/I-1: no default program for the scheme is refused.
func TestOpenRefusesWithNoDefaultProgram(t *testing.T) {
x, calls := stubOpener(t, foundSession, map[string]desktop.Result{
"xdg-mime": {Code: 0, Stdout: ""},
})
x, _ := stubOpener(t, foundSession, desktop.Result{Code: 0, Stdout: ""}, nil)
if _, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"}); err == nil {
t.Fatal("not refused")
}
for _, c := range *calls {
if c.name == "systemd-run" {
t.Fatal("the opener never runs with no default program")
}
}
}
// 935/I-2 (R2-9): every character outside the kept set is percent-encoded, as the bytes of its
// UTF-8 form, and the address reaches the opener as one argument, never a shell command line.
func TestOpenEncodesEveryOtherCharacterAsOneArgument(t *testing.T) {
writeDesktopEntry(t, "browser.desktop", "firefox %u")
x, calls := stubOpener(t, foundSession, map[string]desktop.Result{
"xdg-mime": {Code: 0, Stdout: "browser.desktop\n"},
"systemd-run": {Code: 0},
})
x, calls := stubOpener(t, foundSession, desktop.Result{Code: 0, Stdout: "browser.desktop\n"}, resultOpener(desktop.Result{Code: 0}))
address := `https://example.net/a'b;c d$(e),f"g`
got, err := run(t, x, "node-display-session.open", map[string]any{"address": address})
if err != nil {
t.Fatal(err)
}
want := `https://example.net/a%27b%3Bc%20d%24%28e%29%2Cf%22g`
answer := got.(map[string]any)
if answer["address"] != want {
t.Fatalf("got %q, want %q", answer["address"], want)
answer := got.(nodedisplaysession.OpenAnswer)
if answer.Address != want {
t.Fatalf("got %q, want %q", answer.Address, want)
}
var last runCall
for _, c := range *calls {
@@ -149,10 +160,8 @@ func TestOpenEncodesEveryOtherCharacterAsOneArgument(t *testing.T) {
// 935/I-3: an opener that fails is refused with its status and names no program.
func TestOpenRefusesWhenTheOpenerFails(t *testing.T) {
writeDesktopEntry(t, "browser.desktop", "firefox %u")
x, _ := stubOpener(t, foundSession, map[string]desktop.Result{
"xdg-mime": {Code: 0, Stdout: "browser.desktop\n"},
"systemd-run": {Code: 1, Stderr: "no such unit"},
})
x, _ := stubOpener(t, foundSession, desktop.Result{Code: 0, Stdout: "browser.desktop\n"},
resultOpener(desktop.Result{Code: 1, Stderr: "no such unit"}))
_, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"})
if err == nil || !strings.Contains(err.Error(), "no such unit") {
t.Fatalf("%v", err)
@@ -162,16 +171,13 @@ func TestOpenRefusesWhenTheOpenerFails(t *testing.T) {
// 935/I-3: an opener that ends well answers opened with the program.
func TestOpenAnswersOpenedWithTheProgram(t *testing.T) {
writeDesktopEntry(t, "browser.desktop", "firefox %u")
x, _ := stubOpener(t, foundSession, map[string]desktop.Result{
"xdg-mime": {Code: 0, Stdout: "browser.desktop\n"},
"systemd-run": {Code: 0},
})
x, _ := stubOpener(t, foundSession, desktop.Result{Code: 0, Stdout: "browser.desktop\n"}, resultOpener(desktop.Result{Code: 0}))
got, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"})
if err != nil {
t.Fatal(err)
}
answer := got.(map[string]any)
if answer["started"] != "opened" || answer["program"] != "browser.desktop" || answer["command"] != "firefox" {
answer := got.(nodedisplaysession.OpenAnswer)
if answer.Started != nodedisplaysession.OpenAnswerStartedOpened || answer.Program != "browser.desktop" || answer.Command != "firefox" {
t.Fatalf("%+v", answer)
}
}
@@ -180,28 +186,48 @@ func TestOpenAnswersOpenedWithTheProgram(t *testing.T) {
// names the program only once the opener started).
func TestOpenAnswersOpeningWhenStillRunningAtItsBound(t *testing.T) {
writeDesktopEntry(t, "browser.desktop", "firefox %u")
x, _ := stubOpener(t, foundSession, map[string]desktop.Result{
"xdg-mime": {Code: 0, Stdout: "browser.desktop\n"},
"systemd-run": {TimedOut: true},
})
x, _ := stubOpener(t, foundSession, desktop.Result{Code: 0, Stdout: "browser.desktop\n"}, resultOpener(desktop.Result{TimedOut: true}))
got, err := run(t, x, "node-display-session.open", map[string]any{"address": "https://example.net"})
if err != nil {
t.Fatal(err)
}
answer := got.(map[string]any)
if answer["started"] != "opening" || answer["program"] != "browser.desktop" {
answer := got.(nodedisplaysession.OpenAnswer)
if answer.Started != nodedisplaysession.OpenAnswerStartedOpening || answer.Program != "browser.desktop" {
t.Fatalf("%+v", answer)
}
}
// DQ-7: each call that is acted on writes one act to the log of acts done by hand, with its caller
// and why; a refusal before the opener ran writes none.
func TestOpenWritesOneActDoneByHand(t *testing.T) {
// C-5: cmd/i3-tools runs the opener with its own run (openerRun, not internal/desktop's Exec, which
// kills a command's process group at its deadline, run.go:82-86): it stops waiting at its bound
// without killing what it started, so an opener still running past the bound keeps running and
// finishes on its own.
func TestOpenerRunStopsWaitingWithoutKillingTheProcess(t *testing.T) {
marker := filepath.Join(t.TempDir(), "ran")
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
defer cancel()
start := time.Now()
res := openerRun(ctx, os.Environ(), nil, "sh", "-c", "sleep 0.15; touch "+marker)
if !res.TimedOut {
t.Fatalf("expected TimedOut, got %+v", res)
}
if since := time.Since(start); since > 200*time.Millisecond {
t.Fatalf("openerRun waited past its own bound: %s", since)
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if _, err := os.Stat(marker); err == nil {
return // the process kept running after openerRun gave up waiting, and finished: not killed
}
time.Sleep(10 * time.Millisecond)
}
t.Fatal("the opener process was killed: it never finished past the bound")
}
// DQ-7 (lead answer note (b)): every call writes one act to the log of acts done by hand, with its
// caller and why — a successful one and a refused one alike.
func TestOpenWritesOneActDoneByHandForEveryCall(t *testing.T) {
writeDesktopEntry(t, "browser.desktop", "firefox %u")
x, _ := stubOpener(t, foundSession, map[string]desktop.Result{
"xdg-mime": {Code: 0, Stdout: "browser.desktop\n"},
"systemd-run": {Code: 0},
})
x, _ := stubOpener(t, foundSession, desktop.Result{Code: 0, Stdout: "browser.desktop\n"}, resultOpener(desktop.Result{Code: 0}))
var recorded []string
x.handAct = func(what, why string) error {
recorded = append(recorded, what+"|"+why)
@@ -218,7 +244,7 @@ func TestOpenWritesOneActDoneByHand(t *testing.T) {
if _, err := run(t, x, "node-display-session.open", map[string]any{"address": "ftp://example.net"}); err == nil {
t.Fatal("expected a refusal")
}
if len(recorded) != 0 {
t.Fatalf("a refusal before the opener ran writes no act: %v", recorded)
if len(recorded) != 1 || !strings.Contains(recorded[0], "refused") || !strings.Contains(recorded[0], "ftp://example.net") {
t.Fatalf("a refusal writes an act too (note (b)): %v", recorded)
}
}