87 lines
3.0 KiB
Go
87 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/health"
|
|
)
|
|
|
|
// 1572/AC2 (ADR 0477 §2): the recipe has no field for a Node version, and no constant names one.
|
|
func TestTheRecipeHasNoFieldForAToolchainVersion(t *testing.T) {
|
|
typ := reflect.TypeOf(Recipe{})
|
|
for i := 0; i < typ.NumField(); i++ {
|
|
if n := typ.Field(i).Name; n == "Node" || strings.Contains(n, "Toolchain") {
|
|
t.Errorf("Recipe has the field %s", n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 1572/AC2 and AC4: whatever Node the machine has installed, the module stays healthy, names that version in its
|
|
// health, runs its steps and writes the version to <node>.typescript of the toolchains state.
|
|
func TestAnyInstalledNodeIsHealthyNamedAndReported(t *testing.T) {
|
|
for _, installed := range []string{"v26.10.0", "v26.11.0", "v27.0.1", "v24.9.0"} {
|
|
t.Run(installed, func(t *testing.T) {
|
|
states := newStates()
|
|
m := moduleWith(states, installed)
|
|
if err := m.Check(context.Background()); err != nil {
|
|
t.Fatalf("Check refused: %v", err)
|
|
}
|
|
a := m.Health(context.Background())
|
|
if !a.Healthy || a.State != health.StateHealthy || !strings.Contains(a.Why, installed) {
|
|
t.Errorf("health = %+v, want healthy naming %s", a, installed)
|
|
}
|
|
tc, ok := states.toolchain("ace.typescript")
|
|
if !ok || tc.Version != installed || tc.Recipe != m.Recipe.Version() {
|
|
t.Errorf("ace.typescript = %+v, %v; want version %s and recipe %s", tc, ok, installed, m.Recipe.Version())
|
|
}
|
|
if err := m.Prepare(context.Background()); err != nil {
|
|
t.Errorf("no step can run on node %s: %v", installed, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// 1572/AC3 (ADR 0477 §3): the recipe version is made without a Node version: the recipe read from a lock is the same
|
|
// whatever node is installed, and a version set on the recipe by name moves nothing; it moves with the steps' version
|
|
// and with one byte of the lock.
|
|
func TestTheRecipeVersionIgnoresTheToolchainVersionAndMovesWithStepsAndLock(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "package-lock.json")
|
|
os.WriteFile(path, []byte("lock-a"), 0o600)
|
|
base, err := ReadRecipe(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v := base.Version()
|
|
for _, installed := range []string{"v26.10.0", "v27.0.1"} {
|
|
m := moduleWith(newStates(), installed)
|
|
m.Reload = func() (Recipe, error) { return ReadRecipe(path) }
|
|
if err := m.Check(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := m.recipe().Version(); got != v {
|
|
t.Errorf("with node %s installed the recipe version is %s, want %s", installed, got, v)
|
|
}
|
|
}
|
|
r := base
|
|
if f := reflect.ValueOf(&r).Elem().FieldByName("Node"); f.IsValid() {
|
|
f.SetString("v99.0.0")
|
|
if r.Version() != v {
|
|
t.Errorf("a Node version on the recipe moved its version")
|
|
}
|
|
}
|
|
r = base
|
|
r.Steps += "x"
|
|
if r.Version() == v {
|
|
t.Errorf("the steps' version did not move the recipe version")
|
|
}
|
|
os.WriteFile(path, []byte("lock-b"), 0o600)
|
|
if b, _ := ReadRecipe(path); b.Version() == v {
|
|
t.Errorf("one byte of the lock did not move the recipe version")
|
|
}
|
|
}
|