1package core_test
2
3import (
4 "context"
5 "slices"
6 "testing"
7 "time"
8
9 "github.com/bnema/neferwl/internal/adapters/config"
10 "github.com/bnema/neferwl/internal/core"
11 "github.com/bnema/neferwl/internal/ports"
12)
13
14func powerCore(t *testing.T) (chan ports.InputEvent, chan ports.ClientEvent, chan ports.OutputEvent, chan ports.ClientCommand, chan []ports.Scene) {
15 t.Helper()
16 input := make(chan ports.InputEvent, 4)
17 client := make(chan ports.ClientEvent, 4)
18 output := make(chan ports.OutputEvent, 2)
19 commands := make(chan ports.ClientCommand, 64)
20 scenes := make(chan []ports.Scene, 1)
21 c, err := core.New(config.Defaults(), core.Channels{Input: input, Client: client, Output: output, Commands: commands, Scenes: scenes, Spawn: make(chan ports.SpawnRequest, 4)})
22 if err != nil {
23 t.Fatal(err)
24 }
25 ctx, cancel := context.WithCancel(context.Background())
26 done := make(chan error, 1)
27 go func() { done <- c.Run(ctx) }()
28 t.Cleanup(func() { cancel(); receive(t, done) })
29 output <- ports.OutputAdded{Info: ports.OutputInfo{Name: "OUT-1", Width: 100, Height: 80}}
30 output <- ports.OutputAdded{Info: ports.OutputInfo{Name: "OUT-2", Width: 100, Height: 80}}
31 return input, client, output, commands, scenes
32}
33
34// sceneOf waits for a scene set in which pred holds for the named output.
35func sceneOf(t *testing.T, scenes <-chan []ports.Scene, name string, pred func(ports.Scene) bool) {
36 t.Helper()
37 deadline := time.After(2 * time.Second)
38 for {
39 select {
40 case set := <-scenes:
41 for _, s := range set {
42 if s.Output == name && pred(s) {
43 return
44 }
45 }
46 case <-deadline:
47 t.Fatalf("no matching scene for %s", name)
48 }
49 }
50}
51
52// A client turning an output off marks its scene and tells wayland; back
53// on clears both. Other outputs stay on.
54func TestOutputPower(t *testing.T) {
55 _, client, _, commands, scenes := powerCore(t)
56 sceneOf(t, scenes, "OUT-2", func(s ports.Scene) bool { return !s.Off })
57 client <- ports.OutputPower{Output: "OUT-2", On: false}
58 sceneOf(t, scenes, "OUT-2", func(s ports.Scene) bool { return s.Off })
59 for {
60 if v := commandOf[ports.SetOutputs](t, commands); slices.Equal(v.Off, []string{"OUT-2"}) {
61 break
62 }
63 }
64 client <- ports.OutputPower{Output: "OUT-2", On: true}
65 sceneOf(t, scenes, "OUT-2", func(s ports.Scene) bool { return !s.Off })
66 for {
67 if v := commandOf[ports.SetOutputs](t, commands); len(v.Off) == 0 {
68 break
69 }
70 }
71 // Unknown outputs are ignored.
72 client <- ports.OutputPower{Output: "NOPE", On: false}
73 sceneOf(t, scenes, "OUT-1", func(s ports.Scene) bool { return !s.Off })
74}
75
76// Input reports user activity to wayland, at most once per interval.
77func TestUserActivity(t *testing.T) {
78 input, _, _, commands, _ := powerCore(t)
79 for range 3 {
80 input <- ports.PointerMotion{X: 1, Y: 1}
81 }
82 commandOf[ports.UserActivity](t, commands)
83 deadline := time.After(ports.ActivityInterval / 2)
84 for {
85 select {
86 case c := <-commands:
87 if _, ok := c.(ports.UserActivity); ok {
88 t.Fatal("second UserActivity within the interval")
89 }
90 continue
91 case <-deadline:
92 }
93 break
94 }
95 time.Sleep(ports.ActivityInterval)
96 input <- ports.PointerMotion{X: 2, Y: 2}
97 commandOf[ports.UserActivity](t, commands)
98}