<commit_before>'use strict';

var fs = require('fs');

var lab = exports.lab = require('lab').script();
var code = require('code');

var Workspace = require('../lib/workspace');

lab.experiment('Workspace', function(){

  var space;
  var tmpFilepath = '.tmp/test.js';

  lab.beforeEach(function(done){
    space = new Workspace();
    done();
  });

  lab.test('#filename is a cursor for the current filename', function(done){
    code.expect(space.filename.deref).to.be.a.function();
    code.expect(space.filename.deref()).to.equal('');
    done();
  });

  lab.test('#filename allows updating current filename through update', function(done){
    var newFilename = 'test.js';

    code.expect(space.filename.update).to.be.a.function();

    space.filename.update(function(){
      return newFilename;
    });

    code.expect(space.filename.deref()).to.equal(newFilename);
    done();
  });

  lab.test('#current is a cursor for current file', function(done){
    code.expect(space.current.deref).to.be.a.function();
    code.expect(space.current.deref()).to.equal('function helloWorld(hello){\n  hello = "world";\n}');
    done();
  });

  lab.test('#current allows updating current data through .update', function(done){
    var newText = 'function helloWorld(hello){\n  foo = "bar";\n}';

    code.expect(space.current.update).to.be.a.function();

    space.current.update(function(){
      return newText;
    });

    code.expect(space.current.deref()).to.equal(newText);
    done();
  });

  lab.test('#saveFile will mkdirp and save a file', function(done){
    var newText = 'function helloWorld(hello){\n  foo = "bar";\n}';

    space.saveFile(tmpFilepath, newText, function(err){
      var saved = fs.readFileSync(tmpFilepath, 'utf8');

      code.expect(err).to.not.exist();
      code.expect(saved).to.equal(newText);
      done(err);
    });
  });

  lab.test('#saveFile also handles cursors', function(done){
    var newText = 'function helloWorld(hello){\n  my = "cursor";\n}';

    space.current.update(function(){
      return newText;
    });

    space.saveFile(tmpFilepath, space.current, function(err){
      var saved = fs.readFileSync(tmpFilepath, 'utf8');

      code.expect(err).to.not.exist();
      code.expect(saved).to.equal(newText);
      done(err);
    });
  });

  lab.test('#saveFile adds the file to the directory listing', function(done){
    var newText = 'function helloWorld(hello){\n  foo = "bar";\n}';

    space.saveFile(tmpFilepath, newText, function(err){
      code.expect(err).to.not.exist();
      code.expect(space.directory.contains(tmpFilepath)).to.equal(true);
      done(err);
    });
  });

  lab.test('#saveFile does not add the file to the directory listing twice upon 2nd save', function(done){
    var newText = 'function helloWorld(hello){\n  foo = "bar";\n}';

    space.saveFile(tmpFilepath, newText, function(err){
      code.expect(err).to.not.exist();
      space.saveFile(tmpFilepath, newText, function(err2){
        code.expect(err2).to.not.exist();
        code.expect(space.directory.indexOf(tmpFilepath)).to.equal(space.directory.lastIndexOf(tmpFilepath));
        done(err2);
      });
    });
  });

  lab.test('#saveFile can take workspace.current', function(done){
    space.saveFile(tmpFilepath, space.current, function(err){
      var saved = fs.readFileSync(tmpFilepath, 'utf8');

      code.expect(err).to.not.exist();
      code.expect(space.current.deref()).to.equal(saved);
      done(err);
    });
  });

  lab.test('#loadFile will load a file into current', function(done){
    space.loadFile(tmpFilepath, function(err, contents){
      code.expect(err).to.not.exist();
      code.expect(contents).to.equal(space.current);
      done();
    });
  });

  lab.test('#directory should be a list of files in a directory', function(done){
    code.expect(space.directory.deref).to.be.a.function();
    code.expect(space.directory.deref().size).to.equal(0);
    done();
  });
<commit_msg>clearer message on test - slight reorg<commit_after>'use strict';

var fs = require('fs');

var lab = exports.lab = require('lab').script();
var code = require('code');

var Workspace = require('../lib/workspace');

lab.experiment('Workspace', function(){

  var space;
  var tmpFilepath = '.tmp/test.js';

  lab.beforeEach(function(done){
    space = new Workspace();
    done();
  });

  lab.test('#filename is a cursor for the current filename', function(done){
    code.expect(space.filename.deref).to.be.a.function();
    code.expect(space.filename.deref()).to.equal('');
    done();
  });

  lab.test('#filename allows updating current filename through update', function(done){
    var newFilename = 'test.js';

    code.expect(space.filename.update).to.be.a.function();

    space.filename.update(function(){
      return newFilename;
    });

    code.expect(space.filename.deref()).to.equal(newFilename);
    done();
  });

  lab.test('#current is a cursor for current file', function(done){
    code.expect(space.current.deref).to.be.a.function();
    code.expect(space.current.deref()).to.equal('function helloWorld(hello){\n  hello = "world";\n}');
    done();
  });

  lab.test('#current allows updating current data through .update', function(done){
    var newText = 'function helloWorld(hello){\n  foo = "bar";\n}';

    code.expect(space.current.update).to.be.a.function();

    space.current.update(function(){
      return newText;
    });

    code.expect(space.current.deref()).to.equal(newText);
    done();
  });

  lab.test('#directory should default to an empty list', function(done){
    code.expect(space.directory.deref).to.be.a.function();
    code.expect(space.directory.size).to.equal(0);
    done();
  });

  lab.test('#saveFile will mkdirp and save a file', function(done){
    var newText = 'function helloWorld(hello){\n  foo = "bar";\n}';

    space.saveFile(tmpFilepath, newText, function(err){
      var saved = fs.readFileSync(tmpFilepath, 'utf8');

      code.expect(err).to.not.exist();
      code.expect(saved).to.equal(newText);
      done(err);
    });
  });

  lab.test('#saveFile also handles cursors', function(done){
    var newText = 'function helloWorld(hello){\n  my = "cursor";\n}';

    space.current.update(function(){
      return newText;
    });

    space.saveFile(tmpFilepath, space.current, function(err){
      var saved = fs.readFileSync(tmpFilepath, 'utf8');

      code.expect(err).to.not.exist();
      code.expect(saved).to.equal(newText);
      done(err);
    });
  });

  lab.test('#saveFile adds the file to the directory listing', function(done){
    var newText = 'function helloWorld(hello){\n  foo = "bar";\n}';

    space.saveFile(tmpFilepath, newText, function(err){
      code.expect(err).to.not.exist();
      code.expect(space.directory.contains(tmpFilepath)).to.equal(true);
      done(err);
    });
  });

  lab.test('#saveFile does not add the file to the directory listing twice upon 2nd save', function(done){
    var newText = 'function helloWorld(hello){\n  foo = "bar";\n}';

    space.saveFile(tmpFilepath, newText, function(err){
      code.expect(err).to.not.exist();
      space.saveFile(tmpFilepath, newText, function(err2){
        code.expect(err2).to.not.exist();
        code.expect(space.directory.indexOf(tmpFilepath)).to.equal(space.directory.lastIndexOf(tmpFilepath));
        done(err2);
      });
    });
  });

  lab.test('#saveFile can take workspace.current', function(done){
    space.saveFile(tmpFilepath, space.current, function(err){
      var saved = fs.readFileSync(tmpFilepath, 'utf8');

      code.expect(err).to.not.exist();
      code.expect(space.current.deref()).to.equal(saved);
      done(err);
    });
  });

  lab.test('#loadFile will load a file into current', function(done){
    space.loadFile(tmpFilepath, function(err, contents){
      code.expect(err).to.not.exist();
      code.expect(contents).to.equal(space.current);
      done();
    });
  });

{'fraction_non_alphanumeric': 0.17627801561319567, 'fraction_numerical': 0.002014605892722236, 'mean_word_length': 3.5597014925373136, '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': '6841627', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2498, 'n_words': 468}