The template state takes max-mib (1 to 8192, as the old state section reads it). A state named approvals is refused without key-ttl true, with ttl-seconds other than 172800 (approval.KeptFor, 48 hours), or with max-mib above 64; with none declared its bucket takes the default 64 MiB. Builds: design 47 G A-277, A-270, §677 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
199 lines
16 KiB
Go
199 lines
16 KiB
Go
package judge
|
|
|
|
import (
|
|
"encoding/json"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer"
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
)
|
|
|
|
// A manifest's bus state, asked for in `requires` by template, is judged once and each refusal names the module, the entry
|
|
// and the field (issue 1966 D1, D2; novox/hq design 25 § 12.1, ADR 0595, ADR 0596 §2, ADR 0597 §2).
|
|
func TestBusStateIsJudgedOnce(t *testing.T) {
|
|
const ledger = `{"name":"records","storage":"ledger","template":"ledger","options":{"max-mib":512},"types":[` +
|
|
`{"name":"decision","schema":{"title":{"type":"text"}},"operations":[{"name":"propose","effect":"set-fields","fields":["title"]}]}]}`
|
|
manifest := func(entries string) string {
|
|
return `{"module":"a","version":"1.0","requires":[` + entries + `]}`
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
raw string
|
|
refused []string // words the first problem holds, in order: absent is accepted
|
|
}{
|
|
// 1966/AC8: a state, a log, objects and a ledger within bounds
|
|
{"within bounds", manifest(`{"name":"cache","storage":"key-value","template":"state","options":{"history":8,"key-ttl":true}},` +
|
|
`{"name":"audit","storage":"stream","template":"log","options":{"max-mib":64,"max-age-days":365}},` +
|
|
`{"name":"files","storage":"object-store","template":"objects","options":{"max-mib":2048}},` + ledger), nil},
|
|
// Design 47: every kept data may say its form and its catch-up tool, in an old section and in `requires` by template,
|
|
// and is accepted as read; one saying neither reads as before.
|
|
{"a form and a catch-up tool on every kept kind", `{"module":"a","version":"1.0","tools":["a_take_in"],` +
|
|
`"state":["seen",{"name":"modules","form":1,"catch-up":"a_take_in"}],"logs":[{"name":"acts","form":2}],` +
|
|
`"streams":[{"name":"forge","events":["merged"],"form":1,"catch-up":"a_take_in"}],` +
|
|
`"objects":[{"name":"revisions","max-mib":64,"form":1,"catch-up":"a_take_in"}],"requires":[` +
|
|
`{"name":"holds","storage":"key-value","template":"state","form":1,"catch-up":"a_take_in"},` +
|
|
`{"name":"history","storage":"stream","template":"log","options":{"max-mib":64},"form":1},` +
|
|
`{"name":"marks","storage":"stream","template":"events","options":{"events":["seen"]},"form":1},` +
|
|
`{"name":"blobs","storage":"object-store","template":"objects","options":{"max-mib":64},"form":4,"catch-up":"a_take_in"},` +
|
|
`{"name":"records","storage":"ledger","template":"ledger","options":{"max-mib":512},"form":3,"catch-up":"a_take_in","types":[` +
|
|
`{"name":"decision","schema":{"title":{"type":"text"}},"operations":[{"name":"propose","effect":"set-fields","fields":["title"]}]}]}]}`, nil},
|
|
{"a catch-up tool the module lacks", `{"module":"a","version":"1.0","tools":["a_read"],"logs":[{"name":"acts","form":1,"catch-up":"a_take_in"}]}`,
|
|
[]string{"a's kept log \"acts\"", "catch-up tool \"a_take_in\" is not among the module's tools (a_read)"}},
|
|
{"a catch-up tool the module lacks, in requires", `{"module":"a","version":"1.0","tools":["a_read"],"requires":[` +
|
|
`{"name":"holds","storage":"key-value","template":"state","form":1,"catch-up":"nonexistent"}]}`,
|
|
[]string{"a's kept state \"holds\"", "catch-up tool \"nonexistent\""}},
|
|
{"a form below 0", manifest(`{"name":"x","storage":"key-value","template":"state","form":-1}`), []string{"requires entry \"x\"", "its form is -1"}},
|
|
// 1966/AC7: each refused case, naming the module, the entry and the field
|
|
{"unknown storage type", manifest(`{"name":"x","storage":"tape"}`), []string{"a's bus state \"x\"", "storage type \"tape\""}},
|
|
{"unknown template", manifest(`{"name":"x","storage":"stream","template":"pile"}`), []string{"\"x\"", "template \"pile\""}},
|
|
{"template on another storage type", manifest(`{"name":"x","storage":"stream","template":"state"}`), []string{"\"x\"", "template state is on the storage type key-value"}},
|
|
{"past values 0", manifest(`{"name":"x","storage":"key-value","template":"state","options":{"history":0}}`), []string{"\"x\"", "options \"history\" is 0", "0 is refused"}},
|
|
{"past values 65", manifest(`{"name":"x","storage":"key-value","template":"state","options":{"history":65}}`), []string{"\"x\"", "\"history\" is 65", "1 to 64"}},
|
|
{"a lifetime that is no number", manifest(`{"name":"x","storage":"key-value","template":"state","options":{"ttl-seconds":"soon"}}`), []string{"\"x\"", "\"ttl-seconds\""}},
|
|
{"events age over 30 days", manifest(`{"name":"x","storage":"stream","template":"events","options":{"events":["seen"],"max-age-days":31}}`), []string{"\"x\"", "\"max-age-days\" is 31", "1 to 30"}},
|
|
{"events cap over 8192", manifest(`{"name":"x","storage":"stream","template":"events","options":{"events":["seen"],"max-mib":8193}}`), []string{"\"x\"", "\"max-mib\" is 8193"}},
|
|
{"events naming none", manifest(`{"name":"x","storage":"stream","template":"events","options":{}}`), []string{"\"x\"", "\"events\" is missing"}},
|
|
{"a grant that is no trusted value", manifest(`{"name":"x","storage":"stream","template":"events","options":{"events":["seen"],"grant":"nobody"}}`), []string{"\"x\"", "\"grant\"", "trusted config value"}},
|
|
{"deliveries 11", manifest(`{"name":"x","storage":"stream","template":"work-queue","options":{"events":["job"],"max-deliveries":11}}`), []string{"\"x\"", "\"max-deliveries\" is 11", "1 to 10"}},
|
|
{"duplicates past the age", manifest(`{"name":"x","storage":"stream","template":"work-queue","options":{"events":["job"],"max-age-days":1,"duplicates":86401}}`), []string{"\"x\"", "\"duplicates\"", "longer than the queue's age of 1 days"}},
|
|
{"log age 3651", manifest(`{"name":"x","storage":"stream","template":"log","options":{"max-age-days":3651}}`), []string{"\"x\"", "\"max-age-days\" is 3651"}},
|
|
{"objects without a cap", manifest(`{"name":"x","storage":"object-store","template":"objects"}`), []string{"\"x\"", "\"max-mib\" is missing"}},
|
|
{"when full given as an option", manifest(`{"name":"x","storage":"stream","template":"log","options":{"when-full":"drop-oldest"}}`), []string{"\"x\"", "\"when-full\"", "fixed"}},
|
|
// A-277: a state's max-mib is an option of its template; an approvals state is §677's (key-ttl, 48 hours, at most 64 MiB).
|
|
{"a state capped at 256 MiB", manifest(`{"name":"x","storage":"key-value","template":"state","options":{"max-mib":256}}`), nil},
|
|
{"a state cap over 8192", manifest(`{"name":"x","storage":"key-value","template":"state","options":{"max-mib":8193}}`), []string{"\"x\"", "\"max-mib\" is 8193"}},
|
|
{"an approvals state as §677 declares it", manifest(`{"name":"approvals","storage":"key-value","template":"state","options":{"key-ttl":true,"ttl-seconds":172800,"max-mib":64}}`), nil},
|
|
{"an approvals state with the default cap", manifest(`{"name":"approvals","storage":"key-value","template":"state","options":{"key-ttl":true,"ttl-seconds":172800}}`), nil},
|
|
{"an approvals state lacking key-ttl", manifest(`{"name":"approvals","storage":"key-value","template":"state","options":{"ttl-seconds":172800}}`), []string{"\"approvals\"", "key-ttl true"}},
|
|
{"an approvals state kept a day", manifest(`{"name":"approvals","storage":"key-value","template":"state","options":{"key-ttl":true,"ttl-seconds":86400}}`), []string{"\"approvals\"", "ttl-seconds is 172800, not 86400"}},
|
|
{"an approvals state over 64 MiB", manifest(`{"name":"approvals","storage":"key-value","template":"state","options":{"key-ttl":true,"ttl-seconds":172800,"max-mib":128}}`), []string{"\"approvals\"", "max-mib is at most 64, not 128"}},
|
|
{"another fixed setting given", manifest(`{"name":"x","storage":"key-value","template":"state","options":{"max-value-kib":1024}}`), []string{"\"x\"", "\"max-value-kib\" is not a setting of the template state"}},
|
|
{"a configuration beside a template", manifest(`{"name":"x","storage":"key-value","template":"state","configuration":{"max-mib":1}}`), []string{"\"x\"", "configuration"}},
|
|
{"a name used twice across storage types", manifest(
|
|
`{"name":"x","storage":"key-value","template":"state"},{"name":"x","storage":"stream","template":"log"}`), []string{"\"x\"", "used twice (as key-value and as stream)"}},
|
|
{"a local name holding an underscore", manifest(`{"name":"my_x","storage":"key-value","template":"state"}`), []string{"\"my_x\"", "underscore"}},
|
|
{"a bare stream without when full", manifest(`{"name":"x","storage":"stream","configuration":{"max-mib":10,"max-age-days":"none"}}`), []string{"\"x\"", "\"when-full\" is missing"}},
|
|
{"a bare stream without a cap", manifest(`{"name":"x","storage":"stream","configuration":{"when-full":"refuse","max-age-days":3}}`), []string{"\"x\"", "\"max-mib\" is missing"}},
|
|
{"a bare stream without an age", manifest(`{"name":"x","storage":"stream","configuration":{"when-full":"refuse","max-mib":10}}`), []string{"\"x\"", "\"max-age-days\" is missing"}},
|
|
{"a bare stream, complete", manifest(`{"name":"x","storage":"stream","configuration":{"when-full":"drop-oldest","max-mib":10,"max-age-days":"none"}}`), nil},
|
|
{"a bare stream with a wrong when full", manifest(`{"name":"x","storage":"stream","configuration":{"when-full":"crash","max-mib":10,"max-age-days":9}}`), []string{"\"x\"", "\"when-full\"", "\"refuse\" or \"drop-oldest\""}},
|
|
{"a ledger effect outside the four", manifest(strings.Replace(ledger, `"set-fields"`, `"upsert"`, 1)), []string{"\"records\"", "effect \"upsert\"", "outside the four"}},
|
|
{"a ledger whose history expires", manifest(strings.Replace(ledger, `"options"`, `"configuration":{"max-age-days":30},"options"`, 1)), nil},
|
|
// DLV-I12 (ADR 0595 §8, ADR 0597 §2, §4): each refusal names the module, the entry, the type, the operation or index, and the field.
|
|
{"a set-entry naming no into", manifest(strings.Replace(ledger, `"effect":"set-fields","fields":["title"]`, `"effect":"set-entry","key":"title"`, 1)),
|
|
[]string{"\"records\"", "type \"decision\"", "operation \"propose\"", "no into"}},
|
|
{"a remove naming no key", manifest(strings.Replace(ledger, `"effect":"set-fields","fields":["title"]`, `"effect":"remove","into":"links"`, 1)),
|
|
[]string{"\"records\"", "operation \"propose\"", "no key"}},
|
|
{"a set-entry naming both", manifest(strings.Replace(ledger, `"effect":"set-fields","fields":["title"]`, `"effect":"set-entry","into":"links","key":"title","fields":["title"]`, 1)), nil},
|
|
{"an operation's field the schema lacks", manifest(strings.Replace(ledger, `"fields":["title"]`, `"fields":["title","body"]`, 1)),
|
|
[]string{"\"records\"", "operation \"propose\"", "field \"body\"", "schema does not hold"}},
|
|
{"an index on a field the schema lacks", manifest(strings.Replace(ledger, `"fields":["title"]}]`, `"fields":["title"]}],"indexes":[{"name":"by-owner","on":["owner"]}]`, 1)),
|
|
[]string{"\"records\"", "index \"by-owner\"", "field \"owner\""}},
|
|
{"a digest of a field that is no text", manifest(strings.Replace(ledger, `{"title":{"type":"text"}}`, `{"title":{"type":"text"},"n":{"type":"integer"},"sum":{"type":"text","digest-of":"n"}}`, 1)),
|
|
[]string{"\"records\"", "field \"sum\"", "digest of \"n\"", "no text field"}},
|
|
{"a digest of a text field", manifest(strings.Replace(ledger, `{"title":{"type":"text"}}`, `{"title":{"type":"text"},"sum":{"type":"text","digest-of":"title"}}`, 1)), nil},
|
|
// W1.5 (design 47, *Its form is declared*): a form below 0 is refused where the reader reads it, in `requires` too.
|
|
{"a form below 0", manifest(`{"name":"x","storage":"key-value","template":"state","form":-1}`), []string{"its form is -1"}},
|
|
// W1.6 (ADR 0619 §2): a foundation part keeps its new objects in the old section, with their cap and form (the
|
|
// same objects in the new form it is refused: DLV-J1 in the index's verdicts test).
|
|
{"a foundation part's objects in the old section", `{"module":"mesh-delivery-assigner","version":"0.1","tools":["assigner_graph_again"],` +
|
|
`"objects":[{"name":"revisions","max-mib":256,"form":1,"catch-up":"assigner_graph_again"}]}`, nil},
|
|
} {
|
|
_, problems := Manifest([]byte(c.raw), nil)
|
|
switch {
|
|
case c.refused == nil && len(problems) > 0:
|
|
t.Errorf("%s: refused: %v", c.name, problems)
|
|
case c.refused != nil && len(problems) == 0:
|
|
t.Errorf("%s: accepted", c.name)
|
|
case c.refused != nil:
|
|
text := strings.Join(problems, "\n")
|
|
at := 0
|
|
for _, word := range c.refused {
|
|
i := strings.Index(text[at:], word)
|
|
if i < 0 {
|
|
t.Errorf("%s: problems %q lack %q", c.name, text, word)
|
|
break
|
|
}
|
|
at += i
|
|
}
|
|
}
|
|
}
|
|
// Between the manifests: two modules' names on the bus alike, and a read of bus state that moved (ADR 0596 §2).
|
|
sdk := catalogue.Manifest{Module: "mesh-sdk", Build: &catalogue.Build{Artifacts: []catalogue.Artifact{
|
|
{Name: "go", Kind: catalogue.ArtifactPackage, Language: "go"}, {Name: "img", Kind: catalogue.ArtifactImage}}}}
|
|
sdkGo := "git.novox.be/novox/mesh-sdk/go"
|
|
known := KnownPackages{sdkGo: true}
|
|
entry := func(name string) []catalogue.BusStateEntry {
|
|
return []catalogue.BusStateEntry{{Name: name, Storage: catalogue.StorageKeyValue, Template: catalogue.TemplateState}}
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
shelf catalogue.Shelf
|
|
want string
|
|
}{
|
|
{"names alike", catalogue.Shelf{"a_b": {Module: "a_b", BusState: entry("c")}, "a": {Module: "a", BusState: entry("b_c")}}, "has one owner"},
|
|
{"a read of moved state", catalogue.Shelf{"owner": {Module: "owner", BusState: entry("cache")},
|
|
"reader": {Module: "reader", Reads: []string{"owner.cache"}}}, "reader reads \"owner.cache\", which owner has moved"},
|
|
{"a read of state still kept", catalogue.Shelf{"owner": {Module: "owner", State: []catalogue.StateDeclaration{{Name: "cache"}}},
|
|
"reader": {Module: "reader", Reads: []string{"owner.cache"}}}, ""},
|
|
// DLV-I14 (design 32): the packages section names a known package, by its name in its own language (the packages
|
|
// state's names, and a group member's at its head: known), in a well-formed series; through a judge request too.
|
|
{"packages naming a known package", catalogue.Shelf{"mesh-sdk": sdk, "user": {Module: "user", Version: "1.0", Packages: map[string]string{sdkGo: "0.2"}}}, ""},
|
|
{"packages naming a known package, by request", catalogue.Shelf{"user": {Module: "user", Version: "1.0", Packages: map[string]string{sdkGo: "0.2"}}}, ""},
|
|
{"packages naming a package by the builder's name", catalogue.Shelf{"mesh-sdk": sdk, "user": {Module: "user", Version: "1.0", Packages: map[string]string{"mesh-sdk/go": "0.1"}}},
|
|
`user's packages name "mesh-sdk/go", which no package of the mesh is named`},
|
|
{"packages naming an unknown package, by request", catalogue.Shelf{"user": {Module: "user", Version: "1.0", Packages: map[string]string{"@novox/ui": "0.1"}}},
|
|
`user's packages name "@novox/ui", which no package of the mesh is named`},
|
|
{"packages in an ill-formed series", catalogue.Shelf{"mesh-sdk": sdk, "user": {Module: "user", Version: "1.0", Packages: map[string]string{sdkGo: "v0.2"}}},
|
|
`user's packages accept "` + sdkGo + `" in the series "v0.2"`},
|
|
} {
|
|
got := strings.Join(append(BusStateCatalogueProblems(c.shelf), PackagesCatalogueProblems(c.shelf, known)...), "\n")
|
|
if strings.Contains(c.name, "by request") {
|
|
got = judgedByRequest(t, c.shelf, known)
|
|
}
|
|
if (c.want == "") != (got == "") || !strings.Contains(got, c.want) {
|
|
t.Errorf("%s: between the manifests: %q, want %q", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// judgedByRequest is every refusal the judge's request (mesh-judge's) answers of shelf's manifests, its Packages naming known,
|
|
// after the request went over the wire as the checker sends it.
|
|
func judgedByRequest(t *testing.T, shelf catalogue.Shelf, known KnownPackages) string {
|
|
t.Helper()
|
|
var names []string
|
|
if known != nil {
|
|
names = append([]string{}, slices.Sorted(maps.Keys(known))...)
|
|
}
|
|
c := composer.Case{Name: "change", Manifests: map[string]composer.Manifest{"node-engine": {Raw: []byte(engineManifest("node-engine"))}}}
|
|
for name, m := range shelf {
|
|
raw, err := json.Marshal(m)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.Manifests[name] = composer.Manifest{Raw: raw}
|
|
}
|
|
wire, err := json.Marshal(composer.Request{V: composer.Version, Packages: names, Cases: []composer.Case{c}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, err := composer.Decode(wire)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v, err := Request(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var refused []string
|
|
for _, name := range slices.Sorted(maps.Keys(v.Cases[0].Manifests)) {
|
|
if r := v.Cases[0].Manifests[name].Refused; r != "" {
|
|
refused = append(refused, r)
|
|
}
|
|
}
|
|
return strings.Join(refused, "\n")
|
|
}
|