main_test.go raw
1 package main
2
3 import (
4 "testing"
5 "time"
6
7 "git.smesh.lol/nostrino/pkg/billing"
8 "git.smesh.lol/nostrino/pkg/nwc"
9 )
10
11 const gw = "8964ed8cae7cb41286f9dc56344154f9491eace55ad6a18d7fb025ac02ec18a7"
12
13 // TestAPaymentCreditsItsGateway: the preimage names the gateway and the amount buys time at the
14 // relay's rate. An ordinary invoice payment from a person is not a subscription.
15 func TestAPaymentCreditsItsGateway(t *testing.T) {
16 now := time.Unix(1_800_000_000, 0)
17
18 ledger, err := billing.OpenLedger(t.TempDir()+"/ledger.json", 1000)
19
20 if err != nil {
21 t.Fatal(err)
22 }
23
24 payments := []nwc.Transaction{
25 {Type: "incoming", Preimage: gw, AmountSats: 2000, SettledAt: 100},
26 {Type: "incoming", Invoice: "lnbc1…", AmountSats: 5000, SettledAt: 101},
27 }
28
29 if err := credit(ledger, payments, now); err != nil {
30 t.Fatal(err)
31 }
32
33 if got := ledger.Subscriptions[gw].TotalSats; got != 2000 {
34 t.Fatalf("gateway credited %d sats, want 2000", got)
35 }
36
37 if !ledger.Paid(gw, now) {
38 t.Fatal("the gateway is not paid after its payment")
39 }
40
41 if len(ledger.Subscriptions) != 1 {
42 t.Fatalf("an invoice payment was treated as a subscription: %+v", ledger.Subscriptions)
43 }
44
45 // Scanning again must not buy more time.
46 if err := credit(ledger, payments, now); err != nil {
47 t.Fatal(err)
48 }
49
50 if got := ledger.Subscriptions[gw].TotalSats; got != 2000 {
51 t.Fatalf("the same payment was credited twice: %d", got)
52 }
53 }
54
55 // TestTheRateComesFromTheRelay: the relay sets the price; the sidecar follows it rather than
56 // overwriting it with its own flag.
57 func TestTheRateComesFromTheRelay(t *testing.T) {
58 path := t.TempDir() + "/ledger.json"
59 now := time.Unix(1_800_000_000, 0)
60
61 relay, _ := billing.OpenLedger(path, 5000)
62
63 if _, err := relay.Credit(gw, 5000, 1, now); err != nil {
64 t.Fatal(err)
65 }
66
67 if err := relay.Save(); err != nil {
68 t.Fatal(err)
69 }
70
71 sidecar, err := billing.OpenLedger(path, 1000)
72
73 if err != nil {
74 t.Fatal(err)
75 }
76
77 if sidecar.RateSatsPerDay != 5000 {
78 t.Fatalf("the sidecar overrode the relay's rate: %d", sidecar.RateSatsPerDay)
79 }
80 }
81