ajhahn.de
← eeco
Go 102 lines
package playbooks

import (
	"encoding/json"
	"reflect"
	"testing"

	"github.com/ajhahnde/eeco/internal/cockpit"
)

func TestNames(t *testing.T) {
	got := Names()
	want := []string{"commit", "doc-drift", "handover", "memcheck"}
	if !reflect.DeepEqual(got, want) {
		t.Errorf("Names() = %v, want %v", got, want)
	}
}

func TestAll_SortedAndComplete(t *testing.T) {
	all := All()
	if len(all) != len(Names()) {
		t.Fatalf("All() has %d, Names() has %d", len(all), len(Names()))
	}
	for i, n := range Names() {
		if all[i].Name != n {
			t.Errorf("All()[%d].Name = %q, want %q (must be Name-sorted)", i, all[i].Name, n)
		}
	}
}

func TestGet_NewPlaybooks(t *testing.T) {
	cases := map[string]string{
		"commit":    "commit-msg",
		"doc-drift": "doc-drift",
		"memcheck":  "memory-drift",
	}
	for name, wantWF := range cases {
		pb, err := Get(name)
		if err != nil {
			t.Fatalf("Get(%q): %v", name, err)
		}
		if pb.Name != name {
			t.Errorf("%s: Name = %q", name, pb.Name)
		}
		if pb.MapsToWorkflow != wantWF {
			t.Errorf("%s: MapsToWorkflow = %q, want %q", name, pb.MapsToWorkflow, wantWF)
		}
		if len(pb.Steps) < 5 {
			t.Errorf("%s: Steps = %d, want >=5", name, len(pb.Steps))
		}
	}
}

func TestGet_Handover(t *testing.T) {
	pb, err := Get("handover")
	if err != nil {
		t.Fatalf("Get: %v", err)
	}
	if pb.Name != "handover" {
		t.Errorf("Name = %q", pb.Name)
	}
	if pb.MapsToWorkflow != "handover-refresh" {
		t.Errorf("MapsToWorkflow = %q, want handover-refresh", pb.MapsToWorkflow)
	}
	if len(pb.Steps) < 5 {
		t.Errorf("Steps = %d, want >=5", len(pb.Steps))
	}
	if len(pb.Intent.Forbidden) == 0 {
		t.Error("Intent.Forbidden is empty")
	}
}

func TestGet_Unknown(t *testing.T) {
	if _, err := Get("nope"); err == nil {
		t.Error("expected an error for an unknown playbook")
	}
}

func TestRaw_RoundTrips(t *testing.T) {
	raw, err := Raw("handover")
	if err != nil {
		t.Fatalf("Raw: %v", err)
	}
	var pb cockpit.Playbook
	if err := json.Unmarshal([]byte(raw), &pb); err != nil {
		t.Fatalf("Raw is not valid JSON: %v", err)
	}
	got, err := Get("handover")
	if err != nil {
		t.Fatal(err)
	}
	if !reflect.DeepEqual(pb, got) {
		t.Error("Raw JSON does not round-trip to the same Playbook as Get")
	}
}

func TestRaw_Unknown(t *testing.T) {
	if _, err := Raw("nope"); err == nil {
		t.Error("expected an error for an unknown playbook")
	}
}