package controller

import "testing"

func TestScheduleForK8up(t *testing.T) {
	cases := []struct {
		in      string
		want    string
		wantErr bool
	}{
		{"@hourly", "@hourly", false},
		{"@daily", "@daily", false},
		{"@weekly", "@weekly", false},
		{"@monthly", "@monthly", false},
		{"  @daily  ", "@daily", false},
		{"0 2 * * *", "0 2 * * *", false},
		{"*/15 * * * *", "*/15 * * * *", false},
		{"0 0 * * 1-5", "0 0 * * 1-5", false},
		{"0 0 1 * MON", "0 0 1 * MON", false},         // alphabetic day-of-week
		{"0 0 1 JAN *", "0 0 1 JAN *", false},         // alphabetic month
		{"0 0 * * mon-fri", "0 0 * * mon-fri", false}, // lowercase range of names
		{"@bogus", "", true},
		{"0 2 * *", "", true},     // 4 fields
		{"0 0 0 * * *", "", true}, // 6 fields not valid for k8up
		{"0 2 * * foo", "", true}, // unknown alphabetic name
		{"", "", true},
	}
	for _, c := range cases {
		got, err := ScheduleForK8up(c.in)
		if c.wantErr {
			if err == nil {
				t.Errorf("ScheduleForK8up(%q) expected error, got %q", c.in, got)
			}
			continue
		}
		if err != nil {
			t.Errorf("ScheduleForK8up(%q) unexpected error: %v", c.in, err)
			continue
		}
		if got != c.want {
			t.Errorf("ScheduleForK8up(%q) = %q, want %q", c.in, got, c.want)
		}
	}
}

func TestScheduleForCNPG(t *testing.T) {
	cases := []struct {
		in      string
		want    string
		wantErr bool
	}{
		{"@hourly", "0 0 * * * *", false},
		{"@daily", "0 0 0 * * *", false},
		{"@midnight", "0 0 0 * * *", false},
		{"@weekly", "0 0 0 * * 0", false},
		{"@monthly", "0 0 0 1 * *", false},
		{"@yearly", "0 0 0 1 1 *", false},
		{"@annually", "0 0 0 1 1 *", false},
		{"0 2 * * *", "0 0 2 * * *", false}, // 5-field gains seconds
		{"*/15 * * * *", "0 */15 * * * *", false},
		{"30 0 3 * * *", "30 0 3 * * *", false}, // already 6-field
		{"  @daily ", "0 0 0 * * *", false},
		{"0 0 1 * MON", "0 0 0 1 * MON", false},   // 5-field w/ alphabetic dow gains seconds
		{"0 0 0 1 JAN *", "0 0 0 1 JAN *", false}, // 6-field w/ alphabetic month
		{"@bogus", "", true},
		{"0 2 * *", "", true},       // 4 fields
		{"1 2 3 4 5 6 7", "", true}, // 7 fields
		{"0 2 * * bad", "", true},   // unknown alphabetic name
		{"", "", true},
	}
	for _, c := range cases {
		got, err := ScheduleForCNPG(c.in)
		if c.wantErr {
			if err == nil {
				t.Errorf("ScheduleForCNPG(%q) expected error, got %q", c.in, got)
			}
			continue
		}
		if err != nil {
			t.Errorf("ScheduleForCNPG(%q) unexpected error: %v", c.in, err)
			continue
		}
		if got != c.want {
			t.Errorf("ScheduleForCNPG(%q) = %q, want %q", c.in, got, c.want)
		}
	}
}
