Notes with a TTL by level (errors stay until a key after 3 s), timers that only hide their own note (U-P1-1, crutch #13) and a 200-entry log. All lipgloss styles are built once per theme with a generation counter for render caches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package notify
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var t0 = time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
func TestNoteTTLByLevel(t *testing.T) {
|
|
if TTL(Info) != 3*time.Second || TTL(Success) != 3*time.Second || TTL(Warn) != 6*time.Second || TTL(Error) != 3*time.Second {
|
|
t.Fatal("TTL table")
|
|
}
|
|
var c Center
|
|
info := c.Push(Info, "copied", t0)
|
|
c.Expire(info.ID)
|
|
if _, ok := c.Current(); ok {
|
|
t.Fatal("info survived its TTL")
|
|
}
|
|
|
|
// Error: the timer alone does not hide it; a key afterwards does.
|
|
e := c.Push(Error, "boom", t0)
|
|
c.Expire(e.ID)
|
|
if n, ok := c.Current(); !ok || n.ID != e.ID {
|
|
t.Fatal("error hidden by its timer without a key press")
|
|
}
|
|
c.KeyPressed()
|
|
if _, ok := c.Current(); ok {
|
|
t.Fatal("error not dismissed by a key after its TTL")
|
|
}
|
|
|
|
// Error: a key before the minimum time does not hide it at once.
|
|
e2 := c.Push(Error, "boom2", t0)
|
|
c.KeyPressed()
|
|
if n, ok := c.Current(); !ok || n.ID != e2.ID {
|
|
t.Fatal("error dismissed before its minimum time")
|
|
}
|
|
c.Expire(e2.ID)
|
|
if _, ok := c.Current(); ok {
|
|
t.Fatal("error with an earlier key press not hidden when its timer fired")
|
|
}
|
|
}
|
|
|
|
// Crutch #13: a timer closed whatever was showing.
|
|
func TestTimerClosesOnlyOwnNote(t *testing.T) {
|
|
var c Center
|
|
a := c.Push(Info, "a", t0)
|
|
b := c.Push(Warn, "b", t0)
|
|
c.Expire(a.ID)
|
|
if n, ok := c.Current(); !ok || n.ID != b.ID {
|
|
t.Fatalf("current = %+v %v, want b", n, ok)
|
|
}
|
|
c.Expire(a.ID) // twice: no effect
|
|
c.Expire(999) // unknown: no effect
|
|
if n, _ := c.Current(); n.ID != b.ID {
|
|
t.Fatal("stray expire hid b")
|
|
}
|
|
c.Expire(b.ID)
|
|
if _, ok := c.Current(); ok {
|
|
t.Fatal("b survived")
|
|
}
|
|
// An older live note shows again when a newer one goes.
|
|
x := c.Push(Warn, "x", t0)
|
|
y := c.Push(Info, "y", t0)
|
|
c.Expire(y.ID)
|
|
if n, _ := c.Current(); n.ID != x.ID {
|
|
t.Fatal("older note not shown")
|
|
}
|
|
}
|
|
|
|
func TestLogRing(t *testing.T) {
|
|
var c Center
|
|
for i := 0; i < LogSize+50; i++ {
|
|
c.Push(Info, fmt.Sprint(i), t0)
|
|
}
|
|
l := c.Log()
|
|
if len(l) != LogSize || l[0].Text != "50" || l[len(l)-1].Text != fmt.Sprint(LogSize+49) {
|
|
t.Fatalf("log: %d entries, first %q", len(l), l[0].Text)
|
|
}
|
|
}
|