2022-11-22 18:40:24 +00:00
|
|
|
{.used.}
|
|
|
|
|
2024-07-12 16:19:12 +00:00
|
|
|
import std/[options, sequtils], testutils/unittests, chronos, libp2p/crypto/crypto
|
2023-11-27 17:33:27 +00:00
|
|
|
|
2022-11-22 18:40:24 +00:00
|
|
|
import
|
2024-07-05 22:03:38 +00:00
|
|
|
waku/[
|
|
|
|
common/databases/db_sqlite,
|
|
|
|
common/paging,
|
|
|
|
waku_core,
|
|
|
|
waku_core/message/digest,
|
|
|
|
waku_archive/driver/sqlite_driver,
|
|
|
|
waku_archive,
|
|
|
|
],
|
2023-11-27 17:33:27 +00:00
|
|
|
../waku_archive/archive_utils,
|
2023-04-05 14:01:51 +00:00
|
|
|
../testlib/wakucore
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
suite "Waku Archive - message handling":
|
2023-12-06 13:02:21 +00:00
|
|
|
test "it should archive a valid and non-ephemeral message":
|
2022-11-22 18:40:24 +00:00
|
|
|
## Setup
|
2023-11-27 17:33:27 +00:00
|
|
|
let driver = newSqliteArchiveDriver()
|
|
|
|
let archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
let validSenderTime = now()
|
2024-03-15 23:08:47 +00:00
|
|
|
let message = fakeWakuMessage(ephemeral = false, ts = validSenderTime)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage(DefaultPubSubTopic, message)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
2023-05-25 15:34:34 +00:00
|
|
|
(waitFor driver.getMessagesCount()).tryGet() == 1
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-12-06 13:02:21 +00:00
|
|
|
test "it should not archive ephemeral messages":
|
2022-11-22 18:40:24 +00:00
|
|
|
## Setup
|
2023-11-27 17:33:27 +00:00
|
|
|
let driver = newSqliteArchiveDriver()
|
|
|
|
let archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
2024-03-15 23:08:47 +00:00
|
|
|
let msgList =
|
|
|
|
@[
|
|
|
|
fakeWakuMessage(ephemeral = false, payload = "1"),
|
|
|
|
fakeWakuMessage(ephemeral = true, payload = "2"),
|
|
|
|
fakeWakuMessage(ephemeral = true, payload = "3"),
|
|
|
|
fakeWakuMessage(ephemeral = true, payload = "4"),
|
|
|
|
fakeWakuMessage(ephemeral = false, payload = "5"),
|
|
|
|
]
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
|
|
|
for msg in msgList:
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage(DefaultPubsubTopic, msg)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
2023-05-25 15:34:34 +00:00
|
|
|
(waitFor driver.getMessagesCount()).tryGet() == 2
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2024-07-12 16:19:12 +00:00
|
|
|
test "it should not archive a message with no sender timestamp":
|
2022-11-22 18:40:24 +00:00
|
|
|
## Setup
|
2023-11-27 17:33:27 +00:00
|
|
|
let driver = newSqliteArchiveDriver()
|
|
|
|
let archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
let invalidSenderTime = 0
|
2024-03-15 23:08:47 +00:00
|
|
|
let message = fakeWakuMessage(ts = invalidSenderTime)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage(DefaultPubSubTopic, message)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
2024-07-12 16:19:12 +00:00
|
|
|
(waitFor driver.getMessagesCount()).tryGet() == 0
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-12-06 13:02:21 +00:00
|
|
|
test "it should not archive a message with a sender time variance greater than max time variance (future)":
|
2022-11-22 18:40:24 +00:00
|
|
|
## Setup
|
2023-11-27 17:33:27 +00:00
|
|
|
let driver = newSqliteArchiveDriver()
|
|
|
|
let archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
let
|
|
|
|
now = now()
|
2024-03-15 23:08:47 +00:00
|
|
|
invalidSenderTime = now + MaxMessageTimestampVariance + 1_000_000_000
|
|
|
|
# 1 second over the max variance
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let message = fakeWakuMessage(ts = invalidSenderTime)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage(DefaultPubSubTopic, message)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
2023-05-25 15:34:34 +00:00
|
|
|
(waitFor driver.getMessagesCount()).tryGet() == 0
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-12-06 13:02:21 +00:00
|
|
|
test "it should not archive a message with a sender time variance greater than max time variance (past)":
|
2022-11-22 18:40:24 +00:00
|
|
|
## Setup
|
2023-11-27 17:33:27 +00:00
|
|
|
let driver = newSqliteArchiveDriver()
|
|
|
|
let archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
let
|
|
|
|
now = now()
|
|
|
|
invalidSenderTime = now - MaxMessageTimestampVariance - 1
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let message = fakeWakuMessage(ts = invalidSenderTime)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage(DefaultPubSubTopic, message)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
2023-05-25 15:34:34 +00:00
|
|
|
(waitFor driver.getMessagesCount()).tryGet() == 0
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
procSuite "Waku Archive - find messages":
|
|
|
|
## Fixtures
|
|
|
|
let timeOrigin = now()
|
2024-03-15 23:08:47 +00:00
|
|
|
let msgListA =
|
|
|
|
@[
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 00], contentTopic = ContentTopic("2"), ts = ts(00, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 01], contentTopic = ContentTopic("1"), ts = ts(10, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 02], contentTopic = ContentTopic("2"), ts = ts(20, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 03], contentTopic = ContentTopic("1"), ts = ts(30, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 04], contentTopic = ContentTopic("2"), ts = ts(40, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 05], contentTopic = ContentTopic("1"), ts = ts(50, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 06], contentTopic = ContentTopic("2"), ts = ts(60, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 07], contentTopic = ContentTopic("1"), ts = ts(70, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 08], contentTopic = ContentTopic("2"), ts = ts(80, timeOrigin)
|
|
|
|
),
|
|
|
|
fakeWakuMessage(
|
|
|
|
@[byte 09], contentTopic = ContentTopic("1"), ts = ts(90, timeOrigin)
|
|
|
|
),
|
|
|
|
]
|
2023-01-26 09:19:58 +00:00
|
|
|
|
2022-11-22 18:40:24 +00:00
|
|
|
let archiveA = block:
|
2023-05-25 15:34:34 +00:00
|
|
|
let
|
2023-11-27 17:33:27 +00:00
|
|
|
driver = newSqliteArchiveDriver()
|
|
|
|
archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
for msg in msgListA:
|
2024-03-15 23:08:47 +00:00
|
|
|
require (
|
|
|
|
waitFor driver.put(
|
2024-07-12 16:19:12 +00:00
|
|
|
computeMessageHash(DefaultPubsubTopic, msg), DefaultPubsubTopic, msg
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
|
|
|
).isOk()
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
archive
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
test "handle query":
|
|
|
|
## Setup
|
|
|
|
let
|
2023-11-27 17:33:27 +00:00
|
|
|
driver = newSqliteArchiveDriver()
|
|
|
|
archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
let topic = ContentTopic("1")
|
|
|
|
let
|
2024-03-15 23:08:47 +00:00
|
|
|
msg1 = fakeWakuMessage(contentTopic = topic)
|
2022-11-22 18:40:24 +00:00
|
|
|
msg2 = fakeWakuMessage()
|
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage("foo", msg1)
|
|
|
|
waitFor archive.handleMessage("foo", msg2)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
2024-05-01 18:47:06 +00:00
|
|
|
let req = ArchiveQuery(includeData: true, contentTopics: @[topic])
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let queryRes = waitFor archive.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
queryRes.isOk()
|
|
|
|
|
|
|
|
let response = queryRes.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len == 1
|
|
|
|
response.messages == @[msg1]
|
|
|
|
|
|
|
|
test "handle query with multiple content filters":
|
|
|
|
## Setup
|
|
|
|
let
|
2023-11-27 17:33:27 +00:00
|
|
|
driver = newSqliteArchiveDriver()
|
|
|
|
archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
topic1 = ContentTopic("1")
|
|
|
|
topic2 = ContentTopic("2")
|
|
|
|
topic3 = ContentTopic("3")
|
|
|
|
|
|
|
|
let
|
2024-03-15 23:08:47 +00:00
|
|
|
msg1 = fakeWakuMessage(contentTopic = topic1)
|
|
|
|
msg2 = fakeWakuMessage(contentTopic = topic2)
|
|
|
|
msg3 = fakeWakuMessage(contentTopic = topic3)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage("foo", msg1)
|
|
|
|
waitFor archive.handleMessage("foo", msg2)
|
|
|
|
waitFor archive.handleMessage("foo", msg3)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
2024-05-01 18:47:06 +00:00
|
|
|
let req = ArchiveQuery(includeData: true, contentTopics: @[topic1, topic3])
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let queryRes = waitFor archive.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
queryRes.isOk()
|
|
|
|
|
|
|
|
let response = queryRes.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 2
|
|
|
|
response.messages.anyIt(it == msg1)
|
|
|
|
response.messages.anyIt(it == msg3)
|
|
|
|
|
2024-06-26 10:24:15 +00:00
|
|
|
test "handle query with more than 100 content filters":
|
2023-01-11 11:19:59 +00:00
|
|
|
## Setup
|
|
|
|
let
|
2023-11-27 17:33:27 +00:00
|
|
|
driver = newSqliteArchiveDriver()
|
|
|
|
archive = newWakuArchive(driver)
|
2023-01-11 11:19:59 +00:00
|
|
|
|
2024-06-26 10:24:15 +00:00
|
|
|
let queryTopics = toSeq(1 .. 150).mapIt(ContentTopic($it))
|
2023-01-11 11:19:59 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
let req = ArchiveQuery(contentTopics: queryTopics)
|
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let queryRes = waitFor archive.findMessages(req)
|
2023-01-11 11:19:59 +00:00
|
|
|
|
|
|
|
## Then
|
2024-07-12 16:19:12 +00:00
|
|
|
assert queryRes.isOk(), $queryRes.error
|
2023-01-11 11:19:59 +00:00
|
|
|
|
2024-07-12 16:19:12 +00:00
|
|
|
let response = queryRes.tryGet()
|
2023-01-11 11:19:59 +00:00
|
|
|
check:
|
2024-07-12 16:19:12 +00:00
|
|
|
response.messages.len() == 0
|
2023-01-11 11:19:59 +00:00
|
|
|
|
2022-11-22 18:40:24 +00:00
|
|
|
test "handle query with pubsub topic filter":
|
|
|
|
## Setup
|
|
|
|
let
|
2023-11-27 17:33:27 +00:00
|
|
|
driver = newSqliteArchiveDriver()
|
|
|
|
archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
pubsubTopic1 = "queried-topic"
|
|
|
|
pubsubTopic2 = "non-queried-topic"
|
|
|
|
|
|
|
|
let
|
|
|
|
contentTopic1 = ContentTopic("1")
|
|
|
|
contentTopic2 = ContentTopic("2")
|
|
|
|
contentTopic3 = ContentTopic("3")
|
|
|
|
|
|
|
|
let
|
2024-03-15 23:08:47 +00:00
|
|
|
msg1 = fakeWakuMessage(contentTopic = contentTopic1)
|
|
|
|
msg2 = fakeWakuMessage(contentTopic = contentTopic2)
|
|
|
|
msg3 = fakeWakuMessage(contentTopic = contentTopic3)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage(pubsubtopic1, msg1)
|
|
|
|
waitFor archive.handleMessage(pubsubtopic2, msg2)
|
|
|
|
waitFor archive.handleMessage(pubsubtopic2, msg3)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
# This query targets: pubsubtopic1 AND (contentTopic1 OR contentTopic3)
|
|
|
|
let req = ArchiveQuery(
|
2024-05-01 18:47:06 +00:00
|
|
|
includeData: true,
|
|
|
|
pubsubTopic: some(pubsubTopic1),
|
|
|
|
contentTopics: @[contentTopic1, contentTopic3],
|
2022-11-22 18:40:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let queryRes = waitFor archive.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
queryRes.isOk()
|
|
|
|
|
|
|
|
let response = queryRes.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 1
|
|
|
|
response.messages.anyIt(it == msg1)
|
|
|
|
|
|
|
|
test "handle query with pubsub topic filter - no match":
|
|
|
|
## Setup
|
|
|
|
let
|
2023-11-27 17:33:27 +00:00
|
|
|
driver = newSqliteArchiveDriver()
|
|
|
|
archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
pubsubtopic1 = "queried-topic"
|
|
|
|
pubsubtopic2 = "non-queried-topic"
|
|
|
|
|
|
|
|
let
|
|
|
|
msg1 = fakeWakuMessage()
|
|
|
|
msg2 = fakeWakuMessage()
|
|
|
|
msg3 = fakeWakuMessage()
|
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage(pubsubtopic2, msg1)
|
|
|
|
waitFor archive.handleMessage(pubsubtopic2, msg2)
|
|
|
|
waitFor archive.handleMessage(pubsubtopic2, msg3)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
|
|
|
let req = ArchiveQuery(pubsubTopic: some(pubsubTopic1))
|
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let res = waitFor archive.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isOk()
|
|
|
|
|
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 0
|
|
|
|
|
|
|
|
test "handle query with pubsub topic filter - match the entire stored messages":
|
|
|
|
## Setup
|
|
|
|
let
|
2023-11-27 17:33:27 +00:00
|
|
|
driver = newSqliteArchiveDriver()
|
|
|
|
archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
let pubsubTopic = "queried-topic"
|
|
|
|
|
|
|
|
let
|
2024-03-15 23:08:47 +00:00
|
|
|
msg1 = fakeWakuMessage(payload = "TEST-1")
|
|
|
|
msg2 = fakeWakuMessage(payload = "TEST-2")
|
|
|
|
msg3 = fakeWakuMessage(payload = "TEST-3")
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-05-25 15:34:34 +00:00
|
|
|
waitFor archive.handleMessage(pubsubTopic, msg1)
|
|
|
|
waitFor archive.handleMessage(pubsubTopic, msg2)
|
|
|
|
waitFor archive.handleMessage(pubsubTopic, msg3)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
2024-05-01 18:47:06 +00:00
|
|
|
let req = ArchiveQuery(includeData: true, pubsubTopic: some(pubsubTopic))
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let res = waitFor archive.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isOk()
|
|
|
|
|
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 3
|
|
|
|
response.messages.anyIt(it == msg1)
|
|
|
|
response.messages.anyIt(it == msg2)
|
|
|
|
response.messages.anyIt(it == msg3)
|
|
|
|
|
|
|
|
test "handle query with forward pagination":
|
|
|
|
## Given
|
2024-05-01 18:47:06 +00:00
|
|
|
let req =
|
|
|
|
ArchiveQuery(includeData: true, pageSize: 4, direction: PagingDirection.FORWARD)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-01-26 09:19:58 +00:00
|
|
|
var nextReq = req # copy
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-01-26 09:19:58 +00:00
|
|
|
var pages = newSeq[seq[WakuMessage]](3)
|
|
|
|
var cursors = newSeq[Option[ArchiveCursor]](3)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
for i in 0 ..< 3:
|
2023-05-25 15:34:34 +00:00
|
|
|
let res = waitFor archiveA.findMessages(nextReq)
|
2023-01-26 09:19:58 +00:00
|
|
|
require res.isOk()
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-01-26 09:19:58 +00:00
|
|
|
# Keep query response content
|
|
|
|
let response = res.get()
|
|
|
|
pages[i] = response.messages
|
|
|
|
cursors[i] = response.cursor
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-01-26 09:19:58 +00:00
|
|
|
# Set/update the request cursor
|
|
|
|
nextReq.cursor = cursors[i]
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
2024-07-12 16:19:12 +00:00
|
|
|
cursors[0] == some(computeMessageHash(DefaultPubsubTopic, msgListA[3]))
|
|
|
|
cursors[1] == some(computeMessageHash(DefaultPubsubTopic, msgListA[7]))
|
2023-01-26 09:19:58 +00:00
|
|
|
cursors[2] == none(ArchiveCursor)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-01-26 09:19:58 +00:00
|
|
|
check:
|
2024-03-15 23:08:47 +00:00
|
|
|
pages[0] == msgListA[0 .. 3]
|
|
|
|
pages[1] == msgListA[4 .. 7]
|
|
|
|
pages[2] == msgListA[8 .. 9]
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-01-26 09:19:58 +00:00
|
|
|
test "handle query with backward pagination":
|
2022-11-22 18:40:24 +00:00
|
|
|
## Given
|
2024-05-01 18:47:06 +00:00
|
|
|
let req =
|
|
|
|
ArchiveQuery(includeData: true, pageSize: 4, direction: PagingDirection.BACKWARD)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-01-26 09:19:58 +00:00
|
|
|
var nextReq = req # copy
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-01-26 09:19:58 +00:00
|
|
|
var pages = newSeq[seq[WakuMessage]](3)
|
|
|
|
var cursors = newSeq[Option[ArchiveCursor]](3)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
for i in 0 ..< 3:
|
2023-05-25 15:34:34 +00:00
|
|
|
let res = waitFor archiveA.findMessages(nextReq)
|
2023-01-26 09:19:58 +00:00
|
|
|
require res.isOk()
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-01-26 09:19:58 +00:00
|
|
|
# Keep query response content
|
|
|
|
let response = res.get()
|
|
|
|
pages[i] = response.messages
|
|
|
|
cursors[i] = response.cursor
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2023-01-26 09:19:58 +00:00
|
|
|
# Set/update the request cursor
|
|
|
|
nextReq.cursor = cursors[i]
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
2024-07-12 16:19:12 +00:00
|
|
|
cursors[0] == some(computeMessageHash(DefaultPubsubTopic, msgListA[6]))
|
|
|
|
cursors[1] == some(computeMessageHash(DefaultPubsubTopic, msgListA[2]))
|
2023-01-26 09:19:58 +00:00
|
|
|
cursors[2] == none(ArchiveCursor)
|
|
|
|
|
|
|
|
check:
|
2024-03-15 23:08:47 +00:00
|
|
|
pages[0] == msgListA[6 .. 9]
|
|
|
|
pages[1] == msgListA[2 .. 5]
|
|
|
|
pages[2] == msgListA[0 .. 1]
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
test "handle query with no paging info - auto-pagination":
|
|
|
|
## Setup
|
|
|
|
let
|
2023-11-27 17:33:27 +00:00
|
|
|
driver = newSqliteArchiveDriver()
|
|
|
|
archive = newWakuArchive(driver)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let msgList =
|
|
|
|
@[
|
|
|
|
fakeWakuMessage(@[byte 0], contentTopic = ContentTopic("2")),
|
|
|
|
fakeWakuMessage(@[byte 1], contentTopic = DefaultContentTopic),
|
|
|
|
fakeWakuMessage(@[byte 2], contentTopic = DefaultContentTopic),
|
|
|
|
fakeWakuMessage(@[byte 3], contentTopic = DefaultContentTopic),
|
|
|
|
fakeWakuMessage(@[byte 4], contentTopic = DefaultContentTopic),
|
|
|
|
fakeWakuMessage(@[byte 5], contentTopic = DefaultContentTopic),
|
|
|
|
fakeWakuMessage(@[byte 6], contentTopic = DefaultContentTopic),
|
|
|
|
fakeWakuMessage(@[byte 7], contentTopic = DefaultContentTopic),
|
|
|
|
fakeWakuMessage(@[byte 8], contentTopic = DefaultContentTopic),
|
|
|
|
fakeWakuMessage(@[byte 9], contentTopic = ContentTopic("2")),
|
2022-11-22 18:40:24 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for msg in msgList:
|
2024-03-15 23:08:47 +00:00
|
|
|
require (
|
|
|
|
waitFor driver.put(
|
2024-07-12 16:19:12 +00:00
|
|
|
computeMessageHash(DefaultPubsubTopic, msg), DefaultPubsubTopic, msg
|
2024-03-15 23:08:47 +00:00
|
|
|
)
|
|
|
|
).isOk()
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Given
|
2024-05-01 18:47:06 +00:00
|
|
|
let req = ArchiveQuery(includeData: true, contentTopics: @[DefaultContentTopic])
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let res = waitFor archive.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
res.isOk()
|
|
|
|
|
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
## No pagination specified. Response will be auto-paginated with
|
|
|
|
## up to MaxPageSize messages per page.
|
|
|
|
response.messages.len() == 8
|
|
|
|
response.cursor.isNone()
|
|
|
|
|
|
|
|
test "handle temporal history query with a valid time window":
|
|
|
|
## Given
|
|
|
|
let req = ArchiveQuery(
|
2024-05-01 18:47:06 +00:00
|
|
|
includeData: true,
|
2022-11-22 18:40:24 +00:00
|
|
|
contentTopics: @[ContentTopic("1")],
|
|
|
|
startTime: some(ts(15, timeOrigin)),
|
|
|
|
endTime: some(ts(55, timeOrigin)),
|
2024-03-15 23:08:47 +00:00
|
|
|
direction: PagingDirection.FORWARD,
|
2022-11-22 18:40:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let res = waitFor archiveA.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check res.isOk()
|
|
|
|
|
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len() == 2
|
|
|
|
response.messages.mapIt(it.timestamp) == @[ts(30, timeOrigin), ts(50, timeOrigin)]
|
|
|
|
|
|
|
|
test "handle temporal history query with a zero-size time window":
|
|
|
|
## A zero-size window results in an empty list of history messages
|
|
|
|
## Given
|
|
|
|
let req = ArchiveQuery(
|
|
|
|
contentTopics: @[ContentTopic("1")],
|
|
|
|
startTime: some(Timestamp(2)),
|
2024-03-15 23:08:47 +00:00
|
|
|
endTime: some(Timestamp(2)),
|
2022-11-22 18:40:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let res = waitFor archiveA.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check res.isOk()
|
|
|
|
|
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len == 0
|
|
|
|
|
|
|
|
test "handle temporal history query with an invalid time window":
|
|
|
|
## A history query with an invalid time range results in an empty list of history messages
|
|
|
|
## Given
|
|
|
|
let req = ArchiveQuery(
|
|
|
|
contentTopics: @[ContentTopic("1")],
|
|
|
|
startTime: some(Timestamp(5)),
|
2024-03-15 23:08:47 +00:00
|
|
|
endTime: some(Timestamp(2)),
|
2022-11-22 18:40:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
## When
|
2023-05-25 15:34:34 +00:00
|
|
|
let res = waitFor archiveA.findMessages(req)
|
2022-11-22 18:40:24 +00:00
|
|
|
|
|
|
|
## Then
|
|
|
|
check res.isOk()
|
|
|
|
|
|
|
|
let response = res.tryGet()
|
|
|
|
check:
|
|
|
|
response.messages.len == 0
|