package web
import (
"context"
"net/url"
"path/filepath"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/oltp"
"craftdesign.group/quarry/internal/ops"
)
// A rule set through ci.checks.set is the rule the settings page draws,
// and clearing it through the op takes the row off the page.
func TestRequiredChecksOpAndSettingsPageAgree(t *testing.T) {
db, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
r := ops.NewRegistry()
if err := api.RegisterAll(r, api.Deps{Version: "v", CheckRules: db,
Repos: api.NewRepos(map[string]*api.Repo{"alpha": {Chat: "alpha", Index: landingIndex(t, false)}})}); err != nil {
t.Fatal(err)
}
ctx := ops.WithUser(context.Background(), "dan")
in := url.Values{"repo": {"alpha"}, "branch": {"release-*"}, "checks": {"vet", "go"}}
if _, err := invoke(ctx, r, "ci.checks.set", in); err != nil {
t.Fatalf("FAIL: set: %v", err)
}
panel := protectPanelFor(protectStub{db}, "alpha")
if panel == nil || len(panel.Rules) == 0 || panel.Rules[0].Branch != "release-*" || panel.Rules[0].Checks != "go vet" {
t.Fatalf("FAIL: the settings data is %+v", panel)
}
in.Del("checks")
if _, err := invoke(ctx, r, "ci.checks.set", in); err != nil {
t.Fatalf("FAIL: clear: %v", err)
}
if panel := protectPanelFor(protectStub{db}, "alpha"); panel == nil || len(panel.Rules) != 0 {
t.Errorf("FAIL: the cleared rule is still on the page: %+v", panel)
}
if !t.Failed() {
t.Log("ok")
}
}
type protectStub struct{ *oltp.DB }