reserved answers module, version, artifacts and released (a boolean), or error alone; module, version and artifacts are read as required only without error. contracts/go/clients/meshbuildsclient (artifact go-meshbuildsclient) has Reserved, which answers the reservation or release, a Refusal for the error and an error for an answer that is not one. Builds: design 47 G §679, §582, §586, §650 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
58 lines
2.4 KiB
Go
58 lines
2.4 KiB
Go
package meshbuildsclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshbuilds"
|
|
)
|
|
|
|
func answers(raw string, err error) Call {
|
|
return func(address string, body any) (json.RawMessage, error) {
|
|
if address != "seat:mesh-builds.reserved" {
|
|
return nil, errors.New("asked " + address)
|
|
}
|
|
return json.RawMessage(raw), err
|
|
}
|
|
}
|
|
|
|
// TestReservedReadsEachAnswer is design 47 G §586's check: the client's input and each answer read (§679's three).
|
|
func TestReservedReadsEachAnswer(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
call Call
|
|
want Reservation
|
|
refusal string
|
|
failedOn string
|
|
}{
|
|
{name: "a reservation", call: answers(`{"module":"m","version":"0.1.2","artifacts":[{"artifact":"m:go-m","digest":"sha256:aa"}]}`, nil),
|
|
want: Reservation{Module: "m", Version: "0.1.2", Artifacts: []seat.ReservedAnswerArtifactsItem{{Artifact: "m:go-m", Digest: "sha256:aa"}}}},
|
|
{name: "a release", call: answers(`{"module":"m","version":"0.1.1","artifacts":[{"artifact":"m:go-m","digest":"sha256:bb"}],"released":true}`, nil),
|
|
want: Reservation{Module: "m", Version: "0.1.1", Artifacts: []seat.ReservedAnswerArtifactsItem{{Artifact: "m:go-m", Digest: "sha256:bb"}}, Released: true}},
|
|
{name: "an error alone", call: answers(`{"error":"no entry carries the name x"}`, nil), refusal: "no entry carries the name x"},
|
|
{name: "no module and no error", call: answers(`{}`, nil), failedOn: "answered no reservation"},
|
|
{name: "a call that fails", call: answers("", errors.New("no responders")), failedOn: "did not answer"},
|
|
{name: "what cannot be read", call: answers(`[1]`, nil), failedOn: "cannot be read"},
|
|
} {
|
|
got, err := Reserved(tc.call, seat.ReservedInput{Name: "x", Version: "0.1.1"})
|
|
var r Refusal
|
|
switch {
|
|
case tc.refusal != "":
|
|
if !errors.As(err, &r) || r.Words != tc.refusal {
|
|
t.Errorf("%s: %v, want a refusal %q", tc.name, err, tc.refusal)
|
|
}
|
|
case tc.failedOn != "":
|
|
if err == nil || !strings.Contains(err.Error(), tc.failedOn) {
|
|
t.Errorf("%s: %v, want an error saying %q", tc.name, err, tc.failedOn)
|
|
}
|
|
default:
|
|
if err != nil || got.Module != tc.want.Module || got.Version != tc.want.Version || got.Released != tc.want.Released ||
|
|
len(got.Artifacts) != 1 || got.Artifacts[0] != tc.want.Artifacts[0] {
|
|
t.Errorf("%s: %+v %v, want %+v", tc.name, got, err, tc.want)
|
|
}
|
|
}
|
|
}
|
|
}
|