var parseq = use('parseq', $delay)
var time = use('time')

return {
    test_comments: function() {
        // This test sets up a receiver.
        // In a module test, this might not persist if the runner exits.
        // Wrapped for compatibility.
        
        function load_comment_from_api_requestor(id) {
          return function(cb) {
            return $delay(() => cb({ id, title: `Comment #${id}` }), 0.5)
            // returning the $delay return lets them be cancelled up the chain
          }
        }

        $receiver(tree => {
          var child_reqs = array(tree.children, child => cb => {
            $start(e => send(e.actor, child, cb), "tests/comments")
          })

          var job = parseq.par_all({
            comment: load_comment_from_api_requestor(tree.id),
            children: cb => parseq.par_all(child_reqs, /*time*/null, /*thr*/10)(cb)
          })

          job((result, reason) => {
            if (!result) {
              log.error(reason)
              send(tree, reason)
            }
            
            var obj = object(result.comment)
            obj.children = result.children
            obj.time = time.text()
            send(tree, obj)
          })
        })
    }
}
