Files
mesh-sdk/go/stateobjects/prune_test.go
T
jschoubben 484e2bba3c S11 prune helper and tools: a kept store is pruned toward its cap at its own writes
A process judges a running sum of what its writes put against the figures
read at the bind and after each prune; a bundle judges the fill the
runtime's put answer carries. Past prune-mark (70 %) the oldest keys by
revision go down to prune-target (50 %), in one journal line; a key kept
under _keep/<key> stays, and no reader counts the keep. The generic tools
<module>_state_prune_now and <module>_state_keep act through the hand-act log.

Builds: design 47 H2 §812, H-A-380, H-A-386
2026-11-22 00:17:36 +01:00

60 lines
2.2 KiB
Go

package stateobjects
import (
"errors"
"strings"
"testing"
"time"
stdio "git.novox.be/novox/mesh-sdk/go"
)
type prunable struct {
pruned int
kept map[string]time.Time
}
func (p *prunable) PruneNow() (stdio.Pruned, error) {
p.pruned++
return stdio.Pruned{State: "performed"}, nil
}
func (p *prunable) Keep(key string, until time.Time) error {
p.kept[key] = until
return nil
}
// Design 47 H2 §812 (H-A-380 (3)): `<module>_state_prune_now` (entry, why) and `<module>_state_keep` (entry, key, until,
// why) are written in the hand-act log before they act, and do nothing without why or when the act is not recorded.
func subThePruneToolsPruneAndKeepAsHandActs(t *testing.T) {
p := &prunable{kept: map[string]time.Time{}}
a := &acts{}
tools := map[string]stdio.Tool{}
for _, tool := range PruneTools("mesh-forge-bridge", []Pruning{{Name: "performed", State: p}}, a) {
tools[tool.Name] = tool
}
now, keep := tools["mesh-forge-bridge_state_prune_now"], tools["mesh-forge-bridge_state_keep"]
if now.Run == nil || keep.Run == nil {
t.Fatalf("the tools are %v", tools)
}
if _, err := now.Run(map[string]any{"entry": "performed"}); err == nil || p.pruned != 0 {
t.Fatalf("a prune without why ran: %v", err)
}
if _, err := now.Run(map[string]any{"entry": "performed", "why": "full before a delivery"}); err != nil || p.pruned != 1 {
t.Fatalf("prune now: %v, pruned %d", err, p.pruned)
}
until := time.Now().Add(24 * time.Hour).UTC().Format(time.RFC3339)
if _, err := keep.Run(map[string]any{"entry": "performed", "key": "pr.42", "until": until, "why": "the proof reads it"}); err != nil || p.kept["pr.42"].IsZero() {
t.Fatalf("keep: %v, kept %v", err, p.kept)
}
if _, err := keep.Run(map[string]any{"key": "pr.43", "until": "2000-01-01T00:00:00Z", "why": "late"}); err == nil || !p.kept["pr.43"].IsZero() {
t.Fatalf("a keep until a time past was taken: %v", err)
}
if len(a.done) != 2 || a.done[0].Verb != "mesh-forge-bridge_state_prune_now" || !strings.Contains(a.done[1].What, "pr.42") || a.done[1].Why != "the proof reads it" {
t.Fatalf("the hand acts are %+v", a.done)
}
a.err = errors.New("the hand-act log refused")
if _, err := now.Run(map[string]any{"why": "again"}); err == nil || p.pruned != 1 {
t.Fatalf("a prune not recorded ran: %v", err)
}
}