2023-01-25 17:53:41 +00:00
|
|
|
//
|
|
|
|
// ListTests.swift
|
|
|
|
// damusTests
|
|
|
|
//
|
|
|
|
// Created by William Casarin on 2023-01-25.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import damus
|
|
|
|
|
|
|
|
final class ListTests: XCTestCase {
|
|
|
|
|
|
|
|
override func setUpWithError() throws {
|
|
|
|
// Put setup code here. This method is called before the invocation of each test method in the class.
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDownWithError() throws {
|
|
|
|
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateMuteList() throws {
|
2023-07-26 15:46:44 +00:00
|
|
|
let privkey = test_keypair_full.privkey
|
|
|
|
let pubkey = test_keypair_full.pubkey
|
|
|
|
let to_mute = test_pubkey
|
2023-01-25 17:53:41 +00:00
|
|
|
let keypair = FullKeypair(pubkey: pubkey, privkey: privkey)
|
2024-01-18 01:17:38 +00:00
|
|
|
let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: .user(to_mute, nil))!
|
2023-07-26 15:46:44 +00:00
|
|
|
|
2023-01-25 17:53:41 +00:00
|
|
|
XCTAssertEqual(mutelist.pubkey, pubkey)
|
|
|
|
XCTAssertEqual(mutelist.content, "")
|
2024-01-18 01:17:38 +00:00
|
|
|
XCTAssertEqual(mutelist.tags.count, 1)
|
|
|
|
XCTAssertEqual(mutelist.tags[0][0].string(), "p")
|
|
|
|
XCTAssertEqual(mutelist.tags[0][1].string(), to_mute.hex())
|
2023-01-25 17:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testCreateAndRemoveMuteList() throws {
|
2023-07-26 15:46:44 +00:00
|
|
|
let privkey = test_keypair_full.privkey
|
|
|
|
let pubkey = test_keypair_full.pubkey
|
|
|
|
let to_mute = test_pubkey
|
2023-01-25 17:53:41 +00:00
|
|
|
let keypair = FullKeypair(pubkey: pubkey, privkey: privkey)
|
2024-01-18 01:17:38 +00:00
|
|
|
let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: .user(to_mute, nil))!
|
|
|
|
let new = remove_from_mutelist(keypair: keypair, prev: mutelist, to_remove: .user(to_mute, nil))!
|
2023-07-26 15:46:44 +00:00
|
|
|
|
2023-01-25 17:53:41 +00:00
|
|
|
XCTAssertEqual(new.pubkey, pubkey)
|
|
|
|
XCTAssertEqual(new.content, "")
|
2024-01-18 01:17:38 +00:00
|
|
|
XCTAssertEqual(new.tags.count, 0)
|
2023-01-25 17:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAddToExistingMutelist() throws {
|
2023-07-26 15:46:44 +00:00
|
|
|
let privkey = test_keypair_full.privkey
|
|
|
|
let pubkey = test_keypair_full.pubkey
|
|
|
|
let to_mute = test_pubkey
|
|
|
|
let to_mute_2 = test_pubkey_2
|
2023-01-25 17:53:41 +00:00
|
|
|
let keypair = FullKeypair(pubkey: pubkey, privkey: privkey)
|
2024-01-18 01:17:38 +00:00
|
|
|
let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: .user(to_mute, nil))!
|
|
|
|
let new = create_or_update_mutelist(keypair: keypair, mprev: mutelist, to_add: .user(to_mute_2, nil))!
|
2023-07-26 15:46:44 +00:00
|
|
|
|
2023-01-25 17:53:41 +00:00
|
|
|
XCTAssertEqual(new.pubkey, pubkey)
|
|
|
|
XCTAssertEqual(new.content, "")
|
2024-01-18 01:17:38 +00:00
|
|
|
XCTAssertEqual(new.tags.count, 2)
|
|
|
|
XCTAssertEqual(new.tags[0][0].string(), "p")
|
2023-07-26 15:46:44 +00:00
|
|
|
XCTAssertEqual(new.tags[1][0].string(), "p")
|
2024-01-18 01:17:38 +00:00
|
|
|
// 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.
|
|
|
|
XCTAssertEqual([new.tags[0][1].string(), new.tags[1][1].string()].sorted(), [to_mute.hex(), to_mute_2.hex()].sorted())
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAddToExistingMutelistShouldNotOverrideContent() throws {
|
|
|
|
let privkey = test_keypair_full.privkey
|
|
|
|
let pubkey = test_keypair_full.pubkey
|
|
|
|
let keypair = FullKeypair(pubkey: pubkey, privkey: privkey)
|
|
|
|
let mutelist = NostrEvent(content: "random", keypair: keypair.to_keypair(), kind: NostrKind.mute_list.rawValue, tags: [])
|
|
|
|
let new = create_or_update_mutelist(keypair: keypair, mprev: mutelist, to_add: .user(test_pubkey, nil))!
|
|
|
|
|
|
|
|
XCTAssertEqual(new.content, "random")
|
2023-01-25 17:53:41 +00:00
|
|
|
}
|
|
|
|
}
|