package oltp
import (
"testing"
"time"
)
// The setting is one row per person and channel: off and UTC with no
// quiet hours until chosen, and a bad zone or hour is refused rather
// than stored.
func TestChatSMSIsAChoicePerPersonAndChannel(t *testing.T) {
d := testDB(t)
if got := d.ChatSMS("saur0n", "dan"); got.On || got.TZ != "UTC" || got.QuietStart != got.QuietEnd {
t.Fatalf("FAIL: an untouched setting reads %+v, want off, UTC, no quiet hours", got)
}
if err := d.SetChatSMS("saur0n", "dan", ChatSMS{On: true, QuietStart: 22, QuietEnd: 7, TZ: "America/Chicago"}); err != nil {
t.Fatal(err)
}
if err := d.SetChatSMS("saur0n", "eve", ChatSMS{On: true}); err != nil {
t.Fatal(err)
}
if err := d.SetChatSMS("saur0n", "fay", ChatSMS{On: false, QuietStart: 1, QuietEnd: 2}); err != nil {
t.Fatal(err)
}
for name, bad := range map[string]ChatSMS{
"zone": {On: true, TZ: "Mars/Olympus"},
"local zone": {On: true, TZ: "Local"},
"hour": {On: true, QuietStart: 24},
"negative": {On: true, QuietEnd: -1},
} {
if err := d.SetChatSMS("saur0n", "gus", bad); err == nil || !IsRefusal(err) {
t.Errorf("FAIL: a bad %s was stored or not refused: %v", name, err)
}
}
if err := d.SetChatSMS("saur0n", "", ChatSMS{On: true}); err == nil {
t.Error("FAIL: an anonymous setting was stored")
}
if got := d.ChatSMS("saur0n", "dan"); !got.On || got.QuietStart != 22 || got.QuietEnd != 7 || got.TZ != "America/Chicago" {
t.Errorf("FAIL: read back %+v", got)
}
if got := d.ChatSMS("saur0n", "eve"); got.TZ != "UTC" {
t.Errorf("FAIL: an empty zone reads %q, want UTC", got.TZ)
}
got, err := d.ChatSMSFollowers("saur0n")
if err != nil || len(got) != 2 || got[0].Account != "dan" || got[1].Account != "eve" {
t.Errorf("FAIL: followers = %+v, %v; want dan, eve (fay is off)", got, err)
}
if !t.Failed() {
t.Log("ok")
}
}
// Quiet hours are wall-clock hours in the person's zone; a window may
// wrap midnight and start == end is no window.
func TestChatSMSQuietHours(t *testing.T) {
at := func(zone string, h int) time.Time {
loc, _ := time.LoadLocation(zone)
return time.Date(2026, 11, 24, h, 30, 0, 0, loc)
}
for _, c := range []struct {
name string
s ChatSMS
now time.Time
want bool
}{
{"no window", ChatSMS{QuietStart: 5, QuietEnd: 5, TZ: "UTC"}, at("UTC", 5), false},
{"inside", ChatSMS{QuietStart: 9, QuietEnd: 17, TZ: "UTC"}, at("UTC", 12), true},
{"start is quiet", ChatSMS{QuietStart: 9, QuietEnd: 17, TZ: "UTC"}, at("UTC", 9), true},
{"end is not", ChatSMS{QuietStart: 9, QuietEnd: 17, TZ: "UTC"}, at("UTC", 17), false},
{"wrap late", ChatSMS{QuietStart: 22, QuietEnd: 7, TZ: "UTC"}, at("UTC", 23), true},
{"wrap early", ChatSMS{QuietStart: 22, QuietEnd: 7, TZ: "UTC"}, at("UTC", 3), true},
{"wrap outside", ChatSMS{QuietStart: 22, QuietEnd: 7, TZ: "UTC"}, at("UTC", 12), false},
{"zone shifts it", ChatSMS{QuietStart: 22, QuietEnd: 7, TZ: "America/Chicago"}, at("UTC", 23), false}, // 17:30 in Chicago
{"zone applies", ChatSMS{QuietStart: 0, QuietEnd: 6, TZ: "Asia/Tokyo"}, time.Date(2026, 11, 24, 18, 0, 0, 0, time.UTC), true}, // 03:00 in Tokyo
{"bad zone is not quiet", ChatSMS{QuietStart: 0, QuietEnd: 6, TZ: "nowhere"}, at("UTC", 3), false},
} {
if got := c.s.Quiet(c.now); got != c.want {
t.Errorf("FAIL: %s: Quiet = %v, want %v", c.name, got, c.want)
}
}
if !t.Failed() {
t.Log("ok")
}
}
// Presence is the last time a person posted in or read a channel; the
// reply route is the channel that last texted them.
func TestChatSeenAndSentAreForgeLocalClocks(t *testing.T) {
d := testDB(t)
now := time.Unix(1_800_000_000, 0)
if d.ChatSeenSince("saur0n", "dan", now.Add(-time.Hour)) {
t.Fatal("FAIL: never seen reads as seen")
}
if err := d.TouchChatSeen("saur0n", "dan", now.Add(-30*time.Minute)); err != nil {
t.Fatal(err)
}
if d.ChatSeenSince("saur0n", "dan", now.Add(-20*time.Minute)) {
t.Error("FAIL: a 30 minute old sighting counted inside 20 minutes")
}
if !d.ChatSeenSince("saur0n", "dan", now.Add(-time.Hour)) {
t.Error("FAIL: a 30 minute old sighting missing from the hour")
}
if err := d.TouchChatSeen("saur0n", "dan", now); err != nil {
t.Fatal(err)
}
if !d.ChatSeenSince("saur0n", "dan", now.Add(-time.Minute)) {
t.Error("FAIL: a fresh sighting did not move the clock")
}
if err := d.TouchChatSeen("saur0n", "dan", now.Add(-time.Hour)); err != nil {
t.Fatal(err)
}
if !d.ChatSeenSince("saur0n", "dan", now.Add(-time.Minute)) {
t.Error("FAIL: an older sighting moved the clock back")
}
recent := func(since time.Time) []string {
t.Helper()
got, err := d.RecentChatSMSSent("dan", since)
if err != nil {
t.Fatal(err)
}
var out []string
for _, c := range got {
out = append(out, c.Repo)
}
return out
}
if got := recent(now.Add(-2 * time.Hour)); len(got) != 0 {
t.Fatalf("FAIL: nothing sent reads as %v", got)
}
_ = d.RecordChatSMSSent("dan", "alpha", now.Add(-3*time.Hour))
if got := recent(now.Add(-2 * time.Hour)); len(got) != 0 {
t.Errorf("FAIL: a text three hours old is recent: %v", got)
}
_ = d.RecordChatSMSSent("dan", "alpha", now.Add(-90*time.Minute))
_ = d.RecordChatSMSSent("dan", "beta", now.Add(-10*time.Minute))
if got := recent(now.Add(-2 * time.Hour)); len(got) != 2 || got[0] != "beta" || got[1] != "alpha" {
t.Errorf("FAIL: recent = %v, want beta then alpha", got)
}
// A second text within a minute of the first leaves the row alone.
_ = d.RecordChatSMSSent("dan", "beta", now.Add(-10*time.Minute+30*time.Second))
got, _ := d.RecentChatSMSSent("dan", now.Add(-time.Hour))
if len(got) != 1 || !got[0].At.Equal(now.Add(-10*time.Minute)) {
t.Errorf("FAIL: a text within a minute rewrote the route: %+v", got)
}
if !t.Failed() {
t.Log("ok")
}
}
// A repository's deletion and an account's deletion take their presence
// and reply routes with them: a successor of the name inherits nothing.
func TestChatSeenAndSentGoWithTheRepoAndTheAccount(t *testing.T) {
d := testDB(t)
now := time.Unix(1_800_000_000, 0)
for _, repo := range []string{"saur0n", "other"} {
if err := d.AddRepo(repo, t.TempDir(), "", Public); err != nil {
t.Fatal(err)
}
}
if err := d.AddAccount("dan", "a-long-enough-password", false); err != nil {
t.Fatal(err)
}
seen := func(repo string) bool { return d.ChatSeenSince(repo, "dan", now.Add(-time.Hour)) }
sent := func() map[string]bool {
got, _ := d.RecentChatSMSSent("dan", now.Add(-time.Hour))
m := map[string]bool{}
for _, c := range got {
m[c.Repo] = true
}
return m
}
for _, repo := range []string{"saur0n", "other"} {
_ = d.TouchChatSeen(repo, "dan", now)
_ = d.RecordChatSMSSent("dan", repo, now)
}
if err := d.PurgeRepo("saur0n"); err != nil {
t.Fatal(err)
}
if seen("saur0n") || sent()["saur0n"] || !seen("other") || !sent()["other"] {
t.Errorf("FAIL: after the repo went, seen = %v/%v sent = %v", seen("saur0n"), seen("other"), sent())
}
if err := d.DeleteAccount("dan"); err != nil {
t.Fatal(err)
}
if seen("other") || len(sent()) != 0 {
t.Errorf("FAIL: after the account went, seen = %v sent = %v", seen("other"), sent())
}
if !t.Failed() {
t.Log("ok")
}
}