<commit_before>var Notify = require('pull-notify')
var crypto = require('crypto')
var tape = require('tape')
var Blobs = require('../')
var pull = require('pull-stream')

var MockBlobStore = require('./mock')

function createHash () {
  var args = [].slice.call(arguments)
  var o = {}
  for(var i = 0; i < args.length; i+=2) o[args[i]] = args[i+1]
  return o
}

//create random value that looks like a blob id.
function rand() {
  return '&'+crypto.randomBytes(32).toString('base64')+'.sha256'
}

function Fake(string, length) {
  var b = new Buffer(length)
  var n = Buffer.byteLength(string)
  for(var i = 0; i < length; i += n)
    b.write(string, i)
  return b
}

function hash(buf) {
  buf = 'string' == typeof buf ? new Buffer(buf) : buf
  return '&'+crypto.createHash('sha256')
            .update(buf).digest('base64')+'.sha256'
}


tape('simple', function (t) {
  var blobs = Blobs(MockBlobStore())

  var b = Fake('hello', 256), h = hash(b)
  pull(pull.once(b), blobs.add())

  var req = {}
  req[h] = 0
  var res = {}
  res[h] = 256

  pull(
    pull.once(req),
    blobs.createWantStream(),
    pull.find(null, function (err, _res) {
      if(err) throw err
      //requests 
      t.deepEqual(_res, res)
      t.end()
    })
  )
})

tape('want', function (t) {
  var blobs = Blobs(MockBlobStore())
  var h = hash(Fake('foobar', 64))
  var res = {}
  res[h] = -1

  pull(
    blobs.createWantStream(),
    pull.find(null, function (err, _res) {
      if(err) throw err
      //requests 
      t.deepEqual(_res, res)
      t.end()
    })
  )

  blobs.want(h)
})

function log (name) {
  return pull.through(function (e) {
    console.log(name, e)
  })
}

function peers (nameA, a, nameB, b) {
  var as = a.createWantStream.call({id: nameB, blobs: b})
  var bs = b.createWantStream.call({id: nameA, blobs: a})
  var na = nameA[0].toUpperCase(), nb = nameB[0].toUpperCase()
  pull(as, log(na+nb), bs, log(nb+na), as)
}

tape('want - has', function (t) {
  var alice = Blobs(MockBlobStore())
  var bob   = Blobs(MockBlobStore())
  var blob = Fake('foobar', 64)
  var h = hash(blob)

  peers('alice', alice, 'bob', bob)

  alice.want(h, function (err) {
    t.end()
  })

  pull(pull.once(blob), bob.add())
})

tape('want - want -has', function (t) {
  var alice = Blobs(MockBlobStore())
  var bob   = Blobs(MockBlobStore())
  var carol = Blobs(MockBlobStore())

  var blob = Fake('baz', 64)
  var h = hash(blob)

  peers('alice', alice, 'bob', bob)
  peers('bob', bob, 'carol', carol)

  alice.want(h, function (err) {
    t.end()
  })

  pull(pull.once(blob), carol.add())
})


<commit_msg>test where peers request a thing sympathetically<commit_after>var Notify = require('pull-notify')
var crypto = require('crypto')
var tape = require('tape')
var Blobs = require('../')
var pull = require('pull-stream')

var MockBlobStore = require('./mock')

function createHash () {
  var args = [].slice.call(arguments)
  var o = {}
  for(var i = 0; i < args.length; i+=2) o[args[i]] = args[i+1]
  return o
}

//create random value that looks like a blob id.
function rand() {
  return '&'+crypto.randomBytes(32).toString('base64')+'.sha256'
}

function Fake(string, length) {
  var b = new Buffer(length)
  var n = Buffer.byteLength(string)
  for(var i = 0; i < length; i += n)
    b.write(string, i)
  return b
}

function hash(buf) {
  buf = 'string' == typeof buf ? new Buffer(buf) : buf
  return '&'+crypto.createHash('sha256')
            .update(buf).digest('base64')+'.sha256'
}


tape('simple', function (t) {
  var blobs = Blobs(MockBlobStore())

  var b = Fake('hello', 256), h = hash(b)
  pull(pull.once(b), blobs.add())

  var req = {}
  req[h] = 0
  var res = {}
  res[h] = 256

  pull(
    pull.once(req),
    blobs.createWantStream(),
    pull.find(null, function (err, _res) {
      if(err) throw err
      //requests 
      t.deepEqual(_res, res)
      t.end()
    })
  )
})

tape('want', function (t) {
  var blobs = Blobs(MockBlobStore())
  var h = hash(Fake('foobar', 64))
  var res = {}
  res[h] = -1

  pull(
    blobs.createWantStream(),
    pull.find(null, function (err, _res) {
      if(err) throw err
      //requests 
      t.deepEqual(_res, res)
      t.end()
    })
  )

  blobs.want(h)
})

function log (name) {
  return pull.through(function (e) {
    console.log(name, e)
  })
}

function peers (nameA, a, nameB, b) {
  var as = a.createWantStream.call({id: nameB, blobs: b})
  var bs = b.createWantStream.call({id: nameA, blobs: a})
  var na = nameA[0].toUpperCase(), nb = nameB[0].toUpperCase()
  pull(as, log(na+nb), bs, log(nb+na), as)
}

tape('want - has', function (t) {
  var alice = Blobs(MockBlobStore())
  var bob   = Blobs(MockBlobStore())
  var blob = Fake('foobar', 64)
  var h = hash(blob)

  peers('alice', alice, 'bob', bob)

  alice.want(h, function (err) {
    t.end()
  })

  pull(pull.once(blob), bob.add())
})

tape('want - want -has', function (t) {
  var alice = Blobs(MockBlobStore())
  var bob   = Blobs(MockBlobStore())
  var carol = Blobs(MockBlobStore())

  var blob = Fake('baz', 64)
  var h = hash(blob)

  peers('alice', alice, 'bob', bob)
  peers('bob', bob, 'carol', carol)

  alice.want(h, function (err) {
    t.end()
  })

  pull(pull.once(blob), carol.add())
})

tape('peers want what you have', function (t) {
  var alice = Blobs(MockBlobStore())
  var bob   = Blobs(MockBlobStore())
  var carol = Blobs(MockBlobStore())

  var blob = Fake('baz', 64)
  var h = hash(blob)

  peers('alice', alice, 'bob', bob)
  peers('bob', bob, 'carol', carol)

//  alice.want(h, function (err) {
////    t.end()
//  })

  pull(
    carol.changes(),
    pull.drain(function (_h) {
      t.equal(_h, h)
      t.end()
    })
  )

  alice.want(h, function () {})
  pull(pull.once(blob), alice.add())

})


{'fraction_non_alphanumeric': 0.18634942688433484, 'fraction_numerical': 0.012504341785342132, 'mean_word_length': 3.143165467625899, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6222038', 'n_tokens_mistral': 2327, 'n_tokens_neox': 2220, 'n_words': 538}