Skip to content
quarry Artifact [f6c46e24b1]
Login

Artifact [f6c46e24b1]

Artifact f6c46e24b18421d8398fb5f215264e3849392d744d73758bdc8aef01446228b2:


package web

import (
	"net/url"
	"path/filepath"
	"regexp"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

// The bell offers Highlights between Mentions and Every message.
func TestTheBellOffersHighlightsInOrder(t *testing.T) {
	got := chatContent(t, pageData{Repo: "alpha", ChatViewer: "dan", ChatNotify: "highlights"})
	order := regexp.MustCompile(`data-hook="chat-notify-([a-z]+)"`).FindAllStringSubmatch(got, -1)
	var modes []string
	for _, m := range order {
		modes = append(modes, m[1])
	}
	if strings.Join(modes, ",") != "mentions,highlights,all,off" {
		t.Errorf("FAIL: the bell's choices are %v", modes)
	}
	if !strings.Contains(got, `data-hook="chat-notify-highlights" aria-current="true"`) {
		t.Error("FAIL: highlights is not marked as the current choice")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// /me edits the names and the digest hour next to the notification
// matrix; a short name is refused on the page with the form kept.
func TestAccountPageEditsCallNamesAndDigestHour(t *testing.T) {
	odb, err := oltp.Open(filepath.Join(t.TempDir(), "o.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { odb.Close() })
	prefs := newMemPrefs()
	deps := api.Deps{Version: "t", Repos: api.NewRepos(nil), Attention: &attnStub{}, AttnRead: &memRead{at: map[string]uint64{}},
		Prefs: prefs, PushDevices: newMemDevices(), Highlights: odb}
	r := ops.NewRegistry()
	if err := api.RegisterAll(r, deps); err != nil {
		t.Fatal(err)
	}
	f := prefsForge{h: HandlerWith(r, nil, nil, []Option{WithSessions(fakeSessions{user: "dan"}), WithPrefs(prefs)})}

	_, body := f.get(t, "/me")
	for _, want := range []string{`data-hook="highlights-form"`, `<textarea id="hl-names"`, `name="digestHour"`, `<select id="hl-tz"`, `<option value="7" selected>`} {
		if !strings.Contains(body, want) {
			t.Errorf("FAIL: /me is missing %q", want)
		}
	}

	rec := f.post(t, "/me/highlights", url.Values{"names": {"@Daniel,\ndmestas, Dan  M"}, "digestHour": {"9"}, "tz": {"America/Chicago"}})
	if rec.Code != 200 {
		t.Fatalf("FAIL: saving answered %d: %s", rec.Code, rec.Body.String()[:min(len(rec.Body.String()), 600)])
	}
	if got := odb.CallNames("dan"); strings.Join(got, ",") != "dan m,daniel,dmestas" {
		t.Errorf("FAIL: stored names = %v", got)
	}
	if p := odb.HighlightPref("dan"); p.Hour != 9 || p.TZ != "America/Chicago" {
		t.Errorf("FAIL: stored pref = %+v", p)
	}
	if !strings.Contains(rec.Body.String(), `<option value="9" selected>`) {
		t.Error("FAIL: the saved hour is not the selected one")
	}

	rec = f.post(t, "/me/highlights", url.Values{"names": {"dan"}, "digestHour": {"9"}, "tz": {"UTC"}})
	if rec.Code != 400 || !strings.Contains(rec.Body.String(), `data-hook="highlights-error"`) {
		t.Errorf("FAIL: a short name answered %d without the page's error", rec.Code)
	}
	if strings.Contains(rec.Body.String(), "invalid request") {
		t.Error("FAIL: the page error carries the ops prefix")
	}
	if !regexp.MustCompile(`<textarea[^>]*>dan</textarea>`).MatchString(rec.Body.String()) {
		t.Error("FAIL: a refused save did not keep what was typed")
	}
	if got := odb.CallNames("dan"); strings.Join(got, ",") != "dan m,daniel,dmestas" {
		t.Errorf("FAIL: a refused save changed the names to %v", got)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}