Files
mesh-delivery/modules/mesh-delivery-indexer/judge/needs_test.go
T

75 lines
2.6 KiB
Go

package judge
import (
"slices"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/catalogue"
)
func TestManifestNeeds(t *testing.T) {
for _, row := range []struct {
why, raw string
templates map[string]string
has, not []string
}{
{"a module of its own account", `{"module":"g","version":"1","runs-as":"g"}`, nil,
[]string{"directory-contents-owner", "word:runs-as"}, nil},
{"a module with no account of its own", `{"module":"g","version":"1"}`, nil,
[]string{"word:module"}, []string{"directory-contents-owner"}},
{"a word the contract does not know", `{"module":"u","version":"1","newer-word":{"inside":1}}`, nil,
[]string{"word:module"}, []string{"word:newer-word"}},
{"a template naming a seat's reach", `{"module":"z","version":"1","resources":[{"id":"f","type":"file","template":"files/d.json"}]}`,
map[string]string{"files/d.json": `{"x":"${seat:container-image-registry:reach}"}`},
[]string{"form:seat-reach"}, nil},
{"a template writing the config as json", `{"module":"z","version":"1","resources":[{"id":"f","type":"file","template":"files/c.json"}]}`,
map[string]string{"files/c.json": "${config-json}"}, []string{"form:config-json"}, []string{"form:config-lines"}},
{"a resource with if-config", `{"module":"z","version":"1","resources":[{"id":"f","type":"file","if-config":"x"}]}`, nil,
[]string{"word:resources.if-config"}, nil},
} {
needs, err := ModuleNeeds([]byte(row.raw), row.templates)
if err != nil {
t.Fatalf("%s: %v", row.why, err)
}
for _, n := range row.has {
if !slices.Contains(needs, n) {
t.Errorf("%s: needs %v, not %s", row.why, needs, n)
}
}
for _, n := range row.not {
if slices.Contains(needs, n) {
t.Errorf("%s: needs %v, naming %s", row.why, needs, n)
}
}
}
if _, err := ManifestNeeds([]byte("[]")); err == nil {
t.Error("bytes that are no JSON object: no error")
}
}
// The manifest validator's reading of a manifest's keys equals ManifestReads' words (H-A-669): a word one names and the
// other does not fails here.
func TestTheValidatorReadsTheWordsManifestReadsNames(t *testing.T) {
var contract []string
for _, r := range catalogue.ManifestReads() {
if w, ok := strings.CutPrefix(r, catalogue.ReadWord); ok {
contract = append(contract, w)
}
}
if got := readerWords(); !slices.Equal(got, contract) {
t.Errorf("the validator reads %d words, ManifestReads names %d: only in the validator %v, only in ManifestReads %v",
len(got), len(contract), diff(got, contract), diff(contract, got))
}
}
func diff(a, b []string) []string {
var out []string
for _, x := range a {
if !slices.Contains(b, x) {
out = append(out, x)
}
}
return out
}