Files
mesh-catalog/modules/power/cmd/power/publish_test.go
T
jochen 76b63744e6 Carry the power draw to the bar over the bus, not through a file (hq ADR 0260)
Modules talk over the bus. The power module's bundle puts each fresh reading of its own
sampler in its state draw, keyed by the machine; the bar's block shows that state, which
the bar's bundle watches under the read the mesh grants it. A new catalogue check refuses
a module naming another module's path under /run or /var/lib.
2026-10-08 16:12:37 +02:00

62 lines
1.8 KiB
Go

package main
import (
"errors"
"strings"
"testing"
"time"
)
// Defends novox/hq ADR 0260: the draw reaches the bar over the bus, as this module's state keyed by the
// machine, and only a fresh reading that measured something is put.
func TestAFreshReadingIsPutForThisMachine(t *testing.T) {
m := tree(t, nil)
now := time.Date(2026, 10, 8, 12, 0, 10, 0, time.UTC)
w := 18.2
if err := m.Publish(Draw{Machine: &w}.Reading(now.Add(-5 * time.Second))); err != nil {
t.Fatal(err)
}
var key string
var value Reading
said, err := m.shareDraw("laptop", now, func(k string, v any) error { key, value = k, v.(Reading); return nil })
if err != nil || key != "laptop" || value.Text != "18.2 W machine" || value.Measures != "machine" ||
!strings.HasPrefix(said, "put ") {
t.Fatalf("%q %v: put %q %+v", said, err, key, value)
}
}
func TestAStaleOrEmptyReadingIsNotPut(t *testing.T) {
m := tree(t, nil)
now := time.Date(2026, 10, 8, 12, 1, 0, 0, time.UTC)
never := func(string, any) error { t.Fatal("put"); return nil }
if said, _ := m.shareDraw("laptop", now, never); said != "no reading yet" {
t.Fatalf("%q", said)
}
w := 9.0
if err := m.Publish(Draw{CPU: &w}.Reading(now.Add(-time.Minute))); err != nil {
t.Fatal(err)
}
if said, _ := m.shareDraw("laptop", now, never); said != "the sampler's reading is stale" {
t.Fatalf("%q", said)
}
if err := m.Publish(Draw{}.Reading(now)); err != nil {
t.Fatal(err)
}
if said, _ := m.shareDraw("laptop", now, never); said != "nothing measured" {
t.Fatalf("%q", said)
}
}
func TestARefusedPutIsSaid(t *testing.T) {
m := tree(t, nil)
now := time.Now()
w := 9.0
if err := m.Publish(Draw{CPU: &w}.Reading(now)); err != nil {
t.Fatal(err)
}
if _, err := m.shareDraw("laptop", now, func(string, any) error { return errors.New("refused") }); err == nil {
t.Fatal("a refused put was taken for one done")
}
}