nwaku/tests/waku_store/test_waku_store.nim

120 lines
3.2 KiB
Nim
Raw Normal View History

{.used.}
import std/options, testutils/unittests, chronos, chronicles, libp2p/crypto/crypto
import
2024-04-25 13:09:52 +00:00
../../../waku/[
common/paging,
node/peer_manager,
waku_core,
waku_core/message/digest,
waku_store,
waku_store/client,
waku_store/common,
],
../testlib/[common, wakucore],
./store_utils
suite "Waku Store - query handler":
asyncTest "history query handler should be called":
## Setup
let
serverSwitch = newTestSwitch()
clientSwitch = newTestSwitch()
await allFutures(serverSwitch.start(), clientSwitch.start())
## Given
let serverPeerInfo = serverSwitch.peerInfo.toRemotePeerInfo()
let msg = fakeWakuMessage(contentTopic = DefaultContentTopic)
2024-04-25 13:09:52 +00:00
let hash = computeMessageHash(DefaultPubsubTopic, msg)
let kv = WakuMessageKeyValue(
messageHash: hash, message: some(msg), pubsubTopic: some(DefaultPubsubTopic)
)
2024-04-25 13:09:52 +00:00
var queryHandlerFut = newFuture[(StoreQueryRequest)]()
let queryHandler = proc(
2024-04-25 13:09:52 +00:00
req: StoreQueryRequest
): Future[StoreQueryResult] {.async, gcsafe.} =
var request = req
request.requestId = "" # Must remove the id for equality
queryHandlerFut.complete(request)
return ok(StoreQueryResponse(messages: @[kv]))
let
server = await newTestWakuStore(serverSwitch, handler = queryhandler)
client = newTestWakuStoreClient(clientSwitch)
2024-04-25 13:09:52 +00:00
let req = StoreQueryRequest(
contentTopics: @[DefaultContentTopic], paginationForward: PagingDirection.FORWARD
)
## When
let queryRes = await client.query(req, peer = serverPeerInfo)
## Then
check:
not queryHandlerFut.failed()
queryRes.isOk()
let request = queryHandlerFut.read()
check:
request == req
let response = queryRes.tryGet()
check:
response.messages.len == 1
2024-04-25 13:09:52 +00:00
response.messages == @[kv]
## Cleanup
await allFutures(serverSwitch.stop(), clientSwitch.stop())
asyncTest "history query handler should be called and return an error":
## Setup
let
serverSwitch = newTestSwitch()
clientSwitch = newTestSwitch()
await allFutures(serverSwitch.start(), clientSwitch.start())
## Given
let serverPeerInfo = serverSwitch.peerInfo.toRemotePeerInfo()
2024-04-25 13:09:52 +00:00
var queryHandlerFut = newFuture[(StoreQueryRequest)]()
let queryHandler = proc(
2024-04-25 13:09:52 +00:00
req: StoreQueryRequest
): Future[StoreQueryResult] {.async, gcsafe.} =
var request = req
request.requestId = "" # Must remove the id for equality
queryHandlerFut.complete(request)
return err(StoreError(kind: ErrorCode.BAD_REQUEST))
let
server = await newTestWakuStore(serverSwitch, handler = queryhandler)
client = newTestWakuStoreClient(clientSwitch)
2024-04-25 13:09:52 +00:00
let req = StoreQueryRequest(
contentTopics: @[DefaultContentTopic], paginationForward: PagingDirection.FORWARD
)
## When
let queryRes = await client.query(req, peer = serverPeerInfo)
## Then
check:
not queryHandlerFut.failed()
queryRes.isErr()
let request = queryHandlerFut.read()
check:
request == req
let error = queryRes.tryError()
check:
2024-04-25 13:09:52 +00:00
error.kind == ErrorCode.BAD_REQUEST
## Cleanup
await allFutures(serverSwitch.stop(), clientSwitch.stop())