- Put verifies listings and keeps only the newest version per (author, id); equal seq resolves to the smaller hash so replicas converge - Tombstones are stored so old versions cannot be resurrected - Search by geohash prefix, kind, category, price, area, rooms - Expire removes listings and tombstones past their lifetime Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
322 lines
7.5 KiB
Go
322 lines
7.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.stinnesbeck.com/nils/allmende/internal/identity"
|
|
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
|
)
|
|
|
|
const difficulty = 8
|
|
|
|
var (
|
|
ctx = context.Background()
|
|
t0 = time.Unix(1_800_000_000, 0)
|
|
)
|
|
|
|
type env struct {
|
|
s *Store
|
|
now time.Time
|
|
key *identity.Identity
|
|
}
|
|
|
|
func newEnv(t *testing.T) *env {
|
|
t.Helper()
|
|
e := &env{now: t0}
|
|
e.key, _ = identity.Generate()
|
|
s, err := Open(":memory:", Options{Difficulty: difficulty, Clock: func() time.Time { return e.now }})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { s.Close() })
|
|
e.s = s
|
|
return e
|
|
}
|
|
|
|
func (e *env) listing(t *testing.T, mutate func(*listing.Listing)) *listing.Listing {
|
|
t.Helper()
|
|
id, _ := listing.NewID()
|
|
l := &listing.Listing{
|
|
Version: listing.Version,
|
|
ID: id,
|
|
Seq: 1,
|
|
CreatedAt: e.now.Unix(),
|
|
ExpiresAt: e.now.Add(7 * 24 * time.Hour).Unix(),
|
|
Kind: "rent",
|
|
Category: "apartment",
|
|
Geohash: "u1xjhx",
|
|
Price: &listing.Price{AmountCents: 100000, Currency: "EUR", Period: "month"},
|
|
AreaM2: 50,
|
|
RoomsX2: 4,
|
|
Title: "Wohnung",
|
|
Endpoint: "12D3KooWExamplePeerID",
|
|
}
|
|
if mutate != nil {
|
|
mutate(l)
|
|
}
|
|
if err := l.Seal(e.key, difficulty); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return l
|
|
}
|
|
|
|
func (e *env) put(t *testing.T, l *listing.Listing) Outcome {
|
|
t.Helper()
|
|
o, err := e.s.Put(ctx, l)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return o
|
|
}
|
|
|
|
func TestPutGet(t *testing.T) {
|
|
e := newEnv(t)
|
|
l := e.listing(t, nil)
|
|
if o := e.put(t, l); o != Inserted {
|
|
t.Fatalf("got %v want inserted", o)
|
|
}
|
|
got, err := e.s.Get(ctx, l.Author, l.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a, _ := l.Bytes()
|
|
b, _ := got.Bytes()
|
|
if string(a) != string(b) {
|
|
t.Fatal("stored listing differs from original")
|
|
}
|
|
if o := e.put(t, l); o != Ignored {
|
|
t.Fatalf("duplicate: got %v want ignored", o)
|
|
}
|
|
}
|
|
|
|
func TestRejectsInvalid(t *testing.T) {
|
|
e := newEnv(t)
|
|
l := e.listing(t, nil)
|
|
l.Price.AmountCents = 1 // breaks signature
|
|
_, err := e.s.Put(ctx, l)
|
|
if !errors.Is(err, ErrInvalid) {
|
|
t.Fatalf("got %v, want ErrInvalid", err)
|
|
}
|
|
if n, _ := e.s.Count(ctx); n != 0 {
|
|
t.Fatal("invalid listing was stored")
|
|
}
|
|
}
|
|
|
|
func TestHigherSeqWinsInAnyOrder(t *testing.T) {
|
|
e := newEnv(t)
|
|
v1 := e.listing(t, nil)
|
|
v2 := *v1
|
|
v2.Seq = 2
|
|
v2.Title = "Neuer Titel"
|
|
if err := v2.Seal(e.key, difficulty); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Newer first, older second: older must be ignored.
|
|
if o := e.put(t, &v2); o != Inserted {
|
|
t.Fatalf("v2: %v", o)
|
|
}
|
|
if o := e.put(t, v1); o != Ignored {
|
|
t.Fatalf("v1 after v2: got %v want ignored", o)
|
|
}
|
|
got, _ := e.s.Get(ctx, v1.Author, v1.ID)
|
|
if got.Title != "Neuer Titel" {
|
|
t.Fatalf("got %q", got.Title)
|
|
}
|
|
|
|
// Fresh store, natural order: second is a replacement.
|
|
e2 := newEnv(t)
|
|
e2.key = e.key
|
|
e2.put(t, v1)
|
|
if o := e2.put(t, &v2); o != Replaced {
|
|
t.Fatalf("got %v want replaced", o)
|
|
}
|
|
}
|
|
|
|
func TestEqualSeqConvergesOnSmallerHash(t *testing.T) {
|
|
e := newEnv(t)
|
|
a := e.listing(t, nil)
|
|
b := *a
|
|
b.Title = "Anderer Titel"
|
|
if err := b.Seal(e.key, difficulty); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ha, _ := a.Hash()
|
|
hb, _ := b.Hash()
|
|
winner, loser := a, &b
|
|
if hb < ha {
|
|
winner, loser = &b, a
|
|
}
|
|
|
|
for name, order := range map[string][]*listing.Listing{
|
|
"winner first": {winner, loser},
|
|
"loser first": {loser, winner},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
e2 := newEnv(t)
|
|
e2.key = e.key
|
|
for _, l := range order {
|
|
e2.put(t, l)
|
|
}
|
|
got, _ := e2.s.Get(ctx, a.Author, a.ID)
|
|
if got.Title != winner.Title {
|
|
t.Fatalf("converged on %q, want %q", got.Title, winner.Title)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTombstoneDeletesAndBlocksResurrection(t *testing.T) {
|
|
e := newEnv(t)
|
|
l := e.listing(t, nil)
|
|
e.put(t, l)
|
|
|
|
ts := &listing.Listing{
|
|
Version: listing.Version, ID: l.ID, Seq: l.Seq + 1,
|
|
CreatedAt: e.now.Unix(), ExpiresAt: e.now.Add(30 * 24 * time.Hour).Unix(),
|
|
Tombstone: true, Geohash: l.Geohash,
|
|
}
|
|
if err := ts.Seal(e.key, difficulty); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if o := e.put(t, ts); o != Replaced {
|
|
t.Fatalf("got %v want replaced", o)
|
|
}
|
|
if _, err := e.s.Get(ctx, l.Author, l.ID); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("deleted listing still readable: %v", err)
|
|
}
|
|
if o := e.put(t, l); o != Ignored {
|
|
t.Fatalf("old version resurrected: %v", o)
|
|
}
|
|
if n, _ := e.s.Count(ctx); n != 0 {
|
|
t.Fatalf("count %d, want 0", n)
|
|
}
|
|
|
|
// A tombstone arriving before the listing it deletes still wins.
|
|
e2 := newEnv(t)
|
|
e2.key = e.key
|
|
e2.put(t, ts)
|
|
if o := e2.put(t, l); o != Ignored {
|
|
t.Fatalf("listing after tombstone: %v", o)
|
|
}
|
|
}
|
|
|
|
func TestSameIDDifferentAuthorsDoNotCollide(t *testing.T) {
|
|
e := newEnv(t)
|
|
victim := e.listing(t, nil)
|
|
e.put(t, victim)
|
|
|
|
other, _ := identity.Generate()
|
|
squat := *victim
|
|
squat.Seq = 99
|
|
squat.Title = "Betrug"
|
|
if err := squat.Seal(other, difficulty); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.put(t, &squat)
|
|
|
|
got, err := e.s.Get(ctx, victim.Author, victim.ID)
|
|
if err != nil || got.Title != "Wohnung" {
|
|
t.Fatalf("victim listing disturbed: %v %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestSearch(t *testing.T) {
|
|
e := newEnv(t)
|
|
mk := func(geo, kind string, cents int64, area, rooms int) {
|
|
period := map[string]string{"rent": "month", "sale": "once"}[kind]
|
|
e.put(t, e.listing(t, func(l *listing.Listing) {
|
|
l.Geohash, l.Kind = geo, kind
|
|
l.Price = &listing.Price{AmountCents: cents, Currency: "EUR", Period: period}
|
|
l.AreaM2, l.RoomsX2 = area, rooms
|
|
}))
|
|
}
|
|
mk("u1xjhx", "rent", 90000, 40, 3)
|
|
mk("u1xjhz", "rent", 150000, 80, 6)
|
|
mk("u1xkbb", "rent", 120000, 60, 4)
|
|
mk("u1xjhx", "sale", 30000000, 90, 6)
|
|
mk("u30000", "rent", 100000, 50, 4) // different region
|
|
|
|
count := func(q Query) int {
|
|
t.Helper()
|
|
got, err := e.s.Search(ctx, q)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return len(got)
|
|
}
|
|
cases := []struct {
|
|
name string
|
|
q Query
|
|
want int
|
|
}{
|
|
{"all", Query{}, 5},
|
|
{"region prefix", Query{Geohashes: []string{"u1x"}}, 4},
|
|
{"narrow prefix", Query{Geohashes: []string{"u1xjh"}}, 3},
|
|
{"two prefixes", Query{Geohashes: []string{"u1xk", "u3"}}, 2},
|
|
{"rent in region", Query{Geohashes: []string{"u1x"}, Kind: "rent"}, 3},
|
|
{"price range", Query{Kind: "rent", MinPriceCents: 95000, MaxPriceCents: 130000}, 2},
|
|
{"min area", Query{Kind: "rent", MinAreaM2: 55}, 2},
|
|
{"min rooms", Query{MinRoomsX2: 6}, 2},
|
|
{"limit", Query{Limit: 2}, 2},
|
|
{"offset", Query{Offset: 4}, 1},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := count(tc.q); got != tc.want {
|
|
t.Fatalf("got %d want %d", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
|
|
if _, err := e.s.Search(ctx, Query{Geohashes: []string{"u1x' OR 1=1 --"}}); err == nil {
|
|
t.Fatal("invalid geohash prefix accepted")
|
|
}
|
|
}
|
|
|
|
func TestExpiry(t *testing.T) {
|
|
e := newEnv(t)
|
|
l := e.listing(t, nil)
|
|
e.put(t, l)
|
|
|
|
e.now = e.now.Add(8 * 24 * time.Hour)
|
|
if _, err := e.s.Get(ctx, l.Author, l.ID); !errors.Is(err, ErrNotFound) {
|
|
t.Fatal("expired listing still readable")
|
|
}
|
|
if got, _ := e.s.Search(ctx, Query{}); len(got) != 0 {
|
|
t.Fatal("expired listing in search results")
|
|
}
|
|
n, err := e.s.Expire(ctx)
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("Expire = %d, %v; want 1", n, err)
|
|
}
|
|
}
|
|
|
|
func TestPersistence(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "allmende.db")
|
|
key, _ := identity.Generate()
|
|
opt := Options{Difficulty: difficulty, Clock: func() time.Time { return t0 }}
|
|
|
|
s, err := Open(path, opt)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e := &env{s: s, now: t0, key: key}
|
|
l := e.listing(t, nil)
|
|
e.put(t, l)
|
|
s.Close()
|
|
|
|
s, err = Open(path, opt)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
if _, err := s.Get(ctx, l.Author, l.ID); err != nil {
|
|
t.Fatalf("listing lost after reopen: %v", err)
|
|
}
|
|
}
|