/*GRB*

    Gerbera - https://gerbera.io/

    menu.spec.js - this file is part of Gerbera.

    Copyright (C) 2016-2025 Gerbera Contributors

    Gerbera is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2
    as published by the Free Software Foundation.

    Gerbera is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Gerbera.  If not, see <http://www.gnu.org/licenses/>.

    $Id$
*/

const { expect } = require('chai');

const TestUtils = require('./page/test-utils');
const HomePage = require('./page/home.page');
const LoginPage = require('./page/login.page');

let driver;

describe('Menu Suite', () => {
  let loginPage, homePage;

  before(async () => {
    driver = await TestUtils.newChromeDriver();
    await TestUtils.resetSuite('menu.spec.json', driver);

    loginPage = new LoginPage(driver);
    homePage = new HomePage(driver);

    await TestUtils.resetCookies(driver);
    await loginPage.get(TestUtils.home());
  });

  after(() => driver && driver.quit());

  describe('The menu bar', () => {
    it('without logging in the database and filetype menus are disabled', async () => {
      let disabled = await homePage.isDisabled('nav-db');
      expect(disabled).to.be.true;

      disabled = await homePage.isDisabled('nav-fs');
      expect(disabled).to.be.true;
    });

    it('enables the database and filetype menus upon login', async () => {
      await loginPage.password('pwd');
      await loginPage.username('user');
      await loginPage.submitLogin();

      let menu = await homePage.getDatabaseMenu();
      let menuClass = await menu.getAttribute('class');
      expect(menuClass).to.equal('nav-link');

      menu = await homePage.getFileSystemMenu();
      menuClass = await menu.getAttribute('class');
      expect(menuClass).to.equal('nav-link');
    });

    it('loads the parent database container list when clicking Database', async () => {
      await homePage.clickMenu('nav-db');
      const tree = await homePage.treeItems();
      expect(tree.length).to.equal(6);
    });

    it('loads the clients list when clicking Clients', async () => {
      await homePage.clickMenu('nav-clients');
      const clients = await homePage.clients();
      expect(clients.length).to.equal(5);  // head counts also
    });

    it('loads the parent filesystem container list when clicking Filesystem', async () => {
      await homePage.clickMenu('nav-fs');
      const tree = await homePage.treeItems();
      expect(tree.length).to.equal(19);
    });

    it('clears the tree when Home menu is clicked', async () => {
      await homePage.clickMenu('nav-home');
      const tree = await homePage.treeItems();
      expect(tree.length).to.equal(0);
    });

    it('shows the friendly name in the document title', async () => {
      const title = await homePage.getTitle();
      expect(title).to.equal('Wombat | Gerbera Media Server')
    });

    it('shows the version in the menu', async () => {
      const version = await homePage.getVersion();
      const text = await version.getText();
      expect(text).to.equal('2.6.0');
    });

    it('loads the parent database container list when clicking Database Icon', async () => {
      await homePage.clickMenuIcon('nav-db');
      const tree = await homePage.treeItems();
      expect(tree.length).to.equal(6);
    });
  });
});
