nim-dagger/tests/dagger/bitswap/protobuf/testpayments.nim

51 lines
1.4 KiB
Nim
Raw Normal View History

2021-04-07 10:19:00 +00:00
import pkg/asynctest
import pkg/chronos
import pkg/stew/byteutils
2021-04-07 10:19:00 +00:00
import ../../examples
import ../../../../dagger/bitswap/protobuf/payments
suite "pricing protobuf messages":
let address = EthAddress.example
let price = UInt256.example
let pricing = Pricing(address: address, price: price)
2021-04-07 10:19:00 +00:00
let message = PricingMessage.init(pricing)
test "encodes recipient of payments":
check message.address == @(address.toArray)
test "encodes price per byte":
check message.price == @(price.toBytesBE)
test "decodes recipient of payments":
check Pricing.init(message).?address == address.some
2021-04-07 10:19:00 +00:00
test "decodes price":
check Pricing.init(message).?price == price.some
2021-04-07 10:19:00 +00:00
test "fails to decode when address has incorrect number of bytes":
var incorrect = message
incorrect.address.del(0)
check Pricing.init(incorrect).isNone
test "fails to decode when price has too many bytes":
var incorrect = message
incorrect.price = newSeq[byte](33)
check Pricing.init(incorrect).isNone
suite "channel update messages":
let state = SignedState.example
let update = StateChannelUpdate.init(state)
test "encodes a nitro signed state":
check update.update == state.toJson.toBytes
test "decodes a channel update":
check SignedState.init(update) == state.some
test "fails to decode incorrect channel update":
var incorrect = update
incorrect.update.del(0)
check SignedState.init(incorrect).isNone