Files
mesh-catalog/modules/messenger/cmd/messenger/holdback_test.go
T
jschoubben 6f7d070769 M1-M5: the router holds back the asker's oldest ask behind a fourth and offers it again when one ends; the seat operator-channel's ask-again offers it now (milestone 47/G, issue 1966)
Claude-Session: https://claude.ai/code/session_01JG73KK8HD1gufnqyFxMFdW

ask-again is the seat operator-channel's verb (key in; state or error out), not the module tool messenger_ask_again, and records no hand act (G §394, §450).
(Joined from 681975ec.)
Builds: design 47 G §40, §83, §403 (RTR1, RTR2)

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 14:48:07 +01:00

114 lines
5.0 KiB
Go

package main
import (
"encoding/json"
"strings"
"testing"
"time"
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
)
// ADR 0666 §12, design 46 §6 and test 71: an asker has at most three asks offered at once; a fourth is offered and the
// oldest is held back (its tickets withdrawn, its message edited, its record open), offered again with new tickets
// when one of the three ends, offered now by messenger_ask_again; an ask with the same `about` replaces a held-back
// one, and a Decline ends an ask whose asker may ask again at once.
func TestNoAskIsHiddenTheOldestOfFourIsHeldBackAndOfferedAgain(t *testing.T) {
h, tg, _, c, store, w := newRouter(t)
link(h, store, c, "42", c.now().Add(-2*time.Hour))
ask := func(id, about string) {
a := ackAsk(c, id)
a.About = about
submit(t, h, "mesh-delivery", a)
c.pass(time.Second)
settle(h, c)
}
state := func(id string) string { return store.asks["mesh-delivery."+id].State }
for i, id := range []string{"d-1", "d-2", "d-3"} {
ask(id, "about-"+string(rune('1'+i)))
}
oldTicket := ""
for tk := range store.asks["mesh-delivery.d-1"].Tickets {
oldTicket = tk
}
ask("d-4", "about-4")
// M1: the fourth reaches the channel, the oldest is held back, none is refused.
if len(w.of("mesh-delivery")) != 0 || state("d-4") != StateOpen || state("d-1") != StateHeldBack {
t.Fatalf("refused %+v; d-4 %s, d-1 %s", w.of("mesh-delivery"), state("d-4"), state("d-1"))
}
if rec := store.asks["mesh-delivery.d-1"]; len(rec.Tickets) != 0 || rec.HeldBackSince.IsZero() {
t.Errorf("held back %+v: tickets withdrawn, since set", rec)
}
if m, ok := tg.edits["1"]; !ok || len(m.Actions) != 0 || !strings.Contains(m.Note, "Waits behind newer questions of mesh-delivery") {
t.Errorf("its message was not edited to say it waits: %v %+v", ok, m)
}
if _, err := h.Chosen("telegram", choiceOf(oldTicket, "42", c.now())); err == nil || !strings.Contains(err.Error(), "waits behind newer questions") || len(w.of("mesh-delivery")) != 0 {
t.Errorf("a tap of a withdrawn ticket: %v %+v", err, w.of("mesh-delivery"))
}
// M3: asks lists it with since when and the newer asks it waits behind; open includes it.
q, _ := AsksQueryOf(map[string]any{"key": "mesh-delivery.d-1"})
raw, _ := json.Marshal(q.Narrow(h.AsksNow()))
for _, want := range []string{`"state":"held-back"`, `"held-since":"`, `"behind":["mesh-delivery.d-2","mesh-delivery.d-3","mesh-delivery.d-4"]`} {
if !strings.Contains(string(raw), want) {
t.Errorf("asks lacks %s: %s", want, raw)
}
}
if q, _ = AsksQueryOf(nil); !strings.Contains(string(mustJSON(q.Narrow(h.AsksNow()))), "mesh-delivery.d-1") {
t.Error("the open asks leave out the held-back one")
}
// M5: an ask with the same about replaces a held-back one; the three offered stay three, so the oldest of them is held back.
ask("d-5", "about-1")
if state("d-1") != string(occ.OutcomeReplaced) || state("d-2") != StateHeldBack || state("d-5") != StateOpen || state("d-3") != StateOpen {
t.Fatalf("after d-5 about the held-back ask's thing: d-1 %s d-2 %s d-3 %s d-5 %s", state("d-1"), state("d-2"), state("d-3"), state("d-5"))
}
// M2: when one of the offered ends, the oldest held back is offered again with new tickets and its message asks again.
if a := h.AskAgain("mesh-delivery.d-3"); !strings.Contains(a.Error, "not held back") {
t.Errorf("ask-again of an offered ask: %+v", a)
}
if err := h.TakeAsk("cancel", "mesh-delivery", []byte(`{"id":"d-3"}`)); err != nil {
t.Fatal(err)
}
settle(h, c)
if rec := store.asks["mesh-delivery.d-2"]; rec.State != StateOpen || len(rec.Tickets) == 0 || !rec.HeldBackSince.IsZero() {
t.Fatalf("d-2 after d-3 ended: %+v", rec)
}
if m := tg.edits["2"]; len(m.Actions) == 0 || strings.Contains(m.Note, "Waits behind") {
t.Errorf("its message does not ask again: %+v", m)
}
// M4: ask-again by hand offers a held-back ask now, holding back the oldest offered one; the seat operator-channel's ask-again (G §450).
ask("d-6", "about-6")
ask("d-7", "about-7")
held := ""
for _, id := range []string{"d-2", "d-4", "d-5", "d-6", "d-7"} {
if state(id) == StateHeldBack {
held = id
}
}
if held == "" {
t.Fatal("no ask is held back after two more")
}
if a := h.AskAgain("mesh-delivery.d-none"); a.Error == "" || a.State != "" {
t.Errorf("ask-again of an ask the router holds none of: %+v", a)
}
if a := h.AskAgain("mesh-delivery." + held); a.Error != "" || a.State != string(StateOpen) || state(held) != StateOpen {
t.Fatalf("%s is %s; ask-again answered %+v", held, state(held), a)
}
n := 0
for _, id := range []string{"d-2", "d-4", "d-5", "d-6", "d-7"} {
if state(id) == StateHeldBack {
n++
}
}
if n != 2 || state("d-5") != StateHeldBack {
t.Errorf("%d asks held back after ask-again (d-5 %s): the oldest offered one took its place", n, state("d-5"))
}
// A Decline ends an ask; its asker may ask again at once, and nothing is refused.
for _, wt := range w.of("mesh-delivery") {
if wt.Outcome == occ.OutcomeRefused {
t.Errorf("refused: %+v", wt)
}
}
}
func mustJSON(v any) []byte { raw, _ := json.Marshal(v); return raw }