Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/waits_test.go
T

71 lines
3.4 KiB
Go

package main
import (
"context"
"slices"
"strings"
"testing"
"time"
"mesh-delivery-builder/internal/build"
)
// Design 47 G A-192: a key queued behind a package says its wait in the one wait form, the package as its `part` and `on`; a toolchain
// wait names none.
func TestAWaitBodyNamesItsPart(t *testing.T) {
w := &waitsKept{bound: time.Hour, held: map[string]build.ToolchainWait{}}
pkg := w.body(build.ToolchainWait{Module: "m", Fingerprint: "src2:f", Package: "@novox/sdk@0.1.15"}, "")
if pkg.Part != "@novox/sdk@0.1.15" || pkg.On != "package:@novox/sdk@0.1.15" || pkg.Step != waitStepBuilding {
t.Errorf("a package wait says %+v", pkg)
}
if tc := w.body(build.ToolchainWait{Module: "m", Fingerprint: "src2:f", Language: "go", Version: "go1.26.0"}, ""); tc.Part != "" || tc.On != "toolchain:go@go1.26.0" {
t.Errorf("a toolchain wait says %+v", tc)
}
// H2 H-A-577 (1): a revision wait says `step` build, `on` revision:<R> and its own bound, under the subject token `.revision`
rev := build.ToolchainWait{Module: "m", Fingerprint: "src2:f", Kind: build.WaitKindRevision, Revision: 7, Bound: "2m0s"}
if rv := w.body(rev, ""); rv.Step != "build" || rv.On != "revision:7" || rv.Bound != "2m0s" || slotOf(rev) != ".revision" || slotOf(build.ToolchainWait{}) != "" {
t.Errorf("a revision wait says %+v, slot %q", rv, slotOf(rev))
}
}
// Design 47 H2 H-A-723, H-A-707 (5), DQ-53 (H-A-717): a key holds one wait. A pin wait held over a toolchain wait ends it as `replaced`
// and stands; an equal Hold writes nothing; a pin wait ended `failed` is deleted; its body says `package-wait-minutes` as its bound
// and carries publisher, revision and until, the package its path alone.
func TestAKeyHoldsTheBuildsOneWait(t *testing.T) {
var said []string
var bodies []waitBody
w := newWaitsKept(nil, func(_ context.Context, event, _ string, body any) error {
said = append(said, strings.SplitN(event, ".", 2)[0])
bodies = append(bodies, body.(waitBody))
return nil
}, time.Hour, t.Logf)
ctx := context.Background()
since := time.Unix(100, 0)
tc := build.ToolchainWait{Module: "m", Fingerprint: fpw, Language: "go", Version: "go1.26.0", Since: since}
pin := build.ToolchainWait{Module: "m", Fingerprint: fpw, Package: "git.x/y/sdk", Publisher: "sdk@src2:p", Revision: 7, Bound: "10m0s", Since: since, Until: since.Add(10 * time.Minute)}
w.Hold(ctx, tc)
w.Hold(ctx, tc)
if !slices.Equal(said, []string{"wait-held"}) {
t.Fatalf("a toolchain wait held twice said %v, want one wait-held", said)
}
w.Hold(ctx, pin)
if !slices.Equal(said, []string{"wait-held", "wait-ended", "wait-held"}) || bodies[1].How != build.WaitReplaced || bodies[1].On != "toolchain:go@go1.26.0" {
t.Fatalf("a pin wait over a toolchain wait said %v, %+v, want the toolchain wait ended replaced", said, bodies)
}
if p := bodies[2]; p.Bound != "10m0s" || p.Publisher != "sdk@src2:p" || p.Revision != 7 || !p.Until.Equal(pin.Until) || p.Part != "git.x/y/sdk" || p.On != "package:git.x/y/sdk" {
t.Errorf("the pin wait says %+v", p)
}
w.Hold(ctx, pin)
if len(said) != 3 {
t.Errorf("a Hold equal to the standing wait said %v, want nothing", said[3:])
}
if w.End(ctx, "m", fpw, build.WaitRan) {
t.Error("a pin wait ended ran")
}
if !w.End(ctx, "m", fpw, build.WaitFailed) || len(w.held) != 0 || bodies[len(bodies)-1].How != build.WaitFailed {
t.Errorf("a pin wait ended failed: held %v, said %v", w.held, said)
}
}
var fpw = "src2:" + strings.Repeat("a", 64)