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

chore: Test reopening of swift FFI #739

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions swift/Tests/SwiftNoosphereTests/NoosphereTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,58 @@ final class NoosphereTests: XCTestCase {
print("fin!")
}

func testReopeningNoosphere() throws {
// Create and write content to a sphere
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.
if true {
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