Phase 2: money
Lightning payments through LNbits, wallets, passes, credits, the entitlement resolver and priority lanes. - internal/entitle: pure resolver (one-off, own pass, group pass, group credits, own credits, free) with a table-driven suite and an invariant check over every input combination - internal/billing: wallets, append-only ledger with dedupe keys, stacking passes, purchases, gift codes, referral reward - LNbits client verified against v0.12.12 and v1.7.1 source - Settlement by one conditional UPDATE: idempotent under any mix of webhooks, polls and the check button. Webhooks are unsigned, so every payment is confirmed with LNbits before it is credited - Polling fallback, reconciliation of all unsettled invoices at startup, late payments still honored, no expiry while LNbits is unreachable - Charge, quota and job are written in one transaction; failed, cancelled and undeliverable downloads are refunded exactly once - Paid lane first with one slot of the fleet reserved for free users - Daily download quota and daily volume cap, per-user and per-group rate limits - Bot: /buy /account /limits /credits /redeem /referral /donate, QR invoice message that flips to paid, over-limit offer (passes or this file only), deep links from groups, skip the queue - Fake LNbits server for tests; end-to-end money flows including a payment made while the core was down Cashu redemption is behind an interface and not implemented. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
3b361c17a1
commit
0d50f453a0
@@ -455,3 +455,167 @@ func TestWaitWorkWakesOnEnqueue(t *testing.T) {
|
||||
t.Fatal("WaitWork did not wake up on enqueue")
|
||||
}
|
||||
}
|
||||
|
||||
// claimCap claims as a worker that advertises the given capacity.
|
||||
func (f *fixture) claimCap(worker string, capacity int) *workerproto.Job {
|
||||
f.t.Helper()
|
||||
j, err := f.s.Claim(context.Background(), workerproto.ClaimRequest{WorkerID: worker, Capacity: capacity})
|
||||
if err != nil {
|
||||
f.t.Fatalf("claim: %v", err)
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
||||
func (f *fixture) finish(worker, jobID string) {
|
||||
f.t.Helper()
|
||||
if err := f.s.Complete(context.Background(), workerproto.CompleteRequest{WorkerID: worker, JobID: jobID, StorageKey: "k"}); err != nil {
|
||||
f.t.Fatalf("complete: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func wantClaim(t *testing.T, got *workerproto.Job, want *db.Job, what string) {
|
||||
t.Helper()
|
||||
switch {
|
||||
case want == nil && got != nil:
|
||||
t.Fatalf("%s: claimed %s, want nothing", what, got.ID)
|
||||
case want != nil && got == nil:
|
||||
t.Fatalf("%s: claimed nothing, want %s", what, want.ID)
|
||||
case want != nil && got.ID != want.ID:
|
||||
t.Fatalf("%s: claimed the wrong job", what)
|
||||
}
|
||||
}
|
||||
|
||||
// Acceptance: correct priority ordering with the reserved free slot.
|
||||
func TestPaidFirstWithOneSlotReservedForFree(t *testing.T) {
|
||||
f := newFixture(t, sched.Config{})
|
||||
paid := func(p *sched.EnqueueParams) { p.Lane = db.LanePaid }
|
||||
const w, slots = "w1", 3
|
||||
|
||||
free1, _ := f.enqueue(1)
|
||||
free2, _ := f.enqueue(2)
|
||||
paid1, _ := f.enqueue(11, paid)
|
||||
paid2, _ := f.enqueue(12, paid)
|
||||
paid3, _ := f.enqueue(13, paid)
|
||||
paid4, _ := f.enqueue(14, paid)
|
||||
|
||||
// Free jobs were queued first, yet paid jobs are served first.
|
||||
wantClaim(t, f.claimCap(w, slots), paid1, "slot 1")
|
||||
wantClaim(t, f.claimCap(w, slots), paid2, "slot 2")
|
||||
// The last idle slot is the reserved one: a free job waits and none
|
||||
// runs, so it goes to the free lane although paid jobs are waiting.
|
||||
wantClaim(t, f.claimCap(w, slots), free1, "slot 3 (reserved)")
|
||||
|
||||
// A paid job finishes. A free job is already running, so the
|
||||
// reservation is satisfied and the slot goes to the next paid job.
|
||||
f.finish(w, paid1.ID)
|
||||
wantClaim(t, f.claimCap(w, slots), paid3, "after paid1")
|
||||
|
||||
// The free job finishes. Its slot is again the last idle one with no
|
||||
// free job running, so the second free job is served before paid4.
|
||||
f.finish(w, free1.ID)
|
||||
wantClaim(t, f.claimCap(w, slots), free2, "after free1 (reserved)")
|
||||
|
||||
f.finish(w, paid2.ID)
|
||||
wantClaim(t, f.claimCap(w, slots), paid4, "after paid2")
|
||||
f.finish(w, paid3.ID)
|
||||
wantClaim(t, f.claimCap(w, slots), nil, "queue drained")
|
||||
}
|
||||
|
||||
func TestReservationCostsNothingWhenNoFreeJobWaits(t *testing.T) {
|
||||
f := newFixture(t, sched.Config{})
|
||||
paid := func(p *sched.EnqueueParams) { p.Lane = db.LanePaid }
|
||||
a, _ := f.enqueue(1, paid)
|
||||
b, _ := f.enqueue(2, paid)
|
||||
c, _ := f.enqueue(3, paid)
|
||||
// All three slots serve paid jobs, including the reserved one.
|
||||
wantClaim(t, f.claimCap("w", 3), a, "slot 1")
|
||||
wantClaim(t, f.claimCap("w", 3), b, "slot 2")
|
||||
wantClaim(t, f.claimCap("w", 3), c, "slot 3")
|
||||
}
|
||||
|
||||
func TestSingleSlotHasNoReservation(t *testing.T) {
|
||||
f := newFixture(t, sched.Config{})
|
||||
free1, _ := f.enqueue(1)
|
||||
paid1, _ := f.enqueue(2, func(p *sched.EnqueueParams) { p.Lane = db.LanePaid })
|
||||
// With one slot in total, reserving it would lock paying users out.
|
||||
wantClaim(t, f.claimCap("w", 1), paid1, "the only slot")
|
||||
f.finish("w", paid1.ID)
|
||||
wantClaim(t, f.claimCap("w", 1), free1, "then free")
|
||||
}
|
||||
|
||||
func TestReservationCountsTheWholeFleet(t *testing.T) {
|
||||
f := newFixture(t, sched.Config{})
|
||||
paid := func(p *sched.EnqueueParams) { p.Lane = db.LanePaid }
|
||||
free1, _ := f.enqueue(1)
|
||||
paid1, _ := f.enqueue(2, paid)
|
||||
paid2, _ := f.enqueue(3, paid)
|
||||
|
||||
// Two workers with one slot each: the fleet has two slots. Worker a
|
||||
// claims first; nobody knows about b yet, so capacity is 1 and paid
|
||||
// goes first. When b claims, the fleet is 2, one slot is busy, and the
|
||||
// last idle slot is reserved for the waiting free job.
|
||||
wantClaim(t, f.claimCap("a", 1), paid1, "worker a")
|
||||
wantClaim(t, f.claimCap("b", 1), free1, "worker b (reserved)")
|
||||
if got := f.s.Capacity(); got != 2 {
|
||||
t.Fatalf("fleet capacity = %d, want 2", got)
|
||||
}
|
||||
f.finish("a", paid1.ID)
|
||||
wantClaim(t, f.claimCap("a", 1), paid2, "worker a again")
|
||||
|
||||
// A worker that stopped claiming drops out of the capacity.
|
||||
f.advance(2 * time.Minute)
|
||||
f.claimCap("a", 1)
|
||||
if got := f.s.Capacity(); got != 1 {
|
||||
t.Fatalf("fleet capacity after b went away = %d, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromoteSkipsTheQueue(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
f := newFixture(t, sched.Config{})
|
||||
free1, _ := f.enqueue(1)
|
||||
free2, _ := f.enqueue(2)
|
||||
free3, _ := f.enqueue(3)
|
||||
|
||||
ok, err := f.s.Promote(ctx, free3.ID)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("promote = %v, %v", ok, err)
|
||||
}
|
||||
if pos, _ := f.s.Position(ctx, f.job(free3.ID), 1); pos.Ahead != 0 {
|
||||
t.Fatalf("promoted job has %d jobs ahead, want 0", pos.Ahead)
|
||||
}
|
||||
if pos, _ := f.s.Position(ctx, f.job(free1.ID), 1); pos.Ahead != 1 {
|
||||
t.Fatalf("first free job has %d jobs ahead, want 1", pos.Ahead)
|
||||
}
|
||||
wantClaim(t, f.claim("w"), free3, "promoted job first")
|
||||
wantClaim(t, f.claim("w"), free1, "then FIFO")
|
||||
|
||||
// Promoting twice, or promoting a job that is no longer waiting, is a
|
||||
// no-op that reports false.
|
||||
if ok, _ := f.s.Promote(ctx, free3.ID); ok {
|
||||
t.Fatal("a running job cannot be promoted")
|
||||
}
|
||||
if ok, _ := f.s.Promote(ctx, free2.ID); !ok {
|
||||
t.Fatal("a waiting free job can be promoted")
|
||||
}
|
||||
if ok, _ := f.s.Promote(ctx, free2.ID); ok {
|
||||
t.Fatal("a paid job cannot be promoted again")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPriorityIsFixedAtEnqueue(t *testing.T) {
|
||||
// The lane is stored with the job. Whatever happens to the pass that
|
||||
// paid for it while it waits, the job keeps its place.
|
||||
f := newFixture(t, sched.Config{})
|
||||
free1, _ := f.enqueue(1)
|
||||
paid1, _ := f.enqueue(2, func(p *sched.EnqueueParams) {
|
||||
p.Lane = db.LanePaid
|
||||
p.Source = "own_pass"
|
||||
})
|
||||
f.advance(48 * time.Hour) // far past any pass
|
||||
wantClaim(t, f.claim("w"), paid1, "paid job after its pass expired")
|
||||
wantClaim(t, f.claim("w"), free1, "then the free job")
|
||||
if d, _ := f.store.DeliveriesForJob(context.Background(), paid1.ID, db.DeliveryPending); len(d) != 1 || d[0].Source != "own_pass" {
|
||||
t.Fatalf("the delivery must remember what paid: %+v", d)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user