const indexFile = require('../../src/inverted-index.js');

const content = `[
            {
              "title": "Alice in Wonderland",
              "text": "Alice falls into a rabbit hole and enters a world full of imagination."
            },

            {
              "title": "The Lord of the Rings: The Fellowship of the Ring.",
              "text": "An unusual alliance of man, elf, dwarf, wizard and hobbit seek to destroy a powerful ring."
            }
          ]`;
const filename = 'books.json';
describe('Inverted Index', () => {
  const index = new indexFile.Index();

  describe('Read book data', () => {
    it('checks that json file is not empty', () => {
      expect(() => {
        index.createIndex({}, filename);
      }).toThrow(new Error('the file is empty'));
    });

    it('checks that json file is valid', () => {
      expect(() => {
        index.createIndex('Invalid', filename);
      }).toThrow(new Error('Invalid json file'));
    });
    it('checks that json is valid format', () => {
      expect(() => {
        index.createIndex('[{"key": "value"}]', filename);
      }).toThrow(new Error('Bad JSON format'));
    });
  });

  describe('Populate Index', () => {
    it('checks if index is created', () => {
      index.createIndex(content, filename);
      expect(index.getIndex()).toBeDefined();
      expect(index.getIndex()).not.toEqual({});
    });
    it('ensures index is correct', () => {
      index.createIndex(content, filename);
      const indices = index.getIndex();
      expect(indices['books.json'].alice).toEqual([0]);
      expect(indices['books.json'].of).toEqual([0, 1]);
    });
  });

  describe('Search Index', () => {
    it('checks search index returns correct result', () => {
      index.createIndex(content, filename);
      expect(index.searchIndex('books.json', 'alice')).toEqual({ 'books.json': { alice: [0] } });
      expect(index.searchIndex('books.json', 'lord')).toEqual({ 'books.json': { lord: [1] } });
      expect(index.searchIndex('books.json', 'of')).toEqual({ 'books.json': { of: [0, 1] } });
    });
    it('ensures searchIndex can handle an array of search terms', () => {
      index.createIndex(content, filename);
      expect(() => {
        index.searchIndex('books.json', ['a', 'alice'], 'book', 'me', ['help', ['me', 'out']]);
      }).not.toThrow(new Error());
      expect(index.searchIndex('books.json', ['a', 'alice'], 'book', 'me', ['help', ['me', 'out']]))
      .toEqual({ 'books.json': { a: [0, 1], alice: [0] } });
    });
    it('ensures searchIndex can handle a varied number of arguments', () => {
      index.createIndex(content, filename);
      expect(() => {
        index.searchIndex('books.json', 'arg1');
        index.searchIndex('books.json', 'arg1', 'arg2');
        index.searchIndex('books.json', 'arg1', 'arg2', 'arg3');
      }).not.toThrow(new Error());
    });
    it('ensures filename is optional', () => {
      index.createIndex(content, filename);
      index.createIndex(content, 'morebooks.json');
      expect(index.searchIndex('alice')).toEqual({ 'books.json': { alice: [0] }, 'morebooks.json': { alice: [0] } });
      expect(index.searchIndex('books.json', 'alice')).toEqual({ 'books.json': { alice: [0] } });
    });
  });
});


@@ -75,34 +75,46 @@
  %60%5B%0A            
-%7B%0A
+      %7B%0A      
               %22t
@@ -143,16 +143,22 @@
 rland%22,%0A
+      
         
@@ -260,12 +260,22 @@
     
-%7D,%0A%0A
+      %7D,%0A%0A    
     
@@ -274,34 +274,42 @@
                 
-%7B%0A
+  %7B%0A      
               %22t
@@ -369,16 +369,22 @@
 Ring.%22,%0A
+      
         
@@ -502,16 +502,22 @@
         
+      
 %7D%0A      
@@ -524,12 +524,19 @@
     
+ 
+     
 %5D%60;%0A
+%0A
 cons
@@ -1016,32 +1016,33 @@
 ile'));%0A    %7D);%0A
+%0A
     it('checks t
@@ -1210,33 +1210,32 @@
 ;%0A    %7D);%0A  %7D);%0A
-%0A
   describe('Popu
@@ -1438,32 +1438,33 @@
 al(%7B%7D);%0A    %7D);%0A
+%0A
     it('ensures 
@@ -2130,32 +2130,33 @@
 %5D %7D %7D);%0A    %7D);%0A
+%0A
     it('ensures 
@@ -2571,32 +2571,33 @@
 %5D %7D %7D);%0A    %7D);%0A
+%0A
     it('ensures 
@@ -2933,24 +2933,25 @@
 ));%0A    %7D);%0A
+%0A
     it('ensu

change use of let and const, spacing and reformatted regex