Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/finding_kinds_test.go
T
jschoubben bdb4edd820 Design 47 H1: the indexer's retired call-unanswered names scope call
The tool runner's approval findings of call-unanswered are of scope machine;
the indexer's entry now names scope call, so no approval key is cleared even
where an id form would match. The test's negative ids take the approval form.

Builds: design 47 H H-A-249, G §580
2026-11-21 11:50:27 +01:00

207 lines
7.5 KiB
Go

package index
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"testing"
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient/heard"
"git.novox.be/novox/mesh-sdk/go/views"
)
// Design 47 H H-A-240, H-A-242, H-A-247, G §580: every finding kind the daemon's code raises or clears is one its writer owns
// (FindingKinds, and a views reader's views.ViewKinds), so the writer never refuses a kind of the indexer's own. The test
// reads the daemon's code (this package and cmd/mesh-delivery-indexer) for every place a kind is given to the writer: the
// kind argument of observed, finding, clearFinding, clearHeld and a writer's Clear, the Kind of a Finding literal, and the
// kinds of an SDK part handed the writer (partKinds). mcc.KindCallUnanswered stays readable, so a return to the tool runner's
// kind fails the test. A
// kind there is a string, a constant of the package or a constant named in imported below; any other form fails the test,
// naming its place, but for the pass-through parameter `kind`, a held finding's `.Kind` and the kind cut from a key a views
// reader names, which carry a kind given elsewhere.
func TestFindingKindsHoldEveryKindTheCodeRaises(t *testing.T) {
imported := map[string]string{"mcc.KindCallUnanswered": mcc.KindCallUnanswered, "views.ViewWaitsKind": views.ViewWaitsKind}
// kindArg is the place of the kind among a raising call's arguments.
kindArg := map[string]int{"observed": 2, "finding": 2, "clearFinding": 3, "clearHeld": 4, "Clear": 3}
// partKinds are the kinds each SDK part raises through the writer it is handed (H-A-247 (1)); handed says a literal's field
// gives it the writer.
partKinds := map[string][]string{"views.ProcessReader": {views.EventUnhandledKind}, "views.Emitter": {views.UnsaidKind, views.TooLargeKind},
"heard.Writer": {heard.Kind}}
handed := func(e ast.Expr) bool {
kv, ok := e.(*ast.KeyValueExpr)
if !ok {
return false
}
id, ok := kv.Key.(*ast.Ident)
return ok && (id.Name == "Findings" || id.Name == "Put")
}
fset := token.NewFileSet()
var files []*ast.File
consts := map[string]string{}
for _, dir := range []string{".", filepath.Join("..", "..", "cmd", "mesh-delivery-indexer")} {
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
if !strings.HasSuffix(e.Name(), ".go") || strings.HasSuffix(e.Name(), "_test.go") {
continue
}
f, err := parser.ParseFile(fset, filepath.Join(dir, e.Name()), nil, 0)
if err != nil {
t.Fatal(err)
}
files = append(files, f)
if dir != "." {
continue
}
for _, d := range f.Decls {
g, ok := d.(*ast.GenDecl)
if !ok || g.Tok != token.CONST {
continue
}
for _, s := range g.Specs {
v := s.(*ast.ValueSpec)
for i, n := range v.Names {
if i < len(v.Values) {
if l, ok := v.Values[i].(*ast.BasicLit); ok && l.Kind == token.STRING {
consts[n.Name], _ = strconv.Unquote(l.Value)
}
}
}
}
}
}
}
declared := append(slices.Clone(FindingKinds), views.ViewKinds...)
raised := map[string]string{} // kind -> a place it is raised
kindOf := func(e ast.Expr) (string, bool) {
switch x := e.(type) {
case *ast.BasicLit:
s, err := strconv.Unquote(x.Value)
return s, err == nil
case *ast.Ident:
if x.Name == "kind" {
return "", true // a pass-through parameter: its callers give the kind
}
s, ok := consts[x.Name]
if !ok {
s, ok = imported["index."+x.Name]
}
return s, ok
case *ast.SliceExpr:
return "", true // the kind of a key a views reader names: its own kinds (views.ViewKinds)
case *ast.SelectorExpr:
if x.Sel.Name == "Kind" {
return "", true // a held finding's kind, given when it was raised
}
if p, ok := x.X.(*ast.Ident); ok {
if p.Name == "index" {
s, ok := consts[x.Sel.Name]
return s, ok
}
s, ok := imported[p.Name+"."+x.Sel.Name]
return s, ok
}
}
return "", false
}
note := func(e ast.Expr) {
k, ok := kindOf(e)
if !ok {
t.Errorf("%s: a finding kind the test cannot read; give it as a constant (or name it in imported)", fset.Position(e.Pos()))
return
}
if k != "" {
raised[k] = fset.Position(e.Pos()).String()
}
}
for _, f := range files {
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
var name string
switch fn := x.Fun.(type) {
case *ast.Ident:
name = fn.Name
case *ast.SelectorExpr:
name = fn.Sel.Name
}
if i, ok := kindArg[name]; ok && len(x.Args) > i {
if name == "Clear" && len(x.Args) != 5 {
return true // not a finding writer's Clear(ctx, scope, id, kind, why)
}
note(x.Args[i])
}
case *ast.CompositeLit:
// an SDK part handed the writer raises its own kinds through it (H-A-247 (1))
if s, ok := x.Type.(*ast.SelectorExpr); ok && slices.ContainsFunc(x.Elts, handed) {
if p, ok := s.X.(*ast.Ident); ok && p.Name != "index" { // the indexer's own code is read above
part := p.Name + "." + s.Sel.Name
if kinds, ok := partKinds[part]; ok {
for _, k := range kinds {
raised[k] = fset.Position(x.Pos()).String() + " (" + part + ")"
}
} else {
t.Errorf("%s: %s is handed the finding writer, and the test does not know its kinds: name them in partKinds",
fset.Position(x.Pos()), part)
}
}
}
if s, ok := x.Type.(*ast.SelectorExpr); ok && s.Sel.Name == "Finding" {
for _, el := range x.Elts {
if kv, ok := el.(*ast.KeyValueExpr); ok {
if id, ok := kv.Key.(*ast.Ident); ok && id.Name == "Kind" {
note(kv.Value)
}
}
}
}
}
return true
})
}
for _, k := range []string{PinMissingKind, OutOfStepKind, PackagesNotPublishedKind, CallUnansweredKind, views.EventUnhandledKind, heard.Kind} {
if _, ok := raised[k]; !ok {
t.Errorf("the test no longer finds the kind %s the code raises: it reads the code wrongly", k)
}
}
for k, at := range raised {
if !slices.Contains(declared, k) {
t.Errorf("%s: the code raises the finding kind %q, which the writer's list FindingKinds does not declare", at, k)
}
}
// a retired kind is one no code raises and the writer does not own (H-A-247 (3))
for _, r := range RetiredFindingKinds {
if at, ok := raised[r.Kind]; ok || slices.Contains(declared, r.Kind) {
t.Errorf("the retired kind %q is still declared or raised (%s)", r.Kind, at)
}
}
// H-A-249: the retired call-unanswered names the ids the indexer's unanswered calls are written under, as in a key
covers := func(scope, id string) bool {
for _, r := range RetiredFindingKinds {
if r.Kind == mcc.KindCallUnanswered && (r.Scope == "" || r.Scope == scope) && (slices.Contains(r.IDs, id) ||
slices.ContainsFunc(r.Prefixes, func(p string) bool { return strings.HasPrefix(id, p) })) {
return true
}
}
return false
}
for _, id := range []string{keyID("node-build-go." + StepsVerb), keyID("node-build-image." + StepsVerb), keyID(floorKey("mesh-tools")),
keyID(BuilderVersionsCall)} {
if !covers(mcc.ScopeCall, id) {
t.Errorf("the retired call-unanswered does not cover the indexer's id %s", id)
}
}
// the approval gate's findings: scope machine, id <node>=2e<ask>
if covers("machine", "novox=2eask-1") || covers("machine", "node-build-go.steps") || covers(mcc.ScopeCall, "novox=2eask-1") {
t.Error("the retired call-unanswered covers an id of the tool runner's")
}
}