package packageseats import ( "encoding/json" "errors" "strings" "testing" ) // A publish is checked against the builder's reservation by the seat mesh-builds' verb reserved, asked with the package's name // (design 47 §381, §478). func reservedChecksAPublishAgainstTheReservation(t *testing.T) { for _, c := range []struct { name string answer string err error ok bool why string // part of the reason, or of the error }{ {name: "the reserved version and digest", answer: `{"module":"mesh-sdk","version":"0.1.19","artifacts":[{"artifact":"mesh-sdk:ts","digest":"sha256:aa"}]}`, ok: true}, {name: "another artifact of it carries the digest", answer: `{"module":"mesh-sdk","version":"0.1.19","artifacts":[{"artifact":"mesh-sdk:go"},{"artifact":"mesh-sdk:ts","digest":"sha256:aa"}]}`, ok: true}, {name: "another digest", answer: `{"module":"mesh-sdk","version":"0.1.19","artifacts":[{"artifact":"mesh-sdk:ts","digest":"sha256:bb"}]}`, why: "another digest than sha256:aa"}, {name: "another version reserved", answer: `{"module":"mesh-sdk","version":"0.1.20","artifacts":[]}`, why: "reserved 0.1.20 for @novox/mesh-sdk, not 0.1.19"}, {name: "the builder refused the name", err: errors.New("no entry carries @novox/mesh-sdk"), why: "no entry carries @novox/mesh-sdk"}, {name: "an answer that is none", answer: `{"error":"x"}`, why: "answered no reservation"}, } { asked := "" r := Reserved{Ask: func(address string, body any) (json.RawMessage, error) { b, _ := json.Marshal(body) asked = address + " " + string(b) return json.RawMessage(c.answer), c.err }} ok, why, err := r.Recorded("@novox/mesh-sdk", "0.1.19", "sha256:aa") if asked != `seat:mesh-builds.reserved {"name":"@novox/mesh-sdk","version":"0.1.19"}` { t.Fatalf("%s: asked %s", c.name, asked) } if c.ok { if !ok || err != nil { t.Errorf("%s: not accepted: %v %v", c.name, why, err) } continue } if ok || !strings.Contains(why+fmtErr(err), c.why) { t.Errorf("%s: ok=%v why=%q err=%v, want %q", c.name, ok, why, err, c.why) } } } func fmtErr(err error) string { if err == nil { return "" } return err.Error() }