NostrScriptTests.swift (3656B)
1 // 2 // NostrScriptTests.swift 3 // damusTests 4 // 5 // Created by William Casarin on 2023-06-02. 6 // 7 8 import XCTest 9 @testable import damus 10 11 final class NostrScriptTests: 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 read_bundle_file(name: String, ext: String) throws -> Data { 22 let bundle = Bundle(for: type(of: self)) 23 guard let fileURL = bundle.url(forResource: name, withExtension: ext) else { 24 throw CocoaError(.fileReadNoSuchFile) 25 } 26 27 return try Data(contentsOf: fileURL) 28 } 29 30 func loadTestWasm() throws -> Data { 31 return try read_bundle_file(name: "primal", ext: "wasm") 32 } 33 34 func load_bool_set_test_wasm() throws -> Data { 35 return try read_bundle_file(name: "bool_setting", ext: "wasm") 36 } 37 38 func test_bool_set() throws { 39 // FIXME: https://github.com/damus-io/damus/issues/1835 40 // let data = try load_bool_set_test_wasm().bytes 41 // let pool = RelayPool(ndb: .empty) 42 // let script = NostrScript(pool: pool, data: data) 43 // let pk = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")! 44 // UserSettingsStore.pubkey = pk 45 // let key = pk_setting_key(pk, key: "nozaps") 46 // UserDefaults.standard.set(true, forKey: key) 47 // 48 // let load_err = script.load() 49 // XCTAssertNil(load_err) 50 // 51 // let res = script.run() 52 // switch res { 53 // case .finished: 54 // let set = UserDefaults.standard.bool(forKey: key) 55 // XCTAssertEqual(set, false) 56 // case .runtime_err(let errs): 57 // var c = 0 58 // for err in errs { 59 // print("\(c) test_bool_set runtime err: \(err)") 60 // c += 1 61 // } 62 // XCTAssert(false) 63 // case .suspend: 64 // XCTAssert(false) 65 // break 66 // } 67 } 68 69 /* 70 func test_nostrscript() throws { 71 let data = try loadTestWasm().bytes 72 let pool = RelayPool() 73 let script = NostrScript(pool: pool, data: data) 74 75 let load_err = script.load() 76 XCTAssertNil(load_err) 77 78 let res = script.run() 79 switch res { 80 case .finished: XCTAssert(false) 81 case .runtime_err: XCTAssert(false) 82 case .suspend: 83 XCTAssertEqual(script.waiting_on, .event("sidebar_trending")) 84 break 85 } 86 87 let resume_expected = XCTestExpectation(description: "we got ") 88 pool.register_handler(sub_id: "sidebar_trending") { (relay_id, conn) in 89 if script.runstate?.exited == true { 90 pool.disconnect() 91 resume_expected.fulfill() 92 return 93 } 94 95 guard case .nostr_event(let resp) = conn else { 96 return 97 } 98 99 let with: NScriptResumeWith = .event(resp) 100 guard let res = script.resume(with: with) else { 101 return 102 } 103 104 switch res { 105 case .finished: break 106 case .runtime_err: XCTAssert(false) 107 case .suspend: break 108 } 109 } 110 111 pool.connect(to: ["wss://cache0.primal.net/cache17"]) 112 113 self.wait(for: [resume_expected], timeout: 5.0) 114 } 115 */ 116 117 }