var storage = require("node-persist"); // library for read/write account on local container
var fs = require("fs");
var mainFunctions = require("../scripts/main");
var chai = require("../node_modules/chai"); // per importare il framework chai
var master = ""; // insert your master password

storage.initSync(); //initialise localStorage

//Suite test
describe("Main functions of Password Manager", () => {

    //test case
    it("Generate password", (done) => {

        var newPassword = mainFunctions.generatePassword();

        chai.expect(newPassword).to.be.not.equal(null);
        chai.expect(newPassword).to.be.not.equal("");

        done();
    });

    //test case
    it("Create account", (done) => {
        var account = { name: "testAccount", username: "testAccount", password: "testAccount" };
        var newAccount = mainFunctions.createAccount(account, master);

        chai.expect(newAccount).to.be.not.equal(null);
        done();
    });

    //test case
    it("Get account", (done) => {
        var account = mainFunctions.getAccount("testAccount", master);

        chai.expect(account).to.be.not.equal(null);

        done();
    });

    //test case
    it("Get accounts", (done)    =>  {

        var accounts = mainFunctions.getAccounts(master);

        if (accounts.length !== 0) {
            chai.expect(accounts).to.be.not.equal([])
        } else  {
            chai.expect(accounts).to.be.equal([])
        }

        done();
    })

    //test case
    it("Modify account", (done) => {
        var account = mainFunctions.getAccount("testAccount", master);
        var accountMod = { name:"testModifyAccount", username:"testModifyAccount", password: "testModifyAccount" };
        var newAccount = mainFunctions.modifyAccount(account, accountMod, master);

        chai.expect(newAccount).to.be.not.equal(null);

        done();
    });

    //test case
    it("Delete account", (done) => {
        mainFunctions.deleteAccount("testModifyAccount", master);
        var accountModified = mainFunctions.getAccount("testModifyAccount", master);

        chai.expect(accountModified).to.be.equal(null);

        done();
    });

    //test case
    it("Save accounts", (done)  =>  {

        var accounts = mainFunctions.getAccounts(master);
        mainFunctions.saveAccounts(accounts, master);

        var accountEncrypted = storage.getItemSync("accounts"); // create a variable for control the content of "accounts" file on filesystem
        chai.expect(accountEncrypted).to.be.not.equal("");
        chai.expect(accountEncrypted).to.be.not.equal(null);

        done();
    });

    //test case
    it("Decrypt and encrypt document", (done)   =>  {

        fs.readFile(__dirname + "test.txt", "", function (err, data) {

            var documentsEncrypted = mainFunctions.encryptDocument(data, master);

            chai.expect(documentsEncrypted).to.be.not.equal(null);

            var documentsDecrypted = mainFunctions.decryptDocument(documentsEncrypted.toString(), master)

            chai.expect(documentsDecrypted).to.be.not.equal(null);
        })
        

        done();

    });
});