The site could only reach somebody who was already looking at it. Push existed and adventure used none of it, so the one communal event in the game -- the Siege -- was invisible to anyone not sitting in Matrix, and a player whose adventurer died found out whenever they next opened a tab. Four opt-in categories, every one of them off until asked for: the Siege (realm-wide, begins and ends), your expedition ending, your adventurer wandering off, and a contract landing on you. Turning on news notifications is not consent to be told about the game, so nothing here enrolls anybody automatically. No new wire. Every trigger is a dispatch already landing in adventure_events, so this is Pete-side only and gogobee is untouched. Two things it needed from storage. push_subscriptions now keeps the Matrix localpart alongside the OIDC subject, because every ownership join in the schema is keyed on the localpart and the sender runs on a ticker with no session to read one from -- without it there is no way to answer "whose adventurer is this". And the alerts carry their own watermark, kept apart from the digest's: the two run on different clocks and one column would let each consume the other's backlog. The ownership join is re-read on every pass rather than trusted from the subscription row, so an opt-out or a removal closes the channel at once. It fails closed in both directions, and an unresolved owner can never fall through to a broadcast -- a game alert naming somebody's adventurer, delivered to the wrong phone, is a privacy leak dressed as a feature. An existing subscription carries watermark 0, which read literally means "has never been told anything" and would page every subscriber for the whole history of the realm on the first tick after deploy. Those rows are stamped to now and start from the next dispatch. Verified against a running Pete with a real push service, real P-256 client keys and real encryption: the right person is notified, the wrong one is not, a second pass is silent, and dropping the player from the board takes the channel with it. Claude-Session: https://claude.ai/code/session_012bxpQQJDjC1mTtLN3VVtBQ
106 lines
3.2 KiB
Go
106 lines
3.2 KiB
Go
package storage
|
|
|
|
import "testing"
|
|
|
|
func TestPushSubscriptionLifecycle(t *testing.T) {
|
|
setupTestDB(t)
|
|
|
|
if err := AddPushSubscription("sub-1", "josie", "https://push.example/ep-a", "p256-a", "auth-a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// A second endpoint for the same user (e.g. a second device).
|
|
if err := AddPushSubscription("sub-1", "josie", "https://push.example/ep-b", "p256-b", "auth-b"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// A different user.
|
|
if err := AddPushSubscription("sub-2", "quack", "https://push.example/ep-c", "p256-c", "auth-c"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
subs, err := ListPushSubscriptions()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(subs) != 3 {
|
|
t.Fatalf("got %d subscriptions, want 3", len(subs))
|
|
}
|
|
|
|
// Re-subscribing the same endpoint updates keys in place, not a new row.
|
|
if err := AddPushSubscription("sub-1", "josie", "https://push.example/ep-a", "p256-a2", "auth-a2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
subs, _ = ListPushSubscriptions()
|
|
if len(subs) != 3 {
|
|
t.Fatalf("after re-subscribe got %d rows, want 3 (upsert, not insert)", len(subs))
|
|
}
|
|
var epA PushSubscription
|
|
for _, s := range subs {
|
|
if s.Endpoint == "https://push.example/ep-a" {
|
|
epA = s
|
|
}
|
|
}
|
|
if epA.P256dh != "p256-a2" || epA.Auth != "auth-a2" {
|
|
t.Errorf("re-subscribe did not refresh keys: %+v", epA)
|
|
}
|
|
|
|
if err := RemovePushSubscription("https://push.example/ep-a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
subs, _ = ListPushSubscriptions()
|
|
if len(subs) != 2 {
|
|
t.Fatalf("after remove got %d rows, want 2", len(subs))
|
|
}
|
|
}
|
|
|
|
func TestTouchPushSubscriptionAdvancesWatermark(t *testing.T) {
|
|
setupTestDB(t)
|
|
if err := AddPushSubscription("sub-1", "josie", "https://push.example/ep", "p", "a"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
subs, _ := ListPushSubscriptions()
|
|
orig := subs[0].LastNotifiedAt
|
|
|
|
want := orig + 5000
|
|
if err := TouchPushSubscription("https://push.example/ep", want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
subs, _ = ListPushSubscriptions()
|
|
if subs[0].LastNotifiedAt != want {
|
|
t.Errorf("watermark = %d, want %d", subs[0].LastNotifiedAt, want)
|
|
}
|
|
}
|
|
|
|
func TestNewClassifiedSince(t *testing.T) {
|
|
setupTestDB(t)
|
|
now := nowUnix()
|
|
|
|
insert := func(guid, headline, source, channel string, classified bool, seenAt int64) {
|
|
if err := InsertStory(&Story{
|
|
GUID: guid, Headline: headline, ArticleURL: "https://x/" + guid,
|
|
Source: source, Channel: channel, Classified: classified, SeenAt: seenAt,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
insert("old", "Old news", "Feed A", "tech", true, now-1000)
|
|
insert("fresh1", "Fresh one", "Feed A", "tech", true, now-100)
|
|
insert("fresh2", "Fresh two", "Feed B", "gaming", true, now-50)
|
|
insert("unclassified", "Pending", "Feed A", "", false, now-10)
|
|
insert("discarded", "Junk", "Feed A", "_discarded", true, now-10)
|
|
|
|
got, err := NewClassifiedSince(now-500, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 2 {
|
|
t.Fatalf("got %d stories, want 2 (fresh classified, non-discarded, after watermark)", len(got))
|
|
}
|
|
// Newest first.
|
|
if got[0].GUID != "" && got[0].Headline != "Fresh two" {
|
|
t.Errorf("first result = %q, want newest 'Fresh two'", got[0].Headline)
|
|
}
|
|
if got[0].Source != "Feed B" || got[1].Source != "Feed A" {
|
|
t.Errorf("unexpected order/sources: %q then %q", got[0].Source, got[1].Source)
|
|
}
|
|
}
|