Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

Commit

Permalink
chore: Test reopening of swift FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
jsantell committed Nov 29, 2023
1 parent 60e9a3a commit 1cbad5c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions swift/Tests/SwiftNoosphereTests/NoosphereTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,59 @@ final class NoosphereTests: XCTestCase {
print("fin!")
}

func testReopeningNoosphere() throws {
// Create and write content to a sphere
if true {
let noosphere = ns_initialize("/tmp/foo", "/tmp/bar", nil, nil)

ns_tracing_initialize(NS_NOOSPHERE_LOG_CHATTY.rawValue)

ns_key_create(noosphere, "bob", nil)

let sphere_receipt = ns_sphere_create(noosphere, "bob", nil)
let sphere_identity_ptr = ns_sphere_receipt_identity(sphere_receipt, nil)
let sphere_mnemonic_ptr = ns_sphere_receipt_mnemonic(sphere_receipt, nil)
let sphere_identity = String.init(cString: sphere_identity_ptr!)
let sphere_mnemonic = String.init(cString: sphere_mnemonic_ptr!)

let sphere = ns_sphere_open(noosphere, sphere_identity_ptr, nil)
let file_bytes = "Hello, Subconscious".data(using: .utf8)!
file_bytes.withUnsafeBytes({ rawBufferPointer in
let bufferPointer = rawBufferPointer.bindMemory(to: UInt8.self)
let pointer = bufferPointer.baseAddress!
let bodyRaw = slice_ref_uint8(
ptr: pointer, len: file_bytes.count
)
ns_sphere_content_write(noosphere, sphere, "hello", "text/subtext", bodyRaw, nil, nil)
})
ns_sphere_save(noosphere, sphere, nil, nil)
ns_sphere_free(sphere)
ns_string_free(sphere_identity_ptr)
ns_string_free(sphere_mnemonic_ptr)
ns_sphere_receipt_free(sphere_receipt)
ns_free(noosphere)
}

// Reopen noosphere and sphere and read content.

let noosphere = ns_initialize("/tmp/foo", "/tmp/bar", nil, nil)
let sphere = ns_sphere_open(noosphere, sphere_identity, nil)
let file = ns_sphere_content_read_blocking(noosphere, sphere, "/hello", nil)
let content_type_values = ns_sphere_file_header_values_read(file, "Content-Type")
let content_type = String.init(cString: content_type_values.ptr.pointee!)
let contents = ns_sphere_file_contents_read_blocking(noosphere, file, nil)
let data: Data = .init(bytes: contents.ptr, count: contents.len)
let subtext = String.init(decoding: data, as: UTF8.self)

ns_string_array_free(content_type_values)
ns_bytes_free(contents)
ns_sphere_file_free(file)
ns_sphere_free(sphere)
ns_free(noosphere)

print("fin!")
}


func testInitializeNoosphereThenWriteAFileThenSaveThenReadItBackWithACallback() throws {
let noosphere = ns_initialize("/tmp/foo", "/tmp/bar", nil, nil)
Expand Down

0 comments on commit 1cbad5c

Please sign in to comment.