nim-dagger/tests/codex/blockexchange/protobuf/testpresence.nim

50 lines
1.4 KiB
Nim
Raw Normal View History

2021-04-26 14:34:04 +00:00
import pkg/asynctest
import pkg/chronos
2022-05-19 19:56:03 +00:00
import pkg/codex/blockexchange/protobuf/presence
2021-04-26 14:34:04 +00:00
import ../../examples
import ../../helpers
2021-04-26 14:34:04 +00:00
checksuite "block presence protobuf messages":
2021-04-26 14:34:04 +00:00
let cid = Cid.example
let price = UInt256.example
let presence = Presence(cid: cid, have: true, price: price)
let message = PresenceMessage.init(presence)
test "encodes CID":
check message.cid == cid.data.buffer
test "encodes have/donthave":
var presence = presence
presence.have = true
check PresenceMessage.init(presence).`type` == Have
2021-04-26 14:34:04 +00:00
presence.have = false
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":
check Presence.init(message).?cid == cid.some
test "fails to decode when CID is invalid":
var incorrect = message
incorrect.cid.del(0)
check Presence.init(incorrect).isNone
test "decodes have/donthave":
var message = message
message.`type` = BlockPresenceType.Have
2021-04-26 14:34:04 +00:00
check Presence.init(message).?have == true.some
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