package listing import ( "path/filepath" "testing" "time" ) func quote(id string) Quote { return Quote{ID: id, PaymentHash: "hash-" + id, Invoice: "lnbc-" + id, AmountSats: 500, FeeBPS: 50} } // TestNothingIsListedUntilTheInvoiceIsPaid is the whole point of the package. func TestNothingIsListedUntilTheInvoiceIsPaid(t *testing.T) { gate, err := Open(filepath.Join(t.TempDir(), "listings.json")) if err != nil { t.Fatal(err) } if gate.Lodged("swap-1") { t.Fatal("an unknown swap was reported as paid for") } if err := gate.Record(quote("swap-1")); err != nil { t.Fatal(err) } if gate.Lodged("swap-1") { t.Fatal("a quoted but unpaid swap was reported as paid for") } if len(gate.Pending()) != 1 { t.Fatalf("pending: %+v", gate.Pending()) } if err := gate.Settle("swap-1", time.Now()); err != nil { t.Fatal(err) } if !gate.Lodged("swap-1") { t.Fatal("a settled swap is still not lodgable") } if len(gate.Pending()) != 0 { t.Fatalf("a settled quote is still pending: %+v", gate.Pending()) } } // TestAPaidListingCannotBeUnpaid: re-quoting a paid listing must not reset it. func TestAPaidListingCannotBeUnpaid(t *testing.T) { gate, _ := Open(filepath.Join(t.TempDir(), "listings.json")) if err := gate.Record(quote("swap-2")); err != nil { t.Fatal(err) } if err := gate.Settle("swap-2", time.Now()); err != nil { t.Fatal(err) } if err := gate.Record(quote("swap-2")); err == nil { t.Fatal("a paid listing was re-quoted into an unpaid state") } if !gate.Lodged("swap-2") { t.Fatal("the listing lost its paid state") } } // TestQuotesSurviveARestart: a payment made before a restart still authorises the listing after it. func TestQuotesSurviveARestart(t *testing.T) { path := filepath.Join(t.TempDir(), "listings.json") gate, _ := Open(path) if err := gate.Record(quote("swap-3")); err != nil { t.Fatal(err) } if err := gate.Settle("swap-3", time.Now()); err != nil { t.Fatal(err) } reopened, err := Open(path) if err != nil { t.Fatal(err) } if !reopened.Lodged("swap-3") { t.Fatal("a paid listing was lost across a restart") } if reopened.Quotes["swap-3"].AmountSats != 500 { t.Fatalf("quote: %+v", reopened.Quotes["swap-3"]) } } // TestAQuoteNeedsAReference: without invoice and hash there is nothing to check a payment against. func TestAQuoteNeedsAReference(t *testing.T) { gate, _ := Open(filepath.Join(t.TempDir(), "listings.json")) if err := gate.Record(Quote{ID: "swap-4"}); err == nil { t.Fatal("a quote with no invoice was accepted") } if err := gate.Settle("nothing", time.Now()); err == nil { t.Fatal("settling an unknown quote was accepted") } }