2021-04-26 14:34:04 +00:00
|
|
|
import pkg/chronos
|
2021-08-30 19:25:20 +00:00
|
|
|
|
2022-05-19 19:56:03 +00:00
|
|
|
import pkg/codex/blockexchange/protobuf/presence
|
2024-01-29 20:03:51 +00:00
|
|
|
|
|
|
|
import ../../../asynctest
|
2021-04-26 14:34:04 +00:00
|
|
|
import ../../examples
|
2023-06-22 18:01:21 +00:00
|
|
|
import ../../helpers
|
2021-04-26 14:34:04 +00:00
|
|
|
|
2023-06-22 18:01:21 +00:00
|
|
|
checksuite "block presence protobuf messages":
|
2021-04-26 14:34:04 +00:00
|
|
|
|
2024-01-29 20:03:51 +00:00
|
|
|
let
|
2023-11-14 12:02:17 +00:00
|
|
|
cid = Cid.example
|
|
|
|
address = BlockAddress(leaf: false, cid: cid)
|
|
|
|
price = UInt256.example
|
|
|
|
presence = Presence(address: address, have: true, price: price)
|
|
|
|
message = PresenceMessage.init(presence)
|
2021-04-26 14:34:04 +00:00
|
|
|
|
|
|
|
test "encodes have/donthave":
|
|
|
|
var presence = presence
|
|
|
|
presence.have = true
|
2022-11-15 15:46:21 +00:00
|
|
|
check PresenceMessage.init(presence).`type` == Have
|
2021-04-26 14:34:04 +00:00
|
|
|
presence.have = false
|
2022-11-15 15:46:21 +00:00
|
|
|
check PresenceMessage.init(presence).`type` == DontHave
|
2021-04-26 14:34:04 +00:00
|
|
|
|
|
|
|
test "encodes price":
|
|
|
|
check message.price == @(price.toBytesBE)
|
|
|
|
|
|
|
|
test "decodes CID":
|
2023-11-14 12:02:17 +00:00
|
|
|
check Presence.init(message).?address == address.some
|
2021-04-26 14:34:04 +00:00
|
|
|
|
|
|
|
test "decodes have/donthave":
|
|
|
|
var message = message
|
2022-11-15 15:46:21 +00:00
|
|
|
message.`type` = BlockPresenceType.Have
|
2021-04-26 14:34:04 +00:00
|
|
|
check Presence.init(message).?have == true.some
|
2022-11-15 15:46:21 +00:00
|
|
|
message.`type` = BlockPresenceType.DontHave
|
2021-04-26 14:34:04 +00:00
|
|
|
check Presence.init(message).?have == false.some
|
|
|
|
|
|
|
|
test "decodes price":
|
|
|
|
check Presence.init(message).?price == price.some
|
|
|
|
|
|
|
|
test "fails to decode when price is invalid":
|
|
|
|
var incorrect = message
|
|
|
|
incorrect.price.add(0)
|
|
|
|
check Presence.init(incorrect).isNone
|