Skip to content
quarry Artifact [efda1d87b3]
Login

Artifact [efda1d87b3]

Artifact efda1d87b3af56e9c6e7c78024f2799344ee6f85c9ff42811639dd30a673e1bb:


package oltp

import (
	"path/filepath"
	"testing"
)

func openAttn(t *testing.T) *DB {
	t.Helper()
	db, err := Open(filepath.Join(t.TempDir(), "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { db.Close() })
	return db
}

func TestAttentionReadPointerOnlyMovesForward(t *testing.T) {
	db := openAttn(t)
	if got := db.AttentionReadSeq("alice"); got != 0 {
		t.Fatalf("fresh pointer is %d, want 0", got)
	}
	if err := db.MarkAttentionRead("alice", 12); err != nil {
		t.Fatal(err)
	}
	if err := db.MarkAttentionRead("alice", 4); err != nil {
		t.Fatal(err)
	}
	if got := db.AttentionReadSeq("alice"); got != 12 {
		t.Fatalf("pointer is %d after a backwards mark, want 12", got)
	}
	if err := db.MarkAttentionRead("alice", 30); err != nil {
		t.Fatal(err)
	}
	if got := db.AttentionReadSeq("alice"); got != 30 {
		t.Fatalf("pointer is %d, want 30", got)
	}
	if got := db.AttentionReadSeq("bob"); got != 0 {
		t.Fatalf("bob's pointer is %d, want 0", got)
	}
}

func TestAccountPrefRoundTrip(t *testing.T) {
	db := openAttn(t)
	if got := db.AccountPref("alice", "notify_desktop"); got != "" {
		t.Fatalf("unset pref is %q, want empty", got)
	}
	if err := db.SetAccountPref("alice", "notify_desktop", "on"); err != nil {
		t.Fatal(err)
	}
	if got := db.AccountPref("alice", "notify_desktop"); got != "on" {
		t.Fatalf("pref is %q, want on", got)
	}
	if got := db.AccountPref("bob", "notify_desktop"); got != "" {
		t.Fatalf("bob's pref is %q, want empty", got)
	}
	if err := db.SetAccountPref("alice", "notify_desktop", ""); err != nil {
		t.Fatal(err)
	}
	if got := db.AccountPref("alice", "notify_desktop"); got != "" {
		t.Fatalf("cleared pref is %q, want empty", got)
	}
}

func TestChatRobotPatterns(t *testing.T) {
	db := openAttn(t)
	if err := db.SetChatRobot("alpha", "checkin.* landing.*"); err != nil {
		t.Fatal(err)
	}
	if got := db.ChatRobot("alpha"); got != "checkin.* landing.*" {
		t.Fatalf("patterns are %q", got)
	}
	all, err := db.ChatRobots()
	if err != nil {
		t.Fatal(err)
	}
	if len(all) != 1 || all["alpha"] == "" {
		t.Fatalf("robots are %v, want one entry for alpha", all)
	}
	// Empty is off, and off is the absence of a row: one state, not two.
	if err := db.SetChatRobot("alpha", ""); err != nil {
		t.Fatal(err)
	}
	if got := db.ChatRobot("alpha"); got != "" {
		t.Fatalf("patterns are %q after clearing, want empty", got)
	}
	all, err = db.ChatRobots()
	if err != nil {
		t.Fatal(err)
	}
	if len(all) != 0 {
		t.Fatalf("robots are %v after clearing, want none", all)
	}
}