Files
mesh-delivery/modules/mesh-delivery-sdk/composer/composer_test.go
T
jschoubben eadfcd4cf8 composer: a case names the modules the revision gives no fingerprint, with its none words (G-24 H3)
The assigner fills it from the revision; the node-engine's composer reads it alone to tell a build that can
never be made, and judges the machine with that module held instead of leaving it not judged.

Builds: design 47 G §721, §722, A-470j, A-470k
2026-11-17 12:01:12 +01:00

110 lines
5.0 KiB
Go

package composer
import (
"encoding/json"
"errors"
"slices"
"strings"
"testing"
)
const demo = `{"module": "demo", "version": "1", "claims": [{"name": "node-files"}]}`
// A manifest named by its digest goes on the wire as {"digest": ...}, and Fill puts the bytes the indexer keeps in its
// place; a raw manifest goes as itself.
func TestAManifestIsItsBytesOrItsDigest(t *testing.T) {
r := Request{V: Version, Cases: []Case{{Name: "change", Manifests: map[string]Manifest{
"demo": {Digest: "d1", Fingerprint: "src2:f1"}, "raw": {Raw: json.RawMessage(`{"module":"raw","version":"1"}`)}},
Kept: map[string]map[string]Manifest{"laptop": {"demo": {Digest: "d1"}}}}}}
wire, err := json.Marshal(r)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(wire), `"demo":{"digest":"d1","fingerprint":"src2:f1"}`) || !strings.Contains(string(wire), `"raw":{"module":"raw","version":"1"}`) {
t.Fatalf("on the wire: %s", wire)
}
back, err := Decode(wire)
if err != nil {
t.Fatal(err)
}
asked, fingerprints := 0, []string{}
if err := Fill(&back, func(m Manifest) ([]byte, error) {
asked++
fingerprints = append(fingerprints, m.Fingerprint)
if m.Digest != "d1" {
return nil, errors.New("no such digest")
}
return []byte(demo), nil
}); err != nil {
t.Fatal(err)
}
if asked != 2 || string(back.Cases[0].Manifests["demo"].Raw) != demo || string(back.Cases[0].Kept["laptop"]["demo"].Raw) != demo {
t.Fatalf("filled %d times: %+v", asked, back.Cases[0])
}
// get is given the fingerprint the request names beside the digest, and none where it names none (design 47 G A-310).
if slices.Sort(fingerprints); !slices.Equal(fingerprints, []string{"", "src2:f1"}) {
t.Errorf("get was given the fingerprints %q, want one named and one not", fingerprints)
}
// The known packages: none is no list (not on the wire), an empty list is a list, and a list reads back whole.
for _, known := range [][]string{nil, {}, {"git.novox.be/novox/mesh-sdk", "@novox/mesh-sdk"}} {
wire, err := json.Marshal(Request{V: Version, Packages: known})
if err != nil {
t.Fatal(err)
}
back, err := Decode(wire)
if err != nil || (back.Packages == nil) != (known == nil) || strings.Join(back.Packages, ",") != strings.Join(known, ",") ||
known == nil && strings.Contains(string(wire), "packages") {
t.Errorf("the known packages %#v went as %s and read back %#v (%v)", known, wire, back.Packages, err)
}
}
// K1/AC4 (design 47 G §721, §729; A-469h, A-469i): a request's blame_tries and a verdict's blamed read back whole; a
// verdict with no blamed (a composer from before §721) reads back as no field, told apart from an empty list.
wire, err = json.Marshal(Request{V: Version, BlameTries: 20})
if back, derr := Decode(wire); err != nil || derr != nil || back.BlameTries != 20 || !strings.Contains(string(wire), `"blame_tries":20`) {
t.Errorf("blame_tries 20 went as %s and read back %d (%v, %v)", wire, back.BlameTries, err, derr)
}
// H3 (design 47 G A-470j, A-470k): a case's none, each module the revision gives no fingerprint with its none words,
// reads back whole; a case naming none leaves the field off the wire.
wire, err = json.Marshal(Request{V: Version, Cases: []Case{{Name: "change", Manifests: map[string]Manifest{},
None: map[string]string{"lidarr": "builders disagree: node"}}, {Name: "trunk", Manifests: map[string]Manifest{}}}})
if back, derr := Decode(wire); err != nil || derr != nil || back.Cases[0].None["lidarr"] != "builders disagree: node" ||
back.Cases[1].None != nil || !strings.Contains(string(wire), `"none":{"lidarr":"builders disagree: node"}`) ||
strings.Count(string(wire), `"none"`) != 1 {
t.Errorf("a case's none went as %s and read back %+v (%v, %v)", wire, back.Cases, err, derr)
}
one := Machine{}
one.Blamed = append(one.Blamed, struct {
Module string `json:"module"`
Problem string `json:"problem"`
}{Module: "a", Problem: "holds /etc/x"})
for _, c := range []struct {
name string
wire string
blamed int
old bool
}{
{"blames one", `{"composes":false,"blamed":[{"module":"a","problem":"holds /etc/x"}]}`, 1, false},
{"blames none", `{"composes":false,"blamed":[]}`, 0, false},
{"an old composer", `{"composes":false}`, 0, true},
} {
var m Machine
if err := json.Unmarshal([]byte(c.wire), &m); err != nil {
t.Fatalf("%s: %v", c.name, err)
}
if len(m.Blamed) != c.blamed || (m.Blamed == nil) != c.old {
t.Errorf("%s: %s read back blamed %#v", c.name, c.wire, m.Blamed)
}
again, err := json.Marshal(m)
if err != nil || string(again) != c.wire {
t.Errorf("%s: %s went again as %s (%v)", c.name, c.wire, again, err)
}
}
if wire, _ := json.Marshal(one); string(wire) != `{"composes":false,"blamed":[{"module":"a","problem":"holds /etc/x"}]}` {
t.Errorf("a machine blaming a went as %s", wire)
}
bad := Request{V: Version, Cases: []Case{{Name: "c", Manifests: map[string]Manifest{"x": {Digest: "nope"}}}}}
if err := Fill(&bad, func(Manifest) ([]byte, error) { return nil, errors.New("not there") }); err == nil || !strings.Contains(err.Error(), "nope") {
t.Fatalf("a digest the indexer does not hold: %v", err)
}
}