ssbc / ssb-blobs

blob gossiping ssb-subprotocol
MIT License
12 stars 11 forks source link

broken test in test/secret-stack.js #18

Closed austinfrey closed 5 years ago

austinfrey commented 5 years ago

I think there is a broken test in test/secret-stack.js. this test, https://github.com/ssbc/ssb-blobs/blob/master/test/secret-stack.js#L45, passes because both values are undefined at this point in the test. you can see this when adding a log statement like i've done below. the other issue i'm seeing is that while the blob does indeed get written, the callback is never called with the returned hash value. i added a another t.ok() in the callback below and this never get's called.

var SecretStack = require('secret-stack')
var crypto      = require('crypto')
var tape        = require('tape')
var path        = require('path')
var osenv       = require('osenv')
var mkdirp      = require('mkdirp')
var pull        = require('pull-stream')

//deterministic keys make testing easy.
function hash (s) {
  return crypto.createHash('sha256').update(s).digest()
}

var appkey = hash('TESTBLOBS')

var create = SecretStack({ caps: {shs: appkey} }).use(require('../'))

function tmp (name) {
  var dir = path.join(osenv.tmpdir(), 'testblobs-'+Date.now()+'-'+name)
  mkdirp.sync(dir)
  return dir
}

tape('alice pushes to bob', function (t) {
  var alice = create({ seed: hash('ALICE'), path: tmp('alice') })
  var bob = create({ seed: hash('BOB'), path: tmp('bob') })

  // Avoid race because of async server creation, introduced secret-stack@6.
  //
  // This `once()` function must be run before the event is emit, which is why
  // `create()` must be run directly above. If `once()` is called much later
  // than `create()` then the event is emit before the listener is created.
  bob.once('multiserver:listening', function () {
    alice.connect(bob.address(), function (err, rpc) {
      if(err) throw err
    })

    var hello = new Buffer('Hello World'), _hash

    pull(
      bob.blobs.ls({live: true, long: true}),
      pull.take(1),
      pull.collect(function (err, ary) {
        console.log(ary[0].id, _hash) // prints `undefined undefined`
        t.equal(ary[0].id, _hash)
        t.end()
        alice.close()
        bob.close()
      })
    )

    pull(
      pull.values([hello]),
      alice.blobs.add(function (err, hash) {
        to.ok() // this test will never run
        _hash = hash
        alice.blobs.push(hash)
      })
    )
  })
})
austinfrey commented 5 years ago

i think the issue is here: https://github.com/ssbc/ssb-blobs/blob/master/inject.js#L47

this wrap function is used to wrap blobs.add here: https://github.com/ssbc/ssb-blobs/blob/master/inject.js#L296

it looks like the cb would never make it to the front of the line in the event an id is never passed in.