Files
mesh-contracts/go/clients/meshconfigclient/client_test.go
T
jschoubben 6be23459ab A7: the contracts' fixtures and the tests generated from them name no operator account and no workstation (role names)
Builds: the lead's handover item A7 (fixtures name no operator account or node); no design line names it
Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 04:59:34 +01:00

77 lines
3.0 KiB
Go

package meshconfigclient
import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
// The keys are the earlier form of go/delivery's HeldValueKey and ConfigCandidateKey (golden), which the holder writes.
func TestKeysAreTheEarlierForm(t *testing.T) {
if k, err := HeldValueKey("novox/mesh-catalog#42", "novox", "mail", "quota"); k != "novox.mesh-catalog.42.novox.mail.quota" || err != nil {
t.Errorf("a held value's key is %q, %v", k, err)
}
for pull, want := range map[string]string{"novox/mesh-catalog#42": "novox.mesh-catalog.42", "x#1": "", "a/b": "", "a/b.c#1": ""} {
if got, err := PullKey(pull); got != want || (want == "") != (err != nil) {
t.Errorf("%s keyed %q, %v", pull, got, err)
}
}
if _, err := HeldValueKey("novox/mesh-catalog#42", "", "mail", "quota"); err == nil {
t.Error("a held value without a node was keyed")
}
}
const held = `{"pull":"novox/mesh-catalog#434","head":"abc1234def","declaration":"sha256:3a7b","node":"ace","module":"n8n","key":"media-gid","value":2000,"by":"the operator","approved":"operator@telegram","at":"2026-11-05T12:30:00Z"}`
func TestAHeldValueIsTheOneItsKeyNames(t *testing.T) {
key := "novox.mesh-catalog.434.ace.n8n.media-gid"
v, err := DecodeKept(key, []byte(held))
if err != nil || v.Node != "ace" || v.Value != float64(2000) {
t.Fatalf("read %+v, %v", v, err)
}
if raw, err := ValueOf(v); err != nil || string(raw) != "2000" {
t.Errorf("the value is %s, %v", raw, err)
}
for name, c := range map[string]struct{ key, raw string }{
"another pull request's key": {"novox.mesh-catalog.435.ace.n8n.media-gid", held},
"another module's key": {"novox.mesh-catalog.434.ace.plex.media-gid", held},
"no head": {key, strings.Replace(held, `"head":"abc1234def",`, "", 1)},
"a field it does not know": {key, strings.Replace(held, `"by"`, `"extra":1,"by"`, 1)},
"a pull that is not one": {key, strings.Replace(held, "novox/mesh-catalog#434", "novox", 1)},
} {
if _, err := DecodeKept(c.key, []byte(c.raw)); err == nil {
t.Errorf("%s was read", name)
}
}
}
// Design 47 G: the contracts' fixtures, and the tests generated from them, name no account of the operator and no node
// they run on: a role name stands for either (`operator`, `workstation`).
func TestFixturesNameNoOperatorAccountAndNoWorkstation(t *testing.T) {
root := "../../.."
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
slash := filepath.ToSlash(path)
if !strings.Contains(slash, "/fixtures/") && !strings.HasSuffix(slash, "/contract_test.go") || strings.Contains(slash, "node_modules") {
return nil
}
raw, err := os.ReadFile(path)
if err != nil {
return err
}
for _, word := range []string{"jochen", "shanks"} {
if strings.Contains(string(raw), word) {
t.Errorf("%s names %q: a fixture names the operator and the workstation by role", slash, word)
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
}