<commit_before>var Server = require('../src');
var Client = require('digger-client');
var through = require('through2')
var from = require('from2-array')

function getBasicServer(){

  var digger = Server()

  digger.warehouse({
    select:function(req){
  
      return function(path){

        return from.obj([{
          _digger:{
            path:'/apples/red',
            inode:'bigred',
            tag:'fruit',
            class:['red']
          },
          name:'test'
        }])

      }
    }
  })

  digger.access(function(path, user, mode, next){
    console.log('-------------------------------------------');
    console.log('user')
    console.dir(path)
    console.log('path: ' + path)
    console.log('user: ' + user)
    console.log('mode: ' + mode)

    next()
  })

  digger.on('request', function(type, req){
    console.log('-------------------------------------------');
    console.log(type)
    console.dir(req.url)
    console.dir(req.headers)
  })

  digger.on('event', function(type, name, data){
    console.log('event:' + type + ' ' + name)
    console.dir(data)
  })

  return digger

}

describe('diggerserver', function(){

  describe('constructor', function(){
  
    it('should be a function', function(){
      Server.should.be.type('function');
    })

    it('should create a digger server which should be an event emitter', function(done){
      var diggerdb = Server();

      diggerdb.on('apples', done);
      diggerdb.emit('apples');
    })

  })


  describe('basic queries', function(){

    it('should run with basic access control', function(done){

      var client = Client();
      var digger = Server()

      digger.warehouse({
        select:function(req){
      
          return function(path){

            return from.obj([{
              _digger:{
                path:'/apples/red',
                inode:'bigred',
                tag:'fruit',
                class:['red']
              },
              name:'test'
            }])

          }
        }
      })

      digger.access(function(path, user, mode, next){
        
        path.should.equal('/apples/red/bigred')
        user.should.equal('bob')
        mode.should.equal('read')

        next()
      })

      client.on('request', function(req, res){
        req.headers['x-digger-user'] = 'bob'
        digger.reception(req, res)
      })

      var warehouse = client.connect('/apples');

      warehouse('fruit.red').ship(function(answers){

        answers.models[0].name.should.equal('test')
        done();
        
      })

    })

    it('run an append query with a stub warehouse', function(done){

      var digger = Server();
      var client = Client();

      digger.warehouse({
        append:function(req){
          return through.obj(function(chunk, env, cb){

            chunk._digger.tag.should.equal('folder')
            chunk._digger.path.should.equal('/apples')
            chunk._digger.class.length.should.equal(1)
            chunk._digger.class[0].should.equal('red')
            this.push({
              name:'test'
            })
            cb()
          })
        }
      })

      client.on('request', digger.reception.bind(digger));

      var warehouse = client.connect('/apples');

      var data = client.create('folder').addClass('red');

      warehouse.append(data).ship(function(answers){

        var models = answers.models

        models.length.should.equal(1)
        models[0].name.should.equal('test')

        done();
        
      })

    })

  })


})<commit_msg>test of direct request<commit_after>var Server = require('../src');
var Client = require('digger-client');
var through = require('through2')
var from = require('from2-array')


describe('diggerserver', function(){

  describe('constructor', function(){
  
    it('should be a function', function(){
      Server.should.be.type('function');
    })

    it('should create a digger server which should be an event emitter', function(done){
      var diggerdb = Server();

      diggerdb.on('apples', done);
      diggerdb.emit('apples');
    })

  })


  describe('basic queries', function(){

    it('should run with basic access control', function(done){

      var client = Client();
      var digger = Server()

      digger.warehouse({
        select:function(req){
      
          return function(path){

            return from.obj([{
              _digger:{
                path:'/apples/red',
                inode:'bigred',
                tag:'fruit',
                class:['red']
              },
              name:'test'
            }])

          }
        }
      })

      digger.access(function(path, user, mode, next){
        
        path.should.equal('/apples/red/bigred')
        user.should.equal('bob')
        mode.should.equal('read')

        next()
      })

      client.on('request', function(req, res){
        req.headers['x-digger-user'] = 'bob'
        digger.reception(req, res)
      })

      var warehouse = client.connect('/apples');

      warehouse('fruit.red').ship(function(answers){

        answers.models[0].name.should.equal('test')
        done();
        
      })

    })

    it('run an append query with a stub warehouse', function(done){

      var digger = Server();
      var client = Client();

      digger.warehouse({
        append:function(req){
          return through.obj(function(chunk, env, cb){

            chunk._digger.tag.should.equal('folder')
            chunk._digger.path.should.equal('/apples')
            chunk._digger.class.length.should.equal(1)
            chunk._digger.class[0].should.equal('red')
            this.push({
              name:'test'
            })
            cb()
          })
        }
      })

      client.on('request', digger.reception.bind(digger));

      var warehouse = client.connect('/apples');

      var data = client.create('folder').addClass('red');

      warehouse.append(data).ship(function(answers){

        var models = answers.models

        models.length.should.equal(1)
        models[0].name.should.equal('test')

        done();
        
      })

    })


    it('run a direct query', function(done){

      var digger = Server();
      
      digger.warehouse({
        select:function(req){
      
          return function(path){

            console.log('-------------------------------------------');
            console.log('-------------------------------------------');
            console.log('path')
            console.log(path)
            return from.obj([{
              _digger:{
                path:'/apples/red',
                inode:'bigred',
                tag:'fruit',
                class:['red']
              },
              name:'test'
            }])

          }
        }
      })

      var res = concat(function(results){
        console.log('-------------------------------------------');
        console.log('-------------------------------------------');
        console.dir('here')
        console.dir(results)
      })

      var req = from.obj([
        '/apples/blue'
      ])

      var req = {
        url:'/apples/blue?selector=apples.red',
        headers:{},
        method:'get'
      }

      digger.reception(req, res)

    })

  })


})
{'fraction_non_alphanumeric': 0.18468530505440023, 'fraction_numerical': 0.0019281090758848643, 'mean_word_length': 1.6141108711303096, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5779378', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2000, 'n_words': 357}