2023-06-03 01:51:49 +00:00
|
|
|
//
|
|
|
|
// NostrScriptTests.swift
|
|
|
|
// damusTests
|
|
|
|
//
|
|
|
|
// Created by William Casarin on 2023-06-02.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import damus
|
|
|
|
|
|
|
|
final class NostrScriptTests: 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.
|
|
|
|
}
|
|
|
|
|
2023-07-03 23:59:50 +00:00
|
|
|
func read_bundle_file(name: String, ext: String) throws -> Data {
|
2023-06-03 01:51:49 +00:00
|
|
|
let bundle = Bundle(for: type(of: self))
|
2023-07-03 23:59:50 +00:00
|
|
|
guard let fileURL = bundle.url(forResource: name, withExtension: ext) else {
|
2023-06-03 01:51:49 +00:00
|
|
|
throw CocoaError(.fileReadNoSuchFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return try Data(contentsOf: fileURL)
|
|
|
|
}
|
2023-07-03 23:59:50 +00:00
|
|
|
|
|
|
|
func loadTestWasm() throws -> Data {
|
|
|
|
return try read_bundle_file(name: "primal", ext: "wasm")
|
|
|
|
}
|
|
|
|
|
|
|
|
func load_bool_set_test_wasm() throws -> Data {
|
|
|
|
return try read_bundle_file(name: "bool_setting", ext: "wasm")
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_bool_set() throws {
|
2023-12-22 21:48:59 +00:00
|
|
|
// FIXME: https://github.com/damus-io/damus/issues/1835
|
|
|
|
// let data = try load_bool_set_test_wasm().bytes
|
|
|
|
// let pool = RelayPool(ndb: .empty)
|
|
|
|
// let script = NostrScript(pool: pool, data: data)
|
|
|
|
// let pk = Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!
|
|
|
|
// UserSettingsStore.pubkey = pk
|
|
|
|
// let key = pk_setting_key(pk, key: "nozaps")
|
|
|
|
// UserDefaults.standard.set(true, forKey: key)
|
|
|
|
//
|
|
|
|
// let load_err = script.load()
|
|
|
|
// XCTAssertNil(load_err)
|
|
|
|
//
|
|
|
|
// let res = script.run()
|
|
|
|
// switch res {
|
|
|
|
// case .finished:
|
|
|
|
// let set = UserDefaults.standard.bool(forKey: key)
|
|
|
|
// XCTAssertEqual(set, false)
|
|
|
|
// case .runtime_err(let errs):
|
|
|
|
// var c = 0
|
|
|
|
// for err in errs {
|
|
|
|
// print("\(c) test_bool_set runtime err: \(err)")
|
|
|
|
// c += 1
|
|
|
|
// }
|
|
|
|
// XCTAssert(false)
|
|
|
|
// case .suspend:
|
|
|
|
// XCTAssert(false)
|
|
|
|
// break
|
|
|
|
// }
|
2023-07-03 23:59:50 +00:00
|
|
|
}
|
2023-06-03 01:51:49 +00:00
|
|
|
|
2023-08-03 20:12:57 +00:00
|
|
|
/*
|
2023-06-03 01:51:49 +00:00
|
|
|
func test_nostrscript() throws {
|
2023-07-19 17:04:11 +00:00
|
|
|
let data = try loadTestWasm().bytes
|
2023-06-03 01:51:49 +00:00
|
|
|
let pool = RelayPool()
|
2023-07-04 18:42:16 +00:00
|
|
|
let script = NostrScript(pool: pool, data: data)
|
2023-06-03 01:51:49 +00:00
|
|
|
|
2023-07-04 18:42:16 +00:00
|
|
|
let load_err = script.load()
|
2023-06-03 01:51:49 +00:00
|
|
|
XCTAssertNil(load_err)
|
|
|
|
|
|
|
|
let res = script.run()
|
|
|
|
switch res {
|
|
|
|
case .finished: XCTAssert(false)
|
|
|
|
case .runtime_err: XCTAssert(false)
|
|
|
|
case .suspend:
|
|
|
|
XCTAssertEqual(script.waiting_on, .event("sidebar_trending"))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
let resume_expected = XCTestExpectation(description: "we got ")
|
|
|
|
pool.register_handler(sub_id: "sidebar_trending") { (relay_id, conn) in
|
|
|
|
if script.runstate?.exited == true {
|
|
|
|
pool.disconnect()
|
|
|
|
resume_expected.fulfill()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard case .nostr_event(let resp) = conn else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let with: NScriptResumeWith = .event(resp)
|
|
|
|
guard let res = script.resume(with: with) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch res {
|
|
|
|
case .finished: break
|
|
|
|
case .runtime_err: XCTAssert(false)
|
|
|
|
case .suspend: break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 20:12:57 +00:00
|
|
|
pool.connect(to: ["wss://cache0.primal.net/cache17"])
|
2023-06-03 01:51:49 +00:00
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
self.wait(for: [resume_expected], timeout: 5.0)
|
2023-06-03 01:51:49 +00:00
|
|
|
}
|
2023-08-03 20:12:57 +00:00
|
|
|
*/
|
2023-06-03 01:51:49 +00:00
|
|
|
|
|
|
|
}
|