package web
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/oltp"
)
// Only accounts that have taken part are named; the rest are a count.
// On a brand-new repository nobody had touched, the wizard named five
// of the forge's nine accounts as losing access, when what they lose is
// the read every public repository grants (ec12d0bd, G-42).
func TestTheWizardNamesParticipantsAndCountsTheRest(t *testing.T) {
named, others := splitLosers(
[]string{"calionauta", "danmestas", "dmestas-test", "jva", "lou"},
[]string{"jva", "dmestas", "lou"}) // dmestas holds a role: not in losing, not named
if strings.Join(named, ",") != "jva,lou" || others != 3 {
t.Fatalf("FAIL: named=%v others=%d, want [jva lou] and 3", named, others)
}
t.Logf("ok: named %v, %d counted", named, others)
named, others = splitLosers([]string{"a", "b"}, nil)
if len(named) != 0 || others != 2 {
t.Fatalf("FAIL: with no participants: named=%v others=%d", named, others)
}
t.Log("ok: with no participants nobody is named and everyone is counted")
}
func renderTakePrivate(t *testing.T, p *takePrivatePanel) string {
t.Helper()
var sb strings.Builder
if err := takePrivatePage.Execute(&sb, pageData{Repo: "alpha", TakePrivate: p}); err != nil {
t.Fatalf("render: %v", err)
}
return sb.String()
}
// The page offers a role only to the named; the count is a single
// line carrying its number as data.
func TestTheWizardOffersRolesOnlyToTheNamed(t *testing.T) {
body := renderTakePrivate(t, &takePrivatePanel{Names: []string{"jva"}, Others: 3, Presets: oltp.Presets})
if strings.Count(body, `<select name="role-`) != 1 || !strings.Contains(body, `name="role-jva"`) {
t.Fatalf("FAIL: role selects: %d", strings.Count(body, `<select name="role-`))
}
if !strings.Contains(body, `data-hook="private-others" data-count="3"`) {
t.Fatal("FAIL: the count of the rest is not on the page as data")
}
t.Log("ok: one select for the one named; the rest is a count of 3")
body = renderTakePrivate(t, &takePrivatePanel{Others: 9, Presets: oltp.Presets})
if strings.Contains(body, `<select name="role-`) || !strings.Contains(body, `data-count="9"`) {
t.Fatal("FAIL: with nobody named there must be no roster and a count")
}
t.Log("ok: nobody named -> no roster, count of 9")
}