Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
236 lines
8.0 KiB
Go
236 lines
8.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/ordoschnellic"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func queuedPushFixture(t *testing.T, focused bool) (*Server, store.Notification, time.Time) {
|
|
t.Helper()
|
|
s, token, domain, owner := taskFixture(t)
|
|
sub := testPushSubscription(t)
|
|
raw, err := json.Marshal(storedBrowserPush{Subscription: sub, Origin: "https://wiki.example"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.store.SaveNotificationPush(context.Background(), domain, owner, token, sub.Endpoint, string(raw), focused); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tx, err := s.store.DB.Begin()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer tx.Rollback()
|
|
n, _, err := s.store.CreateNotificationTx(tx, store.Notification{DomainUUID: domain, RecipientUUID: owner, EventUUID: uuid.NewString(), Kind: "task.due", Message: "synthetic private fixture"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s, n, time.Now()
|
|
}
|
|
|
|
func TestOrdoschnellicQueueLeaseAndStaleCompletion(t *testing.T) {
|
|
s, _, at := queuedPushFixture(t, false)
|
|
ctx := context.Background()
|
|
var wg sync.WaitGroup
|
|
claims := make(chan *store.NotificationPushDelivery, 8)
|
|
errs := make(chan error, 8)
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
go func() { defer wg.Done(); d, err := s.store.ClaimNotificationPushAt(ctx, at); claims <- d; errs <- err }()
|
|
}
|
|
wg.Wait()
|
|
close(claims)
|
|
close(errs)
|
|
for err := range errs {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var first *store.NotificationPushDelivery
|
|
count := 0
|
|
for d := range claims {
|
|
if d != nil {
|
|
first = d
|
|
count++
|
|
}
|
|
}
|
|
if count != 1 || first.Attempt != 1 {
|
|
t.Fatalf("claims=%d first=%+v", count, first)
|
|
}
|
|
if d, err := s.store.ClaimNotificationPushAt(ctx, at.Add(ordoschnellic.PushLeaseDuration-time.Millisecond)); err != nil || d != nil {
|
|
t.Fatal("lease not held", d, err)
|
|
}
|
|
second, err := s.store.ClaimNotificationPushAt(ctx, at.Add(ordoschnellic.PushLeaseDuration))
|
|
if err != nil || second == nil || second.Attempt != 2 {
|
|
t.Fatal("lease recovery", second, err)
|
|
}
|
|
if err = s.store.FinishNotificationPushAt(ctx, first, false, false, at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var attempt int
|
|
if err = s.store.DB.QueryRow(`SELECT attempts FROM notification_push_queue`).Scan(&attempt); err != nil || attempt != 2 {
|
|
t.Fatal("stale completion removed current claim", attempt, err)
|
|
}
|
|
if err = s.store.FinishNotificationPushAt(ctx, second, true, false, at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var next, lease int64
|
|
if err = s.store.DB.QueryRow(`SELECT next_at_ms,lease_until_ms FROM notification_push_queue`).Scan(&next, &lease); err != nil || next != at.Add(4*time.Minute).UnixMilli() || lease != 0 {
|
|
t.Fatal("retry state", next, lease, err)
|
|
}
|
|
}
|
|
|
|
func TestOrdoschnellicDispatcherDatabaseOutcomes(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
code int
|
|
focused bool
|
|
queue, subscription, attempts int
|
|
}{{"sent", 201, false, 0, 1, 0}, {"retry", 429, false, 1, 1, 1}, {"expired", 410, false, 0, 0, 0}, {"focused", 201, true, 1, 1, 0}} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
s, n, at := queuedPushFixture(t, test.focused)
|
|
sends := 0
|
|
client := pushHTTPFixture(func(*http.Request) (*http.Response, error) {
|
|
sends++
|
|
return &http.Response{StatusCode: test.code, Body: io.NopCloser(strings.NewReader(""))}, nil
|
|
})
|
|
stats, err := (ordoschnellic.Dispatcher{Queue: notificationDeliveryQueue{s.store}, Sender: ordoschnellic.WebPush{Keys: s.notificationPushKeys, Client: client}, Now: func() time.Time { return at }}).Run(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var queue, subscription, inbox int
|
|
for _, query := range []struct {
|
|
sql string
|
|
out *int
|
|
}{{`SELECT COUNT(*) FROM notification_push_queue`, &queue}, {`SELECT COUNT(*) FROM notification_push_subscriptions`, &subscription}, {`SELECT COUNT(*) FROM notification_inbox WHERE uuid=?`, &inbox}} {
|
|
args := []any{}
|
|
if query.out == &inbox {
|
|
args = append(args, n.UUID)
|
|
}
|
|
if err = s.store.DB.QueryRow(query.sql, args...).Scan(query.out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if queue != test.queue || subscription != test.subscription || inbox != 1 {
|
|
t.Fatalf("queue=%d subscription=%d inbox=%d", queue, subscription, inbox)
|
|
}
|
|
if test.focused {
|
|
if sends != 0 || stats != (ordoschnellic.Stats{}) {
|
|
t.Fatal("foreground sent", sends, stats)
|
|
}
|
|
} else if sends != 1 || stats.Sent+stats.Failed != 1 {
|
|
t.Fatal(sends, stats)
|
|
}
|
|
if queue > 0 {
|
|
var attempts int
|
|
var next int64
|
|
if err = s.store.DB.QueryRow(`SELECT attempts,next_at_ms FROM notification_push_queue`).Scan(&attempts, &next); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
delay := 2 * time.Minute
|
|
if test.focused {
|
|
delay = time.Minute
|
|
}
|
|
if attempts != test.attempts || next != at.Add(delay).UnixMilli() {
|
|
t.Fatal("reschedule", attempts, next)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrdoschnellicHostCancellationAndTransport(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
// No store/job is required: cancellation must prevent all persistence and I/O.
|
|
if err := (&Server{}).deliverNotificationPushContext(ctx, nil); !errors.Is(err, context.Canceled) {
|
|
t.Fatal(err)
|
|
}
|
|
client := notificationDeliveryClient()
|
|
if client.Timeout != 5*time.Second || client.Transport == nil || client.CheckRedirect(nil, nil) != http.ErrUseLastResponse {
|
|
t.Fatal("unsafe push HTTP defaults")
|
|
}
|
|
}
|
|
|
|
func TestOrdoschnellicEligibility(t *testing.T) {
|
|
for _, test := range []struct{ name, sql string }{
|
|
{"session", `UPDATE auth_sessions SET revoked_at_ms=1`},
|
|
{"principal", `UPDATE principals SET state='suspended'`},
|
|
{"membership", `UPDATE domain_memberships SET state='suspended'`},
|
|
{"domain", `UPDATE domains SET enabled=0`},
|
|
{"read", `UPDATE notification_inbox SET status='read'`},
|
|
{"recipient", `UPDATE notification_inbox SET recipient_uuid='other-owner'`},
|
|
{"scope", `UPDATE notification_inbox SET domain_uuid='other-domain'`},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
s, _, at := queuedPushFixture(t, false)
|
|
if _, err := s.store.DB.Exec(test.sql); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d, err := s.store.ClaimNotificationPushAt(context.Background(), at)
|
|
if err != nil || d == nil || d.Subscription != "" {
|
|
t.Fatalf("ineligible claim exposed delivery %+v %v", d, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOrdoschnellicCleanupBoundedAndInboxPreserved(t *testing.T) {
|
|
s, n, at := queuedPushFixture(t, false)
|
|
tx, err := s.store.DB.Begin()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer tx.Rollback()
|
|
for i := 0; i < 105; i++ {
|
|
_, err = tx.Exec(`INSERT INTO notification_push_subscriptions(id,domain_uuid,owner_uuid,session_hash,subscription,expires_at_ms) VALUES(?,?,?,?,?,?)`, fmt.Sprintf("expired-%d", i), n.DomainUUID, n.RecipientUUID, "synthetic-session", "{}", at.Add(-time.Hour).UnixMilli())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for i := 0; i < 505; i++ {
|
|
_, err = tx.Exec(`INSERT INTO notification_push_queue(subscription_id,notification_uuid,created_at_ms,next_at_ms) VALUES(?,?,?,0)`, "expired-0", fmt.Sprintf("old-%d", i), at.Add(-25*time.Hour).UnixMilli())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.store.CleanupNotificationPush(context.Background(), at); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, query := range []struct {
|
|
sql string
|
|
want int
|
|
}{
|
|
{`SELECT COUNT(*) FROM notification_push_subscriptions`, 6},
|
|
{`SELECT COUNT(*) FROM notification_push_queue`, 6},
|
|
{`SELECT COUNT(*) FROM notification_inbox`, 1},
|
|
} {
|
|
var count int
|
|
if err = s.store.DB.QueryRow(query.sql).Scan(&count); err != nil || count != query.want {
|
|
t.Fatalf("%s: count=%d want=%d err=%v", query.sql, count, query.want, err)
|
|
}
|
|
}
|
|
s.store.ReadOnly = true
|
|
if err = s.store.CleanupNotificationPush(context.Background(), at); !errors.Is(err, store.ErrReadOnly) {
|
|
t.Fatal("readonly cleanup", err)
|
|
}
|
|
}
|