mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 08:45:19 +00:00
[codex] Refactor BLE transport event handling (#1266)
* Refactor BLE transport event handling * Make image output paths unique * Keep queued Nostr read receipts alive * Allow self-authored RSR ingress replies --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import bitchat
|
||||
|
||||
struct BLEOutboundWriteBufferTests {
|
||||
@Test
|
||||
func enqueueOrdersWritesByPriority() {
|
||||
var buffer = BLEOutboundWriteBuffer()
|
||||
let peerID = "peer-1"
|
||||
|
||||
_ = buffer.enqueue(
|
||||
data: Data(repeating: 0x01, count: 4),
|
||||
for: peerID,
|
||||
priority: .fileTransfer,
|
||||
capBytes: 64
|
||||
)
|
||||
_ = buffer.enqueue(
|
||||
data: Data(repeating: 0x02, count: 4),
|
||||
for: peerID,
|
||||
priority: .high,
|
||||
capBytes: 64
|
||||
)
|
||||
_ = buffer.enqueue(
|
||||
data: Data(repeating: 0x03, count: 4),
|
||||
for: peerID,
|
||||
priority: .fragment(totalFragments: 2),
|
||||
capBytes: 64
|
||||
)
|
||||
|
||||
let writes = buffer.takeAll(for: peerID)
|
||||
|
||||
#expect(writes.map { Int($0.data.first ?? 0) } == [0x02, 0x03, 0x01])
|
||||
}
|
||||
|
||||
@Test
|
||||
func enqueueTrimsLowestPriorityItemsToCap() {
|
||||
var buffer = BLEOutboundWriteBuffer()
|
||||
let peerID = "peer-1"
|
||||
|
||||
_ = buffer.enqueue(data: Data(repeating: 0x01, count: 8), for: peerID, priority: .low, capBytes: 16)
|
||||
_ = buffer.enqueue(data: Data(repeating: 0x02, count: 8), for: peerID, priority: .fileTransfer, capBytes: 16)
|
||||
let result = buffer.enqueue(data: Data(repeating: 0x03, count: 8), for: peerID, priority: .high, capBytes: 16)
|
||||
|
||||
if case let .enqueued(trimmedBytes, remainingBytes) = result {
|
||||
#expect(trimmedBytes == 8)
|
||||
#expect(remainingBytes == 16)
|
||||
} else {
|
||||
Issue.record("Expected buffered write to trim, not drop as oversized")
|
||||
}
|
||||
|
||||
let writes = buffer.takeAll(for: peerID)
|
||||
|
||||
#expect(writes.map { Int($0.data.first ?? 0) } == [0x03, 0x02])
|
||||
}
|
||||
|
||||
@Test
|
||||
func enqueueRejectsOversizedSingleChunk() {
|
||||
var buffer = BLEOutboundWriteBuffer()
|
||||
|
||||
let result = buffer.enqueue(
|
||||
data: Data(repeating: 0x01, count: 32),
|
||||
for: "peer-1",
|
||||
priority: .high,
|
||||
capBytes: 16
|
||||
)
|
||||
|
||||
if case let .oversized(bytes) = result {
|
||||
#expect(bytes == 32)
|
||||
} else {
|
||||
Issue.record("Expected oversized write to be rejected")
|
||||
}
|
||||
|
||||
#expect(buffer.peripheralIDs.isEmpty)
|
||||
}
|
||||
}
|
||||
@@ -306,6 +306,7 @@ struct NostrTransportTests {
|
||||
let secondPayload = try decodeEmbeddedPayload(from: secondEvent, recipient: recipient).payload
|
||||
#expect(secondPayload.type == .readReceipt)
|
||||
#expect(String(data: secondPayload.data, encoding: .utf8) == "read-2")
|
||||
withExtendedLifetime(transport) {}
|
||||
}
|
||||
|
||||
@Test("Concurrent read receipt enqueue does not crash")
|
||||
|
||||
Reference in New Issue
Block a user