<commit_before>var assert = require('chai').assert;
var $rdf = require('rdflib');
var ns = require('../lib/vocab/ns.js').ns;
var LDP = require('../lib/ldp');

// Helper functions for the FS
var rm = require('./test-utils').rm;
var write = require('./test-utils').write;
var cp = require('./test-utils').cp;
var read = require('./test-utils').read;

describe('LDP', function () {
  var ldp = new LDP();

  describe('readFile', function () {
    it ('return 404 if file does not exist', function (done) {
      ldp.readFile('resources/unexistent.ttl', function(err) {
        assert.equal(err.status, 404);
        done();
      });
    });

    it ('return file if file exists', function (done) {
      // file can be empty as well
      write('hello world', 'fileExists.txt');
      ldp.readFile(__dirname + '/resources/fileExists.txt', function(err, file) {
        rm('fileExists.txt');
        assert.notOk(err);
        assert.equal(file, 'hello world');
        done();
      });
    });
  });

  describe('readContainerMeta', function () {
    it ('should return 404 if .meta is not found', function (done) {
      ldp.readContainerMeta('resources/', function(err) {
        assert.equal(err.status, 404);
        done();
      });
    });

    it ('should return content if metaFile exists', function (done) {
      // file can be empty as well
      write('This function just reads this, does not parse it', '.meta');
      ldp.readContainerMeta(__dirname + '/resources/', function(err, metaFile) {
        rm('.meta');
        assert.notOk(err);
        assert.equal(metaFile, 'This function just reads this, does not parse it');
        done();
      });
    });

    it ('should work also if trailing `/` is not passed', function (done) {
      // file can be empty as well
      write('This function just reads this, does not parse it', '.meta');
      ldp.readContainerMeta(__dirname + '/resources', function(err, metaFile) {
        rm('.meta');
        assert.notOk(err);
        assert.equal(metaFile, 'This function just reads this, does not parse it');
        done();
      });
    });
  });

  describe('put', function() {
    it('should write a file in an existing dir', function(done) {
      ldp.put(__dirname + '/resources/testPut.txt', 'hello world', function (err) {
        assert.notOk(err);
        var found = read('testPut.txt');
        rm('testPut.txt');
        assert.equal(found, 'hello world');
        done();
      });
    });

    it('should fail if a trailing `/` is passed', function(done) {
      ldp.put(__dirname + '/resources/', 'hello world', function (err) {
        assert.equal(err.status, 409);
        done();
      });
    });
  });

  describe('listContainer', function () {
    it('should ldp:contains the same amount of files in dir', function(done) {
      ldp.listContainer(__dirname + '/resources/sampleContainer/', 'https://server.tld', '', function (err, data) {

        var graph = $rdf.graph();
        $rdf.parse(
          data,
          graph,
          'https://server.tld/sampleContainer',
          'text/turtle');

        var statements = graph.each(
          undefined,
          ns.ldp('contains'),
          undefined);

        assert.notEqual(graph.statements.length, 0);
        assert.equal(statements.length, 8);
        assert.notOk(err);
        done();
      });
    });
  });
});<commit_msg>Further tests for directory listing, fix #98<commit_after>var assert = require('chai').assert;
var $rdf = require('rdflib');
var ns = require('../lib/vocab/ns.js').ns;
var LDP = require('../lib/ldp');

// Helper functions for the FS
var rm = require('./test-utils').rm;
var write = require('./test-utils').write;
var cp = require('./test-utils').cp;
var read = require('./test-utils').read;
var fs = require('fs');

describe('LDP', function () {
  var ldp = new LDP();

  describe('readFile', function () {
    it ('return 404 if file does not exist', function (done) {
      ldp.readFile('resources/unexistent.ttl', function(err) {
        assert.equal(err.status, 404);
        done();
      });
    });

    it ('return file if file exists', function (done) {
      // file can be empty as well
      write('hello world', 'fileExists.txt');
      ldp.readFile(__dirname + '/resources/fileExists.txt', function(err, file) {
        rm('fileExists.txt');
        assert.notOk(err);
        assert.equal(file, 'hello world');
        done();
      });
    });
  });

  describe('readContainerMeta', function () {
    it ('should return 404 if .meta is not found', function (done) {
      ldp.readContainerMeta('resources/', function(err) {
        assert.equal(err.status, 404);
        done();
      });
    });

    it ('should return content if metaFile exists', function (done) {
      // file can be empty as well
      write('This function just reads this, does not parse it', '.meta');
      ldp.readContainerMeta(__dirname + '/resources/', function(err, metaFile) {
        rm('.meta');
        assert.notOk(err);
        assert.equal(metaFile, 'This function just reads this, does not parse it');
        done();
      });
    });

    it ('should work also if trailing `/` is not passed', function (done) {
      // file can be empty as well
      write('This function just reads this, does not parse it', '.meta');
      ldp.readContainerMeta(__dirname + '/resources', function(err, metaFile) {
        rm('.meta');
        assert.notOk(err);
        assert.equal(metaFile, 'This function just reads this, does not parse it');
        done();
      });
    });
  });

  describe('put', function() {
    it('should write a file in an existing dir', function(done) {
      ldp.put(__dirname + '/resources/testPut.txt', 'hello world', function (err) {
        assert.notOk(err);
        var found = read('testPut.txt');
        rm('testPut.txt');
        assert.equal(found, 'hello world');
        done();
      });
    });

    it('should fail if a trailing `/` is passed', function(done) {
      ldp.put(__dirname + '/resources/', 'hello world', function (err) {
        assert.equal(err.status, 409);
        done();
      });
    });
  });

  describe('listContainer', function () {
    it('should inherit type if file is .ttl', function(done) {
      write('@prefix dcterms: <http://purl.org/dc/terms/>.' +
            '@prefix o: <http://example.org/ontology>.' +
            '<> a <http://www.w3.org/ns/ldp#MagicType> ;' +
            '   dcterms:title "This is a magic type" ;' +
            '   o:limit 500000.00 .', 'sampleContainer/magicType.ttl');
      
      ldp.listContainer(__dirname + '/resources/sampleContainer/', 'https://server.tld', '', function (err, data) {
        var graph = $rdf.graph();
        $rdf.parse(
          data,
          graph,
          'https://server.tld/sampleContainer',
          'text/turtle');

        var statements = graph
          .each(
            $rdf.sym('https://server.tld/magicType.ttl'),
            ns.rdf('type'),
            undefined)
          .map(function(d) {
            return d.uri;
          });

        assert.equal(statements.length, 2);
        assert.isAbove(statements.indexOf('http://www.w3.org/ns/ldp#MagicType'), -1);
        assert.isAbove(statements.indexOf('http://www.w3.org/ns/posix/stat#File'), -1);

        rm('sampleContainer/magicType.ttl');
        done();
      });
    });

    it('should not inherit type of BasicContainer/Container if type is File', function(done) {
      write('@prefix dcterms: <http://purl.org/dc/terms/>.' +
            '@prefix o: <http://example.org/ontology>.' +
            '<> a <http://www.w3.org/ns/ldp#Container> ;' +
            '   dcterms:title "This is a container" ;' +
            '   o:limit 500000.00 .', 'sampleContainer/containerFile.ttl');

      write('@prefix dcterms: <http://purl.org/dc/terms/>.' +
            '@prefix o: <http://example.org/ontology>.' +
            '<> a <http://www.w3.org/ns/ldp#BasicContainer> ;' +
            '   dcterms:title "This is a container" ;' +
            '   o:limit 500000.00 .', 'sampleContainer/basicContainerFile.ttl');

      ldp.listContainer(__dirname + '/resources/sampleContainer/', 'https://server.tld', '', function (err, data) {
        var graph = $rdf.graph();
        $rdf.parse(
          data,
          graph,
          'https://server.tld/sampleContainer',
          'text/turtle');

        var basicContainerStatements = graph.each(
          $rdf.sym('https://server.tld/basicContainerFile.ttl'),
          ns.rdf('type'),
          undefined);

        assert.equal(basicContainerStatements.length, 1);

        var containerStatements = graph.each(
          $rdf.sym('https://server.tld/containerFile.ttl'),
          ns.rdf('type'),
          undefined);

        assert.equal(containerStatements.length, 1);

        basicContainerStatements.forEach(function(statement) {
          assert.equal(statement.uri, ns.stat('File').uri);
        });

        containerStatements.forEach(function(statement) {
          assert.equal(statement.uri, ns.stat('File').uri);
        });

        rm('sampleContainer/containerFile.ttl');
        rm('sampleContainer/basicContainerFile.ttl');
        done();
      });
    });

    it('should ldp:contains the same amount of files in dir', function(done) {
      ldp.listContainer(__dirname + '/resources/sampleContainer/', 'https://server.tld', '', function (err, data) {
        fs.readdir(__dirname + '/resources/sampleContainer/', function(err, files) {
          var graph = $rdf.graph();
          $rdf.parse(
            data,
            graph,
            'https://server.tld/sampleContainer',
            'text/turtle');

          var statements = graph.each(
            undefined,
            ns.ldp('contains'),
            undefined);

          assert.notEqual(graph.statements.length, 0);
          assert.equal(statements.length, files.length);
          assert.notOk(err);
          done();
        });
      });
    });
  });
});
{'fraction_non_alphanumeric': 0.1671845244061341, 'fraction_numerical': 0.006915906585145836, 'mean_word_length': 2.312749003984064, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 11, 'lorem ipsum': 0, 'www.': 5, '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': '1617099', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2931, 'n_words': 680}