Files
mesh-sdk/go/delivery/buildrecord_test.go
T

87 lines
2.8 KiB
Go

package delivery
import (
"encoding/json"
"os"
"testing"
"time"
)
// The fixture is a record the controller's own encoder wrote (json.Marshal of a chain.Build, mesh-controller
// internal/chain, a throwaway test run once and deleted) with build-agent's build-1792395447088936520 values, plus a
// manifest-digest and one made artifact, which a BuildRecord does not carry.
func loadBuildFixture(t *testing.T) []byte {
t.Helper()
raw, err := os.ReadFile("testdata/build-record-build-agent.json")
if err != nil {
t.Fatal(err)
}
return raw
}
func TestBuildRecordDecodes(t *testing.T) { // 1223/AC1
var r BuildRecord
if err := json.Unmarshal(loadBuildFixture(t), &r); err != nil {
t.Fatal(err)
}
for _, c := range []struct{ name, got, want string }{
{"id", r.ID, "build-1792395447088936520"},
{"module", r.Module, "build-agent"},
{"repository", r.Repository, "novox/mesh-controller"},
{"ref", r.Ref, "main"},
{"commit", r.Commit, "7a74cee83f88e6585a5511c3cc53bdf0f5303d20"},
{"state", r.State, "built"},
{"source-fingerprint", r.SourceFingerprint, "src1:e846efe73716b78e80860baaac96153de2cb76551325753838ca245d2160a3d7"},
} {
if c.got != c.want {
t.Errorf("%s = %q, want %q", c.name, c.got, c.want)
}
}
if want := time.Date(2026, 10, 19, 9, 38, 0, 0, time.UTC); !r.At.Equal(want) {
t.Errorf("at = %v, want %v", r.At, want)
}
if r.SourceInputs == nil {
t.Fatal("source-inputs missing")
}
fp, none := SourceFingerprint(*r.SourceInputs)
if none != "" || fp != r.SourceFingerprint {
t.Errorf("SourceFingerprint of the decoded inputs = %q (none %q), the record says %q", fp, none, r.SourceFingerprint)
}
}
func TestBuildRecordWithoutInputs(t *testing.T) { // 1223/AC2
var m map[string]json.RawMessage
if err := json.Unmarshal(loadBuildFixture(t), &m); err != nil {
t.Fatal(err)
}
delete(m, "source-inputs")
raw, _ := json.Marshal(m)
var r BuildRecord
if err := json.Unmarshal(raw, &r); err != nil {
t.Fatal(err)
}
if r.SourceInputs != nil {
t.Errorf("source-inputs = %+v, want nil", r.SourceInputs)
}
if r.Module != "build-agent" {
t.Errorf("module = %q, want build-agent", r.Module)
}
}
func TestBuildKeys(t *testing.T) { // 1223/AC3
// Literal values of mesh-controller internal/broker/controller_buckets.go:101 (BucketName(ControllerSeat, ChainBuilds)
// is "mesh-controller_builds") and internal/chain/keys.go:35-46.
if BuildsBucket != "mesh-controller_builds" {
t.Errorf("BuildsBucket = %q", BuildsBucket)
}
if got := BuildKey("build-agent", "build-1"); got != "build-agent.build.build-1" {
t.Errorf("BuildKey = %q", got)
}
if got := BuildKey("", "build-1"); got != "req.build-1" {
t.Errorf("BuildKey without a module = %q", got)
}
if got := NewestKey("build-agent"); got != "build-agent.newest" {
t.Errorf("NewestKey = %q", got)
}
}