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)): `_state_prune_now` (entry, why) and `_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) } }