Skip to content
quarry Artifact [795a6c9fa5]
Login

Artifact [795a6c9fa5]

Artifact 795a6c9fa5fe4a9b53d4ebe778581b4e8137f89d29b499dc51cb1ed13867e22e:


package oltp

import (
	"strings"
	"testing"
	"time"
)

// Names are stored without @, in lower case and once; a name under four
// characters, a phrase and an eleventh name are refused with a reason.
func TestCleanCallNames(t *testing.T) {
	got, err := CleanCallNames([]string{"@Daniel", " dmestas ", "DANIEL", ""})
	if err != nil || strings.Join(got, ",") != "daniel,dmestas" {
		t.Errorf("FAIL: cleaned = %v, %v", got, err)
	}
	if got, err := CleanCallNames([]string{"@Dan   M"}); err != nil || strings.Join(got, ",") != "dan m" {
		t.Errorf("FAIL: a two-word name cleaned to %v, %v", got, err)
	}
	for name, in := range map[string][]string{
		"three characters": {"dan"},
		"three after @":    {"@bob"},
		"short phrase":     {"a b c"},
		"punctuation":      {"dan.m!"},
		"eleven":           strings.Fields("aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk"),
	} {
		if _, err := CleanCallNames(in); !IsRefusal(err) {
			t.Errorf("FAIL: %s: err = %v, want a refusal", name, err)
		}
	}
	if ten, err := CleanCallNames(strings.Fields("aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj")); err != nil || len(ten) != 10 {
		t.Errorf("FAIL: ten names: %v, %v", ten, err)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// Highlights is a stored bell level, and the names, hour, pending sends
// and digest clock live and die with their account and channel.
func TestHighlightsStore(t *testing.T) {
	d := testDB(t)
	if err := d.AddRepo("alpha", t.TempDir(), "", Public); err != nil {
		t.Fatal(err)
	}
	if err := d.SetChatNotify("alpha", "dan", ChatHighlights); err != nil {
		t.Fatalf("FAIL: highlights refused: %v", err)
	}
	if got := d.ChatNotify("alpha", "dan"); got != ChatHighlights {
		t.Errorf("FAIL: mode reads %q", got)
	}
	if fs, _ := d.ChatNotifyHighlights(); len(fs) != 1 || fs[0].Account != "dan" || fs[0].Repo != "alpha" {
		t.Errorf("FAIL: followers = %+v", fs)
	}
	if got, _ := d.ChatNotifyEvery("alpha"); len(got) != 0 {
		t.Errorf("FAIL: highlights counted as every message: %v", got)
	}

	if err := d.SetCallNames("dan", []string{"@Daniel", "dmestas"}); err != nil {
		t.Fatal(err)
	}
	if err := d.SetCallNames("dan", []string{"x"}); !IsRefusal(err) {
		t.Errorf("FAIL: a short name was stored: %v", err)
	}
	if got := d.CallNames("dan"); strings.Join(got, ",") != "daniel,dmestas" {
		t.Errorf("FAIL: names = %v", got)
	}
	if idx, _ := d.CallNameIndex(); len(idx["daniel"]) != 1 || idx["daniel"][0] != "dan" {
		t.Errorf("FAIL: index = %v", idx)
	}

	if p := d.HighlightPref("dan"); p.Hour != DefaultDigestHour || p.TZ != "UTC" {
		t.Errorf("FAIL: default pref = %+v", p)
	}
	if err := d.SetChatSMS("alpha", "dan", ChatSMS{TZ: "America/Chicago", QuietStart: 1, QuietEnd: 2}); err != nil {
		t.Fatal(err)
	}
	if p := d.HighlightPref("dan"); p.TZ != "America/Chicago" {
		t.Errorf("FAIL: the zone from texts was not reused: %+v", p)
	}
	if err := d.SetHighlightPref("dan", HighlightPref{Hour: 24}); !IsRefusal(err) {
		t.Errorf("FAIL: hour 24 stored: %v", err)
	}
	if err := d.SetHighlightPref("dan", HighlightPref{Hour: 9, TZ: "Europe/Paris"}); err != nil {
		t.Fatal(err)
	}
	if p := d.HighlightPref("dan"); p.Hour != 9 || p.TZ != "Europe/Paris" {
		t.Errorf("FAIL: pref = %+v", p)
	}

	now := time.Now()
	if d.HasHighlightPending("alpha", "dan") {
		t.Errorf("FAIL: pending before any send")
	}
	_ = d.AddHighlightPending("alpha", "dan", "t1", now)
	_ = d.AddHighlightPending("alpha", "dan", "t2", now)
	_ = d.AddHighlightPending("alpha", "eve", "t3", now)
	if !d.HasHighlightPending("alpha", "dan") {
		t.Errorf("FAIL: no pending after a send")
	}
	if got, _ := d.TakeHighlightPending("alpha", "dan"); len(got) != 2 {
		t.Errorf("FAIL: took %v", got)
	}
	if d.HasHighlightPending("alpha", "dan") || !d.HasHighlightPending("alpha", "eve") {
		t.Errorf("FAIL: take left dan or took eve's")
	}
	_ = d.AddHighlightPending("alpha", "eve", "t4", now.Add(2*HighlightPendingGrace))
	if got, _ := d.TakeHighlightPending("alpha", "eve"); len(got) != 1 || got[0] != "t4" {
		t.Errorf("FAIL: stale pending kept: %v", got)
	}

	if !d.HighlightDigestAt("alpha", "dan").IsZero() {
		t.Errorf("FAIL: a digest time before any run")
	}
	_ = d.SetHighlightDigestAt("alpha", "dan", now)
	if got := d.HighlightDigestAt("alpha", "dan"); got.Unix() != now.Unix() {
		t.Errorf("FAIL: digest time = %v", got)
	}

	if err := d.AddAccount("dan", "a-long-enough-password", false); err != nil {
		t.Fatal(err)
	}
	if err := d.DeleteAccount("dan"); err != nil {
		t.Fatal(err)
	}
	if len(d.CallNames("dan")) != 0 || d.HighlightPref("dan").Hour != DefaultDigestHour {
		t.Errorf("FAIL: a successor of the same name inherits names or hour")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}