damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

InvoiceTests.swift (5872B)


      1 //
      2 //  InvoiceTests.swift
      3 //  damusTests
      4 //
      5 //  Created by William Casarin on 2022-10-18.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 
     12 extension Block {
     13     var asInvoice: Invoice? {
     14         switch self {
     15         case .invoice(let invoice):
     16             return invoice
     17         default:
     18             return nil
     19         }
     20     }
     21 }
     22 
     23 
     24 final class InvoiceTests: XCTestCase {
     25 
     26     override func setUpWithError() throws {
     27         // Put setup code here. This method is called before the invocation of each test method in the class.
     28     }
     29 
     30     override func tearDownWithError() throws {
     31         // Put teardown code here. This method is called after the invocation of each test method in the class.
     32     }
     33     
     34     func testParseAnyAmountInvoice() throws {
     35         let invstr = "LNBC1P3MR5UJSP5G7SA48YD4JWTTPCHWMY4QYN4UWZQCJQ8NMWKD6QE3HCRVYTDLH9SPP57YM9TSA9NN4M4XU59XMJCXKR7YDV29DDP6LVQUT46ZW6CU3KE9GQDQ9V9H8JXQ8P3MYLZJCQPJRZJQF60PZDVNGGQWQDNERZSQN35L8CVQ3QG2Z5NSZYD0D3Q0JW2TL6VUZA7FYQQWKGQQYQQQQLGQQQQXJQQ9Q9QXPQYSGQ39EM4QJMQFKZGJXZVGL7QJMYNSWA8PGDTAGXXRG5Z92M7VLCGKQK2L2THDF8LM0AUKAURH7FVAWDLRNMVF38W4EYJDNVN9V4Z9CRS5CQCV465C"
     36         let parsed = parse_note_content(content: .content(invstr,nil)).blocks
     37 
     38         XCTAssertNotNil(parsed)
     39         XCTAssertEqual(parsed.count, 1)
     40         let invoiceOrNil = parsed[0].asInvoice
     41         XCTAssertNotNil(invoiceOrNil)
     42         guard let invoice = invoiceOrNil else {
     43             return
     44         }
     45         XCTAssertEqual(invoice.amount, .any)
     46         //XCTAssertEqual(invoice.expiry, 604800)
     47         //XCTAssertEqual(invoice.created_at, 1666139119)
     48         XCTAssertEqual(invoice.string, invstr)
     49     }
     50     
     51     func testTextAfterInvoice() throws {
     52         let invstr = """
     53 LNBC1P3MR5UJSP5G7SA48YD4JWTTPCHWMY4QYN4UWZQCJQ8NMWKD6QE3HCRVYTDLH9SPP57YM9TSA9NN4M4XU59XMJCXKR7YDV29DDP6LVQUT46ZW6CU3KE9GQDQ9V9H8JXQ8P3MYLZJCQPJRZJQF60PZDVNGGQWQDNERZSQN35L8CVQ3QG2Z5NSZYD0D3Q0JW2TL6VUZA7FYQQWKGQQYQQQQLGQQQQXJQQ9Q9QXPQYSGQ39EM4QJMQFKZGJXZVGL7QJMYNSWA8PGDTAGXXRG5Z92M7VLCGKQK2L2THDF8LM0AUKAURH7FVAWDLRNMVF38W4EYJDNVN9V4Z9CRS5CQCV465C  hi there
     54 """
     55         let parsed = parse_note_content(content: .content(invstr,nil)).blocks
     56 
     57         XCTAssertNotNil(parsed)
     58         XCTAssertEqual(parsed.count, 2)
     59         let invoiceOrNil = parsed[0].asInvoice
     60         XCTAssertNotNil(invoiceOrNil)
     61         XCTAssertEqual(parsed[1].asText, "  hi there")
     62         guard let invoice = invoiceOrNil else {
     63             return
     64         }
     65         XCTAssertEqual(invoice.amount, .any)
     66         //XCTAssertEqual(invoice.expiry, 604800)
     67         //XCTAssertEqual(invoice.created_at, 1666139119)
     68     }
     69     
     70     func testParseInvoiceUpper() throws {
     71         let invstr = "LNBC100N1P357SL0SP5T9N56WDZTUN39LGDQLR30XQWKSG3K69Q4Q2RKR52APLUJW0ESN0QPP5MRQGLJK62Z20Q4NVGR6LZCYN6FHYLZCCWDVU4K77APG3ZMRKUJJQDPZW35XJUEQD9EJQCFQV3JHXCMJD9C8G6T0DCXQYJW5QCQPJRZJQT56H4GVP5YX36U2UZQA6QWCSK3E2DUUNFXPPZJ9VHYPC3WFE2WSWZ607UQQ3XQQQSQQQQQQQQQQQLQQYG9QYYSGQAGX5H20AEULJ3GDWX3KXS8U9F4MCAKDKWUAKASAMM9562FFYR9EN8YG20LG0YGNR9ZPWP68524KMDA0T5XP2WYTEX35PU8HAPYJAJXQPSQL29R"
     72         let parsed = parse_note_content(content: .content(invstr,nil)).blocks
     73 
     74         XCTAssertNotNil(parsed)
     75         XCTAssertEqual(parsed.count, 1)
     76         let invoiceOrNil = parsed[0].asInvoice
     77         XCTAssertNotNil(invoiceOrNil)
     78         guard let invoice = invoiceOrNil else {
     79             return
     80         }
     81         XCTAssertEqual(invoice.amount, .specific(10000))
     82         XCTAssertEqual(invoice.expiry, 604800)
     83         XCTAssertEqual(invoice.created_at, 1666139119)
     84         XCTAssertEqual(invoice.string, invstr)
     85     }
     86     
     87     func testParseInvoiceWithPrefix() throws {
     88         let invstr = "lightning:lnbc100n1p357sl0sp5t9n56wdztun39lgdqlr30xqwksg3k69q4q2rkr52aplujw0esn0qpp5mrqgljk62z20q4nvgr6lzcyn6fhylzccwdvu4k77apg3zmrkujjqdpzw35xjueqd9ejqcfqv3jhxcmjd9c8g6t0dcxqyjw5qcqpjrzjqt56h4gvp5yx36u2uzqa6qwcsk3e2duunfxppzj9vhypc3wfe2wswz607uqq3xqqqsqqqqqqqqqqqlqqyg9qyysgqagx5h20aeulj3gdwx3kxs8u9f4mcakdkwuakasamm9562ffyr9en8yg20lg0ygnr9zpwp68524kmda0t5xp2wytex35pu8hapyjajxqpsql29r"
     89         let parsed = parse_note_content(content: .content(invstr,nil)).blocks
     90 
     91         XCTAssertNotNil(parsed)
     92         XCTAssertEqual(parsed.count, 1)
     93         XCTAssertNotNil(parsed[0].asInvoice)
     94     }
     95     
     96     func testParseInvoiceWithPrefixCapitalized() throws {
     97         let invstr = "LIGHTNING:LNBC100N1P357SL0SP5T9N56WDZTUN39LGDQLR30XQWKSG3K69Q4Q2RKR52APLUJW0ESN0QPP5MRQGLJK62Z20Q4NVGR6LZCYN6FHYLZCCWDVU4K77APG3ZMRKUJJQDPZW35XJUEQD9EJQCFQV3JHXCMJD9C8G6T0DCXQYJW5QCQPJRZJQT56H4GVP5YX36U2UZQA6QWCSK3E2DUUNFXPPZJ9VHYPC3WFE2WSWZ607UQQ3XQQQSQQQQQQQQQQQLQQYG9QYYSGQAGX5H20AEULJ3GDWX3KXS8U9F4MCAKDKWUAKASAMM9562FFYR9EN8YG20LG0YGNR9ZPWP68524KMDA0T5XP2WYTEX35PU8HAPYJAJXQPSQL29R"
     98         let parsed = parse_note_content(content: .content(invstr,nil)).blocks
     99 
    100         XCTAssertNotNil(parsed)
    101         XCTAssertEqual(parsed.count, 1)
    102         XCTAssertNotNil(parsed[0].asInvoice)
    103     }
    104     
    105     func testParseInvoice() throws {
    106         let invstr = "lnbc100n1p357sl0sp5t9n56wdztun39lgdqlr30xqwksg3k69q4q2rkr52aplujw0esn0qpp5mrqgljk62z20q4nvgr6lzcyn6fhylzccwdvu4k77apg3zmrkujjqdpzw35xjueqd9ejqcfqv3jhxcmjd9c8g6t0dcxqyjw5qcqpjrzjqt56h4gvp5yx36u2uzqa6qwcsk3e2duunfxppzj9vhypc3wfe2wswz607uqq3xqqqsqqqqqqqqqqqlqqyg9qyysgqagx5h20aeulj3gdwx3kxs8u9f4mcakdkwuakasamm9562ffyr9en8yg20lg0ygnr9zpwp68524kmda0t5xp2wytex35pu8hapyjajxqpsql29r"
    107         let parsed = parse_note_content(content: .content(invstr,nil)).blocks
    108 
    109         XCTAssertNotNil(parsed)
    110         XCTAssertEqual(parsed.count, 1)
    111         let invoiceOrNil = parsed[0].asInvoice
    112         XCTAssertNotNil(invoiceOrNil)
    113         guard let invoice = invoiceOrNil else {
    114             return
    115         }
    116         XCTAssertEqual(invoice.amount, .specific(10000))
    117         XCTAssertEqual(invoice.expiry, 604800)
    118         XCTAssertEqual(invoice.created_at, 1666139119)
    119         XCTAssertEqual(invoice.string, invstr)
    120     }
    121 
    122 }