Files
magpie/internal/awake/awake_test.go
T
yetone 56b770ec99 settings: keep awake can keep the display on too (#975, Hu9956: magpie's keep-awake held the system up, but the screen still went black and locked, and their agents record the screen to check their work, which a locked screen stops) — Settings › General's "Keep awake while agents work" row now has three segments, Off / On / Screen on too, saved as keepAwake and the new keepAwakeDisplay (default off, so On and every existing settings.json behave as before). awake.Keep takes a Level (Off, System, Display) in place of a bool; a level changed while held releases and takes the hold anew. On a Mac the display level runs caffeinate -i -d -w <pid> (pmset -g assertions on a caffeinate of the same flags shows PreventUserIdleDisplaySleep beside PreventUserIdleSystemSleep); on Windows SetThreadExecutionState gets ES_DISPLAY_REQUIRED too; on Linux logind's idle inhibitor (systemd-inhibit --what=idle:sleep, unchanged) is all there is, and whether the screen stays on is up to the desktop. keepAwakeDisplay is this computer's own (KeepOwn) and goes in prefsKeep, so another setting's save keeps it. The row's line says the display stays on when it's chosen; it keeps its English in data-en, so a language change re-translates it. zh/ja/de strings added. No TUI or CLI has keep-awake, so none changed. TestKeepDisplay (taken once, taken anew with the display, not again at the same level, back to System, released at Off), TestCaffeinateArgs, TestExecutionState (GOOS=windows test build) and TestKeepAwakeKeptAndOwn cover it; go test ./internal/awake ./internal/settings ./internal/gui ./internal/gateway pass, -race -count=5 on awake. keep-awake.test.cjs now runs en/zh/ja/de at 900px and 440px in Chromium and WebKit (display posts keepAwake+keepAwakeDisplay true, the line changes, another setting's save keeps it, segments fit the row, On and Off clear it, no click moves the page): 32/32 twice; gui-ja, gui-de, i18n-keys and click-scroll pass. go vet (darwin, windows, linux), darwin, linux (nogui) and windows builds pass. Not tried with an agent recording the screen; the reporter is asked to confirm.
2026-10-06 15:33:28 +08:00

146 lines
3.7 KiB
Go

package awake
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestWant(t *testing.T) {
now := time.Now()
for _, c := range []struct {
st State
want bool
}{
{State{}, false},
{State{Busy: true}, true},
{State{Last: now.Add(-time.Minute)}, true},
{State{Last: now.Add(-Grace - time.Second)}, false},
} {
if got := want(c.st, now); got != c.want {
t.Errorf("want(%+v) = %v, want %v", c.st, got, c.want)
}
}
}
// TestKeep holds sleep off while the agents work and the setting is on, once,
// and lets go when they are idle, when it is turned off and when Keep ends.
func TestKeep(t *testing.T) {
oldHold, oldTick := hold, tick
defer func() { hold, tick = oldHold, oldTick }()
tick = time.Millisecond
var held, takes atomic.Int32
hold = func(bool) (func(), error) {
held.Add(1)
takes.Add(1)
return func() { held.Add(-1) }, nil
}
var mu sync.Mutex
on, st := true, State{}
set := func(o bool, s State) { mu.Lock(); on, st = o, s; mu.Unlock() }
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
Keep(ctx, func() Level {
mu.Lock()
defer mu.Unlock()
if on {
return System
}
return Off
},
func() State { mu.Lock(); defer mu.Unlock(); return st })
close(done)
}()
wait := func(n int32, what string) {
t.Helper()
for end := time.Now().Add(2 * time.Second); held.Load() != n; {
if time.Now().After(end) {
t.Fatalf("%s: held %d, want %d", what, held.Load(), n)
}
time.Sleep(time.Millisecond)
}
}
wait(0, "idle")
set(true, State{Busy: true})
wait(1, "busy")
time.Sleep(20 * time.Millisecond)
if takes.Load() != 1 {
t.Fatalf("taken %d times while busy, want once", takes.Load())
}
set(true, State{Last: time.Now()})
time.Sleep(20 * time.Millisecond)
wait(1, "within grace")
set(true, State{Last: time.Now().Add(-Grace - time.Minute)})
wait(0, "past grace")
set(false, State{Busy: true})
time.Sleep(20 * time.Millisecond)
wait(0, "off")
set(true, State{Busy: true})
wait(1, "on again")
cancel()
<-done
wait(0, "ended")
}
// TestKeepDisplay holds the display on only at Display, and takes the hold
// anew when the level changes while the agents work (#975).
func TestKeepDisplay(t *testing.T) {
oldHold, oldTick := hold, tick
defer func() { hold, tick = oldHold, oldTick }()
tick = time.Millisecond
var mu sync.Mutex
var holds []bool // display of each hold taken
live := 0
hold = func(display bool) (func(), error) {
mu.Lock()
defer mu.Unlock()
holds = append(holds, display)
live++
return func() { mu.Lock(); live--; mu.Unlock() }, nil
}
level := System
set := func(l Level) { mu.Lock(); level = l; mu.Unlock() }
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
Keep(ctx, func() Level { mu.Lock(); defer mu.Unlock(); return level },
func() State { return State{Busy: true} })
close(done)
}()
waitFor := func(what string, ok func() bool) {
t.Helper()
for end := time.Now().Add(2 * time.Second); ; {
mu.Lock()
good := ok()
mu.Unlock()
if good {
return
}
if time.Now().After(end) {
mu.Lock()
defer mu.Unlock()
t.Fatalf("%s: holds %v, live %d", what, holds, live)
}
time.Sleep(time.Millisecond)
}
}
waitFor("system", func() bool { return len(holds) == 1 && !holds[0] && live == 1 })
set(Display)
waitFor("display", func() bool { return len(holds) == 2 && holds[1] && live == 1 })
time.Sleep(20 * time.Millisecond)
mu.Lock()
if len(holds) != 2 {
t.Errorf("taken again at the same level: %v", holds)
}
mu.Unlock()
set(System)
waitFor("system again", func() bool { return len(holds) == 3 && !holds[2] && live == 1 })
set(Off)
waitFor("off", func() bool { return live == 0 })
cancel()
<-done
}