gate_test.go raw
1 package listing
2
3 import (
4 "path/filepath"
5 "testing"
6 "time"
7 )
8
9 func quote(id string) Quote {
10 return Quote{ID: id, PaymentHash: "hash-" + id, Invoice: "lnbc-" + id, AmountSats: 500, FeeBPS: 50}
11 }
12
13 // TestNothingIsListedUntilTheInvoiceIsPaid is the whole point of the package.
14 func TestNothingIsListedUntilTheInvoiceIsPaid(t *testing.T) {
15 gate, err := Open(filepath.Join(t.TempDir(), "listings.json"))
16
17 if err != nil {
18 t.Fatal(err)
19 }
20
21 if gate.Lodged("swap-1") {
22 t.Fatal("an unknown swap was reported as paid for")
23 }
24
25 if err := gate.Record(quote("swap-1")); err != nil {
26 t.Fatal(err)
27 }
28
29 if gate.Lodged("swap-1") {
30 t.Fatal("a quoted but unpaid swap was reported as paid for")
31 }
32
33 if len(gate.Pending()) != 1 {
34 t.Fatalf("pending: %+v", gate.Pending())
35 }
36
37 if err := gate.Settle("swap-1", time.Now()); err != nil {
38 t.Fatal(err)
39 }
40
41 if !gate.Lodged("swap-1") {
42 t.Fatal("a settled swap is still not lodgable")
43 }
44
45 if len(gate.Pending()) != 0 {
46 t.Fatalf("a settled quote is still pending: %+v", gate.Pending())
47 }
48 }
49
50 // TestAPaidListingCannotBeUnpaid: re-quoting a paid listing must not reset it.
51 func TestAPaidListingCannotBeUnpaid(t *testing.T) {
52 gate, _ := Open(filepath.Join(t.TempDir(), "listings.json"))
53
54 if err := gate.Record(quote("swap-2")); err != nil {
55 t.Fatal(err)
56 }
57
58 if err := gate.Settle("swap-2", time.Now()); err != nil {
59 t.Fatal(err)
60 }
61
62 if err := gate.Record(quote("swap-2")); err == nil {
63 t.Fatal("a paid listing was re-quoted into an unpaid state")
64 }
65
66 if !gate.Lodged("swap-2") {
67 t.Fatal("the listing lost its paid state")
68 }
69 }
70
71 // TestQuotesSurviveARestart: a payment made before a restart still authorises the listing after it.
72 func TestQuotesSurviveARestart(t *testing.T) {
73 path := filepath.Join(t.TempDir(), "listings.json")
74
75 gate, _ := Open(path)
76
77 if err := gate.Record(quote("swap-3")); err != nil {
78 t.Fatal(err)
79 }
80
81 if err := gate.Settle("swap-3", time.Now()); err != nil {
82 t.Fatal(err)
83 }
84
85 reopened, err := Open(path)
86
87 if err != nil {
88 t.Fatal(err)
89 }
90
91 if !reopened.Lodged("swap-3") {
92 t.Fatal("a paid listing was lost across a restart")
93 }
94
95 if reopened.Quotes["swap-3"].AmountSats != 500 {
96 t.Fatalf("quote: %+v", reopened.Quotes["swap-3"])
97 }
98 }
99
100 // TestAQuoteNeedsAReference: without invoice and hash there is nothing to check a payment against.
101 func TestAQuoteNeedsAReference(t *testing.T) {
102 gate, _ := Open(filepath.Join(t.TempDir(), "listings.json"))
103
104 if err := gate.Record(Quote{ID: "swap-4"}); err == nil {
105 t.Fatal("a quote with no invoice was accepted")
106 }
107
108 if err := gate.Settle("nothing", time.Now()); err == nil {
109 t.Fatal("settling an unknown quote was accepted")
110 }
111 }
112