package inventory import ( "context" "fmt" "testing" "github.com/novox/mesh-controller/internal/catalogue" ) // **Every artifact made has one state, and a kept one says why** (novox/hq ADR 0251): what the // store's own tools set beside what the store holds, so an operator reads why something stays. func TestEveryArtifactMadeHasAStateAndAKeptOneSaysWhy(t *testing.T) { inv := fresh(t) ctx := context.Background() var made []string for i := 1; i <= 8; i++ { made = append(made, built(t, inv, fmt.Sprintf("b%02d", i), "web", i)) } // The definition names the oldest and the newest: the newest is kept for both reasons. m := catalogue.Manifest{Module: "web", Version: "1", Resources: []map[string]any{ {"id": "app", "type": "container", "name": "web", "image": made[0]}, {"id": "next", "type": "container", "name": "web-next", "image": made[7]}, }} if err := inv.RegisterModule(ctx, m, Source{Repository: "https://forge.invalid/web.git"}); err != nil { t.Fatal(err) } if err := inv.MarkCollected(ctx, []string{made[1]}); err != nil { t.Fatal(err) } states, err := inv.Artifacts(ctx) if err != nil { t.Fatal(err) } if len(states) != len(made) { t.Fatalf("%d states for %d artifacts made", len(states), len(made)) } want := []struct { state string why []string }{ {ArtifactKept, []string{KeptByDefinition}}, {ArtifactCollected, nil}, {ArtifactEligible, nil}, {ArtifactKept, []string{KeptByRecentBuild}}, {ArtifactKept, []string{KeptByRecentBuild}}, {ArtifactKept, []string{KeptByRecentBuild}}, {ArtifactKept, []string{KeptByRecentBuild}}, {ArtifactKept, []string{KeptByRecentBuild, KeptByDefinition}}, } for i, s := range states { if s.Reference != made[i] { t.Fatalf("state %d is of %s, want %s: the oldest first", i, s.Reference, made[i]) } if s.State != want[i].state || fmt.Sprint(s.Why) != fmt.Sprint(want[i].why) { t.Errorf("%s: %s %v, want %s %v", s.Reference, s.State, s.Why, want[i].state, want[i].why) } } collect, err := inv.ToCollect(ctx) if err != nil { t.Fatal(err) } if len(collect) != 1 || collect[0] != made[2] { t.Errorf("ToCollect is %v, want only the eligible %s", collect, made[2]) } }