--region takes a place name as well as a code ("Bonn", "Köln", "cologne",
"name:dub" to force a name that looks like a code). The node uses the same
GeoNames list as the browser (internal/places), follows the place's cell plus
any cell within 40 km, and prints what each name became.
The node reminds the owner on its terminal, once a day per listing, about
every listing with 5 days or less left (the UI marks the same range). The
sign-in token is never in the line.
scripts/e2e/ui.ts drives the real bundle in jsdom against real nodes: a buyer
writes from a listing page, the owner reads and answers in their own page and
the buyer's page shows the verified answer; a node with no region gets its
first one by place name in the browser and keeps it over a restart. e2e.sh
also checks --region by name and the reminder, and keeps its scratch files in
the checkout's .tmp.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
111 lines
3.4 KiB
Go
111 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
|
)
|
|
|
|
func testReminder(now *time.Time, ls ...*listing.Listing) (*reminder, *bytes.Buffer) {
|
|
var out bytes.Buffer
|
|
log := slog.New(slog.NewTextHandler(&out, &slog.HandlerOptions{ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
|
|
if a.Key == slog.TimeKey {
|
|
return slog.Attr{}
|
|
}
|
|
return a
|
|
}}))
|
|
return &reminder{
|
|
list: func(context.Context) ([]*listing.Listing, error) { return ls, nil },
|
|
log: log,
|
|
httpAddr: "0.0.0.0:8080",
|
|
now: func() time.Time { return *now },
|
|
said: map[string]time.Time{},
|
|
}, &out
|
|
}
|
|
|
|
func ends(id, title string, in time.Duration, now time.Time) *listing.Listing {
|
|
return &listing.Listing{ID: id, Title: title, ExpiresAt: now.Add(in).Unix()}
|
|
}
|
|
|
|
func TestOnlyListingsThatRunOutSoonAreMentioned(t *testing.T) {
|
|
now := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC)
|
|
r, out := testReminder(&now,
|
|
ends("a", "Far away", 20*24*time.Hour, now),
|
|
ends("b", "Soon", 3*24*time.Hour, now),
|
|
ends("c", "Edge", remindWithin, now),
|
|
ends("d", "Already over", -time.Hour, now),
|
|
)
|
|
if n := r.check(context.Background()); n != 2 {
|
|
t.Fatalf("mentioned %d, want 2:\n%s", n, out)
|
|
}
|
|
got := out.String()
|
|
for _, want := range []string{"level=WARN", "listing=Soon", "left=\"3 days\"", "listing=Edge", "left=\"5 days\"",
|
|
"renew=http://127.0.0.1:8080/#/mine"} { // a wildcard bind opens on loopback
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("missing %q in:\n%s", want, got)
|
|
}
|
|
}
|
|
for _, no := range []string{"Far away", "Already over", "token", "login"} {
|
|
if strings.Contains(got, no) {
|
|
t.Errorf("%q must not appear:\n%s", no, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAListingIsMentionedOncePerDayNotOnEveryCheck(t *testing.T) {
|
|
now := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC)
|
|
r, _ := testReminder(&now, ends("b", "Soon", 4*24*time.Hour, now))
|
|
ls := func() int { return r.check(context.Background()) }
|
|
if ls() != 1 {
|
|
t.Fatal("the first check must mention it")
|
|
}
|
|
now = now.Add(time.Hour)
|
|
if ls() != 0 {
|
|
t.Fatal("an hour later it must stay quiet")
|
|
}
|
|
now = now.Add(remindAgain)
|
|
if ls() != 1 {
|
|
t.Fatal("a day later it must say it again")
|
|
}
|
|
}
|
|
|
|
func TestARenewedListingIsNotMentioned(t *testing.T) {
|
|
now := time.Date(2026, 11, 18, 12, 0, 0, 0, time.UTC)
|
|
l := ends("b", "Soon", 2*24*time.Hour, now)
|
|
r, _ := testReminder(&now, l)
|
|
if r.check(context.Background()) != 1 {
|
|
t.Fatal("expected a reminder")
|
|
}
|
|
l.ExpiresAt = now.Add(30 * 24 * time.Hour).Unix() // renewed in the browser
|
|
now = now.Add(2 * remindAgain)
|
|
if r.check(context.Background()) != 0 {
|
|
t.Fatal("a renewed listing must not be mentioned")
|
|
}
|
|
}
|
|
|
|
func TestAFailingStoreIsNotAReminder(t *testing.T) {
|
|
now := time.Now()
|
|
r, out := testReminder(&now)
|
|
r.list = func(context.Context) ([]*listing.Listing, error) { return nil, errors.New("db closed") }
|
|
if r.check(context.Background()) != 0 || strings.Contains(out.String(), "WARN") {
|
|
t.Fatalf("a read error is not a warning to the owner:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestTimeLeftReadsLikeAPerson(t *testing.T) {
|
|
for d, want := range map[time.Duration]string{
|
|
5 * 24 * time.Hour: "5 days", 49 * time.Hour: "2 days", 36 * time.Hour: "2 days",
|
|
30 * time.Hour: "30 hours", 3 * time.Hour: "3 hours", 20 * time.Minute: "less than an hour",
|
|
} {
|
|
if got := leftText(d); got != want {
|
|
t.Errorf("%v: %q, want %q", d, got, want)
|
|
}
|
|
}
|
|
}
|