package main import ( "testing" "time" "git.smesh.lol/nostrino/pkg/billing" "git.smesh.lol/nostrino/pkg/nwc" ) const gw = "8964ed8cae7cb41286f9dc56344154f9491eace55ad6a18d7fb025ac02ec18a7" // TestAPaymentCreditsItsGateway: the preimage names the gateway and the amount buys time at the // relay's rate. An ordinary invoice payment from a person is not a subscription. func TestAPaymentCreditsItsGateway(t *testing.T) { now := time.Unix(1_800_000_000, 0) ledger, err := billing.OpenLedger(t.TempDir()+"/ledger.json", 1000) if err != nil { t.Fatal(err) } payments := []nwc.Transaction{ {Type: "incoming", Preimage: gw, AmountSats: 2000, SettledAt: 100}, {Type: "incoming", Invoice: "lnbc1…", AmountSats: 5000, SettledAt: 101}, } if err := credit(ledger, payments, now); err != nil { t.Fatal(err) } if got := ledger.Subscriptions[gw].TotalSats; got != 2000 { t.Fatalf("gateway credited %d sats, want 2000", got) } if !ledger.Paid(gw, now) { t.Fatal("the gateway is not paid after its payment") } if len(ledger.Subscriptions) != 1 { t.Fatalf("an invoice payment was treated as a subscription: %+v", ledger.Subscriptions) } // Scanning again must not buy more time. if err := credit(ledger, payments, now); err != nil { t.Fatal(err) } if got := ledger.Subscriptions[gw].TotalSats; got != 2000 { t.Fatalf("the same payment was credited twice: %d", got) } } // TestTheRateComesFromTheRelay: the relay sets the price; the sidecar follows it rather than // overwriting it with its own flag. func TestTheRateComesFromTheRelay(t *testing.T) { path := t.TempDir() + "/ledger.json" now := time.Unix(1_800_000_000, 0) relay, _ := billing.OpenLedger(path, 5000) if _, err := relay.Credit(gw, 5000, 1, now); err != nil { t.Fatal(err) } if err := relay.Save(); err != nil { t.Fatal(err) } sidecar, err := billing.OpenLedger(path, 1000) if err != nil { t.Fatal(err) } if sidecar.RateSatsPerDay != 5000 { t.Fatalf("the sidecar overrode the relay's rate: %d", sidecar.RateSatsPerDay) } }