Files
mesh-catalog/modules/xclip/cmd/xclip-tools/xclip_test.go
T
jochen b1b7e58e4b xclip: the package, and the operator's clipboard from the mesh (hq to-be 42 phase 2.7)
A package and nothing else, the tool the desktop's scripts depend on.
Four Go tools: copy, paste (text, base64 for other types, empty when
nothing), targets and session.

The runtime is given no session words, but runs as the account in the
machine's own namespace, so the session is found rather than configured:
the process's DISPLAY, else the account's processes' DISPLAY and XAUTHORITY
from /proc (the window manager's first), else the only X socket with
~/.Xauthority. Measured with both variables unset: :1 found through i3, the
server answered. With no session every tool says so and runs nothing.
2026-10-04 13:02:23 +02:00

107 lines
3.5 KiB
Go

package main
import (
"strings"
"testing"
)
func TestTheManifestIsThePackageAndNothingElse(t *testing.T) {
m := readManifest(t)
holdsTheBundle(t, m, "xclip")
if got := strings.Join(m.packages(), ","); got != "xclip" || len(m.Resources) != 1 {
t.Errorf("packages %s, resources %v", got, m.Resources)
}
}
// aSession gives the tools an X session on :1 with the account's cookie.
func aSession(t *testing.T) {
t.Helper()
_, sockets := aMachine(t, map[string]string{"DISPLAY": ":1", "XAUTHORITY": "/home/op/.Xauthority"})
aSocket(t, sockets, "X1")
}
func TestCopyRunsDetachedInTheSessionWithTheTextOnItsInput(t *testing.T) {
aSession(t)
f := using(t, func(string, Cmd) Result { return ok("") })
got, err := Copy("hello", "clipboard", "")
if err != nil || got.Bytes != 5 || got.Type != "UTF8_STRING" || got.Session.Display != ":1" {
t.Fatalf("%+v %v", got, err)
}
c := f.asked[0]
if f.lines()[0] != "xclip -selection clipboard -in" || c.Stdin != "hello" || !c.Detached {
t.Errorf("%v %+v", f.lines(), c)
}
if strings.Join(c.Env, " ") != "DISPLAY=:1 XAUTHORITY=/home/op/.Xauthority" {
t.Errorf("env %v", c.Env)
}
if _, err := Copy("<b>x</b>", "primary", "text/html"); err != nil || f.lines()[1] != "xclip -selection primary -in -t text/html" {
t.Errorf("%v %v", f.lines(), err)
}
if _, err := Copy(strings.Repeat("a", MostCopy+1), "clipboard", ""); err == nil {
t.Error("more than 1 MiB")
}
if _, err := Copy("x", "clipboard", "-o"); err == nil {
t.Error("an option as a type")
}
}
func TestPasteAnswersTextBase64OrEmpty(t *testing.T) {
aSession(t)
answer := Result{Stdout: "some text"}
f := using(t, func(string, Cmd) Result { return answer })
got, err := Paste("clipboard", "")
if err != nil || got.Text != "some text" || got.Bytes != 9 || got.Empty {
t.Fatalf("%+v %v", got, err)
}
if f.lines()[0] != "xclip -selection clipboard -out" || f.asked[0].Detached {
t.Errorf("%v", f.lines())
}
answer = Result{Stdout: "\x89PNG\r\n\x1a\n\xff"}
got, _ = Paste("clipboard", "image/png")
if got.Text != "" || got.Base64 == "" {
t.Errorf("binary: %+v", got)
}
answer = Result{Status: 1, Stderr: "Error: target UTF8_STRING not available\n"}
got, err = Paste("clipboard", "")
if err != nil || !got.Empty {
t.Errorf("an empty clipboard is an answer: %+v %v", got, err)
}
}
func TestADisplayThatRefusesIsSaidWithHowItWasFound(t *testing.T) {
aSession(t)
using(t, func(string, Cmd) Result { return Result{Status: 1, Stderr: "Error: Can't open display: :1\n"} })
_, err := Paste("clipboard", "")
if err == nil || !strings.Contains(err.Error(), "refused the connection") || !strings.Contains(err.Error(), "environment") {
t.Fatalf("%v", err)
}
}
func TestWithoutASessionNothingRunsAndTheToolSaysWhy(t *testing.T) {
aMachine(t, map[string]string{})
f := using(t, func(string, Cmd) Result { return ok("") })
if _, err := Copy("x", "clipboard", ""); err == nil || !strings.Contains(err.Error(), "no graphical session") {
t.Fatalf("%v", err)
}
got, err := SessionCheck()
if err != nil || got["found"] != false || !strings.Contains(got["why"].(string), "logged in") {
t.Fatalf("%v %v", got, err)
}
if len(f.asked) != 0 {
t.Errorf("nothing runs without a session: %v", f.lines())
}
}
func TestTargetsListWhatASelectionOffers(t *testing.T) {
aSession(t)
using(t, func(string, Cmd) Result { return ok("TIMESTAMP\nTARGETS\nUTF8_STRING\ntext/html\n") })
got, err := Targets("clipboard")
if err != nil || len(got["targets"].([]string)) != 4 {
t.Fatalf("%v %v", got, err)
}
s, _ := SessionCheck()
if s["answers"] != true {
t.Errorf("%v", s)
}
}