72 lines
2.7 KiB
Go
72 lines
2.7 KiB
Go
package delivery
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// 1471/AC4: an encoded step with V 99 is refused with ErrUnknownVersion; so is a state 'done'.
|
|
func TestDecodeStepRefusesAnUnknownVersionAndAState(t *testing.T) {
|
|
good, err := EncodeStep(Step{ID: "k3j2", Tool: "fetch", Node: "ace", State: StepRunning, Started: time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := DecodeStep(good); err != nil {
|
|
t.Fatalf("a good step: %v", err)
|
|
}
|
|
v99 := strings.Replace(string(good), `"v":1`, `"v":99`, 1)
|
|
if _, err := DecodeStep([]byte(v99)); !errors.Is(err, ErrUnknownVersion) {
|
|
t.Errorf("version 99: %v, want ErrUnknownVersion", err)
|
|
}
|
|
done := strings.Replace(string(good), `"running"`, `"done"`, 1)
|
|
if _, err := DecodeStep([]byte(done)); err == nil || errors.Is(err, ErrUnknownVersion) {
|
|
t.Errorf("state done: %v, want a refusal of the state", err)
|
|
}
|
|
if _, err := EncodeStep(Step{ID: "a", Tool: "fetch", Node: "ace", State: "done"}); err == nil {
|
|
t.Error("EncodeStep wrote the state done")
|
|
}
|
|
}
|
|
|
|
func TestStepKeyIsNodeDotIDAndRefusesWhatIsNotOneToken(t *testing.T) {
|
|
key, err := StepKey("ace", "k3j2")
|
|
if err != nil || key != "ace.k3j2" {
|
|
t.Fatalf("StepKey = %q, %v", key, err)
|
|
}
|
|
node, id, err := ParseStepKey(key)
|
|
if err != nil || node != "ace" || id != "k3j2" {
|
|
t.Errorf("ParseStepKey = %q, %q, %v", node, id, err)
|
|
}
|
|
for _, id := range []string{"", "a.b", "A", "a-b", "a b", "*"} {
|
|
if _, err := StepKey("ace", id); err == nil {
|
|
t.Errorf("StepKey accepted the id %q", id)
|
|
}
|
|
}
|
|
for _, key := range []string{"ace", "ace.", "ace.a.b"} {
|
|
if _, _, err := ParseStepKey(key); err == nil {
|
|
t.Errorf("ParseStepKey accepted %q", key)
|
|
}
|
|
}
|
|
if StepsBucket("mesh-build-go") != "mesh-build-go_steps" {
|
|
t.Errorf("bucket = %q", StepsBucket("mesh-build-go"))
|
|
}
|
|
}
|
|
|
|
func TestStepEncodingKeepsItsFieldsAndOmitsAnOpenEnd(t *testing.T) {
|
|
at := time.Date(2026, 10, 20, 12, 0, 0, 0, time.UTC)
|
|
raw, _ := EncodeStep(Step{ID: "a1", Tool: "fetch", Node: "ace", State: StepRunning, Started: at})
|
|
want := `{"v":1,"id":"a1","tool":"fetch","node":"ace","state":"running","started":"2026-10-20T12:00:00Z"}`
|
|
if string(raw) != want {
|
|
t.Errorf("running = %s", raw)
|
|
}
|
|
raw, _ = EncodeStep(Step{ID: "a1", Tool: "fetch", Node: "ace", State: StepPassed, Started: at, Ended: at, Result: []byte(`{"sha":"x"}`)})
|
|
got, err := DecodeStep(raw)
|
|
if err != nil || got.State != StepPassed || string(got.Result) != `{"sha":"x"}` || !got.Ended.Equal(at) {
|
|
t.Errorf("passed = %+v, %v", got, err)
|
|
}
|
|
if _, err := EncodeStep(Step{ID: "a1", Tool: "fetch", Node: "ace", State: StepPassed, Result: []byte(`{`)}); err == nil {
|
|
t.Error("a result that is not JSON was written")
|
|
}
|
|
}
|