describe("Example usage of @interopio/wdio-iocd-service", () => {
  it("should have access to browser.io()", async () => {
    // A fully working io.Connect Desktop client
    const io = await browser.io();

    expect(io).toHaveAttribute("appManager");
    expect(io).toHaveAttribute("contexts");
    expect(io).toHaveAttribute("channels");
  });

  it("should raise a test notifications, and then navigate to notifications panel", async () => {
    const io = await browser.io();
    await io.notifications.configure({ enable: true });
    await io.notifications.raise({
      title: "Test Notification title",
      body: "Test Notification body",
      toastExpiry: 999999,
    });

    await browser.switchToLaunchpad();
    await browser.$(".notifications-button button").waitAndClick();
    await browser.switchToNotificationPanel();
    await browser.pause(2000);
  });

  it("should allow using the desktop-client prefs API", async () => {
    const io = await browser.io();

    const sampleApp = "ExampleApplication";
    const mockedAppPrefs = { lazyLoadAssets: true };

    // used to store the "update" for app prefs
    let expectedSetPrefs = null;

    // Subscribe for changes on the sampleApp prefs
    io.prefs.subscribeFor(sampleApp, ({ data }) => (expectedSetPrefs = data));

    await io.prefs.setFor(sampleApp, mockedAppPrefs);

    // expect the changed prefs to match what was set
    expect(expectedSetPrefs).toMatchObject(mockedAppPrefs);
  });

  it("should allow opening apps from the appManager API", async () => {
    const io = await browser.io();

    // Start the client list app
    const instanceClientList = await io.appManager
      .application("channelsclientlist")
      .start();

    // Switch to the client list window
    const clientListWindow = await instanceClientList.getWindow();
    await browser.switchById(clientListWindow.id);

    // Check that the app-client-list element exists in DOM
    const appClientListElement = await $("app-client-list");
    await expect(appClientListElement).toBeExisting();

    // Start the client portfolio app
    const instancePortfolio = await io.appManager
      .application("channelsclientportfolio")
      .start();

    // Switch to the client portfolio window
    const clientPortfolioList = await instancePortfolio.getWindow();
    await browser.switchById(clientPortfolioList.id);

    // Check that the app-client-portfolio element exists in DOM
    const clientPortfolioElement = await $("app-client-portfolio");
    await expect(clientPortfolioElement).toBeExisting();
  });
});
