Files
mesh-catalog/modules/claude-licence-manager/cmd/manager/conditions_test.go
T
jschoubben cff1005422
mesh/delivery merged: source.main moved to novox/mesh-catalog@6d000647de2d
mesh/check being carried to cff1005422c1 on 6d000647de2d | from f9dcbc295723
P2: the licence manager is four parts of one manifest (issue 1409)
The one bundle that did everything becomes the manager bundle, which runs long and answers the seat's ten verbs and
the three acting tools, and a tools bundle of its own that answers the four reading tools and writes nothing, so they
answer while the manager fails to load or stops. Both import the module's one package for the layouts of the kept
data and the log entries, the key form, the entries' names and the settings with their validity; the settings file
and the account with its directory stay as they are.

Builds: design 39 parts §1, §2, §3, §4, §5, §6, §7, §8
2026-11-12 12:08:03 +01:00

252 lines
9.8 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
. "claude-licence-manager/internal/licences"
)
// saying gives mm's manager a state `conditions` and records each condition it says, in the form the operator
// channel's client encodes, refusing what it refuses.
func saying(mm *miniMesh) (*memState, *[]oc.Condition) {
kept := newMemState(func() time.Time { return mm.now })
said := &[]oc.Condition{}
mm.m.Conditions = kept
mm.m.Say = func(c oc.Condition) error {
if _, err := occ.EncodeCondition(c); err != nil {
return err
}
if _, err := occ.ConditionSubject(Publisher); err != nil {
return err
}
*said = append(*said, c)
return nil
}
return kept, said
}
// of is what was said of key, as "state" per saying.
func of(said []oc.Condition, key string) []string {
var out []string
for _, c := range said {
if c.Key == key {
out = append(out, c.State)
}
}
return out
}
// design 39 bus-and-switch §9/AC1, §11, §33, B17, B37: each kind of condition the manager holds of itself is written in
// `conditions` with its body and since, said once with its key, severity, scope and id, its words opened by their
// verdict, and said cleared, its key deleted, once it no longer holds.
func TestAConditionIsKeptThenSaidOnce(t *testing.T) {
ctx := context.Background()
file := filepath.Join(t.TempDir(), "settings.json")
for _, c := range []struct {
name, key, severity string
raise, fix func(mm *miniMesh)
}{
{"an invalid pair", "settings.room_below_pct.invalid", occ.SeverityWarning,
func(mm *miniMesh) { mm.m.Settings.RoomBelow = 100 }, func(mm *miniMesh) { mm.m.Settings.RoomBelow = 90 }},
{"a setting outside its bound", "settings.switch_at_pct.invalid", occ.SeverityWarning,
func(mm *miniMesh) {
_ = os.WriteFile(file, []byte(`{"switch_at_pct": 150}`), 0o600)
mm.m.Settings = SettingsFrom(file)
},
func(mm *miniMesh) {
_ = os.WriteFile(file, []byte(`{"switch_at_pct": 98}`), 0o600)
mm.m.Settings = SettingsFrom(file)
}},
} {
mm := newMesh(t)
kept, said := saying(mm)
c.raise(mm)
for range 2 {
if err := mm.m.SayConditions(ctx, false); err != nil {
t.Fatalf("%s: %v", c.name, err)
}
}
if got := of(*said, c.key); strings.Join(got, ",") != "raised" {
t.Fatalf("%s: said %v of %s, all said %+v", c.name, got, c.key, *said)
}
s := (*said)[0]
if s.Severity != c.severity || s.Subject.Scope != "module" || s.Subject.ID != Publisher || s.Headline == "" ||
s.Summary == "" || s.Resolved == "" || !strings.HasPrefix(s.Explanation, occ.NeedsYou+s.Needs) || s.Needs == "" {
t.Fatalf("%s: said %+v", c.name, s)
}
var k KeptCondition
if e, _ := kept.Get(c.key); e == nil || json.Unmarshal(e.Value, &k) != nil || k.Body.Key != c.key || k.Since == "" {
t.Fatalf("%s: kept %+v", c.name, k)
}
c.fix(mm)
_ = mm.m.SayConditions(ctx, false)
if got := of(*said, c.key); strings.Join(got, ",") != "raised,cleared" {
t.Fatalf("%s: said %v", c.name, got)
}
if e, _ := kept.Get(c.key); e != nil {
t.Fatalf("%s: the key is kept after its clearing", c.name)
}
}
}
// design 39 bus-and-switch §9/AC2, design 39 parts §11: at its start the manager says cleared each key of `conditions` it no longer holds,
// and says raised again each it still holds, unchanged or not.
func TestAStartClearsWhatItNoLongerHolds(t *testing.T) {
ctx := context.Background()
mm := newMesh(t)
kept, said := saying(mm)
gone := condition("licence.gone.no-room", occ.SeverityUrgent, "No licence has room for 1 machine", "s", "add one",
"", "x has a licence with room again")
mm.m.Settings.WarnAt = 100
_ = mm.m.SayConditions(ctx, false)
_, _ = kept.Put(gone.Key, KeptCondition{Body: gone, Since: "2026-11-11T20:00:00Z"})
// design 39 parts §11: a carry-over-failed key kept from before the carry-over left is cleared like any other.
carry := condition("carry-over-failed", occ.SeverityUrgent, "The licence manager could not carry over its licences",
"s", "", "", "The licence manager carried over its licences")
_, _ = kept.Put(carry.Key, KeptCondition{Body: carry, Since: "2026-11-12T09:00:00Z"})
*said = nil
if err := mm.m.SayConditions(ctx, true); err != nil {
t.Fatal(err)
}
if strings.Join(of(*said, gone.Key), ",") != "cleared" || strings.Join(of(*said, carry.Key), ",") != "cleared" ||
strings.Join(of(*said, "settings.warn_at_pct.invalid"), ",") != "raised" {
t.Fatalf("a start said %+v", *said)
}
if (*said)[0].Resolved != carry.Resolved || (*said)[1].Resolved != gone.Resolved {
t.Fatalf("the clearings carry %+v", *said)
}
for _, key := range []string{gone.Key, carry.Key} {
if e, _ := kept.Get(key); e != nil {
t.Fatalf("%s, no longer held, is kept", key)
}
}
}
// design 39 bus-and-switch §29/AC1, B39: with every licence at its threshold, no-room is said once, its headline a
// count of machines, its words naming the licence, the window's reset and every other licence's room; a reading with
// room clears it.
func TestNoRoomIsSaidOnceAndClearedByRoom(t *testing.T) {
ctx := context.Background()
mm := fourOnA(t)
_, said := saying(mm)
_ = mm.store.SaveLicence(ctx, sub("b", read(20, 95)))
_ = mm.store.SaveLicence(ctx, sub("c", read(40, 99)))
for range 2 {
_ = mm.m.SayConditions(ctx, false)
}
key := "licence.a.no-room"
if strings.Join(of(*said, key), ",") != "raised" {
t.Fatalf("said %+v", *said)
}
var c oc.Condition
for _, s := range *said {
if s.Key == key {
c = s
}
}
if c.Severity != occ.SeverityUrgent || c.Headline != "No licence has room for 4 machines" ||
c.Needs != "add a licence, or wait for a's weekly window to reset at 2026-11-14T09:00:00Z" ||
!strings.Contains(c.Explanation, "a is at 99 % of its weekly window, with ace, g14, laptop, server bound to it") ||
!strings.Contains(c.Explanation, "b has 5 % room, its weekly window not below 90 %") ||
c.Resolved != "ace, g14, laptop, server have a licence with room again" {
t.Fatalf("no-room %+v", c)
}
_ = mm.store.SaveLicence(ctx, sub("b", read(10, 20)))
_ = mm.m.SayConditions(ctx, false)
if strings.Join(of(*said, key), ",") != "raised,cleared" {
t.Fatalf("said %v after a reading with room", of(*said, key))
}
}
// design 39 bus-and-switch §30/AC1, B39: a rising series of readings says near-limit once at the crossing and again only
// when its words change; with no other licence having room it needs the operator; a reading after the reset clears it.
func TestNearLimitIsSaidOncePerCrossing(t *testing.T) {
ctx := context.Background()
mm := fourOnA(t)
_, said := saying(mm)
key := "licence.a.near-limit"
var last oc.Condition
for _, w := range []float64{80, 89, 91, 91, 95} {
_ = mm.store.SaveLicence(ctx, sub("a", read(95, w))) // a session at 95 % takes no part (C4)
_ = mm.m.SayConditions(ctx, false)
for _, s := range *said {
if s.Key == key {
last = s
}
}
}
if strings.Join(of(*said, key), ",") != "raised,raised" || last.Headline != "a at 95% weekly" || last.Needs != "" ||
!strings.HasPrefix(last.Explanation, occ.NothingToDo+" ace, g14, laptop, server move to b at 99 %.") ||
!strings.Contains(last.Explanation, "which resets at 2026-11-14T09:00:00Z") {
t.Fatalf("said %v, last %+v", of(*said, key), last)
}
_ = mm.store.SaveLicence(ctx, sub("b", read(0, 95)))
_ = mm.store.SaveLicence(ctx, sub("c", read(0, 95)))
_ = mm.m.SayConditions(ctx, false)
if c := (*said)[len(*said)-1]; c.Key != key || c.Needs != "no other licence has room" {
t.Fatalf("with no target said %+v", c)
}
_ = mm.store.SaveLicence(ctx, sub("a", read(3, 1)))
_ = mm.m.SayConditions(ctx, false)
if got := of(*said, key); got[len(got)-1] != "cleared" || len(got) != 4 {
t.Fatalf("after the reset said %v", got)
}
}
// design 39 bus-and-switch §31/AC1, B34, B38: three failed readings in a row, the latest success then older than
// usage_stale_minutes, say usage-unread naming the vendor's reason, needing the operator for the consumers bound to it;
// one success clears it. A subscription never read successfully is said once its adoption is that old.
func TestUsageUnreadIsSaidAndCleared(t *testing.T) {
ctx := context.Background()
mm := newMesh(t)
_, said := saying(mm)
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
if err := mm.m.ReadUsage(ctx); err != nil {
t.Fatal(err)
}
key := "licence." + KeyFor(licence1) + ".usage-unread"
mm.vendor.usageErr = errors.New("429 Too Many Requests: rate limited")
for i := 1; i <= 3; i++ {
mm.now = t0.Add(time.Duration(i) * 6 * time.Minute)
if err := mm.m.ReadUsage(ctx); err != nil {
t.Fatal(err)
}
if got := len(of(*said, key)); (i < 3 && got != 0) || (i == 3 && got != 1) {
t.Fatalf("after %d failed readings said %v", i, of(*said, key))
}
}
c := (*said)[len(*said)-1]
if c.Severity != occ.SeverityWarning || c.Needs != "laptop are not moved off it while its usage is unknown" ||
!strings.Contains(c.Explanation, "the vendor answers: 429 Too Many Requests: rate limited") {
t.Fatalf("usage-unread %+v", c)
}
mm.vendor.usageErr = nil
_ = mm.m.ReadUsage(ctx)
if strings.Join(of(*said, key), ",") != "raised,cleared" {
t.Fatalf("after a success said %v", of(*said, key))
}
// B38: never read successfully since its adoption.
never := Licence{Name: "new@example.org", Kind: "subscription", AdoptedAt: mm.now.UnixMilli()}
_ = mm.store.SaveLicence(ctx, never)
_ = mm.m.SayConditions(ctx, false)
newKey := "licence." + KeyFor(never.Name) + ".usage-unread"
if len(of(*said, newKey)) != 0 {
t.Fatal("said at its adoption")
}
mm.now = mm.now.Add(16 * time.Minute)
_ = mm.m.SayConditions(ctx, false)
if strings.Join(of(*said, newKey), ",") != "raised" {
t.Fatalf("said %v of a licence never read", of(*said, newKey))
}
}