import { browser, expect } from '@wdio/globals';

describe('Agent Orchestrator — Smoke Tests', () => {
  it('should render the application window', async () => {
    // Wait for the app to fully load before any tests
    await browser.waitUntil(
      async () => (await browser.getTitle()) === 'Agent Orchestrator',
      { timeout: 10_000, timeoutMsg: 'App did not load within 10s' },
    );
    const title = await browser.getTitle();
    expect(title).toBe('Agent Orchestrator');
  });

  it('should display the status bar', async () => {
    const statusBar = await browser.$('[data-testid="status-bar"]');
    await expect(statusBar).toBeDisplayed();
  });

  it('should show version text in status bar', async () => {
    const version = await browser.$('.status-bar .version');
    await expect(version).toBeDisplayed();
    const text = await version.getText();
    expect(text).toContain('Agent Orchestrator');
  });

  it('should display the sidebar rail', async () => {
    const sidebarRail = await browser.$('[data-testid="sidebar-rail"]');
    await expect(sidebarRail).toBeDisplayed();
  });

  it('should display the workspace area', async () => {
    const workspace = await browser.$('.workspace');
    await expect(workspace).toBeDisplayed();
  });

  it('should toggle sidebar with settings button', async () => {
    // Click settings button via data-testid
    await browser.execute(() => {
      const btn = document.querySelector('[data-testid="settings-btn"]');
      if (btn) (btn as HTMLElement).click();
    });

    const sidebarPanel = await browser.$('.sidebar-panel');
    await sidebarPanel.waitForDisplayed({ timeout: 5000 });
    await expect(sidebarPanel).toBeDisplayed();

    // Click the close button to close
    await browser.execute(() => {
      const btn = document.querySelector('.panel-close');
      if (btn) (btn as HTMLElement).click();
    });
    await browser.pause(500);
    await expect(sidebarPanel).not.toBeDisplayed();
  });
});
