mesh/delivery merged: source.main moved to novox/mesh-catalog@bfa4a0851acf
mesh/check checking b7f78d47c5c4 on bfa4a0851acf
Claude-Session: https://claude.ai/code/session_01JG73KK8HD1gufnqyFxMFdW
149 lines
5.9 KiB
Go
149 lines
5.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Review in parts (to-be 55 §5, AC50): the head and each section are judged apart, at most two at once; a usual revision
|
|
// judges only the sections it changed; a part that could not be judged keeps the others' verdicts and is the only one asked
|
|
// again; a version's `reviewed` is said once, and each part's verdict is said as `part-reviewed`.
|
|
func TestAVersionIsReviewedInPartsAndARevisionJudgesOnlyWhatChanged(t *testing.T) {
|
|
w, _ := ledgerWorld(t)
|
|
var running, most atomic.Int32
|
|
var mu sync.Mutex
|
|
var asked []string
|
|
var failOn string
|
|
inner := w.k.Judge
|
|
// The first judgement is counted at once; the test sees that no second one runs beside it.
|
|
var arrived atomic.Int32
|
|
meet := make(chan struct{})
|
|
w.k.Judge = func(q string, s map[string]any, model string) (json.RawMessage, string, error) {
|
|
n := running.Add(1)
|
|
if arrived.Add(1) == MostParallelParts {
|
|
close(meet)
|
|
}
|
|
if arrived.Load() <= MostParallelParts {
|
|
select {
|
|
case <-meet:
|
|
case <-time.After(5 * time.Second):
|
|
}
|
|
}
|
|
for {
|
|
m := most.Load()
|
|
if n <= m || most.CompareAndSwap(m, n) {
|
|
break
|
|
}
|
|
}
|
|
defer running.Add(-1)
|
|
mu.Lock()
|
|
asked = append(asked, q)
|
|
bad := failOn != "" && strings.Contains(q, failOn)
|
|
mu.Unlock()
|
|
if bad {
|
|
return nil, "", fmt.Errorf("the agent holds no licence")
|
|
}
|
|
return inner(q, s, model)
|
|
}
|
|
n := w.reserved(t, "author@g14")
|
|
p := valid(n)
|
|
p.Sections[0].Text = "The merge window of ADR 0276 §1 waits too long."
|
|
failOn = `section "Consequences"`
|
|
if _, err := w.k.Propose("author@g14", p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
said := w.reviewAll(t)
|
|
e := mustGet(t, w.k.Store, Key{KindDecision, n})
|
|
// One part failed: the record stays proposed, owed again, and the four others keep their verdicts.
|
|
if e.Current.Status != StatusProposed || e.Current.Judging == nil || e.Current.Judging.State != JudgingFailed || len(e.Current.Parts) != 4 {
|
|
t.Fatalf("%v: %s, %+v, %d parts", said, e.Current.Status, e.Current.Judging, len(e.Current.Parts))
|
|
}
|
|
if most.Load() != 1 || MostParallelParts != 1 {
|
|
t.Fatalf("%d judgements at once; exactly 1 (the node judges one at a time), const %d", most.Load(), MostParallelParts)
|
|
}
|
|
if got := len(w.said.of(EventPartReviewed)); got != 4 || len(w.said.of(EventReviewed)) != 1 {
|
|
t.Fatalf("part-reviewed %d, reviewed %d", got, len(w.said.of(EventReviewed)))
|
|
}
|
|
// Asked again after the pause, only the part that failed is judged, and the version is accepted.
|
|
failOn = ""
|
|
mu.Lock()
|
|
asked = nil
|
|
mu.Unlock()
|
|
w.clock = w.clock.Add(ReviewPause + 1)
|
|
w.reviewAll(t)
|
|
if len(asked) != 1 || !strings.Contains(asked[0], `section "Consequences"`) {
|
|
t.Fatalf("the retry judged %d parts", len(asked))
|
|
}
|
|
e = mustGet(t, w.k.Store, Key{KindDecision, n})
|
|
if e.Current.Status != StatusAccepted || len(e.Current.Parts) != 5 || len(e.Current.Reviews) != 1 {
|
|
t.Fatalf("%s %d parts %d reviews", e.Current.Status, len(e.Current.Parts), len(e.Current.Reviews))
|
|
}
|
|
if len(w.said.of(EventReviewed)) != 2 { // the failed try, and the version once
|
|
t.Fatalf("reviewed said %d times", len(w.said.of(EventReviewed)))
|
|
}
|
|
// A progressive insight changing one section judges that section and, its head unchanged, nothing else.
|
|
mu.Lock()
|
|
asked = nil
|
|
mu.Unlock()
|
|
q := valid(n)
|
|
q.Sections[0].Text = p.Sections[0].Text
|
|
q.Sections[1].Text = "1. **Keep it.** Rejected, and why."
|
|
q.Insight = "the options were not said"
|
|
if _, err := w.k.Propose("author@g14", q); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w.reviewAll(t)
|
|
// The head (it names the insight), the changed section, and the Context: it names ADR 0276, whose clause this record
|
|
// changed when it was accepted, so what it names changed.
|
|
if len(asked) != 3 {
|
|
t.Fatalf("a revision of one section judged %d parts", len(asked))
|
|
}
|
|
// The mark of a part in flight is cleared when it is answered.
|
|
if keys, _ := w.k.States.State(askedState).Keys(); len(keys) != 0 {
|
|
t.Fatalf("parts still marked asked: %v", keys)
|
|
}
|
|
}
|
|
|
|
// A section's frame is only what it names: the clauses by number, the records by number, and the digest of its Context.
|
|
func TestASectionPartSeesItsFrameAndNotTheOtherSections(t *testing.T) {
|
|
r := valid(300)
|
|
rec := &Record{Kind: KindDecision, Number: 300, Title: r.Title, Topic: r.Topic, Date: r.Date, Status: StatusProposed,
|
|
Clauses: []Clause{{N: 1, Text: "**First.** one."}, {N: 2, Text: "**Second.** two."}}, Sections: []Section{
|
|
{Name: "Context", Text: "context text"}, {Name: "Consequences", Text: "It changes clause 2 and ADR 0276."}}}
|
|
linked := []*Record{{Kind: KindDecision, Number: 276, Title: "Window", Status: StatusAccepted, Clauses: []Clause{{N: 1, Text: "**W.** window.", Status: ClauseInForce, Source: SourceWritten}}}}
|
|
parts := partsOf(rec, linked)
|
|
if len(parts) != 3 || parts[0].name != HeadPart || parts[2].name != "Consequences" {
|
|
t.Fatalf("%+v", parts)
|
|
}
|
|
q := parts[2].question
|
|
for _, want := range []string{"Clause 2 it names: **Second.** two.", "ADR 0276", "digest sha256:", "It changes clause 2"} {
|
|
if !strings.Contains(q, want) {
|
|
t.Errorf("the section part lacks %q", want)
|
|
}
|
|
}
|
|
for _, never := range []string{"**First.** one.", "context text"} {
|
|
if strings.Contains(q, never) {
|
|
t.Errorf("the section part holds %q, which it does not name", never)
|
|
}
|
|
}
|
|
if strings.Contains(parts[0].question, "It changes clause 2") {
|
|
t.Error("the head holds a section's text")
|
|
}
|
|
// A change to a clause the section does not name, or to the links, leaves its digest; a change to one it names does not.
|
|
other := rec.Clone()
|
|
other.Clauses[0].Text = "**First.** one, changed."
|
|
other.Links = append(other.Links, Link{Kind: LinkCites, To: Ref{Kind: KindDecision, Number: 276}})
|
|
if partsOf(other, linked)[2].digest != parts[2].digest {
|
|
t.Error("a change the section does not name changed its digest")
|
|
}
|
|
other.Clauses[1].Text = "**Second.** two, changed."
|
|
if partsOf(other, linked)[2].digest == parts[2].digest {
|
|
t.Error("a change to a clause the section names kept its digest")
|
|
}
|
|
}
|