package web
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
)
type fakeProtect struct{ rules map[string][]oltp.Protection }
func (f fakeProtect) Protections(repo string) ([]oltp.Protection, error) { return f.rules[repo], nil }
func (f fakeProtect) SetProtectionBy(string, string, []string, string) error { return nil }
// The settings page states the cost where the rule is set: on this
// forge `i` means less than it means in fossil, and a stock client can
// be refused a push its capability string says it may make. Both are
// surprising to someone who knows fossil, and a page that set the rule
// without saying so would be the RFC's own "never disguised as
// fossil's" broken in the UI.
func TestSettingsPageStatesWhatProtectionCosts(t *testing.T) {
src := settingsSource
for _, want := range []string{
`action="{{repoURL .Repo}}/settings/protect"`,
"means less than it means in",
"stored outside the repository",
"byte-identical",
} {
if !strings.Contains(src, want) {
t.Errorf("settings page lacks %q", want)
}
}
}
// No policy store: the section is not drawn, rather than drawn empty
// and unsaveable. With one: an empty list is the ordinary state and the
// page must still offer the first row.
func TestProtectPanelOffersAFirstRowAndHidesWithoutAStore(t *testing.T) {
if p := protectPanelFor(nil, "alpha"); p != nil {
t.Error("a forge with no policy store drew the section")
}
p := protectPanelFor(fakeProtect{rules: map[string][]oltp.Protection{}}, "alpha")
if p == nil || len(p.Rules) != 0 || p.Blank != 0 {
t.Fatalf("empty policy gave %+v, want no rules and a blank row at 0", p)
}
p = protectPanelFor(fakeProtect{rules: map[string][]oltp.Protection{
"alpha": {{Branch: "trunk", Owners: []string{"alice", "bob"}}, {Branch: "release-*", Owners: []string{"bob"}}},
}}, "alpha")
if len(p.Rules) != 2 || p.Blank != 2 {
t.Fatalf("two rules gave %+v", p)
}
if p.Rules[0].Owners != "alice bob" {
t.Errorf("owners rendered as %q, want space-separated for the input", p.Rules[0].Owners)
}
}