69 lines
2.4 KiB
Go
69 lines
2.4 KiB
Go
package notify
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"past/internal/store"
|
|
"past/internal/testdb"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type testSender struct {
|
|
calls int
|
|
to, from int64
|
|
}
|
|
|
|
func (s *testSender) SendSubscribeMessage(to int64, typ string, data map[string]string, page string, activity, from int64) bool {
|
|
s.calls++
|
|
s.to, s.from = to, from
|
|
return true
|
|
}
|
|
|
|
func TestRetrySelectionAndPermissionRecheck(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
visible int64
|
|
count int64
|
|
force bool
|
|
bound bool
|
|
want int
|
|
}{
|
|
{"retry failed event", 1, 1, false, true, 1},
|
|
{"retry cap", 1, 3, false, true, 0},
|
|
{"sharing switched off after scan", 0, 0, false, true, 0},
|
|
{"relationship changed after scan", 1, 0, false, false, 0},
|
|
{"force still checks sharing", 0, 0, true, true, 0},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
steps := []testdb.Step{{Match: "SELECT a.*", Check: func(query string, _ []any) error {
|
|
for _, part := range []string{"nl.status='success'", "sl.status='success'", "cp.status='active'", "u.couple_id=cp.id", "COALESCE(u.period_visible,0)<>0", "JOIN subscribe_templates"} {
|
|
if !strings.Contains(query, part) {
|
|
t.Errorf("missing guard %s", part)
|
|
}
|
|
}
|
|
if !tt.force && (!strings.Contains(query, "COUNT(*)") || !strings.Contains(query, "a.created_at >= ?")) {
|
|
t.Error("missing bounded retry")
|
|
}
|
|
return nil
|
|
}, Columns: []string{"id", "couple_id", "user_id", "to_user_id", "action_type", "created_at"}, Rows: [][]driver.Value{{int64(9), int64(5), int64(1), int64(2), "period_add", "2026-10-11 12:00:00"}}}}
|
|
allowed := [][]driver.Value(nil)
|
|
if tt.bound {
|
|
allowed = [][]driver.Value{{tt.visible}}
|
|
}
|
|
steps = append(steps, testdb.Step{Match: "SELECT u.period_visible", Args: []any{int64(2), int64(1), int64(5)}, Columns: []string{"period_visible"}, Rows: allowed})
|
|
if tt.bound && tt.visible != 0 && !tt.force {
|
|
steps = append(steps, testdb.Step{Match: "SELECT COUNT(*) as cnt FROM subscribe_message_logs", Args: []any{int64(9), int64(2)}, Columns: []string{"cnt"}, Rows: [][]driver.Value{{tt.count}}})
|
|
}
|
|
db, _ := testdb.Open(t, steps)
|
|
sender := &testSender{}
|
|
Once(&store.DB{DB: db}, sender, tt.force)
|
|
if sender.calls != tt.want {
|
|
t.Fatalf("sent %d want %d", sender.calls, tt.want)
|
|
}
|
|
if sender.calls > 0 && (sender.to != 2 || sender.from != 1) {
|
|
t.Fatalf("message must go to partner: from=%d to=%d", sender.from, sender.to)
|
|
}
|
|
})
|
|
}
|
|
}
|