nostrscript: add a helper function

This commit is contained in:
William Casarin
2023-07-03 16:59:50 -07:00
parent a6cbf50def
commit 30e33a01c1
9 changed files with 98 additions and 3 deletions

View File

@@ -18,14 +18,46 @@ final class NostrScriptTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func loadTestWasm() throws -> Data {
func read_bundle_file(name: String, ext: String) throws -> Data {
let bundle = Bundle(for: type(of: self))
guard let fileURL = bundle.url(forResource: "primal", withExtension: "wasm") else {
guard let fileURL = bundle.url(forResource: name, withExtension: ext) else {
throw CocoaError(.fileReadNoSuchFile)
}
return try Data(contentsOf: fileURL)
}
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 {
var data = try load_bool_set_test_wasm().bytes
let pool = RelayPool()
let script = NostrScript(pool: pool)
let pk = "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"
UserSettingsStore.pubkey = pk
let key = pk_setting_key(pk, key: "nozaps")
UserDefaults.standard.set(true, forKey: key)
let load_err = script.load(wasm: &data)
XCTAssertNil(load_err)
let res = script.run()
switch res {
case .finished:
let set = UserDefaults.standard.bool(forKey: key)
XCTAssertEqual(set, false)
case .runtime_err: XCTAssert(false)
case .suspend:
XCTAssert(false)
break
}
}
func test_nostrscript() throws {
var data = try loadTestWasm().bytes