damus

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

ListTests.swift (3345B)


      1 //
      2 //  ListTests.swift
      3 //  damusTests
      4 //
      5 //  Created by William Casarin on 2023-01-25.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 final class ListTests: XCTestCase {
     12 
     13     override func setUpWithError() throws {
     14         // Put setup code here. This method is called before the invocation of each test method in the class.
     15     }
     16 
     17     override func tearDownWithError() throws {
     18         // Put teardown code here. This method is called after the invocation of each test method in the class.
     19     }
     20 
     21     func testCreateMuteList() throws {
     22         let privkey = test_keypair_full.privkey
     23         let pubkey = test_keypair_full.pubkey
     24         let to_mute = test_pubkey
     25         let keypair = FullKeypair(pubkey: pubkey, privkey: privkey)
     26         let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: .user(to_mute, nil))!
     27 
     28         XCTAssertEqual(mutelist.pubkey, pubkey)
     29         XCTAssertEqual(mutelist.content, "")
     30         XCTAssertEqual(mutelist.tags.count, 1)
     31         XCTAssertEqual(mutelist.tags[0][0].string(), "p")
     32         XCTAssertEqual(mutelist.tags[0][1].string(), to_mute.hex())
     33     }
     34 
     35     func testCreateAndRemoveMuteList() throws {
     36         let privkey = test_keypair_full.privkey
     37         let pubkey = test_keypair_full.pubkey
     38         let to_mute = test_pubkey
     39         let keypair = FullKeypair(pubkey: pubkey, privkey: privkey)
     40         let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: .user(to_mute, nil))!
     41         let new = remove_from_mutelist(keypair: keypair, prev: mutelist, to_remove: .user(to_mute, nil))!
     42 
     43         XCTAssertEqual(new.pubkey, pubkey)
     44         XCTAssertEqual(new.content, "")
     45         XCTAssertEqual(new.tags.count, 0)
     46     }
     47     
     48     func testAddToExistingMutelist() throws {
     49         let privkey = test_keypair_full.privkey
     50         let pubkey = test_keypair_full.pubkey
     51         let to_mute = test_pubkey
     52         let to_mute_2 = test_pubkey_2
     53         let keypair = FullKeypair(pubkey: pubkey, privkey: privkey)
     54         let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: .user(to_mute, nil))!
     55         let new = create_or_update_mutelist(keypair: keypair, mprev: mutelist, to_add: .user(to_mute_2, nil))!
     56 
     57         XCTAssertEqual(new.pubkey, pubkey)
     58         XCTAssertEqual(new.content, "")
     59         XCTAssertEqual(new.tags.count, 2)
     60         XCTAssertEqual(new.tags[0][0].string(), "p")
     61         XCTAssertEqual(new.tags[1][0].string(), "p")
     62         // This test failed once out of like 10 tries, due to the tags being in the incorrect order. So I decided to put the elements in an array and sort it. That way if the mutelist tags aren't in the expected order it won't fail the test.
     63         XCTAssertEqual([new.tags[0][1].string(), new.tags[1][1].string()].sorted(), [to_mute.hex(), to_mute_2.hex()].sorted())
     64     }
     65 
     66     func testAddToExistingMutelistShouldNotOverrideContent() throws {
     67         let privkey = test_keypair_full.privkey
     68         let pubkey = test_keypair_full.pubkey
     69         let keypair = FullKeypair(pubkey: pubkey, privkey: privkey)
     70         let mutelist = NostrEvent(content: "random", keypair: keypair.to_keypair(), kind: NostrKind.mute_list.rawValue, tags: [])
     71         let new = create_or_update_mutelist(keypair: keypair, mprev: mutelist, to_add: .user(test_pubkey, nil))!
     72 
     73         XCTAssertEqual(new.content, "random")
     74     }
     75 }