7f42cc73be
Seven fixes, each answering a line comment on the stack.
**Weather no longer invents Moscow** (PR 50). extractWeatherLocation returned
the string "Moscow" when he named no city and voice.weather.default_location
was unset — a made-up answer presented as fact, which is the one thing maven
must never do. It returns "" now and the query path says it does not know.
**Digest statuses are a defined type** (PR 50). DigestStatus string plus the
three constants, so a rule name cannot reach the status column.
**Quiet-mode negation is not adjacency** (PR 53). The OFF list carried
{"не","тих"}, an adjacency pattern, so "не надо тихий режим" missed OFF, hit
the ON pattern {"тих","режим"}, and asking for quiet mode to stop turned it
on. Negators are scanned over the whole utterance now, with the two ON phrases
that are themselves built on "не" excluded. "тихий режим выключи" works too,
which it did not before.
**Pattern stability uses a median band** (PR 54). max/min over the extremes
asked whether every gap resembles every other gap, so 7,7,7,7,20 — four clean
weeks and one holiday — was thrown away at a ratio of 2.9. Each interval is
now tested against the median and 70% must be in band, and the reported
interval is the median of the in-band ones, so a holiday no longer drags a
weekly habit to "every 9.6 days". The reviewer's 5,8,10,3 is still rejected.
**The weekday profile stops reciting everyday habits** (PR 59). "What do I do
on Saturdays?" answered "you drink water" — true, and useless, because it is
equally true of every other day. Activities that are habits on six or more
weekdays move to Profile.Everyday and are read back as daily habits instead of
as an answer about that day.
**Russian phrase tables move out of Go** (PR 59, PR 61). The behaviour glosses
and weekday names, and the task capture/urgency/list vocabulary, are now
behavior_ru.json and task_phrases.json, embedded with go:embed. Single-binary
deploy is unchanged; wording edits are no longer source diffs.
**nginx template stops taking nginx down** (PR 52). Two host-side failure
modes, both plausible causes of today's crash. The $connection_upgrade map is
fatal when duplicated, so it moved to its own nginx-upgrade-map.conf with a
grep-first note. And `listen 10.42.0.1:80` fails with EADDRNOTAVAIL when wg0
is not up yet, so nginx exits on a reboot that beats WireGuard — the header
now documents net.ipv4.ip_nonlocal_bind.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TrVSBKe3RFDF4fGYKWYQnX
203 lines
6.1 KiB
Go
203 lines
6.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestDigestEntryRoundTrips — a suppressed care candidate lands durably and
|
|
// comes back out of PendingDigestEntries with its severity and body intact.
|
|
func TestDigestEntryRoundTrips(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
|
|
id, deduped, err := s.EnqueueDigestEntry(ctx, "break", 2, "ты долго не отдыхала", now, now.Add(24*time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
if deduped {
|
|
t.Fatal("first enqueue must not report deduped")
|
|
}
|
|
if id == 0 {
|
|
t.Fatal("want a nonzero id")
|
|
}
|
|
|
|
entries, err := s.PendingDigestEntries(ctx, now)
|
|
if err != nil {
|
|
t.Fatalf("pending: %v", err)
|
|
}
|
|
if len(entries) != 1 || entries[0].ID != id {
|
|
t.Fatalf("want 1 pending entry with id %d, got %+v", id, entries)
|
|
}
|
|
if entries[0].Rule != "break" || entries[0].Severity != 2 || entries[0].Body != "ты долго не отдыхала" {
|
|
t.Fatalf("entry contents wrong: %+v", entries[0])
|
|
}
|
|
}
|
|
|
|
// TestDigestEntrySurvivesRestart — durability is the whole point: a fresh
|
|
// Store handle on the same file must see the same pending entry, exactly
|
|
// like the delivery outbox's crash-recovery promise.
|
|
func TestDigestEntrySurvivesRestart(t *testing.T) {
|
|
dir := t.TempDir()
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
|
|
s1, err := Open(ctx, dir+"/m.db")
|
|
if err != nil {
|
|
t.Fatalf("open: %v", err)
|
|
}
|
|
id, _, err := s1.EnqueueDigestEntry(ctx, "break", 2, "перерыв", now, now.Add(24*time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
if err := s1.Close(); err != nil {
|
|
t.Fatalf("close: %v", err)
|
|
}
|
|
|
|
// simulated restart: a brand new Store handle on the same file.
|
|
s2, err := Open(ctx, dir+"/m.db")
|
|
if err != nil {
|
|
t.Fatalf("reopen: %v", err)
|
|
}
|
|
defer func() { _ = s2.Close() }()
|
|
|
|
entries, err := s2.PendingDigestEntries(ctx, now)
|
|
if err != nil {
|
|
t.Fatalf("pending after restart: %v", err)
|
|
}
|
|
if len(entries) != 1 || entries[0].ID != id {
|
|
t.Fatalf("digest entry did not survive restart: %+v", entries)
|
|
}
|
|
}
|
|
|
|
// TestDigestEntryDedupesSameRuleAndBody — the same suppressed nudge
|
|
// repeating across ticks (quiet hours holding for hours) must not pile up
|
|
// into several copies of itself; he hears it once.
|
|
func TestDigestEntryDedupesSameRuleAndBody(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
|
|
id1, deduped1, err := s.EnqueueDigestEntry(ctx, "break", 2, "перерыв нужен", now, now.Add(24*time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("first enqueue: %v", err)
|
|
}
|
|
if deduped1 {
|
|
t.Fatal("first enqueue should not be deduped")
|
|
}
|
|
|
|
for i := 0; i < 2; i++ {
|
|
id2, deduped2, err := s.EnqueueDigestEntry(ctx, "break", 2, "перерыв нужен", now.Add(time.Minute), now.Add(25*time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("repeat enqueue: %v", err)
|
|
}
|
|
if !deduped2 {
|
|
t.Fatal("repeat enqueue of the same rule+body should report deduped")
|
|
}
|
|
if id2 != id1 {
|
|
t.Fatalf("deduped enqueue should return the original id: want %d got %d", id1, id2)
|
|
}
|
|
}
|
|
|
|
entries, err := s.PendingDigestEntries(ctx, now)
|
|
if err != nil {
|
|
t.Fatalf("pending: %v", err)
|
|
}
|
|
if len(entries) != 1 {
|
|
t.Fatalf("want exactly 1 pending entry after 3 enqueues of the same nudge, got %d", len(entries))
|
|
}
|
|
}
|
|
|
|
// TestDigestEntryExpiresRatherThanDeliversLate — a stale entry (past its
|
|
// expires_ts) must not surface in PendingDigestEntries, and the sweep should
|
|
// mark it expired instead of leaving it around to be delivered late.
|
|
func TestDigestEntryExpiresRatherThanDeliversLate(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
created := time.Now()
|
|
expiresAt := created.Add(time.Hour)
|
|
|
|
id, _, err := s.EnqueueDigestEntry(ctx, "water", 1, "стакан воды", created, expiresAt)
|
|
if err != nil {
|
|
t.Fatalf("enqueue: %v", err)
|
|
}
|
|
|
|
afterExpiry := expiresAt.Add(time.Minute)
|
|
|
|
// even before the sweep runs, a stale entry must not be handed back as
|
|
// pending — "not yet swept" must not mean "still deliverable".
|
|
entries, err := s.PendingDigestEntries(ctx, afterExpiry)
|
|
if err != nil {
|
|
t.Fatalf("pending: %v", err)
|
|
}
|
|
if len(entries) != 0 {
|
|
t.Fatalf("stale entry must not be returned as pending, got %+v", entries)
|
|
}
|
|
|
|
n, err := s.ExpireStaleDigestEntries(ctx, afterExpiry)
|
|
if err != nil {
|
|
t.Fatalf("expire sweep: %v", err)
|
|
}
|
|
if n != 1 {
|
|
t.Fatalf("want 1 entry expired, got %d", n)
|
|
}
|
|
|
|
var status DigestStatus
|
|
if err := s.db.QueryRowContext(ctx, `SELECT status FROM digest_entries WHERE id = ?`, id).Scan(&status); err != nil {
|
|
t.Fatalf("read back: %v", err)
|
|
}
|
|
if status != DigestExpired {
|
|
t.Fatalf("status: want %q, got %q", DigestExpired, status)
|
|
}
|
|
|
|
// idempotent: a second sweep finds nothing new.
|
|
n2, err := s.ExpireStaleDigestEntries(ctx, afterExpiry.Add(time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("second sweep: %v", err)
|
|
}
|
|
if n2 != 0 {
|
|
t.Fatalf("second sweep should find nothing, got %d", n2)
|
|
}
|
|
}
|
|
|
|
// TestDigestEntryDrainMarksDrainedNotDeleted — draining is bookkeeping, not
|
|
// deletion: the row survives as an audit trail of what she actually said.
|
|
func TestDigestEntryDrainMarksDrainedNotDeleted(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
now := time.Now()
|
|
|
|
id1, _, err := s.EnqueueDigestEntry(ctx, "break", 2, "перерыв", now, now.Add(24*time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("enqueue 1: %v", err)
|
|
}
|
|
id2, _, err := s.EnqueueDigestEntry(ctx, "break2", 2, "другое", now, now.Add(24*time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("enqueue 2: %v", err)
|
|
}
|
|
|
|
if err := s.DrainDigestEntries(ctx, []int64{id1, id2}, now.Add(time.Hour)); err != nil {
|
|
t.Fatalf("drain: %v", err)
|
|
}
|
|
|
|
entries, err := s.PendingDigestEntries(ctx, now.Add(time.Hour))
|
|
if err != nil {
|
|
t.Fatalf("pending: %v", err)
|
|
}
|
|
if len(entries) != 0 {
|
|
t.Fatalf("drained entries must not still be pending, got %+v", entries)
|
|
}
|
|
|
|
for _, id := range []int64{id1, id2} {
|
|
var status DigestStatus
|
|
if err := s.db.QueryRowContext(ctx, `SELECT status FROM digest_entries WHERE id = ?`, id).Scan(&status); err != nil {
|
|
t.Fatalf("read back %d: %v", id, err)
|
|
}
|
|
if status != DigestDrained {
|
|
t.Fatalf("entry %d status: want %q, got %q", id, DigestDrained, status)
|
|
}
|
|
}
|
|
}
|