import { AppPage } from "./app.po";
import {
  browser,
  by,
  element,
  ElementFinder,
  logging,
  protractor,
} from "protractor";
import { WorkspacesComponent } from "../../src/app/workspaces/workspaces.component";

describe("workspace-project Test Page Download capability", () => {
  let page: AppPage;

  beforeEach(() => {
    page = new AppPage();
    page.navigateToWorkspacesHome();
    expect(browser.getCurrentUrl()).toBe(`${browser.baseUrl}workspaces`);
  });

  it("should display science product text box for download parameters", () => {
    const splInputTxtBox: ElementFinder = element(by.css("#splInput"));
    const splInputLabel: ElementFinder = element(by.css("label[for=splInput]"));
    expect(splInputLabel.getText()).toEqual("Science Product");
    expect(splInputTxtBox.getAttribute("value")).toEqual(
      "uid://evla/image/3d3db489-9331-4e61-aa80-002bc2989b1e"
    );
  });

  it("should display email text box for download parameters", () => {
    const emailTxtBox: ElementFinder = element(by.css("#userEmail"));
    const emailLabel: ElementFinder = element(by.css("label[for=userEmail]"));
    expect(emailLabel.getText()).toEqual("Email Address");
    expect(emailTxtBox.getAttribute("value")).toEqual("");
  });

  it('should display a "launch download capability" button', () => {
    expect(page.getLaunchDownloadCapabilityButton().getText()).toEqual(
      "Launch download capability"
    );
  });

  it('should display capability request and execution when "launch download capability" button clicked', () => {
    const launchDownloadCapabilityButton: ElementFinder = element(
      by.css("#launchDownloadCapabilityBtn")
    );
    browser
      .wait(
        protractor.ExpectedConditions.elementToBeClickable(
          launchDownloadCapabilityButton
        ),
        5000
      )
      .then(() => {
        launchDownloadCapabilityButton.click();
      });

    browser.sleep(1000);
    expect(
      page.getCapabilityResults().element(by.css("#request-0")).getText()
    ).toContain("Capability request created: ");
    expect(
      page.getCapabilityResults().element(by.css("#execution-0")).getText()
    ).toContain("Capability execution created: ");
  });

  afterEach(async () => {
    // Assert that there are no errors emitted from the browser
    const logs = await browser.manage().logs().get(logging.Type.BROWSER);
    expect(logs).not.toContain(
      jasmine.objectContaining({
        level: logging.Level.SEVERE,
      } as logging.Entry)
    );
  });
});