1package app
2
3import (
4 "context"
5 "runtime"
6 "testing"
7 "time"
8
9 "github.com/stretchr/testify/require"
10
11 "github.com/bnema/neferwl/internal/adapters/clock"
12 "github.com/bnema/neferwl/internal/adapters/config"
13 "github.com/bnema/neferwl/internal/adapters/logging"
14 "github.com/bnema/neferwl/internal/adapters/xkb"
15 "github.com/bnema/neferwl/internal/ports"
16)
17
18func TestHintPipesOffByDefault(t *testing.T) {
19 require.False(t, config.Defaults().Hint.Enable)
20 off := newPipes(false, false)
21 require.Nil(t, off.hintScans)
22 require.Nil(t, off.hintResults)
23 require.Nil(t, off.hintCaptured)
24 // No demultiplexing: the outputs reply on the wayland channel itself.
25 require.Equal(t, off.captured, off.capturedAll)
26 ch := off.core()
27 require.Nil(t, ch.HintScans)
28 require.Nil(t, ch.Hints)
29 on := newPipes(false, true)
30 require.NotNil(t, on.hintScans)
31 require.NotNil(t, on.hintResults)
32 require.NotNil(t, on.hintCaptured)
33 require.NotEqual(t, on.captured, on.capturedAll)
34}
35
36func TestHintOffStartsNoHintGoroutine(t *testing.T) {
37 t.Setenv("XDG_RUNTIME_DIR", t.TempDir())
38 ctx, cancel := context.WithCancel(context.Background())
39 defer cancel()
40 done := make(chan error, 1)
41 go func() {
42 done <- Run(ctx, Options{Backend: "headless", NoXwayland: true, Config: config.Defaults(), NoTerminal: true, Timeout: 800 * time.Millisecond})
43 }()
44 time.Sleep(400 * time.Millisecond)
45 buf := make([]byte, 1<<20)
46 dump := string(buf[:runtime.Stack(buf, true)])
47 require.NotContains(t, dump, "adapters/hints.")
48 require.NotContains(t, dump, "routeCaptured")
49 select {
50 case <-done:
51 case <-time.After(3 * time.Second):
52 t.Fatal("session did not stop")
53 }
54}
55
56func TestRelayConfigKeepsStartupHintEnable(t *testing.T) {
57 ctx, cancel := context.WithCancel(context.Background())
58 defer cancel()
59 cur := config.Defaults()
60 in := make(chan ports.ConfigChanged, 1)
61 out := make(chan ports.ConfigChanged, 1)
62 go relayConfig(ctx, cur, in, out, make(chan *xkb.Keymap, 1), make(chan ports.InputDevicesConfig, 1), newCursors(clock.System{}, 0), make(chan ports.ClientCommand, 1), logging.For(ctx, "config"))
63 next := config.Defaults()
64 next.Hint.Enable = true
65 in <- ports.ConfigChanged{Config: next}
66 got := (<-out).Config.Hint
67 require.False(t, got.Enable)
68}
69
70func TestRelayConfigKeepsStartupHintDetector(t *testing.T) {
71 ctx, cancel := context.WithCancel(context.Background())
72 defer cancel()
73 cur := config.Defaults()
74 in := make(chan ports.ConfigChanged, 1)
75 out := make(chan ports.ConfigChanged, 1)
76 go relayConfig(ctx, cur, in, out, make(chan *xkb.Keymap, 1), make(chan ports.InputDevicesConfig, 1), newCursors(clock.System{}, 0), make(chan ports.ClientCommand, 1), logging.For(ctx, "config"))
77 next := config.Defaults()
78 next.Hint.Detector, next.Hint.Model, next.Hint.Threshold = "/other/nh", "/other/models", 0.5
79 in <- ports.ConfigChanged{Config: next}
80 got := (<-out).Config.Hint
81 require.Equal(t, cur.Hint.Detector, got.Detector)
82 require.Equal(t, cur.Hint.Model, got.Model)
83 require.Equal(t, cur.Hint.Threshold, got.Threshold)
84}