2025-02-11 15:03:56 +01:00
|
|
|
import pkg/chronos
|
|
|
|
|
import pkg/questionable/results
|
|
|
|
|
import pkg/asynctest/chronos/unittest
|
|
|
|
|
|
|
|
|
|
import ../../../codexcrawler/components/todolist
|
|
|
|
|
import ../../../codexcrawler/utils/asyncdataevent
|
|
|
|
|
import ../../../codexcrawler/types
|
|
|
|
|
import ../../../codexcrawler/state
|
2025-02-12 14:48:57 +01:00
|
|
|
import ../mocks/mockstate
|
2025-02-12 15:16:59 +01:00
|
|
|
import ../mocks/mockmetrics
|
2025-02-11 15:03:56 +01:00
|
|
|
import ../helpers
|
|
|
|
|
|
|
|
|
|
suite "TodoList":
|
|
|
|
|
var
|
|
|
|
|
nid: Nid
|
|
|
|
|
state: MockState
|
2025-02-12 15:16:59 +01:00
|
|
|
metrics: MockMetrics
|
2025-02-11 15:03:56 +01:00
|
|
|
todo: TodoList
|
|
|
|
|
|
|
|
|
|
setup:
|
|
|
|
|
nid = genNid()
|
|
|
|
|
state = createMockState()
|
2025-02-12 15:16:59 +01:00
|
|
|
metrics = createMockMetrics()
|
2025-02-11 15:03:56 +01:00
|
|
|
|
2025-02-12 15:16:59 +01:00
|
|
|
todo = TodoList.new(state, metrics)
|
2025-03-21 13:04:10 +01:00
|
|
|
(await todo.awake()).tryGet()
|
2025-02-11 15:03:56 +01:00
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
|
(await todo.stop()).tryGet()
|
|
|
|
|
state.checkAllUnsubscribed()
|
|
|
|
|
|
|
|
|
|
proc fireNewNodesDiscoveredEvent(nids: seq[Nid]) {.async.} =
|
|
|
|
|
(await state.events.newNodesDiscovered.fire(nids)).tryGet()
|
|
|
|
|
|
2025-02-13 14:55:45 +01:00
|
|
|
proc fireNodesToRevisitEvent(nids: seq[Nid]) {.async.} =
|
|
|
|
|
(await state.events.nodesToRevisit.fire(nids)).tryGet()
|
2025-02-11 15:03:56 +01:00
|
|
|
|
|
|
|
|
test "discovered nodes are added to todo list":
|
|
|
|
|
await fireNewNodesDiscoveredEvent(@[nid])
|
|
|
|
|
let item = (await todo.pop).tryGet()
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
item == nid
|
|
|
|
|
|
2025-02-13 14:55:45 +01:00
|
|
|
test "revisit nodes are added to todo list":
|
|
|
|
|
await fireNodesToRevisitEvent(@[nid])
|
2025-02-11 15:03:56 +01:00
|
|
|
let item = (await todo.pop).tryGet()
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
item == nid
|
|
|
|
|
|
2025-02-12 15:16:59 +01:00
|
|
|
test "newNodesDiscovered event updates todo metric":
|
|
|
|
|
await fireNewNodesDiscoveredEvent(@[nid])
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
metrics.todo == 1
|
|
|
|
|
|
2025-02-13 14:55:45 +01:00
|
|
|
test "nodesToRevisit event updates todo metric":
|
|
|
|
|
await fireNodesToRevisitEvent(@[nid])
|
2025-02-12 15:16:59 +01:00
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
metrics.todo == 1
|
|
|
|
|
|
2025-02-13 09:33:13 +01:00
|
|
|
test "does not store duplicates":
|
|
|
|
|
await fireNewNodesDiscoveredEvent(@[nid])
|
2025-02-13 14:55:45 +01:00
|
|
|
await fireNodesToRevisitEvent(@[nid])
|
2025-02-13 09:33:13 +01:00
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
metrics.todo == 1
|
|
|
|
|
|
2025-02-11 15:03:56 +01:00
|
|
|
test "pop on empty todo list waits until item is added":
|
|
|
|
|
let popFuture = todo.pop()
|
|
|
|
|
check:
|
|
|
|
|
not popFuture.finished
|
|
|
|
|
|
|
|
|
|
await fireNewNodesDiscoveredEvent(@[nid])
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
popFuture.finished
|
|
|
|
|
popFuture.value.tryGet() == nid
|
2025-02-12 15:39:52 +01:00
|
|
|
|
|
|
|
|
test "pop updates todo metric":
|
|
|
|
|
await fireNewNodesDiscoveredEvent(@[nid])
|
|
|
|
|
|
|
|
|
|
discard (await todo.pop()).tryGet()
|
|
|
|
|
|
|
|
|
|
check:
|
|
|
|
|
metrics.todo == 0
|