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