2020-11-03 20:20:40 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import testutils/unittests
|
2023-02-07 09:45:25 +00:00
|
|
|
import
|
2023-08-07 14:11:46 +00:00
|
|
|
stew/results,
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/waku_core/message,
|
|
|
|
../../waku/waku_core/time,
|
2023-02-13 10:43:49 +00:00
|
|
|
./testlib/common
|
2020-11-03 20:20:40 +00:00
|
|
|
|
2023-02-13 10:43:49 +00:00
|
|
|
suite "Waku Payload":
|
2022-08-01 10:37:45 +00:00
|
|
|
test "Encode/Decode waku message with timestamp":
|
|
|
|
## Test encoding and decoding of the timestamp field of a WakuMessage
|
|
|
|
|
|
|
|
## Given
|
|
|
|
let
|
|
|
|
version = 0'u32
|
|
|
|
payload = @[byte 0, 1, 2]
|
|
|
|
timestamp = Timestamp(10)
|
|
|
|
msg = WakuMessage(payload: payload, version: version, timestamp: timestamp)
|
2023-02-07 09:45:25 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
## When
|
2024-03-15 23:08:47 +00:00
|
|
|
let pb = msg.encode()
|
2022-11-07 15:24:16 +00:00
|
|
|
let msgDecoded = WakuMessage.decode(pb.buffer)
|
2023-02-07 09:45:25 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
msgDecoded.isOk()
|
2023-02-07 09:45:25 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
let timestampDecoded = msgDecoded.value.timestamp
|
|
|
|
check:
|
|
|
|
timestampDecoded == timestamp
|
|
|
|
|
|
|
|
test "Encode/Decode waku message without timestamp":
|
2023-02-07 09:45:25 +00:00
|
|
|
## Test the encoding and decoding of a WakuMessage with an empty timestamp field
|
2022-08-01 10:37:45 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
let
|
|
|
|
version = 0'u32
|
|
|
|
payload = @[byte 0, 1, 2]
|
|
|
|
msg = WakuMessage(payload: payload, version: version)
|
2023-02-07 09:45:25 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
## When
|
2024-03-15 23:08:47 +00:00
|
|
|
let pb = msg.encode()
|
2022-11-07 15:24:16 +00:00
|
|
|
let msgDecoded = WakuMessage.decode(pb.buffer)
|
2022-08-01 10:37:45 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
msgDecoded.isOk()
|
2023-02-07 09:45:25 +00:00
|
|
|
|
2022-08-01 10:37:45 +00:00
|
|
|
let timestampDecoded = msgDecoded.value.timestamp
|
|
|
|
check:
|
2023-02-07 09:45:25 +00:00
|
|
|
timestampDecoded == Timestamp(0)
|