Files
mesh-sdk/go/declaration/order_test.go
T
jschoubben 3218da4ee4 J3: mesh-sdk within its test budget, each file of declaration and the provider harness one test of subtests
The unit test functions fall from 587 to 476 (the budget is 500): the cases of each of twenty test files in
declaration and provider/harness become subtests of one test named for the file, under their own names, their bodies
unchanged. Integration tests stay at 30 (budget 50).

Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
2026-11-04 14:28:23 +01:00

67 lines
2.8 KiB
Go

package declaration
import (
"strings"
"testing"
)
// A declaration says where it stands — the controller's lease epoch and its sequence — inside what is
// signed (novox/hq to-be 45 §6). Absent is no claim; below zero is no order the mesh assigns.
func withOrder(order string) string {
return `{"declaration":1,` + order + `"resources":[{"id":"etc","type":"directory","path":"/etc/mesh","mode":"0755"}]}`
}
func testADeclarationCarriesItsEpochAndSequence(t *testing.T) {
d, err := Parse([]byte(withOrder(`"epoch":57,"sequence":3,`)))
if err != nil {
t.Fatalf("a declaration carrying its order was refused: %v", err)
}
if d.Epoch != 57 || d.Sequence != 3 {
t.Fatalf("read epoch %d, sequence %d", d.Epoch, d.Sequence)
}
// An older controller's: no order, and no claim.
d, err = Parse([]byte(withOrder(``)))
if err != nil || d.Epoch != 0 || d.Sequence != 0 {
t.Fatalf("a declaration with no order read as %+v, %v", d, err)
}
}
func testAnOrderBelowZeroIsRefused(t *testing.T) {
for _, order := range []string{`"epoch":-1,`, `"sequence":-4,`} {
refusal := refusalFor(t, withOrder(order))
if !strings.Contains(refusal.Error(), "below zero") {
t.Errorf("%s: refused for something else: %v", order, refusal)
}
}
}
// **The assignment generation it was composed from, and whether it is a put-back** (novox/hq issue 234):
// absent is no claim; below zero is none the mesh assigns; a carried bundle puts nothing back.
func testADeclarationCarriesItsGenerationAndWhetherItIsAPutBack(t *testing.T) {
d, err := Parse([]byte(withOrder(`"sequence":3,"generation":41,"put_back":true,`)))
if err != nil {
t.Fatalf("a declaration carrying its generation was refused: %v", err)
}
if d.Generation != 41 || !d.PutBack {
t.Fatalf("read generation %d, put-back %v", d.Generation, d.PutBack)
}
if d, err = Parse([]byte(withOrder(``))); err != nil || d.Generation != 0 || d.PutBack {
t.Fatalf("a declaration with no generation read as %+v, %v", d, err)
}
if refusal := refusalFor(t, withOrder(`"generation":-2,`)); !strings.Contains(refusal.Error(), "below zero") {
t.Errorf("a generation below zero was refused for something else: %v", refusal)
}
if _, err := ParseTrusted([]byte(withOrder(`"generation":3,"put_back":true,`))); err == nil ||
!strings.Contains(err.Error(), "put-back") {
t.Errorf("a carried bundle said it was a put-back and was not refused for it: %v", err)
}
}
// TestOrder holds this file's cases, each a subtest of its own name (mesh-sdk's test budget: at most 500 unit tests).
func TestOrder(t *testing.T) {
t.Run("ADeclarationCarriesItsEpochAndSequence", testADeclarationCarriesItsEpochAndSequence)
t.Run("AnOrderBelowZeroIsRefused", testAnOrderBelowZeroIsRefused)
t.Run("ADeclarationCarriesItsGenerationAndWhetherItIsAPutBack", testADeclarationCarriesItsGenerationAndWhetherItIsAPutBack)
}