Files
mesh-catalog/modules/mesh-knowledge/cmd/mesh-knowledge/restore_test.go
T
2026-11-25 17:22:06 +01:00

157 lines
6.5 KiB
Go

package main
import (
"context"
"fmt"
"strings"
"testing"
"time"
)
// 466/AC5: a record that lost an insight and a field an earlier accepted version held, in a version made by propose
// (a pending version, then an accepted one marked insight, as the old accept wrote it), gets them back in one version by
// the knowledge base; a dry run lists each entry and writes nothing; a second run writes nothing; what a refresh removed
// is not restored; a record with a version waiting for its review is skipped, saying so.
func TestRestoreGivesBackWhatAVersionDropped(t *testing.T) {
eachStore(t, func(t *testing.T, s Store) {
w := imported(t, s)
ctx := context.Background()
var tools []toolRun
for _, tool := range moduleTools(w.k, callerOf) {
tools = append(tools, toolRun{tool.Name, tool.Run})
}
// accepted is a record accepted with reconstructed: false, then given one insight by the knowledge base as it
// is now; it answers its key and the number of the version that holds them.
accepted := func(t *testing.T) (Key, int) {
n := w.reserved(t, "author@g14")
p := valid(n)
w.accepted(t, "author@g14", p)
key := Key{KindDecision, n}
e := mustGet(t, s, key)
r := e.Current.Clone()
no := false
r.Reconstructed, r.Source = &no, "hq@abc:02-DECISIONS/x.md"
if err := s.Save(ctx, Version{By: "test", At: time.Now(), Why: "seed", Record: r}, true, false, e.Revision); err != nil {
t.Fatal(err)
}
p.Insight = "it said less"
p.Sections[2].Text = "Merges land sooner, by about a minute."
w.accepted(t, "author@g14", p)
return key, mustGet(t, s, key).Versions
}
// lose is what an older propose and accept did with a second insight: a pending version, then the accepted one
// holding only the new insight and none of the fields.
lose := func(t *testing.T, key Key, mark bool) {
e := mustGet(t, s, key)
in := Insight{Date: "2026-10-12", Before: "it said more", By: "corrector@ace"}
pend := e.Current.Clone()
pend.Status, pend.Insights, pend.Reconstructed, pend.Source = StatusProposed, nil, nil, ""
pend.Pending = &PendingState{Insight: in}
if err := s.Save(ctx, Version{By: "corrector@ace", At: time.Now(), Why: "a progressive insight proposed", Record: pend, Insight: true}, false, false, e.Revision); err != nil {
t.Fatal(err)
}
e = mustGet(t, s, key)
if mark {
// another record's acceptance, or the recompute of marks, writes a copy of the served version while the
// insight waits.
if err := s.Save(ctx, Version{By: "the knowledge base", At: time.Now(), Why: "a mark withdrawn", Record: e.Current.Clone()}, true, false, e.Revision); err != nil {
t.Fatal(err)
}
e = mustGet(t, s, key)
}
lost := e.Current.Clone()
lost.Insights, lost.Reconstructed, lost.Source = []Insight{in}, nil, ""
if err := s.Save(ctx, Version{By: "the knowledge base", At: time.Now(), Why: "accepted after the knowledge base's review", Record: lost, Insight: true}, true, true, e.Revision); err != nil {
t.Fatal(err)
}
}
run := func(t *testing.T, key Key, args map[string]any) map[string]any {
args["kind"], args["number"] = key.Kind, float64(key.Number)
out, err := callTool(t, tools, RestoreTool, args)
if err != nil {
t.Fatal(err)
}
return out
}
records := func(out map[string]any) []map[string]any {
var got []map[string]any
for _, r := range out["records"].([]any) {
got = append(got, r.(map[string]any))
}
return got
}
for _, mark := range []bool{false, true} {
t.Run(fmt.Sprintf("a version made by propose dropped them (a mark between: %v)", mark), func(t *testing.T) {
key, held := accepted(t)
lose(t, key, mark)
before := mustGet(t, s, key).Versions
if mark {
held += 2 // the entries come from the mark, a copy of the served version, written after the pending version
}
want := []string{fmt.Sprintf("insight 2026-10-11 by author@g14 (version %d)", held), fmt.Sprintf("reconstructed (version %d)", held), fmt.Sprintf("source (version %d)", held)}
dry := run(t, key, map[string]any{"dry-run": true})
recs := records(dry)
if len(recs) != 1 || fmt.Sprint(recs[0]["restored"]) != fmt.Sprint(toAny(want)) || recs[0]["skipped"] != nil ||
dry["written"] != false || mustGet(t, s, key).Versions != before {
t.Fatalf("dry run: %v", dry)
}
out := run(t, key, map[string]any{})
e := mustGet(t, s, key)
in := e.Current.Insights
if out["written"] != true || out["versions-written"] != float64(1) || e.Versions != before+1 || len(in) != 2 ||
in[0].Before != "it said less" || in[0].By != "author@g14" || in[1].Before != "it said more" || in[1].By != "corrector@ace" ||
e.Current.Reconstructed == nil || *e.Current.Reconstructed || e.Current.Source == "" {
t.Fatalf("not restored: %v, %d versions (was %d), %+v", out, e.Versions, before, in)
}
if got := w.said.of(EventChanged); len(got) == 0 || got[len(got)-1]["by"] != "the knowledge base" {
t.Fatalf("no changed event by the knowledge base: %v", got)
}
again := run(t, key, map[string]any{})
if len(records(again)) != 0 || again["written"] != false || mustGet(t, s, key).Versions != before+1 {
t.Fatalf("a second run wrote: %v", again)
}
})
}
t.Run("what a refresh removed is not restored", func(t *testing.T) {
key, _ := accepted(t)
e := mustGet(t, s, key)
refreshed := e.Current.Clone()
refreshed.Reconstructed = nil
if err := s.Save(ctx, Version{By: "import@test", At: time.Now(), Why: "refreshed", Record: refreshed}, true, false, e.Revision); err != nil {
t.Fatal(err)
}
before := mustGet(t, s, key).Versions
out := run(t, key, map[string]any{})
if e := mustGet(t, s, key); len(records(out)) != 0 || e.Versions != before || e.Current.Reconstructed != nil {
t.Fatalf("a field a refresh removed was put back: %v", out)
}
})
t.Run("a record with a version waiting is skipped, and says so", func(t *testing.T) {
key, _ := accepted(t)
lose(t, key, false)
p := insightTo(t, w, key)
p.Insight = "it said nothing of the day"
if _, err := w.k.Propose("author@g14", p); err != nil {
t.Fatal(err)
}
before := mustGet(t, s, key).Versions
out := run(t, key, map[string]any{})
recs := records(out)
if len(recs) != 1 || !strings.Contains(fmt.Sprint(recs[0]["skipped"]), "waits for its review") ||
out["written"] != false || mustGet(t, s, key).Versions != before {
t.Fatalf("a record with a version waiting: %v", out)
}
})
})
}
func toAny(list []string) []any {
out := make([]any, len(list))
for i, s := range list {
out[i] = s
}
return out
}